Table
Meals
Longest wait (steps)
Step

  • Thinking
  • Hungry, no fork
  • Hungry, holding one fork
  • Eating

More about the dining philosophers problem

Dining philosophers simulator

Five philosophers sit round a table with a bowl of spaghetti each and one fork between each pair of neighbours. To eat, a philosopher needs both forks next to them. It sounds harmless, yet if every philosopher picks up the left fork at the same moment, each one waits forever for the right one. Edsger Dijkstra's puzzle is the classic picture of a deadlock, the bug that freezes programs sharing files, database rows or memory. Watch the dinner run, freeze it on purpose, and try the strategies that keep everyone fed. Philosophers sit round a table with one fork between each pair, and each needs both neighbouring forks to eat. Press Play to watch them think (grey), get hungry (orange) and eat (green); a dashed arrow shows the fork someone is waiting for. Choose a strategy on the settings panel. Left fork first can freeze in a deadlock, sooner with a longer pause between the two forks. Press All hungry at once to make everyone reach at the same moment, or click a philosopher to make them hungry. The timeline below shows what each philosopher did, step by step.

How it works

  1. Press Play and watch the philosophers think, get hungry and eat, with an arrow from each waiting philosopher to the fork they need.
  2. Keep left fork first and press All hungry at once, or simply wait: sooner or later the dinner freezes in a deadlock.
  3. Switch to lower-numbered fork first, the waiter or putting the fork back, and compare meals, waiting times and the timeline.

Why the dinner freezes

With left fork first, every philosopher picks up the fork on their left and then waits for the one on their right. Most of the time someone gets both forks, eats and puts them down, and the queue moves on. But if all of them hold their left fork at once, each one's right fork is the left fork of a neighbour who is waiting too. The waiting forms a closed circle, nobody will ever let go, and the dinner stops for good. The longer a philosopher holds one fork before reaching for the second, the more likely that moment becomes, which is why such bugs can hide in a program for months.

Three ways out

A deadlock needs four things at once: forks that only one can hold, holding one while waiting for another, nobody taking a fork away, and a circle of waiting. Remove any one and it cannot happen. Numbering the forks and always taking the lower number first breaks the circle, because one philosopher reaches the other way. A waiter who lets at most four of the five sit down leaves room for someone to get both forks. Putting the first fork back when the second is taken removes the holding, but philosophers who move in step can then keep picking up and putting down forever, a livelock, unless they wait a random time before trying again.

Frequently asked questions

What is a deadlock?

A situation in which every member of a group waits for something another member holds, so nobody can ever continue. Here each philosopher holds one fork and waits for the next. In a computer, two programs that each lock a file and then wait for the other's file are deadlocked in the same way.

What is the difference between deadlock, livelock and starvation?

In a deadlock nothing moves: everybody is blocked. In a livelock everybody keeps busy, picking up and putting down forks, but no progress is made. Starvation means the dinner as a whole goes on, but one philosopher is unlucky again and again and waits much longer than the others; the longest wait tile shows it.

Why does taking the lower-numbered fork first work?

A deadlock needs a circle in which each philosopher waits for the next. If everyone takes forks in increasing order, the philosopher waiting for the highest-numbered fork already holds a lower one, and nobody can hold the highest fork while waiting for a lower one, so the circle can never close. Programs use the same rule: always lock resources in a fixed order.

Is the waiter fair?

The waiter seats hungry philosophers in the order they arrived, so nobody is passed over for ever, and with one seat empty some seated philosopher can always eat. The price is a central waiter that everyone must ask, which can become a bottleneck in a real system.

What are the four conditions for a deadlock?

Mutual exclusion (a fork has one holder), hold and wait (a philosopher keeps one fork while waiting for the other), no preemption (nobody takes a fork away) and circular wait. These are the Coffman conditions. Remove any one and no deadlock can occur: lower-numbered fork first breaks the circle, and putting the fork back breaks hold and wait.

Dining Philosophers: how do I record a video?

Press the red Record button in the playback bar: it films what the stage shows. Press it again to stop, and the video is saved to your device. Recording is free; you only need to sign in. Without a subscription the video carries a small PictorX logo in a corner. The button beside Record opens the video options: the shape (as shown, square, story 9:16 or wide 16:9), the length (until you stop, or 5, 10, 15 or 30 seconds for social media) and the file: MP4, WebM or an animated GIF. You can add one of three music tracks: calm, bright or beat.

Ready to try it?

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

Go to Editor