A visualization of the Dining Philosophers problem.
This project solves the Dining Philosophers problem, a classic synchronization challenge where multiple philosophers (threads) share forks (mutexes) to eat, think, and sleep without deadlocks or starvation. The goal is to ensure threads safely access shared resources while logging their states in real-time.
For background on this classic CS problem: Dining Philosophers Problem
- Thread-based simulation: Each philosopher is a thread, and forks are protected by mutexes.
- Starvation prevention: Philosophers must eat within
time_to_dieor the program terminates. - Real-time logging: Timestamped output for all actions (eating, sleeping, thinking, etc.).
- Configurable parameters: Adjust philosopher count, timings, and optional meal limits.
- C Compiler (
cc/gcc/clang) - Make (GNU Make)
- Unix-based OS (Linux/macOS)
Clone the repository:
git clone git@github.com:mtelek/philosophers.git
cd philosophers/philo
make ./philo <philos> <die> <eat> <sleep> [<meals>]| Parameter | Description | Example Value |
|---|---|---|
| philos | Number of philosophers | 4 |
| die | Time until starvation | 800 |
| eat | Time spent eating | 200 |
| sleep | Time spent sleeping | 200 |
| meals | Optional meal count to stop simulation | 5 |
The philosopher states can be visualized using this interactive tool created by nafuka11:
Philosophers Visualizer
- Run your program and redirect the output to a file:
./philo 4 800 200 200 > output.txt - Paste your
output.txtcontents into the visualizer's input field
For comprehensive testing, you can use the LazyPhilosophersTester created by MichelleJiam: