Lecture 22
Lecture 22
Operating System Concepts – 9th Edition 5.1 Silberschatz, Galvin and Gagne
Semaphore Usage
Counting semaphore – integer value can range over an
unrestricted domain
Binary semaphore – integer value can range only between 0
and 1
Same as a mutex lock
Used only when the resource is mutually exclusive, and
we have only one unit of that resource e.g. printer
Can solve various synchronization problems
Consider P1 and P2 that require S1 to happen before S2
Create a semaphore “synch” initialized to 0
P1:
S1;
signal(synch);
P2:
wait(synch);
S2;
Can implement a counting semaphore S as a binary
semaphore
Operating System Concepts – 9th Edition 5.2 Silberschatz, Galvin and Gagne
Semaphore Implementation
Must guarantee that no two processes can execute
the wait() and signal() on the same semaphore at
the same time
Thus, the implementation becomes the critical
section problem where the wait and signal code are
placed in the critical section
Could now have busy waiting in critical section
implementation
But implementation code is short
Little busy waiting if critical section rarely
occupied
Note that applications may spend lots of time in
critical sections and therefore this is not a good
solution
Operating System Concepts – 9th Edition 5.3 Silberschatz, Galvin and Gagne
Semaphore Implementation with no Busy waiting
Operating System Concepts – 9th Edition 5.4 Silberschatz, Galvin and Gagne
Implementation with no Busy waiting (Cont.)
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add this process to S->list;
block();
}
}
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P);
}
}
Operating System Concepts – 9th Edition 5.5 Silberschatz, Galvin and Gagne
Deadlock and Starvation
Deadlock – two or more processes are waiting indefinitely
for an event that can be caused by only one of the waiting
processes
Let S and Q be two semaphores initialized to 1
P0 P1
wait(S); wait(Q);
wait(Q); wait(S);
... ...
signal(S); signal(Q);
signal(Q); signal(S);
Operating System Concepts – 9th Edition 5.6 Silberschatz, Galvin and Gagne
Classical Problems of Synchronization
Classical problems used to test newly-proposed
synchronization schemes
Bounded-Buffer Problem
Readers and Writers Problem
Dining-Philosophers Problem
Operating System Concepts – 9th Edition 5.7 Silberschatz, Galvin and Gagne
Bounded-Buffer Problem
Operating System Concepts – 9th Edition 5.8 Silberschatz, Galvin and Gagne
Bounded Buffer Problem (Cont.)
do {
...
/* produce an item in next_produced */
...
wait(empty);
wait(mutex);
...
/* add next produced to the buffer */
...
signal(mutex);
signal(full);
} while (true);
Operating System Concepts – 9th Edition 5.9 Silberschatz, Galvin and Gagne
Bounded Buffer Problem (Cont.)
The structure of the consumer process
Do {
wait(full);
wait(mutex);
...
/* remove an item from buffer to next_consumed */
...
signal(mutex);
signal(empty);
...
/* consume the item in next consumed */
...
} while (true);
Operating System Concepts – 9th Edition 5.10 Silberschatz, Galvin and Gagne