Operating Systems Module 4: Concurrency Lesson 4.4 15 min

The classic problems

Three famous synchronisation puzzles: the bounded buffer, readers and writers, and the dining philosophers. Each one teaches a pattern you will reuse.

Locks, semaphores and condition variables are the tools. This lesson is about using them well. Computer scientists have a short list of puzzles that capture most of what goes wrong in real concurrent programs. If you can solve these three, you can recognise them inside bigger systems: a web server’s request queue, a database’s read and write locks, a chain of services that each need two resources.

Producer and consumer: the bounded buffer

One thread produces items (reads requests, decodes frames, generates work) and another consumes them. They are connected by a bounded buffer: a fixed number of slots. Three things must be true:

  • the producer must wait when the buffer is full,
  • the consumer must wait when the buffer is empty,
  • only one thread may change the buffer at a time.

That is three semaphores, and each has a clear meaning:

  • empty counts free slots. It starts at the buffer size.
  • full counts items waiting. It starts at 0.
  • mutex protects the buffer itself. It starts at 1.

The producer does wait(empty), wait(mutex), puts an item in, signal(mutex), signal(full). The consumer does the mirror image: wait(full), wait(mutex), takes one, signal(mutex), signal(empty).

Here the producer is quick (one tick per item) and the consumer is slow (three ticks). The buffer has three slots.

