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:
p.(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:
eat only while holding both adjacent forks.wantsToEat returns.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.
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.
Input:
Output:
1 <= n <= 60DiningPhilosophers instance.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:
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.
For each call to wantsToEat:
pickLeftFork.pickRightFork.eat while both locks are held.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.
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.
Each call performs a constant number of lock, semaphore, and callback operations.
O(1) work per wantsToEat call and O(n) calls per philosopher.O(1) shared state: five fork locks and one four-permit semaphore.