150 likes | 294 Vues
Lecture 05 Process Synchronization. Background The Critical-Section Problem. Outline. What is the problem with cooperating processes that share data? Mutual Exclusion: Software Approaches Example taken from: William Stalling . Operating Systems. Internals and Design Principles .
E N D
Lecture 05Process Synchronization Background The Critical-Section Problem M.B. Ibáñez
Outline • What is the problem with cooperating processes that share data? • Mutual Exclusion: Software Approaches Example taken from: William Stalling. Operating Systems. Internals and Design Principles. Third Edition. Prentice Hall. 1997. M.B. Ibáñez
Process A i = 0 while ( i < 10 ) { i++; } System.out.println (“A wins”); Process B i = 0 while ( i > -10 ) { i--; } System.out.println (“B wins”); Example: Counting Conteststatic int i M.B. Ibáñez
Protocol Igloo M.B. Ibáñez
Concepts • Mutual Exclusion • Mechanisms that ensure that only one person or process is doing certain things at one time • Critical Section • A section of code in which only one process may be executing at a given time • Synchronization • The use of atomic operations to ensure the correct operation of cooperating processes M.B. Ibáñez
Process 0 … while turn <> 0 do {nothing}; < critical section > turn := 1; ... Process 1 … while turn <> 1 do {nothing}; < critical section > turn := 0; ... First Solution: Busy Waitingvar turn: 0..1 M.B. Ibáñez
Comments • This solution guarantees the mutual exclusion property. • Drawbacks: • Processes must strictly alternate in their use of their critical section • If one process fails, the other process is permanently blocked • Busy waiting: the thwarted process can do nothing productive until it gets permission to enter its critical section M.B. Ibáñez
Process 0 … while flag[1] do {nothing}; flag[0] := true; < critical section >; flag[0] := false; ... Process 1 … while flag[0] do {nothing}; flag[1] := true; < critical section >; flag[1] := false; ... Second Solutionvar flag: array [0..1] of boolean; M.B. Ibáñez
Comments • This solution does not guarantee mutualexclusion. Consider the following sequence: P0 executes the while statement and finds flag[1] set to false. P1 executes the while statement and finds flag[0] set to false P0 sets flag[0] to true and enter its critical section P1 sets flag[1] to true and enter its critical section M.B. Ibáñez
Process 0 … flag[0] := true; while flag[1] do {nothing}; < critical section >; flag[0] := false; ... Process 0 … flag[1] := true; while flag[0] do {nothing}; < critical section >; flag[1] := false; ... Third Solutionvar flag: array [0..1] of boolean; M.B. Ibáñez
Comments • Mutual exclusion is guarantee • Drawbacks: • If one process fails inside its critical section, the other process is blocked • If both processes set their flags to true before either has executed the while statement, then each think that the other has entered its critical section, causing deadlock. M.B. Ibáñez
Process 0 … flag[0] := true; while flag[1] do { flag[0] := false; <delay for a short time>; flag[0] := true; }; <critical section>; flag[0] := false; ... Process 1 … flag[1] := true; while flag[0] do { flag[1] := false; <delay for a short time>; flag[1] := true; }; <critical section>; flag[1] := false; ... Fourth Solution M.B. Ibáñez
Comments • The mutual exclusion is guaranteed • Consider the following sequence of events: P0 sets flag[0] to true P1 sets flag[1] to true P0 checks flag[1] P1 checks flag[0] P0 sets flag[0] to false P1 sets flag[1] to false P0 sets flag[0] to true P1 sets flag[1] to true M.B. Ibáñez
procedure P0; begin repeat flag[0] := true; while flag[1] do if turn = 1 then begin flag[0] := false; while turn = 1 do {nothing}; flag[0] := true; end <critical section>; turn := 1; flag[0] := false; <remainder> forever end; procedure P1; begin repeat flag[1] := true; while flag[0] do if turn = 0 then begin flag[1] := false; while turn = 0 do {nothing}; flag[1] := true; end <critical section>; turn := 0; flag[1] := false; <remainder> forever end; A Correct SolutionDekker’s Algorithm I M.B. Ibáñez
var flag array[0..1] ofboolean; turn: 0..1; begin flag[0] := false; flag[1] := false; turn := 1; parbegin P0; P1; parend end A Correct SolutionDekker’s Algorithm II M.B. Ibáñez