Professional Documents
Culture Documents
Semphores
Semphores
Semaphores
1
Semaphores
2
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
3
Definition of a Semaphore
S>=0 (1)
S = S0+ #Up(S) - #Down(S) (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.
5
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;
7
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.
Busy-wait semaphore :
down(S) The value of S is tested
loop in a busy-wait loop. The
<if (S>0) { entire if-statement is
S=S-1; break; executed as an atomic
}> operation, but there may
end loop; be interleaving between
up(S) cycles of the loop.
S = S + 1.
11
Other Semaphore Definitions
12
Other Semaphore Definitions
14
Exercise
process
process Bill
Bill {{ process
process BenBen {{
while
while (true)
(true) {{ while
while (true)
(true) {{
//
// Bill
Bill makes
makes sandwiches
sandwiches //
// //
// Ben
Ben makes
makes soup
soup //
//
SYNCH
SYNCH 1;
1; SYNCH
SYNCH 2;
2;
//
// Bill
Bill delivers
delivers //
// //
// Ben
Ben delivers
delivers //
//
}} }}
}} }}
Solution
Semaphore
Semaphore first
first == 0;
0;
Semaphore
Semaphore second
second == 0;
0;
process
process Bill
Bill {{ process
process BenBen {{
while
while (true)
(true) {{ while
while (true)
(true) {{
//
// Bill
Bill makes
makes sandwiches
sandwiches //
// //
// Ben
Ben makes
makes soup
soup //
//
down(first);
down(first); up(first);
up(first);
up(second);
up(second); down(second);
down(second);
//
// Bill
Bill delivers
delivers //
// //
// Ben
Ben delivers
delivers //
//
}} }}
}} }}
16
Exercise
OR …
Semaphore
Semaphore first
first == 0;
0;
Semaphore
Semaphore second
second == 0;
0;
process
process Bill
Bill {{ process
process BenBen {{
while
while (true)
(true) {{ while
while (true)
(true) {{
//
// Bill
Bill makes
makes sandwiches
sandwiches //
// //
// Ben
Ben makes
makes soup
soup //
//
down(second);
down(second);
up(second);
up(second); up(first);
up(first);
down(first);
down(first); //
// Ben
Ben delivers
delivers //
//
//
// Bill
Bill delivers
delivers //
// }}
}} }}
}}
17
Exercise
Semaphore
Semaphore first
first == 0;
0;
Semaphore
Semaphore second
second == 0;
0;
process
process Bill
Bill {{ process
process BenBen {{
while
while (true)
(true) {{ while
while (true)
(true) {{
//
// Bill
Bill makes
makes sandwiches
sandwiches //
// //
// Ben
Ben makes
makes soup
soup //
//
up(first);
up(first);
up(second);
up(second); down(second);
down(second);
down(first);
down(first); //
// Ben
Ben delivers
delivers //
//
//
// Bill
Bill delivers
delivers //
// }}
}} }}
}}
18
Producer – Consumer Problem
19
Producer – Consumer Problem
A buffer is a
queue of data
elements
head tail
20
Producer – Consumer Problem: infinite buffer
22
Producer – Consumer Problem: infinite buffer
26
The Dining Philosophers
27
The Dining Philosophers
28
The Readers and Writers Problem
reader
writer
reader database
reader
29
The Readers and Writers Problem
Semaphore rw = 1;
30
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 ?
31
The Sleeping Barber
32
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;
33
The Sleeping Barber
34