Philosopher dinner
Problem statement

5 philosophers are seated around a table, with food in from of them.
To eat, every philosopher needs two forks, the ones at her/his left and right sides.
Every philosopher alternates periods in which she/he medidates with periods in which she/he eats.
Modeling every philosopher as a different process or thread and the forks as resources, design a solution for the problem.
A solution
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 an implementations of this solution!
Code

A race condition
This solution may work some times, but in general suffers from race conditions.
Let’s look at a code snippet:

Last updated