Producer-consumer
A producer thread makes items and a consumer thread uses them. Between them is a buffer with 3 slots. Semaphore empty counts free slots (3), full counts filled slots (0), and mutex (1) lets one thread touch the buffer at a time.
To put item A, the producer calls sem_wait(empty): 3 becomes 2, so a slot is reserved. It takes the mutex, writes A into slot 0, gives the mutex back, then calls sem_post(full): 0 becomes 1. One item is ready.
The producer puts B and C the same way. Now empty is 0 and full is 3. For item D, sem_wait(empty) finds 0, so the producer is put to sleep. It does not spin or overwrite a slot: it waits.
The consumer calls sem_wait(full): 3 becomes 2. It takes A from slot 0 under the mutex, then calls sem_post(empty). That post wakes the sleeping producer, which puts D into the freed slot 0.
Readers-writers
Readers only look at the shared data, so any number may read together. The first reader in locks writers out; later readers just add 1 to the count. Here two readers read v1 at once, and readers = 2.
A writer changes the data, so it needs it alone. It waits on the write lock. Each reader that leaves subtracts 1, and the last one out, at readers = 0, unlocks. Only then does the writer write v2.
In this simple lock a new reader may join whenever the count is above 0. If readers keep overlapping, the count never reaches 0 and the writer waits forever: starvation. Fairer locks hold new readers back while a writer waits.
Dining philosophers
Five philosophers sit at a round table with one fork between each pair. To eat, philosopher p needs both forks beside it: left fork p and right fork (p + 1) mod 5. Each fork is a lock that only one may hold.
Suppose all five pick up their left fork at the same moment. Every fork is now held, and each philosopher waits for its right fork, held by its neighbour. The waiting forms a circle, so nobody can ever eat: deadlock.
The fix: always pick up the lower-numbered fork first. Philosopher 4 needs forks 4 and 0, so it reaches for fork 0 first and loses it to philosopher 0. Fork 4 stays free, so philosopher 3 takes it and eats.
Each problem stands for a real pattern: a queue between threads, data read far more often than written, and several locks taken together. Counting semaphores, a reader count and one fixed lock order solve them.
What happens
;; step 1/11 · pc-setup the bounded buffer (OSTEP Figure 31.12, CS162 lecture 8) sem_init(&empty, 0, 3); /* free slots */ sem_init(&full, 0, 0); /* filled slots */ sem_init(&mutex, 0, 1); /* a lock */ producer must wait when the buffer is full consumer must wait when the buffer is empty only one thread at a time may change the buffer
The mutex is taken only around the buffer itself, after sem_wait(empty) or sem_wait(full). Taking it first and then sleeping on empty or full while holding it deadlocks: the other thread could never get in to post (OSTEP Figure 31.11). This reader-writer lock prefers readers. POSIX pthread_rwlock_rdlock and Linux kernel rw_semaphore are real reader-writer locks; with priority scheduling POSIX lets a reader in only if no writer is waiting, which stops writer starvation.