VOOZH about

URL: https://www.geeksforgeeks.org/operating-systems/dining-philosophers-problem/

⇱ Dining Philosophers Problem - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Dining Philosophers Problem

Last Updated : 23 Jul, 2025

The Dining Philosopher Problem is a classic synchronization and concurrency problem that deals with resource sharing, deadlock, and starvation in systems where multiple processes require limited resources. In this article, we will discuss the Dining Philosopher Problem in detail along with proper implementation.

Problem Statement

The Dining Philosopher Problem involves 'n' philosophers sitting around a circular table. Each philosopher alternates between two states: thinking and eating. To eat, a philosopher needs two chopsticks, one on their left and one on their right. However, the number of chopsticks is equal to the number of philosophers, and each chopstick is shared between two neighboring philosophers.

The standard problem considers the value of 'n' as 5 i.e. we deal with 5 Philosophers sitting around a circular table.

Constraints and Conditions for theProblem

  • Every Philosopher needs two forks to eat.
  • Every Philosopher may pick up the forks on the left or right but only one fork at once.
  • Philosophers only eat when they have two forks. We have to design such a protocol i.e. pre and post protocol which ensures that a philosopher only eats if he or she has two forks.
  • Each fork is either clean or dirty.

👁 Dining Philosophers Problem

Solution

The correctness properties it needs to satisfy are:

  • Mutual Exclusion Principle: No two Philosophers can have the two forks simultaneously.
  • Free from Deadlock: Each philosopher can get the chance to eat in a certain finite time.
  • Free from Starvation: When few Philosophers are waiting then one gets a chance to eat in a while.
  • No strict Alternation
  • Proper utilization of time

Algorithm

loop forever
p1: think
p2: preprotocol
p3: eat
p4: postprotocol

First Attempt

We assume that each philosopher is initialized with its index I and that addition is implicitly modulo 5. Each fork is modeled as a semaphore where wait corresponds to taking a fork and signal corresponds to putting down a fork.

Algorithm

semaphore array[0..4] fork ← [1, 1, 1, 1, 1]
loop forever
p1 : think
p2 : wait(fork[i])
p3 : wait(fork[i + 1])
p4 : eat
p5 : signal(fork[i])
p6 : signal(fork[i + 1])

Problem with this solution:

This solution may lead to a deadlock under an interleaving that has all the philosophers pick up their left forks before any of them tries to pick up a right fork. In this case, all the Philosophers are waiting for the right fork but no one will execute a single instruction.

Second Attempt

One way to tackle the above situation is to limit the number of philosophers entering the room to four. By doing this one of the philosophers will eventually get both the fork and execute all the instruction leading to no deadlock.

Algorithm

semaphore array[0..4] fork ← [1, 1, 1, 1, 1]

semaphore room ← 4
loop forever
p1 : think
p2 : wait(room)
p3 : wait(fork[i])
p4 : wait(fork[i + 1])
p5 : eat
p6 : signal(fork[i])
p7 : signal(fork[i + 1])
p8 : signal(room)

In this solution, we somehow interfere with the given problem as we allow only four philosophers.

Third Attempt

We use the asymmetric algorithm in the attempt where the first four philosophers execute the original solution but the fifth philosopher waits for the right fork and then for the left fork.

Algorithm

semaphore array [0..4] fork ← [1,1,1,1,1]

For thefirst Four Philosophers

loop forever
p1 : think
p2 : wait(fork[i])
p3 : wait(fork[i + 1])
p4 : eat
p5 : signal(fork[i])
p6: signal(fork[i + 1])                   

For the Fifth Philosopher

loop forever
p1 : think
p2 : wait(fork[0])
p3 : wait(fork[4])
p4 : eat
p5 : signal(fork[0])
p6 : signal(fork[4])

Note: This solution is also known as Chandy/Misra Solution.

Advantages of this Solution

  • Allows a large degree of concurrency
  • Free from Starvation
  • Free from Deadlock
  • More Flexible Solution
  • Economical
  • Fairness
  • Boundedness

The above discussed the solution for the problem using semaphore. Now with monitors, Here, Monitor maintains an array of the fork which counts the number of free forks available to each philosopher. The take Forks operation waits on a condition variable until two forks are available. It decrements the number of forks available to its neighbor before leaving the monitor. After eating, a philosopher calls release Forks which updates the array fork and checks if freeing these forks makes it possible to signal.

Algorithm

monitor ForkMonitor:
integer array[0..4]
fork ← [2,2,2,2,2]
condition array[0..4]OKtoEat

operation takeForks(integer i)
if(fork[i]!=2)
waitC(OKtoEat[i])

fork[i+1]<- fork[i+1]-1
fork[i-1] <- fork[i-1]-1

operation releaseForks(integer i)
fork[i+1] <- fork[i+1]+1
fork[i-1] <- fork[i-1]

if(fork[i+1]==2)
signalC(OKtoEat[i+1])

if(fork[i-1]==2)
signalC(OKtoEat[i-1])

For Each Philosopher

loop forever :
p1 : think
p2 : takeForks(i)
p3 : eat
p4 : releaseForks(i)

Here Monitor will ensure all such needs mentioned above.

Implementation of Dining Philosophers Problem

Here is the Program for the same using monitors in C++ as follows.

Output

👁 Dining Philosophers Problem
Output for Dining Philosophers problem for the above program

Conclusion

The Dining Philosopher Problem teaches us how to handle shared resources in a system while avoiding issues like deadlock and starvation. It highlights challenges like deadlock and starvation and teaches how to prevent them using proper synchronization techniques. The problem is a classic example used to study synchronization and resource allocation in concurrent systems.

Comment
Article Tags:
Article Tags: