AlgoMaster Logo

The Dining Philosophers

medium8 min readUpdated August 31, 2026
Listen to this chapter
Unlock Audio

Five philosophers sit around a circular table. There is one fork between each pair of neighboring philosophers, and a philosopher can eat only while holding both adjacent forks.

The philosophers are numbered 0 through 4. For philosopher p:

  • The left fork is fork p.
  • The right fork is fork (p + 1) % 5.

The judge calls:

wantsToEat(philosopher, pickLeftFork, pickRightFork, eat, putLeftFork, putRightFork)

The supplied callbacks record the philosopher's actions. Your implementation must invoke them so that:

  • A fork is held by at most one philosopher at a time.
  • A philosopher calls eat only while holding both adjacent forks.
  • Every picked-up fork is put down before wantsToEat returns.
  • All philosophers eventually finish without deadlock.

The judge creates one shared DiningPhilosophers instance and five threads. Each thread calls wantsToEat n times for its philosopher.

The judge also preloads the standard concurrency and callback APIs for each supported language. You do not need to add import, include, or using statements.

Example 1:

Input:

Output:

Explanation: This is one possible order in which the eat callbacks may complete. Other orders are valid if fork ownership is respected and every philosopher eats once.

Example 2:

Input:

Output:

Constraints

  • 1 <= n <= 60
  • There are exactly five philosophers and five forks.
  • The five philosopher threads share one DiningPhilosophers instance.
  • The supplied callbacks are thread-safe and return quickly.

Starter Code

Understanding the Problem

Protecting each fork with a lock prevents two neighbors from using the same fork simultaneously, but it does not by itself prevent deadlock.

If all five philosophers pick up their left forks together, every philosopher holds one fork and waits for the right fork held by a neighbor. No thread can make progress.

We need both:

  • Mutual exclusion: one lock per fork.
  • Deadlock prevention: prevent all five philosophers from competing for their second fork at the same time.

One standard solution uses a footman semaphore with four permits. At most four philosophers may attempt to acquire forks concurrently, so at least one philosopher is outside the circular wait.

Synchronization Strategy

For each call to wantsToEat:

  1. Acquire one of the footman's four permits.
  2. Lock the philosopher's left fork and invoke pickLeftFork.
  3. Lock the right fork and invoke pickRightFork.
  4. Invoke eat while both locks are held.
  5. Invoke the put callbacks and unlock both forks.
  6. Release the footman permit.

The order of putting down the two forks is not important, but each put callback must correspond to a fork the philosopher currently holds.

The footman permit must be released even if the operation exits unexpectedly, so implementations should use their language's structured cleanup mechanism where appropriate.

Correctness

Each fork has its own lock. A philosopher invokes a pick callback only after acquiring that fork's lock and keeps the lock until invoking the corresponding put callback. Therefore, two neighboring philosophers cannot hold the same fork simultaneously.

The eat callback runs only after both fork locks have been acquired and before either is released. Thus, every meal occurs while the philosopher owns both adjacent forks.

The footman allows at most four philosophers into the fork-acquisition region. A circular wait involving all five philosophers is therefore impossible: at least one philosopher is not holding a fork, leaving a break in the cycle. Some admitted philosopher can acquire both forks, eat, release them, and allow the remaining philosophers to progress.

Every completed call releases both forks and its footman permit. Consequently, repeated calls can continue, and all philosophers finish without deadlock.

Code

Complexity Analysis

Each call performs a constant number of lock, semaphore, and callback operations.

  • Time complexity: O(1) work per wantsToEat call and O(n) calls per philosopher.
  • Space complexity: O(1) shared state: five fork locks and one four-permit semaphore.