Download presentation
Presentation is loading. Please wait.
1
Operating Systems {week 10}
Rensselaer Polytechnic Institute CSC 432 – Operating Systems David Goldschmidt, Ph.D. Operating Systems {week 10}
2
A need for synchronization (i)
Without synchronization amongst processes (and threads), results are unpredictable how do variables x and y become corrupted?
3
A need for synchronization (ii)
Processes compete for resources Once obtained, the resource is fully dedicated to a process Often, mutual exclusion is required No other process is allowed access to the resource Processes cooperate with other processes Shared resources Specific ordering or sequencing of events all of this applies to threads, too!
4
Critical sections (i) To synchronize processes (or threads), first identify the critical sections of code If process Pi is executing in its critical section, no other process can be executing in their critical sections A critical section guarantees mutual exclusion to one or more resources
5
Critical sections (ii)
The operating system must control access to critical sections and guarantee progress: if no process is executing in its critical section process selection cannot be postponed indefinitely (starvation) then one or more processes wish to enter their critical sections and process selection must be fair and avoid deadlock and
6
Peterson’s solution Peterson’s solution is a two-process solution
Processes Pj and Pk share two variables: Variable turn indicates whose turn it is to enter the critical section The flag array specifies if a process is ready to enter its critical section int turn; boolean flag[2]; // Process Pj while ( true ) { flag[j] = true; // Pj ready turn = k; while ( flag[k] && turn == k ) ; // busy wait // CRITICAL SECTION HERE flag[j] = false; }
7
Producer-Consumer Problem (i)
Model for cooperating processes: A producer process produces information that is consumed by a consumer process e.g. client-server, transaction processing, etc. Implement using a shared memory segment as a shared buffer Producer adds to the buffer Consumer empties the buffer
8
Producer-Consumer Problem (ii)
also known as the bounded-buffer problem
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.