The Dining Philosophers - Problem

The classic Dining Philosophers Problem is a fundamental concurrency challenge that illustrates the complexities of resource sharing in multithreaded environments.

The Setup: Five philosophers sit around a circular table, each with a bowl of spaghetti. Between each pair of adjacent philosophers lies a single fork. To eat, a philosopher must acquire both the fork to their left and the fork to their right.

The Challenge: Design a thread-safe solution where philosophers can alternate between thinking and eating without causing deadlock or starvation. Each philosopher is represented by a separate thread, and multiple philosophers may want to eat simultaneously.

Your Task: Implement the wantsToEat() method that coordinates the philosopher's actions:

  • philosopher: ID of the philosopher (0-4) who wants to eat
  • pickLeftFork() and pickRightFork(): Functions to acquire forks
  • eat(): Function called when both forks are acquired
  • putLeftFork() and putRightFork(): Functions to release forks

Constraints: The solution must be deadlock-free and ensure no philosopher starves while maintaining thread safety for concurrent access.

Input & Output

Basic Execution Flow
$ Input: 5 philosophers (0,1,2,3,4) want to eat concurrently
โ€บ Output: All philosophers can eat without deadlock using proper synchronization
๐Ÿ’ก Note: With correct fork ordering or semaphore control, philosophers can safely alternate between thinking and eating without getting stuck in deadlock
Deadlock Scenario (Naive Approach)
$ Input: All 5 philosophers simultaneously pick up left fork
โ€บ Output: System deadlocks - no philosopher can proceed
๐Ÿ’ก Note: When all philosophers grab their left fork simultaneously, each waits for their right fork (which is the left fork of their neighbor), creating a circular wait condition
Asymmetric Solution Success
$ Input: Philosophers 1,3 pick right fork first; 0,2,4 pick left fork first
โ€บ Output: At least one philosopher can always complete eating
๐Ÿ’ก Note: Breaking the symmetry ensures that there's always a path for progress, preventing circular dependency and deadlock

Visualization

Tap to expand
P0Even: Lโ†’RP1Odd: Rโ†’LP2Even: Lโ†’RP3Odd: Rโ†’LP4Even: Lโ†’R๐ŸŽฏ Solution: Asymmetric Fork Ordering Prevents Circular Dependencies
Understanding the Visualization
1
The Setup
5 philosophers sit in a circle, each needs 2 forks to eat, but only 5 forks total exist
2
The Problem
If everyone grabs their left fork simultaneously, everyone waits for right fork = deadlock
3
The Solution
Break symmetry: some pick left-then-right, others pick right-then-left
Key Takeaway
๐ŸŽฏ Key Insight: By breaking symmetry in resource acquisition order, we eliminate the circular wait condition that causes deadlock while maintaining maximum concurrency

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

Constant time operations for mutex acquisition

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only requires mutexes for forks, no additional space

n
2n
โœ“ Linear Space

Constraints

  • Exactly 5 philosophers numbered 0 to 4
  • Each philosopher must acquire both adjacent forks to eat
  • Solution must be deadlock-free and starvation-free
  • Multiple threads may call wantsToEat() concurrently
  • Fork operations are atomic and thread-safe
Asked in
Google 45 Microsoft 38 Amazon 32 Meta 28
67.2K Views
Medium Frequency
~35 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen