The dining philosophers problem explained

Some of the hardest bugs in computing come not from wrong calculations but from programs waiting for each other. The dining philosophers, a puzzle from the 1960s, shrinks that problem to a dinner table: a handful of philosophers, a few forks and one simple rule. It is still the standard way to explain deadlock in courses on operating systems and concurrent programming.

Five philosophers round a table with five forks; four hold a fork and wait, shown by arrows, around a closed circle
Five philosophers round a table with five forks; four hold a fork and wait, shown by arrows, around a closed circle

The puzzle

Five philosophers sit round a table. They spend their lives thinking and eating. Between each pair of neighbours lies a single fork, and a philosopher can only eat with both forks next to them, picked up one at a time. Philosophers do not talk to each other. The task is to give them a rule for picking up forks so that nobody gets stuck and everybody eats again and again.

Where it comes from

In 1965 Edsger Dijkstra set an exam question at the Technical University of Eindhoven about five computers competing for access to shared tape drives. Tony Hoare soon retold it with philosophers and forks, sometimes chopsticks, which made it memorable. Dijkstra was then developing semaphores, one of the first tools for coordinating programs that run at the same time, and the puzzle showed why such tools are needed and how easily they are misused.

Edsger W. Dijkstra at the blackboard
Edsger W. Dijkstra at the blackboard
Tony Hoare, who invented quicksort in 1959 and published it in 1961.
Tony Hoare, who invented quicksort in 1959 and published it in 1961.

The deadlock

The obvious rule is to pick up the left fork, then the right one, eat, and put both down. It works almost always. But if all five pick up their left fork at the same moment, every right fork is already taken, and each philosopher waits for a neighbour who is also waiting. Nobody will ever eat again. Because it needs bad timing, such a deadlock may appear once in a million runs, which makes it very hard to find by testing.

The four conditions

In 1971 Edward Coffman and colleagues listed four conditions that must all hold for a deadlock. Mutual exclusion: a fork can be held by only one philosopher. Hold and wait: a philosopher keeps one fork while waiting for another. No preemption: nobody can take a fork away. Circular wait: there is a circle of philosophers, each waiting for the next. Every cure for deadlock breaks at least one of these conditions.

Cures that work

Resource ordering numbers the forks and has everyone pick up the lower number first, which makes a circle of waiting impossible; it is the rule programmers use when they lock several resources. An arbitrator, the waiter, lets only four of the five sit down, or hands out both forks at once. In 1984 K. Mani Chandy and Jayadev Misra gave a solution with no central waiter, in which forks are clean or dirty and passed on request, that also prevents starvation and works for any number of philosophers.

Livelock and starvation

Removing a deadlock does not guarantee a good dinner. If philosophers put down their first fork whenever the second is taken and try again, they can all repeat the same moves in step forever: a livelock, as when two people in a corridor keep stepping aside the same way. Waiting a random time before trying again breaks the rhythm, the same trick Ethernet uses after a collision. Starvation is subtler: the dinner goes on, but one philosopher is unlucky every time and waits much longer than the others.

Real deadlocks

Databases lock rows and tables and must detect deadlocks between transactions, usually by finding a circle in a graph of who waits for whom and cancelling one transaction. Operating systems lock files and devices, and multithreaded programs lock shared memory. In 1997 the Mars Pathfinder probe kept resetting itself because of a related problem, priority inversion, between tasks sharing a resource; engineers fixed it from Earth by changing one setting.

Explore it

Open the dining philosophers simulator to watch two to nine philosophers think, wait and eat. An arrow shows who waits for which fork, and a timeline records every step. Leave left fork first running until it freezes, or press All hungry at once to cause a deadlock immediately, then switch to ordered forks, the waiter or putting a fork back, and compare meals and waiting times. With fixed waits the polite philosophers fall into a livelock; random waits set them free.

Frequently asked questions

Why philosophers and forks?

It is just a story that makes the abstract problem easy to picture. The philosophers stand for programs or processes, the forks for resources such as files, printers, database rows or locks, and eating for doing work that needs two resources at once. Some versions use chopsticks, which makes needing two of them more natural.

Can a deadlock be detected instead of prevented?

Yes. A system can build a graph of who is waiting for whom and look for a circle. If it finds one, it breaks the deadlock by cancelling or restarting one of the waiting programs. Databases do exactly this; the simulator shows the circle as red arrows.

Does the problem get worse with more philosophers?

A deadlock with left fork first needs everyone to hold a fork at the same moment, which gets rarer as the table grows, so it can take longer to appear. It is still certain to happen eventually. With only two philosophers it happens quickly.

What does this have to do with semaphores and mutexes?

A mutex or semaphore is how a program picks up a fork: it locks a resource so that nobody else can use it until it is released. The dining philosophers show that locks alone are not enough: the order in which they are taken decides whether a program can freeze.

Ready to try it?

Open PictorX in your browser — no install, no credit card required to start.

Open the dining philosophers simulator