Table of Contents
This project is about the Dining Philosophers problem, which is a classic synchronization and concurrency problem that illustrates challenges in avoiding deadlock and ensuring the proper use of resources among multiple threads/processes. The challenge is to design a solution that avoids deadlock and ensures that philosophers can make progress without conflicts. There were several restrictions by the subject. The program must not have any data races, only a few standard library functions were allowed and the project must follow the 42 Norm. For more informations check out the Subject PDF.
- Clone the repo
git clone https://github.com/42sin/philosophers.git && cd philosophers
- Compile (philo for threads & mutexes/philo_bonus for processes & semaphores)
cd philo && make
- Run
./philosophers <number_of_philosophers> <time_to_die> <time_to_eat> <time_to_sleep>
To run the program a few variables are needed:
- number_of_philosophers: The amount of philosophers and also the number of forks.
- time_to_die: In milliseconds, If a philosopher doesn’t start eating ’time_to_die’ milliseconds after starting their last meal or the beginning of the simulation, he dies.
- time_to_eat: In milliseconds, The time it takes for a philosopher to eat. During that time they will need to keep their forks.
- time_to_sleep: In milliseconds, The time the philosopher will spend sleeping.
- number_of_times_each_philosopher_must_eat: If all philosophers eat at least ’number_of_times_each_philosopher_must_eat’ the simulation will stop. If not specified, the simulation will stop only at the death of a philosopher.
Each action of a philosopher will result in his state being printed with a timestamp and his number.
A philosopher can:
- grab a fork
- eat
- sleep
- think
- die
Here is a basic example of a running simulation.
Distributed under the MIT License. See LICENSE
for more information.