A fast producer and a slow consumer

  1. Start The producer makes 8 items and the consumer uses them. The buffer has 3 slots. Three semaphores keep them honest: empty counts free slots (starts at 3), full counts items waiting (starts at 0), and mutex lets one thread at a time touch the buffer itself.
    Time
    0 ticks
    Buffer
    0 of 3 slots full
    empty (free slots)
    3
    full (items to take)
    0
    mutex
    free
    Produced / consumed
    0 / 0
  2. Consumer blocks on full Consumer runs wait(full). The buffer is empty: full is 0, so there is nothing to take. Consumer sleeps until the other thread signals full.
    Time
    0 ticks
    Buffer
    0 of 3 slots full
    empty (free slots)
    3
    full (items to take)
    0
    mutex
    free
    Produced / consumed
    0 / 0
  3. Producer puts item 1 in the buffer The buffer now holds 1 of 3. The producer is inside the critical section, so nobody else can change the buffer at the same moment.
    Time
    1 ticks
    Buffer
    1 of 3 slots full
    empty (free slots)
    2
    full (items to take)
    0
    mutex
    locked
    Produced / consumed
    1 / 0
  4. Producer signals full, waking the consumer Producer runs signal(full). The consumer was asleep on it, so it is woken and carries on.
    Time
    1 ticks
    Buffer
    1 of 3 slots full
    empty (free slots)
    2
    full (items to take)
    0
    mutex
    free
    Produced / consumed
    1 / 0
  5. Consumer takes item 1 The buffer now holds 0 of 3.
    Time
    1 ticks
    Buffer
    0 of 3 slots full
    empty (free slots)
    2
    full (items to take)
    0
    mutex
    locked
    Produced / consumed
    1 / 1
  6. Producer puts item 2 in the buffer The buffer now holds 1 of 3. The producer is inside the critical section, so nobody else can change the buffer at the same moment.
    Time
    2 ticks
    Buffer
    1 of 3 slots full
    empty (free slots)
    2
    full (items to take)
    0
    mutex
    locked
    Produced / consumed
    2 / 1
  7. Producer puts item 3 in the buffer The buffer now holds 2 of 3. The producer is inside the critical section, so nobody else can change the buffer at the same moment.
    Time
    3 ticks
    Buffer
    2 of 3 slots full
    empty (free slots)
    1
    full (items to take)
    1
    mutex
    locked
    Produced / consumed
    3 / 1
  8. Producer puts item 4 in the buffer The buffer now holds 3 of 3. The producer is inside the critical section, so nobody else can change the buffer at the same moment.
    Time
    4 ticks
    Buffer
    3 of 3 slots full
    empty (free slots)
    0
    full (items to take)
    2
    mutex
    locked
    Produced / consumed
    4 / 1
  9. Consumer takes item 2 The buffer now holds 2 of 3.
    Time
    4 ticks
    Buffer
    2 of 3 slots full
    empty (free slots)
    0
    full (items to take)
    2
    mutex
    locked
    Produced / consumed
    4 / 2
  10. Producer puts item 5 in the buffer The buffer now holds 3 of 3. The producer is inside the critical section, so nobody else can change the buffer at the same moment.
    Time
    5 ticks
    Buffer
    3 of 3 slots full
    empty (free slots)
    0
    full (items to take)
    2
    mutex
    locked
    Produced / consumed
    5 / 2
  11. Producer blocks on empty Producer runs wait(empty). The buffer is full: empty is 0, so there is nowhere to put the item. Producer sleeps until the other thread signals empty.
    Time
    6 ticks
    Buffer
    3 of 3 slots full
    empty (free slots)
    0
    full (items to take)
    3
    mutex
    free
    Produced / consumed
    5 / 2
  12. Consumer takes item 3 The buffer now holds 2 of 3.
    Time
    7 ticks
    Buffer
    2 of 3 slots full
    empty (free slots)
    0
    full (items to take)
    2
    mutex
    locked
    Produced / consumed
    5 / 3
  13. Consumer signals empty, waking the producer Consumer runs signal(empty). The producer was asleep on it, so it is woken and carries on.
    Time
    7 ticks
    Buffer
    2 of 3 slots full
    empty (free slots)
    0
    full (items to take)
    2
    mutex
    free
    Produced / consumed
    5 / 3
  14. Producer puts item 6 in the buffer The buffer now holds 3 of 3. The producer is inside the critical section, so nobody else can change the buffer at the same moment.
    Time
    7 ticks
    Buffer
    3 of 3 slots full
    empty (free slots)
    0
    full (items to take)
    2
    mutex
    locked
    Produced / consumed
    6 / 3
  15. Producer blocks on empty Producer runs wait(empty). The buffer is full: empty is 0, so there is nowhere to put the item. Producer sleeps until the other thread signals empty.
    Time
    8 ticks
    Buffer
    3 of 3 slots full
    empty (free slots)
    0
    full (items to take)
    3
    mutex
    free
    Produced / consumed
    6 / 3
  16. Consumer takes item 4 The buffer now holds 2 of 3.
    Time
    10 ticks
    Buffer
    2 of 3 slots full
    empty (free slots)
    0
    full (items to take)
    2
    mutex
    locked
    Produced / consumed
    6 / 4
  17. Consumer signals empty, waking the producer Consumer runs signal(empty). The producer was asleep on it, so it is woken and carries on.
    Time
    10 ticks
    Buffer
    2 of 3 slots full
    empty (free slots)
    0
    full (items to take)
    2
    mutex
    free
    Produced / consumed
    6 / 4
  18. Producer puts item 7 in the buffer The buffer now holds 3 of 3. The producer is inside the critical section, so nobody else can change the buffer at the same moment.
    Time
    10 ticks
    Buffer
    3 of 3 slots full
    empty (free slots)
    0
    full (items to take)
    2
    mutex
    locked
    Produced / consumed
    7 / 4
  19. Producer blocks on empty Producer runs wait(empty). The buffer is full: empty is 0, so there is nowhere to put the item. Producer sleeps until the other thread signals empty.
    Time
    11 ticks
    Buffer
    3 of 3 slots full
    empty (free slots)
    0
    full (items to take)
    3
    mutex
    free
    Produced / consumed
    7 / 4
  20. Consumer takes item 5 The buffer now holds 2 of 3.
    Time
    13 ticks
    Buffer
    2 of 3 slots full
    empty (free slots)
    0
    full (items to take)
    2
    mutex
    locked
    Produced / consumed
    7 / 5
  21. Consumer signals empty, waking the producer Consumer runs signal(empty). The producer was asleep on it, so it is woken and carries on.
    Time
    13 ticks
    Buffer
    2 of 3 slots full
    empty (free slots)
    0
    full (items to take)
    2
    mutex
    free
    Produced / consumed
    7 / 5
  22. Producer puts item 8 in the buffer The buffer now holds 3 of 3. The producer is inside the critical section, so nobody else can change the buffer at the same moment.
    Time
    13 ticks
    Buffer
    3 of 3 slots full
    empty (free slots)
    0
    full (items to take)
    2
    mutex
    locked
    Produced / consumed
    8 / 5
  23. Consumer takes item 6 The buffer now holds 2 of 3.
    Time
    16 ticks
    Buffer
    2 of 3 slots full
    empty (free slots)
    0
    full (items to take)
    2
    mutex
    locked
    Produced / consumed
    8 / 6
  24. Consumer takes item 7 The buffer now holds 1 of 3.
    Time
    19 ticks
    Buffer
    1 of 3 slots full
    empty (free slots)
    1
    full (items to take)
    1
    mutex
    locked
    Produced / consumed
    8 / 7
  25. Consumer takes item 8 The buffer now holds 0 of 3.
    Time
    22 ticks
    Buffer
    0 of 3 slots full
    empty (free slots)
    2
    full (items to take)
    0
    mutex
    locked
    Produced / consumed
    8 / 8
  26. All 8 items moved Every item went through the buffer exactly once. The buffer never held more than 3 of its 3 slots. The producer had to wait 3 times and the consumer 1 time: whichever side is faster spends time asleep, which is exactly what we want instead of wasting the CPU.
    Time
    22 ticks
    Buffer
    0 of 3 slots full
    empty (free slots)
    3
    full (items to take)
    0
    mutex
    free
    Produced / consumed
    8 / 8

