1. Home
  2. Operating Systems
  3. Dining Philosophers Problem

Dining Philosophers Problem

Five philosophers, five forks, and a table where everyone grabs a fork at once. See deadlock happen and two classic ways to prevent it.

Interactive 3DIntermediate12 min readOSUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · Left arrowRight arrow step

What's happening

The dining philosophers problem is the classic example of deadlock and starvation. Five philosophers, five forks, and everybody grabs a fork at the same time. Press Dine.

Pseudocode

  1. philosopher i needs fork i and fork (i−1) mod 5
  2. naive: pick up fork i, then fork i−1 // can deadlock!
  3. ordered: pick up the lower-numbered fork first, then the other
  4. waiter: enter the room only if fewer than 4 are inside
  5. eat for one round, then put both forks down
Read this model as text · 5 steps

One run of the model above, written out step by step. Useful with a screen reader, on a slow connection, or for quick revision. The values may differ from the run on your screen.

  1. The dining philosophers problem is the classic example of deadlock and starvation. Five philosophers, five forks, and everybody grabs a fork at the same time. Press Dine.
  2. Five philosophers share five forks. Each needs two forks (the one on each side) to eat. Strategy: Naive: first take the right fork, then the left.
  3. Round 1: P0 picks up fork 0; P1 picks up fork 1; P2 picks up fork 2; P3 picks up fork 3; P4 picks up fork 4.
  4. Deadlock! Nobody can move: P0 waits for fork 4 (held by P4); P1 waits for fork 0 (held by P0); P2 waits for fork 1 (held by P1); P3 waits for fork 2 (held by P2); P4 waits for fork 3 (held by P3). Each philosopher holds one fork and waits for a neighbour who is waiting too.
  5. All four deadlock conditions hold: mutual exclusion, hold and wait, no preemption and circular wait. Remove any one (for example circular wait by ordering the forks) and the problem disappears.

Try this in the 3D model

  • Run the Naive strategy. In which round does it deadlock, and what is every philosopher holding?
  • Run Resource ordering. Which philosopher picks up forks in a different order from the others?
  • Run the Waiter strategy. Who has to wait outside, and why does that prevent deadlock?
  • For each strategy, name which of the four deadlock conditions is broken.

The setting

Five philosophers sit around a round table. Between each pair lies one fork, so there are five forks in total. A philosopher alternates between thinking and eating, and to eat needs both the fork on the left and the fork on the right. Two neighbours can never use the same fork at the same time (mutual exclusion).

It looks harmless, but it models real problems: several processes each needing two locks.

The naive solution deadlocks

think()
pick_up(fork[i])
pick_up(fork[i−1])
eat()
put_down both

If all five philosophers pick up their first fork at the same moment, each holds one fork and waits for the neighbour’s fork. Nobody can proceed. This is a deadlock.

Four conditions for deadlock

A deadlock needs all four at once:

  1. Mutual exclusion: a fork has one owner.
  2. Hold and wait: a philosopher keeps one fork while waiting for the other.
  3. No preemption: forks cannot be taken away.
  4. Circular wait: P0 waits for P1, P1 for P2, …, P4 for P0.

Remove one of them and deadlock is impossible. Compare with the Banker’s algorithm (avoidance) and the wait-for graph (detection).

Fix 1: resource ordering

Number the forks and always pick up the lower-numbered fork first. Philosopher 4 (forks 4 and 3) now takes fork 3 first, while philosopher 0 (forks 0 and 4) takes fork 0 first. A cycle of waiting can no longer form, so circular wait is broken.

Fix 2: a waiter

A waiter lets at most n − 1 = 4 philosophers sit down. With 4 people and 5 forks, at least one philosopher always finds both forks free, finishes, and releases them. This is the same idea as a counting semaphore in process synchronization.

Code

import threading

forks = [threading.Lock() for _ in range(5)]
seats = threading.Semaphore(4)          # the waiter

def philosopher(i):
    left, right = i, (i - 1) % 5
    first, second = sorted((left, right))   # resource ordering
    with seats:                              # at most 4 at the table
        with forks[first]:
            with forks[second]:
                print(f"P{i} eats")

Common mistakes

  • Thinking that more forks is the only fix. Changing the order or limiting entry is enough.
  • Releasing the first fork when the second is busy without a back-off. This avoids deadlock but can cause livelock.
  • Confusing deadlock (nobody moves) with starvation (somebody never gets a turn).

Complexity at a glance

Case / operationTimeWhy
Naive solutioncan deadlockCircular wait is possible when everyone holds one fork.
Resource orderingdeadlock-freeBreaks circular wait; starvation is still possible with an unfair scheduler.
Waiter (n − 1 seats)deadlock-freeAt least one philosopher can always get both forks.
Extra spaceOne lock or semaphore per fork

Quick check

Test yourself — pick an answer to see if you got it.

1. Why does the naive solution deadlock?

2. Which of Coffman's conditions does resource ordering break?

3. Why does letting only 4 philosophers sit at the table prevent deadlock?

4. What is starvation?

Saved in this browser.
Spotted a mistake or a bug in the 3D model?

Report a mistake

in Dining Philosophers Problem. Thank you — every report makes the lesson better for the next reader.

We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.