Process Synchronization
Chapter 7
CS 431 -- Operating Systems
Dr. Tim McGuire
Sam Houston State University


Problems with concurrent execution

An example static char a;
void echo()
{ cin >> a;
cout << a;
}
Race Conditions The critical section problem The critical section problem Framework for analysis of solutions while(true) { entry section
critical section
exit section
remainder section
}// loop forever
Requirements for a valid solution to the critical section problem Types of solutions Software solutions Algorithm 1 Algorithm 1 global view

Algorithm 2

Process Pi:
while(true)
{ flag[i] = true;
while(flag[j]){/* do nothing */};
CS
flag[i] = false;
RS
} // loop forever
Algorithm 3 (Peterson’s algorithm) Process Pi:
while(true){ flag[i] = true;
// I want in
turn = j;
// but I let the other in
while (flag[j]&&turn==j){/* do nothing */};
CS
flag[i] = false;
// I no longer want in
RS
} // loop forever
Algorithm 3: proof of correctness What about process failures? n-process solution: bakery algorithm Process Pi:
repeat choosing[i]:=true;
number[i]:=max(number[0]..number[n-1])+1;
choosing[i]:=false;
for j:=0 to n-1 do { while (choosing[j]) {};
while (number[j]!=0 and (number[j],j)<(number[i],i)){};
}
CS
number[i]:=0;
RS
forever
Drawbacks of software solutions Hardware solutions: interrupt disabling Process Pi:
repeat disable interrupts
critical section
enable interrupts
remainder section
forever
Hardware solutions: special machine instructions The test-and-set instruction bool testset(int& i)
{ if (i==0) { i=1;
return true;
}
else { return false; }
}
Process Pi:
repeat repeat{}
until testset(b);
CS
b:=0;
RS
forever
Using xchg for mutual exclusion Process Pi:
repeat k:=1
repeat xchg(k,b) until k=0;
CS
b:=0;
RS
forever
Semaphores type semaphore = record count: integer;
queue: list of process
end;
var S: semaphore;
Semaphore’s operations wait(S): S.count--;
if (S.count<0) { block this process
place this process in S.queue
}


signal(S):

S.count++;
if (S.count<=0) { remove a process P from S.queue
place this process P on ready list
}
Semaphores: observations Using semaphores for solving critical section problems Process Pi: repeat wait(S);
CS
signal(S);
RS
forever
Using semaphores to synchronize processes The producer/consumer problem P/C: unbounded buffer Solution of P/C: unbounded buffer

P/C: unbounded buffer

P/C: finite circular buffer of size k P/C: finite circular buffer of size k Solution of P/C: finite circular buffer of size k

The Dining Philosophers Problem

Process Pi: repeat think;
wait(fork[i]);
wait(fork[i+1 mod 5]);
eat;
signal(fork[i+1 mod 5]);
signal(fork[i]);
forever
Process Pi: repeat
think;
wait(T);
wait(fork[i]);
wait(fork[i+1 mod 5]);
eat;
signal(fork[i+1 mod 5]);
signal(fork[i]);
signal(T);
forever
Binary semaphores Problems with semaphores Monitors Monitor Condition variables Monitor Producer/Consumer problem Monitor for the bounded P/C problem Message Passing Synchronization in message passing Addressing in message passing Mailboxes and Ports Ownership of ports and mailboxes Message format Enforcing mutual exclusion with message passing The bounded-buffer P/C problem with message passing