Process Synchronization Continued: Producer/Consumer (Unbounded Buffer) Producer/Consumer (Bounded Buffer)
Process Synchronization Continued: Producer/Consumer (Unbounded Buffer) Producer/Consumer (Bounded Buffer)
Continued
7.4 Semaphores
7.5 Classic Problems of Synchronization
Producer/Consumer (unbounded buffer)
Producer/Consumer (bounded buffer)
Busy Waiting Semaphores
The simplest way to S is an integer variable
implement that, apart from
semaphores. initialization, can only
be accessed through 2
Useful when critical atomic and mutually
sections last for a exclusive operations:
short time, or we have
lots of CPUs.
wait(S):
while S<=0 do ;
S initialized to positive S--;
value (to allow
someone in at the
beginning). signal(S):
S++;
Using semaphores for solving critical
section problems
For n processes
Initialize semaphore Process Pi:
“mutex” to 1 repeat
wait(mutex);
Then only one process
CS
is allowed into CS
signal(mutex);
(mutual exclusion)
RS
To allow k processes forever
into CS at a time,
simply initialize mutex
to k
Synchronizing Processes using
Semaphores
Two processes: Define a semaphore
“synch”
• P1 and P2
• Initialize synch to 0
Statement S1 in P1
needs to be
performed before Put this in P2:
statement S2 in P2 wait(synch);
S2;
Need to make P2
wait until P1 tells it
it is OK to proceed And this in in P1:
S1;
signal(synch);
Busy-Waiting Semaphores:
Observations
When S>0:
• the number of processes that can execute
wait(S) without being blocked = S
When S=0: one or more processes are
waiting on S
Semaphore is never negative
When S becomes >0, the first process that
tests S enters enters its CS
• random selection (a race)
• fails bounded waiting condition
Blocking Semaphores
typedef struct {
int count;
struct PCB *queue;
} semaphore;
semaphore S;
When a process must wait for a semaphore S, it
is blocked and put on the semaphore’s queue
Signal(S) removes one process from the queue
and moves it to Ready.
Semaphore Operations in OS (atomic)
void wait(semaphore S){
S.count--;
if (S.count<0) {
add this process to S.queue
block this process
}
}
signal(S){
S.count++;
if (S.count<=0) {
move one process P from S.queue
to ready list
}
Negative count indicates number of processes
waiting
Semaphores: Implementation
wait(S); wait(Q);
wait(Q); wait(S);
. .
. .
signal(S); signal(Q);
signal(Q); signal(S);
signalB(S):
if (S.queue is empty) {
S.value = 1;
} else {
move a process P from S.queue
to ready list
}
Some Classic Synchronization
Problems
Bounded Buffer (Producer/Consumer)
Readers-Writers Problem
The Producer/Consumer Problem
A producer process produces information that is
consumed by a consumer process
• Example: Implementation of pipes on Unix systems
Again:
• Use a semaphore “mutex” for mutual exclusion
on buffer access
• and a semaphore “full” to synchronize producer
and consumer on the number of consumable
items (full spaces)
But we have to add:
• a semaphore “empty” to synchronize producer
and consumer on the number of empty spaces
Producer/Consumer:
Bounded Buffer (Solution)
Initialization: mutex.count:=1; //mutual excl.
full.count:=0; //full spaces
empty.count:=k; //empty spaces