4 Semaphores
4 Semaphores
Semaphores
1
CSC3021 Concurrent Programming: §4 Semaphores
Reading
2
CSC3021 Concurrent Programming: §4 Semaphores
Semaphores
3
CSC3021 Concurrent Programming: §4 Semaphores
4.1: Definition of a Semaphore
down(S)
if S > 0 then
S=S-1
else
suspend the action of the process making the call. The
process is said to be suspended on S.
up(S)
if there are processes that have been suspended on S
wake one of them.
else
S=S+1
4
CSC3021 Concurrent Programming: §4 Semaphores
Definition of a Semaphore
S>=0 (4.1)
S = S0+ #Up(S) - #Down(S) (4.2)
where,
S0 is the initial value of the semaphore,
#Up(S) is the number of Up operations executed on S,
#Down(S) is the number of completed Down operations
executed on S.
6
CSC3021 Concurrent Programming: §4 Semaphores
4.2: Mutual Exclusion using a Semaphore
Process P1 Process P2
while (true) { while (true) {
nonCriticalSection1; nonCriticalSection2;
down(S); down(S);
criticalSection1; criticalSection2;
up(S); up(S);
} }
end P1; end P2;
8
CSC3021 Concurrent Programming: §4 Semaphores
Mutual Exclusion using a Semaphore
Blocked-set
There semaphore
are many : of semaphore in the literature. It is
definitions
down(S) to be able to distinguish between them because the
important
if S > 0 then
correctness of a program will depend on the definition being used.
S=S-1
else
suspend the action of the process making the call.
up(S)
if there are processes that have been suspended on S
wake one of them.
else
S=S+1
Blocked-queue
There are many semaphore
definitions :of semaphore in the literature. It is
down(S) to be able to distinguish between them because the
important
if S > 0 then
correctness of a program will depend on the definition being
used. S=S-1
else
suspend the action of the process making the call and
append to a FIFO queue.
up(S)
if there are processes that have been suspended on S
wake the process at the head of the queue.
else
S = S + 1.
15
CSC3021 Concurrent Programming: §4 Semaphores
Exercise
Solution
Semaphore first = 0;
Semaphore second = 0;
17
CSC3021 Concurrent Programming: §4 Semaphores
Exercise
OR …
Semaphore first = 0;
Semaphore second = 0;
18
CSC3021 Concurrent Programming: §4 Semaphores
Exercise
Semaphore first = 0;
Semaphore second = 0;
19
CSC3021 Concurrent Programming: §4 Semaphores
4.4 Producer – Consumer Problem
20
CSC3021 Concurrent Programming: §4 Semaphores
Producer – Consumer Problem
A buffer is a
queue of data
elements
head tail
21
CSC3021 Concurrent Programming: §4 Semaphores
Producer – Consumer Problem: infinite buffer
23
CSC3021 Concurrent Programming: §4 Semaphores
Producer – Consumer Problem: infinite buffer
27
CSC3021 Concurrent Programming: §4 Semaphores
The Dining Philosophers
28
CSC3021 Concurrent Programming: §4 Semaphores
The Dining Philosophers – First Attempt
process philosopher[i=0..4]
while (true) {
down(fork[i]);
down(fork[(i+1)%5]);
eat;
up(fork[i]);
up(fork[(i+1)%5]);
think;
}
end philosopher;
29
CSC3021 Concurrent Programming: §4 Semaphores
The Dining Philosophers
30
CSC3021 Concurrent Programming: §4 Semaphores
4:5 The Dining Philosophers - Exercise
Solution:
Let #Pi be the number of philosophers holding fork i.
Then: #Pi = #Down(fork[i]) - #Up(fork[i])
By the semaphore invariant (4.2):
fork[i] = 1 + #Up(fork[i]) - #Down(fork[i])
fork[i] = 1 - #Pi
The value of a semaphore is always non-negative (4.1), so
#Pi <= 1
31
CSC3021 Concurrent Programming: §4 Semaphores
Deadlock
32
CSC3021 Concurrent Programming: §4 Semaphores
Deadlock can arise if four conditions hold
simultaneously
33
CSC3021 Concurrent Programming: §4 Semaphores
Wait-for cycle for the dining philosophers (1st attempt)
Has 0 awaits 1
0
Has 4 awaits 0
4
1 Has 1 awaits 2
3 2
Has 2 awaits 3
Has 3 awaits 4
34
CSC3021 Concurrent Programming: §4 Semaphores
Breaking the wait-for cycle (correct version)
Has 0 awaits 1
0
Awaits 3
4
1 Has 1 awaits 2
3 2
Has 2 awaits 3
Has 3, can grab 4
35
CSC3021 Concurrent Programming: §4 Semaphores
Deadlock Prevention
reader
writer
reader database
reader
37
CSC3021 Concurrent Programming: §4 Semaphores
The Readers and Writers Problem
Semaphore rw = 1;
38
CSC3021 Concurrent Programming: §4 Semaphores
The Readers and Writers Problem
int nr =0;
Semaphore rw =1;
process Reader[i=1..M] Semaphore mutexr = 1;
while (true) {
down(mutexr); process Writer[i=1..N]
nr=nr+1; while (true) {
if (nr==1) down(rw); down(rw);
up(mutexr); write_the_database;
read_the_database; up(rw);
down(mutexr) }
nr=nr-1; end Writer;
if (nr==0) up(rw);
up(mutexr);
}
end Reader; Under what circumstances
is this solution unfair ?
39
CSC3021 Concurrent Programming: §4 Semaphores
4.7: The Sleeping Barber
40
CSC3021 Concurrent Programming: §4 Semaphores
The Sleeping Barber
Semaphore mutex=1;
Semaphore customers=0, cutting=0, barber=0;
int waiting=0, numChairs= ..;
process Customer[i=1..M]
process SleepingBarber
while (true) {
while (true) {
down(mutex);
down(customers);
if (waiting<numChairs){
down(mutex);
waiting++;
waiting--;
up(customers);
up(barber);
up(mutex);
up(mutex);
down(barber);
down(cutting);
up(cutting);
}
}
end SleepingBarber;
else up(mutex);
}
end Customer;
41
CSC3021 Concurrent Programming: §4 Semaphores
The Sleeping Barber
42
CSC3021 Concurrent Programming: §4 Semaphores