A classic problem in concurrency and multithreading, inspired by Edsger Dijkstra's Dining Philosophers problem. The project is designed to learn about thread synchronization, race conditions, mutexes, and deadlocks.
Problem description N philosophers are sitting around a circular table. Each philosopher has only one fork on their left and one on their right. There are exactly N forks for N philosophers.
Each philosopher follows a simple routine:
- Think 🤔
- Pick up two forks (one on the left, one on the right) to eat 🍽️
- Eat for a while 😋
- Put the forks down and go back to thinking 🔄
The challenge arises because if all philosophers pick up one fork at the same time, they will deadlock (waiting forever for the second fork).
Goal of the Project Mandatory part Use C language, implement a simulation of this problem using threads (POSIX threads, pthread library). No philosopher starves (i.e., they eventually eat). The program avoids deadlocks. The output logs each philosopher’s actions (thinking, eating, sleeping). This is done by using mutexes or other synchronization techniques to prevent race conditions.
Bonus part part To be added
Common Issues & Debugging
- Race conditions: If multiple threads modify shared data without proper locking.
- Deadlocks: If all philosophers pick up one fork and wait for the second.
- Starvation: If some philosophers get to eat repeatedly while others never do.
✅ TBA (with bonus)
TBA 1. Clone the repo:
git clone https://github.com/dmitrijslasko/42_philosophers.git2. Go into the folder:
Mandatory part: cd philosophers42/philo
Bonus part: cd philosophers42/philo_bonus3. Build the program:
make4. Launch the program with your parameters, e.g.:
./philo 10 800 200 200 10- Debug mode. Compile with:
make debugMORE TO BE ADDED
TBA
TBA
TBA
TBA
Feel free to contact me: Slack: @dmlasko E-mail: dmitrijs.lasko@gmail.com Telegram: @lalamoose