The producer filled the buffer and then spent most of the run asleep, waiting for room. No item was lost and none was read twice, and no thread burned CPU while it waited. That is the whole point of the design.

Now the other way round: a slow producer (three ticks) and a fast consumer (one tick).

A slow producer and a fast consumer

  1. Start The producer makes 5 items and the consumer uses them. The buffer has 3 slots. Three semaphores keep them honest: empty counts free slots (starts at 3), full counts items waiting (starts at 0), and mutex lets one thread at a time touch the buffer itself.
    Time
    0 ticks
    Buffer
    0 of 3 slots full
    empty (free slots)
    3
    full (items to take)
    0
    mutex
    free
    Produced / consumed
    0 / 0
  2. Consumer blocks on full Consumer runs wait(full). The buffer is empty: full is 0, so there is nothing to take. Consumer sleeps until the other thread signals full.
    Time
    0 ticks
    Buffer
    0 of 3 slots full
    empty (free slots)
    3
    full (items to take)
    0
    mutex
    free
    Produced / consumed
    0 / 0
  3. Producer puts item 1 in the buffer The buffer now holds 1 of 3. The producer is inside the critical section, so nobody else can change the buffer at the same moment.
    Time
    3 ticks
    Buffer
    1 of 3 slots full
    empty (free slots)
    2
    full (items to take)
    0
    mutex
    locked
    Produced / consumed
    1 / 0
  4. Producer signals full, waking the consumer Producer runs signal(full). The consumer was asleep on it, so it is woken and carries on.
    Time
    3 ticks
    Buffer
    1 of 3 slots full
    empty (free slots)
    2
    full (items to take)
    0
    mutex
    free
    Produced / consumed
    1 / 0
  5. Consumer takes item 1 The buffer now holds 0 of 3.
    Time
    3 ticks
    Buffer
    0 of 3 slots full
    empty (free slots)
    2
    full (items to take)
    0
    mutex
    locked
    Produced / consumed
    1 / 1
  6. Consumer blocks on full Consumer runs wait(full). The buffer is empty: full is 0, so there is nothing to take. Consumer sleeps until the other thread signals full.
    Time
    4 ticks
    Buffer
    0 of 3 slots full
    empty (free slots)
    3
    full (items to take)
    0
    mutex
    free
    Produced / consumed
    1 / 1
  7. Producer puts item 2 in the buffer The buffer now holds 1 of 3. The producer is inside the critical section, so nobody else can change the buffer at the same moment.
    Time
    6 ticks
    Buffer
    1 of 3 slots full
    empty (free slots)
    2
    full (items to take)
    0
    mutex
    locked
    Produced / consumed
    2 / 1
  8. Producer signals full, waking the consumer Producer runs signal(full). The consumer was asleep on it, so it is woken and carries on.
    Time
    6 ticks
    Buffer
    1 of 3 slots full
    empty (free slots)
    2
    full (items to take)
    0
    mutex
    free
    Produced / consumed
    2 / 1
  9. Consumer takes item 2 The buffer now holds 0 of 3.
    Time
    6 ticks
    Buffer
    0 of 3 slots full
    empty (free slots)
    2
    full (items to take)
    0
    mutex
    locked
    Produced / consumed
    2 / 2
  10. Consumer blocks on full Consumer runs wait(full). The buffer is empty: full is 0, so there is nothing to take. Consumer sleeps until the other thread signals full.
    Time
    7 ticks
    Buffer
    0 of 3 slots full
    empty (free slots)
    3
    full (items to take)
    0
    mutex
    free
    Produced / consumed
    2 / 2
  11. Producer puts item 3 in the buffer The buffer now holds 1 of 3. The producer is inside the critical section, so nobody else can change the buffer at the same moment.
    Time
    9 ticks
    Buffer
    1 of 3 slots full
    empty (free slots)
    2
    full (items to take)
    0
    mutex
    locked
    Produced / consumed
    3 / 2
  12. Producer signals full, waking the consumer Producer runs signal(full). The consumer was asleep on it, so it is woken and carries on.
    Time
    9 ticks
    Buffer
    1 of 3 slots full
    empty (free slots)
    2
    full (items to take)
    0
    mutex
    free
    Produced / consumed
    3 / 2
  13. Consumer takes item 3 The buffer now holds 0 of 3.
    Time
    9 ticks
    Buffer
    0 of 3 slots full
    empty (free slots)
    2
    full (items to take)
    0
    mutex
    locked
    Produced / consumed
    3 / 3
  14. Consumer blocks on full Consumer runs wait(full). The buffer is empty: full is 0, so there is nothing to take. Consumer sleeps until the other thread signals full.
    Time
    10 ticks
    Buffer
    0 of 3 slots full
    empty (free slots)
    3
    full (items to take)
    0
    mutex
    free
    Produced / consumed
    3 / 3
  15. Producer puts item 4 in the buffer The buffer now holds 1 of 3. The producer is inside the critical section, so nobody else can change the buffer at the same moment.
    Time
    12 ticks
    Buffer
    1 of 3 slots full
    empty (free slots)
    2
    full (items to take)
    0
    mutex
    locked
    Produced / consumed
    4 / 3
  16. Producer signals full, waking the consumer Producer runs signal(full). The consumer was asleep on it, so it is woken and carries on.
    Time
    12 ticks
    Buffer
    1 of 3 slots full
    empty (free slots)
    2
    full (items to take)
    0
    mutex
    free
    Produced / consumed
    4 / 3
  17. Consumer takes item 4 The buffer now holds 0 of 3.
    Time
    12 ticks
    Buffer
    0 of 3 slots full
    empty (free slots)
    2
    full (items to take)
    0
    mutex
    locked
    Produced / consumed
    4 / 4
  18. Consumer blocks on full Consumer runs wait(full). The buffer is empty: full is 0, so there is nothing to take. Consumer sleeps until the other thread signals full.
    Time
    13 ticks
    Buffer
    0 of 3 slots full
    empty (free slots)
    3
    full (items to take)
    0
    mutex
    free
    Produced / consumed
    4 / 4
  19. Producer puts item 5 in the buffer The buffer now holds 1 of 3. The producer is inside the critical section, so nobody else can change the buffer at the same moment.
    Time
    15 ticks
    Buffer
    1 of 3 slots full
    empty (free slots)
    2
    full (items to take)
    0
    mutex
    locked
    Produced / consumed
    5 / 4
  20. Producer signals full, waking the consumer Producer runs signal(full). The consumer was asleep on it, so it is woken and carries on.
    Time
    15 ticks
    Buffer
    1 of 3 slots full
    empty (free slots)
    2
    full (items to take)
    0
    mutex
    free
    Produced / consumed
    5 / 4
  21. Consumer takes item 5 The buffer now holds 0 of 3.
    Time
    15 ticks
    Buffer
    0 of 3 slots full
    empty (free slots)
    2
    full (items to take)
    0
    mutex
    locked
    Produced / consumed
    5 / 5
  22. All 5 items moved Every item went through the buffer exactly once. The buffer never held more than 1 of its 3 slots. The producer had to wait 0 times and the consumer 5 times: whichever side is faster spends time asleep, which is exactly what we want instead of wasting the CPU.
    Time
    15 ticks
    Buffer
    0 of 3 slots full
    empty (free slots)
    3
    full (items to take)
    0
    mutex
    free
    Produced / consumed
    5 / 5

