oslecture6-7
oslecture6-7
Synchronization
Outline
Cooperating Processes
The Bounded Buffer Producer-Consumer
Problem
The Critical Section Problem
Synchronization Hardware
Semaphores
Classical Problems of Synchronization
Critical Regions
Monitors
Cooperating Processes
Concurrent Processes can be
Independent processes
cannot affect or be affected by the execution of another
process.
Cooperating processes
can affect or be affected by the execution of another process.
Advantages of process cooperation:
Information sharing
Computation speedup
Modularity
Convenience(e.g. editing, printing, compiling)
Concurrent execution requires
process communication and process synchronization
Producer-Consumer Problem
Notation -
Lexicographic order(ticket#, process id#)
(a,b) < (c,d) if (a<c) or if ((a=c) and (b < d))
max(a0,….an-1) is a number, k, such that k >=ai
for i = 0,…,n-1
Shared Data
var choosing: array[0..n-1] of boolean;(initialized to false)
number: array[0..n-1] of integer; (initialized to 0)
Bakery Algorithm (cont.)
repeat
choosing[i] := true;
number[i] := max(number[0], number[1],…,number[n-1]) +1;
choosing[i] := false;
for j := 0 to n-1
do begin
while choosing[j] do no-op;
while number[j] <> 0
and (number[j] ,j) < (number[i],i) do no-op;
end;
critical section
number[i]:= 0;
remainder section
until false;
Hardware Solutions for
Synchronization
Mutual exclusion solutions presented depend
on memory hardware having read/write cycle.
until false;
Semaphore
Shared data
type item = ….;
var buffer: array[0..n-1] of item;
full, empty, mutex : semaphore;
nextp, nextc :item;
full := 0; empty := n; mutex := 1;
Bounded Buffer Problem
Producer process - creates filled buffers
repeat
…
produce an item in nextp
…
wait (empty);
wait (mutex);
…
add nextp to buffer
…
signal (mutex);
signal (full);
until false;
Bounded Buffer Problem
Shared Data
var mutex, wrt: semaphore (=1);
readcount: integer (= 0);
Writer Process
wait(wrt);
…
writing is performed
...
signal(wrt);
Readers-Writers Problem
Reader process
wait(mutex);
readcount := readcount +1;
if readcount = 1 then wait(wrt);
signal(mutex);
...
reading is performed
...
wait(mutex);
readcount := readcount - 1;
if readcount = 0 then signal(wrt);
signal(mutex);
Dining-Philosophers Problem
Shared Data
var chopstick: array [0..4] of semaphore (=1 initially);
Dining Philosophers Problem
Philosopher i :
repeat
wait (chopstick[i]);
wait (chopstick[i+1 mod 5]);
…
eat
...
signal (chopstick[i]);
signal (chopstick[i+1 mod 5]);
…
think
…
until false;
Higher Level
Synchronization
Timing errors are still possible with semaphores
Example 1
signal (mutex);
…
critical region
...
wait (mutex);
Example 2
wait(mutex);
…
critical region
...
wait (mutex);
Example 3
wait(mutex);
…
critical region
...
Forgot to signal
Conditional Critical Regions
Region x when B do S
var mutex, first-delay, second-delay: semaphore;
first-count, second-count: integer;
Mutually exclusive access to the critical
section is provided by mutex.
If a process cannot enter the critical section because the
Boolean expression B is false,
it initially waits on the first-delay semaphore;
moved to the second-delay semaphore before it is allowed to
reevaluate B.
Implementation
begin
for i := 0 to 4
do state[i] := thinking;
end;