The dining philosophers problem is a classic synchronization problem where multiple philosophers sit around a table with a bowl of spaghetti in the center. Each philosopher has a fork between themselves and the adjacent philosopher. A philosopher can only eat when they have both the left and right forks. They can either think, sleep, or eat at any given time.
The challenge is to ensure that all philosophers get a fair chance to eat (all threads execute their routine) without causing deadlocks or resource starvation. For that, mutexes are used to protect against data racing. And it is necessary to handle them correctly to avoid deadlock.
- Clone the repository:
git clone https://github.com/Guiribei/dining-philosophers-problem.git
- Change to the repository directory:
cd dining-philosophers-problem
- Compile the project using the provided Makefile:
make
- The number of philosophers (and forks).
- The time in milliseconds for a philosopher to die if they haven't started eating.
- The time in milliseconds for a philosopher to eat.
- The time in milliseconds for a philosopher to sleep.
- (Optional) The number of times each philosopher must eat before the simulation ends.
./philo number_of_philosophers time_to_die time_to_eat time_to_sleep [number_of_times_to_eat]
./philo 5 800 200 200 10
This example will run the simulation with 5 philosophers, each having 800 ms to eat before they die. They will take 200 ms to eat, 200 ms to sleep, and must eat 10 times each before the simulation ends.