Bounded buffer and readers–writers
Bounded buffer and readers–writers
Recorded by Dr. Krithika L.B. Slot m3-bounded-rw
Three problems that test every synchronization tool — solved with semaphores, traced step by step, with the deadlocks and starvation each can hide.
By the end of this session you should be able to:
These three problems model a huge range of real synchronization situations. Every new synchronization mechanism is tested against them.
n buffer slots, a producer and a consumer (Session 3.1), now solved with three semaphores:
Mutual exclusion on the buffer itself.
Counts empty slots; the producer waits on it.
Counts full slots; the consumer waits on it.
/* Producer */ /* Consumer */
do { do {
/* produce an item */ wait(full);
wait(empty); wait(mutex);
wait(mutex); /* remove item from buffer */
/* add item to buffer */ signal(mutex);
signal(mutex); signal(empty);
signal(full); /* consume the item */
} while (true); } while (true);If the producer did wait(mutex) before wait(empty) and the buffer were full, it would block while holding mutex — and the consumer could never get mutex to remove an item. Deadlock. Always wait on the counting semaphore first.
A shared database is accessed by readers (only read) and writers (update). Many readers may read simultaneously, but a writer needs exclusive access. In the first readers–writers problem, no reader is kept waiting unless a writer already has access (readers have priority — writers may starve). In the second problem, once a writer is waiting no new reader may start (writers may starve readers).
Held by a writer, or by the group of readers as a whole.
Protects read_count.
How many readers are reading now.
/* Writer */ /* Reader */
do { do {
wait(rw_mutex); wait(mutex);
/* writing is performed */ read_count++;
signal(rw_mutex); if (read_count == 1)
} while (true); wait(rw_mutex); /* first reader locks out writers */
signal(mutex);
/* reading is performed */
wait(mutex);
read_count--;
if (read_count == 0)
signal(rw_mutex); /* last reader lets writers in */
signal(mutex);
} while (true);read_count 0 → 1. First reader → wait(rw_mutex) succeeds. R1 reads.
read_count → 2. Not the first reader, so no wait on rw_mutex. R1 and R2 read together.
Writer does wait(rw_mutex) — held by the readers → W blocks.
read_count → 3; R3 joins immediately even though W is waiting. This is why writers can starve.
Each reader decrements read_count; when the last one leaves (0), it does signal(rw_mutex).
W wakes up and writes with exclusive access. Readers arriving now block on rw_mutex (the first) or on mutex (the rest).
Many systems provide reader–writer locks directly (pthread_rwlock_t), useful when reads far outnumber writes.
Five philosophers sit at a round table with a bowl of rice and five chopsticks, one between each pair. A philosopher alternately thinks and eats; to eat she needs both neighbouring chopsticks, picked up one at a time. It models allocating several resources among several processes without deadlock or starvation.
Philosopher i needs chopsticks i and (i + 1) mod 5.
semaphore chopstick[5]; /* all initialised to 1 */
do { /* philosopher i */
wait(chopstick[i]); /* left */
wait(chopstick[(i + 1) % 5]);/* right */
/* eat */
signal(chopstick[i]);
signal(chopstick[(i + 1) % 5]);
/* think */
} while (true);If all five become hungry at once and each picks up her left chopstick, every chopstick becomes 0 and each waits forever for her right one — a circular wait (Session 2.6). Remedies:
room = 4).A deadlock-free solution may still allow starvation of one philosopher.
Bounded buffer and readers–writers
Recorded by Dr. Krithika L.B. Slot m3-bounded-rw
Dining philosophers
Recorded by Dr. Krithika L.B. Slot m3-dining
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 0mutex = 1, empty = 8, full = 0.
The group of readers holds rw_mutex collectively; only the first reader needs to lock writers out, and only the last reader releases it.
Yes — as long as at least one reader is reading, new readers keep entering and read_count never drops to 0.
With five chopsticks and four philosophers, at least one philosopher can always get both chopsticks, eat and release them.
Circular wait.
Every solution here depends on getting the order of wait() and signal() exactly right. One slip anywhere breaks it. Monitors move that responsibility into the language.
Continue to 3.6 — Monitors →