Semaphores: wait and signal
Semaphores: wait and signal
Recorded by Dr. Krithika L.B. Slot m3-semaphores
Dijkstra's wait() and signal(): binary and counting semaphores, using them for mutual exclusion, ordering and resource counting, and implementing them without busy waiting.
By the end of this session you should be able to:
Dijkstra's semaphore S is an integer variable that, apart from initialisation, is accessed only through two
atomic operations: wait() (originally P, from Dutch proberen, “to test”) and
signal() (V, verhogen, “to increment”).
wait(S) {
while (S <= 0)
; /* busy wait */
S--;
}
signal(S) {
S++;
}Value 0 or 1. Behaves like a mutex lock: initialise to 1, wait before the CS, signal after.
Any non-negative value. Initialise to the number of available instances of a resource; each user does wait to take one and signal to return it.
semaphore mutex = 1;
do {
wait(mutex);
/* critical section */
signal(mutex);
/* remainder section */
} while (true);semaphore synch = 0;
/* P1 */ /* P2 */
S1; wait(synch); /* blocks until P1 signals */
signal(synch); S2;The busy-waiting definition wastes CPU (a spinlock). Instead, each semaphore gets a waiting list. A process
that must wait blocks itself (moves to the waiting state, Session 2.1); signal wakes up
one waiting process (moves it to ready).
typedef struct {
int value;
struct process *list; /* processes waiting on this semaphore */
} semaphore;
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add this process to S->list;
block(); /* system call: running -> waiting */
}
}
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P); /* system call: waiting -> ready */
}
}In this implementation the value can become negative: if S = −k, then k processes are waiting. wait and signal must themselves be atomic — the kernel protects them with interrupt disabling (single CPU) or a spinlock (SMP); the busy waiting is moved from the application's long critical section to these few instructions.
Starvation (indefinite blocking) happens if the waiting list is served in LIFO order, for example. Priority inversion: a high-priority process waits for a lock held by a low-priority one, which is itself preempted by a medium-priority process — the high-priority process effectively waits for the medium one. The fix is priority inheritance: the lock holder temporarily inherits the waiting process's high priority (famously needed on the Mars Pathfinder rover in 1997).
#include <semaphore.h>
sem_t s;
sem_init(&s, 0, 1); /* 0 = shared between threads of this process; initial value 1 */
sem_wait(&s); /* wait / P */
/* critical section */
sem_post(&s); /* signal / V */
sem_destroy(&s);Semaphores: wait and signal
Recorded by Dr. Krithika L.B. Slot m3-semaphores
Blocking semaphores, deadlock and priority inversion
Recorded by Dr. Krithika L.B. Slot m3-semaphore-impl
Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.
Answer each question in your own words first, then reveal the model answer.
Revealed 0The resource is fully in use and three processes are blocked in its waiting list.
Semaphore s = 0; P1: A; signal(s). P2: wait(s); B.
Waiting processes do not consume CPU cycles; they sleep until signalled.
P0: wait(S); wait(Q). P1: wait(Q); wait(S). If each completes its first wait before the other's second, both block forever.
A low-priority process holding a lock needed by a high-priority process temporarily runs at the high priority so it can release the lock quickly.
With semaphores in hand we can solve the three classic problems every OS course uses to test a synchronization tool.
Continue to 3.5 — Classical synchronization problems →