Philosopher dinner - Solution 1

Solution 1

State Diagram

This is a possible solution for the dining-philosopher problem.

  • when a philosopher gets hungry, he/she first gets the left fork and then holds it while waits for the right one.

Let’s look at the implementation of this solution!

Code

Deadlock conditions

This solution works some times, but can suffer from deadlock.

Let’s identify the four necessary conditions.

  • mutual exclusion – the forks are not sharable at the same time.

  • hold and wait – each philosopher, while waiting to acquire the right fork, holds the left one.

  • no preemption – only the philosophers can release the fork(s) in their possession.

  • circular wait – if all philosophers acquire the left fork, there is a chain in which every philosopher waits for a fork in possession of another philosopher.

Last updated