Press Try it and change the sizes and speeds. Whichever side is faster ends up asleep, and a bigger buffer smooths out bursts but cannot make the slow side go faster.

What if you swap the two waits?

The order of the two waits in the producer and the consumer matters. Suppose each thread locks the mutex first, and only then waits for a free slot or an item.

Swapped waits

  1. Start The producer makes 4 items and the consumer uses them. The buffer has 3 slots. Three semaphores keep them honest: empty counts free slots (starts at 3), full counts items waiting (starts at 0), and mutex lets one thread at a time touch the buffer itself. This version has a mistake in it: both threads take the mutex before they wait on empty or full.
    Time
    0 ticks
    Buffer
    0 of 3 slots full
    empty (free slots)
    3
    full (items to take)
    0
    mutex
    free
    Produced / consumed
    0 / 0
  2. Consumer locks the mutex Consumer runs wait(mutex) and gets it. It now holds the mutex while it goes on to wait for an item.
    Time
    0 ticks
    Buffer
    0 of 3 slots full
    empty (free slots)
    3
    full (items to take)
    0
    mutex
    locked
    Produced / consumed
    0 / 0
  3. Consumer blocks on full Consumer runs wait(full). The buffer is empty: full is 0, so there is nothing to take. Consumer sleeps until the other thread signals full. It is asleep while holding the mutex.
    Time
    0 ticks
    Buffer
    0 of 3 slots full
    empty (free slots)
    3
    full (items to take)
    0
    mutex
    locked
    Produced / consumed
    0 / 0
  4. Producer blocks on mutex Producer runs wait(mutex). The mutex is held by the other thread. Producer sleeps until the other thread signals mutex.
    Time
    1 ticks
    Buffer
    0 of 3 slots full
    empty (free slots)
    3
    full (items to take)
    0
    mutex
    locked
    Produced / consumed
    0 / 0
  5. Deadlock: both threads are asleep Neither thread can move. The consumer holds the mutex and waits for an item, while the producer needs that mutex to make one. Each is waiting for the other, forever. Taking the mutex first and the counting semaphore second is the mistake: the wait on empty or full must come BEFORE the mutex.
    Time
    1 ticks
    Buffer
    0 of 3 slots full
    empty (free slots)
    3
    full (items to take)
    0
    mutex
    locked
    Produced / consumed
    0 / 0

Both threads sleep forever, each waiting for something only the other can give. That is a deadlock, the subject of Module 5. The rule from this: wait for the counting semaphore first, then take the mutex, and never sleep while holding a lock that someone else will need.

Readers and writers

A shared data structure, such as a cache or a table, is read far more often than it is changed. Many readers can look at the same data at the same time without harm, since none of them changes it. A writer needs it all to itself. So the rule is:

  • any number of readers at once, or
  • exactly one writer, and no readers while it writes.

A read-write lock gives you that. A common design has the first reader lock out the writers, and the last reader let them back in:

readers_writers.c
sem_t room;            /* 1: whoever holds it has the data (a writer, or the group of readers) */
sem_t count_lock;      /* 1: protects the reader count */
int readers = 0;

void reader(void) {
    sem_wait(&count_lock);
    if (++readers == 1) sem_wait(&room);   /* the first reader locks writers out */
    sem_post(&count_lock);

    /* ...read the data, together with other readers... */

    sem_wait(&count_lock);
    if (--readers == 0) sem_post(&room);   /* the last reader lets writers in */
    sem_post(&count_lock);
}

void writer(void) {
    sem_wait(&room);                       /* alone */
    /* ...change the data... */
    sem_post(&room);
}

It is correct, but unfair. As long as new readers keep arriving before the old ones leave, readers never drops to 0, room is never released, and a waiting writer sleeps forever. This is writer starvation, the same kind of problem as a long job starved by short ones in scheduling (Lesson 3.2). Fair versions make new readers queue behind a waiting writer, at some cost to read speed. When you pick a read-write lock, check which version you have.

Dining philosophers

Five philosophers sit at a round table. Between each pair of neighbours is one fork. A philosopher needs both adjacent forks to eat. Each one picks up the fork on the left, then the one on the right, eats, and puts both down. It looks harmless. Watch.

Everyone takes the left fork

  1. Start 5 philosophers sit at a round table with one fork between each pair. To eat, a philosopher needs both neighbouring forks. Each one will pick up one fork, then the other, eat, and put both down. Every philosopher picks up the fork on the left first.
    Have eaten
    0 of 5
    Eating now
    0
    Forks in use
    0 of 5
    Waiting
    nobody
  2. P0 picks up fork 0 Fork 0 was free, so P0 takes it and now holds one fork.
    Have eaten
    0 of 5
    Eating now
    0
    Forks in use
    1 of 5
    Waiting
    nobody
  3. P1 picks up fork 1 Fork 1 was free, so P1 takes it and now holds one fork.
    Have eaten
    0 of 5
    Eating now
    0
    Forks in use
    2 of 5
    Waiting
    nobody
  4. P2 picks up fork 2 Fork 2 was free, so P2 takes it and now holds one fork.
    Have eaten
    0 of 5
    Eating now
    0
    Forks in use
    3 of 5
    Waiting
    nobody
  5. P3 picks up fork 3 Fork 3 was free, so P3 takes it and now holds one fork.
    Have eaten
    0 of 5
    Eating now
    0
    Forks in use
    4 of 5
    Waiting
    nobody
  6. P4 picks up fork 4 Fork 4 was free, so P4 takes it and now holds one fork.
    Have eaten
    0 of 5
    Eating now
    0
    Forks in use
    5 of 5
    Waiting
    nobody
  7. P0 waits for fork 1 P0 wants fork 1, but P1 is holding it. P0 must wait, and keeps the fork it already has.
    Have eaten
    0 of 5
    Eating now
    0
    Forks in use
    5 of 5
    Waiting
    P0
  8. P1 waits for fork 2 P1 wants fork 2, but P2 is holding it. P1 must wait, and keeps the fork it already has.
    Have eaten
    0 of 5
    Eating now
    0
    Forks in use
    5 of 5
    Waiting
    P0, P1
  9. P2 waits for fork 3 P2 wants fork 3, but P3 is holding it. P2 must wait, and keeps the fork it already has.
    Have eaten
    0 of 5
    Eating now
    0
    Forks in use
    5 of 5
    Waiting
    P0, P1, P2
  10. P3 waits for fork 4 P3 wants fork 4, but P4 is holding it. P3 must wait, and keeps the fork it already has.
    Have eaten
    0 of 5
    Eating now
    0
    Forks in use
    5 of 5
    Waiting
    P0, P1, P2, P3
  11. P4 waits for fork 0 P4 wants fork 0, but P0 is holding it. P4 must wait, and keeps the fork it already has.
    Have eaten
    0 of 5
    Eating now
    0
    Forks in use
    5 of 5
    Waiting
    P0, P1, P2, P3, P4
  12. Deadlock: everyone is waiting Nobody can move. Every philosopher holds one fork and needs the next one, which a neighbour is holding. Each waits for the next, round the whole table, forever. This is a deadlock.
    Have eaten
    0 of 5
    Eating now
    0
    Forks in use
    5 of 5
    Waiting
    P0, P1, P2, P3, P4

