100 likes | 219 Views
Rensselaer Polytechnic Institute CSC 432 – Operating Systems David Goldschmidt, Ph.D. Operating Systems {week 10}. A need for synchronization (i). Without synchronization amongst processes (and threads), results are unpredictable. how do variables x and y become corrupted?.
E N D
Rensselaer Polytechnic Institute CSC 432 – Operating Systems David Goldschmidt, Ph.D. Operating Systems{week 10}
A need for synchronization (i) • Without synchronization amongst processes (and threads), results are unpredictable how dovariables x and y become corrupted?
A need for synchronization (ii) • Processes compete for resources • Once obtained, the resource isfully 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!
Critical sections (i) • To synchronize processes(or threads), first identifythe critical sections of code • If process Pi is executing inits critical section, no other process can be executing in their critical sections • A critical section guarantees mutual exclusionto one or more resources
Critical sections (ii) • The operating system must control access to critical sections and guarantee progress: if no process isexecuting in itscritical section process selection cannot be postponed indefinitely (starvation) then and and one or more processeswish to enter theircritical sections process selection must be fair and avoid deadlock
Peterson’s solution • Peterson’s solution is a two-process solution • Processes Pj and Pk share two variables: • Variable turn indicateswhose turn it is toenter the critical section • The flag arrayspecifies if a processis ready to enter itscritical section int turn; boolean flag[2]; // Process Pj while ( true ) { flag[j] = true; // Pjready turn = k; while ( flag[k] && turn == k ) ; // busy wait // CRITICAL SECTION HERE flag[j] = false; }
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
Producer-Consumer Problem (ii) also known as the bounded-buffer problem