Lecture 05 Process Synchronization Background The Critical-Section Problem Autumn 2000 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. Autumn 2000 M.B. Ibáñez Example: Counting Contest static int i Process A i=0 while ( i < 10 ) { i++; } System.out.println (“A wins”); Autumn 2000 Process B i=0 while ( i > -10 ) { i--; } System.out.println (“B wins”); M.B. Ibáñez Protocol Igloo Autumn 2000 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 Autumn 2000 M.B. Ibáñez First Solution: Busy Waiting var turn: 0..1 Process 0 … while turn <> 0 do {nothing}; < critical section > turn := 1; ... Autumn 2000 Process 1 … while turn <> 1 do {nothing}; < critical section > turn := 0; ... 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 Autumn 2000 M.B. Ibáñez Second Solution var flag: array [0..1] of boolean; Process 0 … while flag[1] do {nothing}; flag[0] := true; < critical section >; flag[0] := false; ... Autumn 2000 Process 1 … while flag[0] do {nothing}; flag[1] := true; < critical section >; flag[1] := false; ... M.B. Ibáñez Comments • This solution does not guarantee mutual exclusion. 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 Autumn 2000 M.B. Ibáñez Third Solution var flag: array [0..1] of boolean; Process 0 … flag[0] := true; while flag[1] do {nothing}; < critical section >; flag[0] := false; ... Autumn 2000 Process 0 … flag[1] := true; while flag[0] do {nothing}; < critical section >; flag[1] := false; ... 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. Autumn 2000 M.B. Ibáñez Fourth Solution Process 0 Process 1 … flag[0] := true; while flag[1] do { flag[0] := false; <delay for a short time>; flag[0] := true; }; <critical section>; flag[0] := false; ... … flag[1] := true; while flag[0] do { flag[1] := false; <delay for a short time>; flag[1] := true; }; <critical section>; flag[1] := false; ... Autumn 2000 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 Autumn 2000 M.B. Ibáñez A Correct Solution Dekker’s Algorithm I 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; Autumn 2000 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; M.B. Ibáñez A Correct Solution Dekker’s Algorithm II var flag array[0..1] of boolean; turn: 0..1; Autumn 2000 begin flag[0] := false; flag[1] := false; turn := 1; parbegin P0; P1; parend end M.B. Ibáñez