Info & Theory
In the dining philosophers problem, five philosophers
sit around a table with one fork between each
pair. A philosopher must hold both neighbouring forks to
eat, so forks are shared resources needing coordination.
Naive — deadlock
Each grabs the left fork, then waits for the
right. If all grab left at once, everyone holds
one fork and waits forever — deadlock via circular wait.
Resource ordering
Number the forks; always take the
lower-numbered fork first. One philosopher reaches
for the same fork as a neighbour and loses, breaking the cycle.
Arbiter / waiter
A central waiter grants permission to pick up forks, limiting how many philosophers contend at once and serialising access so no cycle forms.
Limit to 4 seated
Allow at most four of the five to reach for forks. With
four philosophers and five forks, at least one can always get
both — breaking hold-and-wait.
Coffman conditions
Deadlock needs all four: mutual exclusion, hold-and-wait, no preemption and circular wait. Each strategy here removes one of them.
Frequently asked questions
What is the dining philosophers problem?
It is a classic concurrency puzzle by Edsger Dijkstra: five philosophers sit around a table with one fork between each pair. A philosopher needs both neighbouring forks to eat, so the forks model shared resources that must be coordinated without deadlock or starvation.
Why does the naive solution deadlock?
If every philosopher picks up their left fork first and then waits for the right, all five can hold one fork and wait forever for the other. This circular wait satisfies all four Coffman conditions, producing deadlock.
What are the four Coffman conditions for deadlock?
Deadlock requires mutual exclusion, hold-and-wait, no preemption and circular wait to all hold simultaneously. Breaking any one of them prevents deadlock, which is exactly what the strategies in this simulation do.
How does resource ordering prevent deadlock?
If forks are numbered and every philosopher always picks up the lower-numbered fork first, the circular-wait condition cannot form. At least one philosopher will grab two forks and eat, breaking the cycle.
How does the arbiter (waiter) solution work?
A central waiter grants permission to pick up forks. By allowing at most a safe number of philosophers to reach for forks at once, the arbiter serialises contention and guarantees no circular wait can form.
Why does limiting seats to four help?
With only four of the five philosophers allowed to sit and reach for forks at once, there are always enough forks for at least one philosopher to obtain both and eat. This breaks hold-and-wait at the table level.
What is the difference between deadlock and starvation?
Deadlock means no philosopher can make progress at all. Starvation means the system progresses, but a particular philosopher is repeatedly unlucky and never gets to eat. A good solution avoids both.
What states does a philosopher cycle through?
Each philosopher cycles through thinking, becoming hungry and trying to acquire forks, then eating once both forks are held, before releasing the forks and thinking again.
How does a deadlock detector work?
A simple detector flags deadlock when every philosopher is hungry and holding one fork while waiting for another, so no philosopher can ever proceed. In this simulation the detector raises a warning when all five are stuck.
Is the dining philosophers problem still relevant?
Yes. It models real concurrent systems where threads compete for several locks, such as databases and operating-system kernels. The same lock-ordering and arbitration techniques used to solve it are used in production software.