All five picked up the left fork, each at a moment when it was free, and then all five waited. Every individual step was legal. It is the combination that is stuck. This pattern is the classic deadlock: holding one resource while waiting for another, in a circle.

The simplest cure is to break the circle. Let the last philosopher pick up the right fork first.

One philosopher goes right first

  1. Start 5 philosophers sit at a round table with one fork between each pair. To eat, a philosopher needs both neighbouring forks. Each one will pick up one fork, then the other, eat, and put both down. One philosopher (P4) is told to pick up the fork on the other side first.
    Have eaten
    0 of 5
    Eating now
    0
    Forks in use
    0 of 5
    Waiting
    nobody
  2. P0 picks up fork 0 Fork 0 was free, so P0 takes it and now holds one fork.
    Have eaten
    0 of 5
    Eating now
    0
    Forks in use
    1 of 5
    Waiting
    nobody
  3. P1 picks up fork 1 Fork 1 was free, so P1 takes it and now holds one fork.
    Have eaten
    0 of 5
    Eating now
    0
    Forks in use
    2 of 5
    Waiting
    nobody
  4. P2 picks up fork 2 Fork 2 was free, so P2 takes it and now holds one fork.
    Have eaten
    0 of 5
    Eating now
    0
    Forks in use
    3 of 5
    Waiting
    nobody
  5. P3 picks up fork 3 Fork 3 was free, so P3 takes it and now holds one fork.
    Have eaten
    0 of 5
    Eating now
    0
    Forks in use
    4 of 5
    Waiting
    nobody
  6. P4 waits for fork 0 P4 wants fork 0, but P0 is holding it. P4 must wait, and keeps the fork it already has.
    Have eaten
    0 of 5
    Eating now
    0
    Forks in use
    4 of 5
    Waiting
    P4
  7. P0 waits for fork 1 P0 wants fork 1, but P1 is holding it. P0 must wait, and keeps the fork it already has.
    Have eaten
    0 of 5
    Eating now
    0
    Forks in use
    4 of 5
    Waiting
    P0, P4
  8. P1 waits for fork 2 P1 wants fork 2, but P2 is holding it. P1 must wait, and keeps the fork it already has.
    Have eaten
    0 of 5
    Eating now
    0
    Forks in use
    4 of 5
    Waiting
    P0, P1, P4
  9. P2 waits for fork 3 P2 wants fork 3, but P3 is holding it. P2 must wait, and keeps the fork it already has.
    Have eaten
    0 of 5
    Eating now
    0
    Forks in use
    4 of 5
    Waiting
    P0, P1, P2, P4
  10. P3 picks up fork 4 Fork 4 was free, so P3 takes it. P3 now holds both forks.
    Have eaten
    0 of 5
    Eating now
    0
    Forks in use
    5 of 5
    Waiting
    P0, P1, P2, P4
  11. P3 eats P3 has both forks and eats.
    Have eaten
    0 of 5
    Eating now
    1
    Forks in use
    5 of 5
    Waiting
    P0, P1, P2, P4
  12. P3 puts both forks down P3 has finished and puts both forks back on the table, so the neighbours may be able to use them.
    Have eaten
    1 of 5
    Eating now
    0
    Forks in use
    3 of 5
    Waiting
    P0, P1, P2, P4
  13. P2 picks up fork 3 Fork 3 was free, so P2 takes it. P2 now holds both forks.
    Have eaten
    1 of 5
    Eating now
    0
    Forks in use
    4 of 5
    Waiting
    P0, P1, P4
  14. P2 eats P2 has both forks and eats.
    Have eaten
    1 of 5
    Eating now
    1
    Forks in use
    4 of 5
    Waiting
    P0, P1, P4
  15. P2 puts both forks down P2 has finished and puts both forks back on the table, so the neighbours may be able to use them.
    Have eaten
    2 of 5
    Eating now
    0
    Forks in use
    2 of 5
    Waiting
    P0, P1, P4
  16. P1 picks up fork 2 Fork 2 was free, so P1 takes it. P1 now holds both forks.
    Have eaten
    2 of 5
    Eating now
    0
    Forks in use
    3 of 5
    Waiting
    P0, P4
  17. P1 eats P1 has both forks and eats.
    Have eaten
    2 of 5
    Eating now
    1
    Forks in use
    3 of 5
    Waiting
    P0, P4
  18. P1 puts both forks down P1 has finished and puts both forks back on the table, so the neighbours may be able to use them.
    Have eaten
    3 of 5
    Eating now
    0
    Forks in use
    1 of 5
    Waiting
    P0, P4
  19. P0 picks up fork 1 Fork 1 was free, so P0 takes it. P0 now holds both forks.
    Have eaten
    3 of 5
    Eating now
    0
    Forks in use
    2 of 5
    Waiting
    P4
  20. P0 eats P0 has both forks and eats.
    Have eaten
    3 of 5
    Eating now
    1
    Forks in use
    2 of 5
    Waiting
    P4
  21. P0 puts both forks down P0 has finished and puts both forks back on the table, so the neighbours may be able to use them.
    Have eaten
    4 of 5
    Eating now
    0
    Forks in use
    0 of 5
    Waiting
    P4
  22. P4 picks up fork 0 Fork 0 was free, so P4 takes it and now holds one fork.
    Have eaten
    4 of 5
    Eating now
    0
    Forks in use
    1 of 5
    Waiting
    nobody
  23. P4 picks up fork 4 Fork 4 was free, so P4 takes it. P4 now holds both forks.
    Have eaten
    4 of 5
    Eating now
    0
    Forks in use
    2 of 5
    Waiting
    nobody
  24. P4 eats P4 has both forks and eats.
    Have eaten
    4 of 5
    Eating now
    1
    Forks in use
    2 of 5
    Waiting
    nobody
  25. P4 puts both forks down P4 has finished and puts both forks back on the table, so the neighbours may be able to use them.
    Have eaten
    5 of 5
    Eating now
    0
    Forks in use
    0 of 5
    Waiting
    nobody
  26. Everyone has eaten All 5 philosophers have eaten. One of them picked up the forks in a different order from the rest, so the circle of waiting could never close.
    Have eaten
    5 of 5
    Eating now
    0
    Forks in use
    0 of 5
    Waiting
    nobody

