Deadlock
Deadlock is not bad luck and it is not a race you lose occasionally. It is a cycle: each program holds something the next one needs, all the way round. With five philosophers and five forks the whole world is small enough to count rather than watch — every order in which they can reach for forks is enumerable, the ones that hang can be listed exactly, and the fix can be checked against all of them instead of demonstrated on one.
Five philosophers, five forks, and the order that hangs
1 Five philosophers, five forks, and each one reaching left then right
Five philosophers around a table, one fork between each pair. To eat you need both.
2 The orders that finish, and the orders where every philosopher holds one fork
One round of reaching, everybody taking a single fork before anybody takes a second. This is one specific order, not an accident.
| philosopher | holds | waiting for | held by |
|---|---|---|---|
| Aristotle | fork 0 | fork 1 | Boethius |
| Boethius | fork 1 | fork 2 | Cicero |
| Cicero | fork 2 | fork 3 | Diogenes |
| Diogenes | fork 3 | fork 4 | Epictetus |
| Epictetus | fork 4 | fork 0 | Aristotle |
Every one of them holds a fork and waits for the next: Aristotle → Boethius → Cicero → Diogenes → Epictetus → Aristotle. Nothing can move, and nothing is broken.
3 All four of Coffman's conditions, checked against the state that hung
Coffman’s four conditions, read off the state above rather than recited. Deadlock needs all four; removing any one is enough.
| condition | holds? | why |
|---|---|---|
| mutual exclusion | yes | a fork is held by one philosopher or none; that is what a fork is |
| hold and wait | yes | 5 are holding one fork and waiting for another |
| no pre-emption | yes | nothing takes a fork back; a philosopher puts one down only after eating |
| circular wait | yes | a cycle: Aristotle waits for Boethius waits for Cicero waits for Diogenes waits for Epictetus |
4 Number the forks and take the lower first: every order finishes, counted
Every opening order there is, counted both ways. Not a sample.
- opening orders
- 3,125
- hang, reaching left first
- 278 (8.9 per cent)
- hang, taking the lower fork first
- 0
Neither rare nor certain, which is exactly what makes deadlock hard to find by running the program: 278 of 3125 openings hang, and the other 2847 finish and tell you nothing is wrong.
These ran in this browser when the page loaded. Each claim, whether it held, and the number behind it.
| claim | held | measured |
|---|---|---|
| taking left then right, one round each, deadlocks all 5 | yes | a cycle of 5: every philosopher waits for the next |
| of all 3125 opening interleavings, 278 deadlock | yes | it is neither rare nor certain, which is what makes it hard to catch by running it |
| numbering the forks and taking the lower first: 0 of 3125 deadlock | yes | every opening finishes |
| the ordered version breaks exactly one of Coffman's four conditions | yes | circular wait |
What is real here, and what is not
This counts openings, not every possible schedule
The enumeration walks every way the first five grabs can be interleaved — 3,125 of them — and then lets the system run freely to a fixed point. That is not the same as every possible schedule of the whole meal, which is unbounded. What it does establish is exactly what the page claims: of those openings, 278 reach a state from which nothing can proceed, and none do once the forks are numbered.
The fix is a total order on the resource, not politeness
Taking the lower-numbered fork first works because it makes a cycle impossible: in any set of philosophers each holding one fork, one of them holds the highest-numbered fork in the set and is therefore waiting for nothing in it. Timeouts, backoff and retrying are not this. They make deadlock rarer and leave it possible, and this page does not model them.
Nothing here is concurrent
Five philosophers is a loop over five entries in an array, and the interleaving is a list this page walks. There is no thread, no lock and no scheduler, so the page cannot show you a race — only why the counting rule is the right counting rule. A real system adds the part where you cannot see the state at all, which is most of what makes this hard.
The problem is Dijkstra's, the name is Hoare's, and the first citation here was wrong
This page cited EWD123, Cooperating Sequential Processes, as where the dining philosophers first appear. They do not appear in it: the archived copy in this repository is 109,280 bytes and contains the words philosopher, dining, quintuple and fork exactly nought times. What it does contain is the semaphore and the deadly embrace, which is why it is still cited. Dijkstra gives the history himself in EWD1000: EWD123 “was written in 1965 and served as lecture notes for my course in the fall semester of that year. (The problem of The Dining Quintuple —later dubbed ‘The Dining Philosophers’ by Tony Hoare— was the examination problem at the end of that semester.)” So the year is right, the document was not, and the familiar name is Hoare's rather than Dijkstra's. EWD198 works the problem under Dijkstra's name for it. An outside reader caught this on the live page on 2026-08-22. No gate here could have: every one of them checks that a cited document was fetched, not that it says what the page says it says.
Numbering the forks fixes deadlock and does nothing about starvation
A total order on the resource guarantees the group as a whole keeps moving. It guarantees nothing to any particular philosopher: with the right repeated schedule one of them can wait while the others eat, forever, and every check on this page stays green throughout because nothing is deadlocked. The two problems get confused constantly and they have different fixes — starvation wants fairness, a queue or a ticket, which this model does not have. Dijkstra's own solution used a semaphore per philosopher for exactly that reason.
Sources
- E. W. Dijkstra, Cooperating Sequential Processes, EWD123, 1965. The lecture notes for the course: semaphores, mutual exclusion, and the deadly embrace. The dining philosophers are not in it.
- E. W. Dijkstra, EWD198. Working the “Problem of the Dining Quintuple”, which is this problem under the name Dijkstra gave it.
- E. W. Dijkstra, EWD1000. His own account of where it came from: the Dining Quintuple was the examination problem at the end of the 1965 course, and Hoare renamed it later.
- E. G. Coffman, M. Elphick and A. Shoshani, System Deadlocks, ACM Computing Surveys 3(2), 1971. The four necessary conditions.
- Logical Art, the studio this belongs to.