Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

Session 3.5: Classical synchronization problems: bounded buffer, readers–writers, dining philosophers

Three problems that test every synchronization tool — solved with semaphores, traced step by step, with the deadlocks and starvation each can hide.

Module 3 1.5 hours Theory CO3
← Before
3.4 Semaphores
wait() and signal()
You are here
3.5 Classical synchronization problems: bounded buffer, readers–writers, dining philosophers
Module 3 · Process Synchronization
Next →
3.6 Monitors
Safer synchronization

Learning objectives

By the end of this session you should be able to:

  • Solve the bounded-buffer problem with mutex, empty and full semaphores and explain the order of waits.
  • Solve the first readers–writers problem and explain why writers may starve.
  • State the dining-philosophers problem and show how the naive solution deadlocks.
  • Propose three remedies for deadlock among the philosophers.

Lecture notes

These three problems model a huge range of real synchronization situations. Every new synchronization mechanism is tested against them.

1. The bounded-buffer (producer–consumer) problem

n buffer slots, a producer and a consumer (Session 3.1), now solved with three semaphores:

mutex = 1

Mutual exclusion on the buffer itself.

empty = n

Counts empty slots; the producer waits on it.

full = 0

Counts full slots; the consumer waits on it.

Producer and consumer
/* 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);
Bounded buffer, n = 2 — producer runs ahead and is stopped by emptyStep through the interleaving one instruction at a time.
Order of the wait() calls matters

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.

2. The readers–writers problem

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).

rw_mutex = 1

Held by a writer, or by the group of readers as a whole.

mutex = 1

Protects read_count.

read_count = 0

How many readers are reading now.

First readers–writers solution
/* 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.

3. The dining-philosophers problem

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.

rice P0 P1 P2 P3 P4

Philosopher i needs chopsticks i and (i + 1) mod 5.

Simple semaphore solution — and its flaw
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);
Deadlock!

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:

  • Allow at most four philosophers at the table at once (a counting semaphore room = 4).
  • Pick up chopsticks only if both are available (inside a critical section) — the monitor solution, Session 3.6.
  • Asymmetric solution: odd philosophers pick left then right; even ones right then left — breaks the circular wait.

A deadlock-free solution may still allow starvation of one philosopher.

VideoBytes

Bounded buffer and readers–writers

Video coming soon

Bounded buffer and readers–writers

Recorded by Dr. Krithika L.B. Slot m3-bounded-rw

Dining philosophers

Video coming soon

Dining philosophers

Recorded by Dr. Krithika L.B. Slot m3-dining

Interactive lessons

Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.

Lab connection

Check your understanding

Answer each question in your own words first, then reveal the model answer.

Revealed 0
What are the initial values of mutex, empty and full for a buffer of size 8?

mutex = 1, empty = 8, full = 0.

Why does the first reader wait on rw_mutex but later readers don't?

The group of readers holds rw_mutex collectively; only the first reader needs to lock writers out, and only the last reader releases it.

Can writers starve in the first readers–writers solution?

Yes — as long as at least one reader is reading, new readers keep entering and read_count never drops to 0.

Why does allowing only four philosophers at the table prevent deadlock?

With five chopsticks and four philosophers, at least one philosopher can always get both chopsticks, eat and release them.

Which deadlock condition does the asymmetric philosophers solution break?

Circular wait.

Key takeaways

  • Bounded buffer: mutex = 1, empty = n, full = 0; wait on counting semaphores before mutex.
  • Readers–writers: read_count + mutex + rw_mutex; first reader locks, last reader unlocks; writers may starve.
  • Dining philosophers: naive solution deadlocks; limit diners, take both or neither, or go asymmetric.

Where this leads

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 →