Now somebody always has a way forward. P4 is waiting for fork 0, which P0 holds, so P4 never grabs fork 4, and P3 can complete its pair and eat. When it puts its forks down, P2 can go, and so on, round the table. The other philosophers still wait sometimes, but the waiting never becomes a circle.

The lesson is general. If every thread takes its locks in a different order, you are one unlucky schedule away from a deadlock. If all threads agree on an order, no circle can form. That is lock ordering, and it is the first thing to try. Module 5 shows why it works.

In the real world

The bounded buffer is everywhere. A Unix pipe (next lesson) is one, with the kernel as the buffer. A web server’s request queue, a logging thread fed by many others, and an audio player’s buffer are the same pattern: a producer, a consumer, a bounded queue, and back-pressure when the consumer cannot keep up.

Readers and writers is how databases and file systems let many queries run together while a change gets exclusive access. In Linux, the pthread_rwlock_t is a read-write lock, and the kernel has its own versions. The philosophers are a model for any code that needs several locks at once, which is why large systems document the order in which locks must be taken.

Quick check

Quick check

  1. In the bounded buffer, what do the semaphores empty and full count?
  2. What goes wrong if the producer takes the mutex before it waits on empty?
  3. What is writer starvation in a readers-writers lock?
  4. In the dining philosophers, how does making one philosopher pick up the right fork first stop the deadlock?

Recap

  • Bounded buffer: empty counts free slots, full counts items, mutex guards the buffer. The faster side ends up asleep, which is the back-pressure we want.
  • Wait for the counting semaphore before taking the mutex. Sleeping while holding a lock others need is a deadlock.
  • Readers and writers: many readers or one writer. Simple versions can starve writers.
  • Dining philosophers: everyone holding one resource and waiting for another is a deadlock. A fixed lock order breaks the circle.

Next: so far the threads shared memory. Processes cannot. How do they talk to each other?

Your progress is saved in this browser. Sign in to keep it on every device. Free, no password.

Join the discussion