The Philosophers project is an implementation of the classic Dining Philosophers Problem using threading and mutexes in C. The project simulates a group of philosophers sitting at a table with a bowl of spaghetti and a limited number of forks. Philosophers alternate between eating, thinking, and sleeping, ensuring no data races or deadlocks occur. 🍝🧵🕰️
- Multi-threaded simulation of philosophers' behavior.
- Mutex-based synchronization to manage shared resources (forks).
- Handles starvation prevention and ensures philosophers do not die unnecessarily.
- Optional bonus implementation using processes and semaphores.
- Program Name:
philo
- Arguments:
./philo number_of_philosophers time_to_die time_to_eat time_to_sleep [number_of_times_each_philosopher_must_eat]
number_of_philosophers
: Number of philosophers and forks.time_to_die
: Maximum time (ms) a philosopher can go without eating before dying.time_to_eat
: Time (ms) a philosopher spends eating.time_to_sleep
: Time (ms) a philosopher spends sleeping.number_of_times_each_philosopher_must_eat
(optional): Ends the simulation once all philosophers have eaten the specified times. 🍽️🕒🛌
All actions of philosophers are logged in the following format:
[timestamp_in_ms] [philosopher_id] [action]
- Actions:
has taken a fork
is eating
is sleeping
is thinking
died
📜⏱️👨💻
- Written in C.
- Adheres to the 42 Norm.
- No memory leaks or unexpected crashes.
- No race conditions or deadlocks.
- Clone the repository:
git clone https://github.com/MohammadHusssein/42-philosophers.git cd 42-philosophers/philo
- Compile the program:
make
- Run the simulation:
This command simulates 5 philosophers with the following parameters:
./philo 5 800 200 200
time_to_die = 800 ms
time_to_eat = 200 ms
time_to_sleep = 200 ms
👨🍳⏳💡
Here are some general test cases to use: Philo tests
I encourage to come up with your own test cases and try to find any case that your program fails at. 🧪🖥️✅
This project is licensed under the MIT License. Feel free to use, modify, and distribute the code as long as proper attribution is given. 📜🔓✨