A project made in accordance with the philosophers project which is part of the Codam Core Curriculum. It is a version of the dining philosophers problem. One or more philosophers sit at a round table. There is a large bowl of spaghetti in the middle of the table. Philosophers need two forks to eat the spaghetti. There are as many forks as philosophers. The challenge is to write a program that lets the philosophers dine without starving. Each philosopher is a thread and the availability of each fork is protected by a mutex.
Start by cloning the repository:
git clone https://github.com/fkoolhoven/philosophers.git
Go into the philo directory and compile by using make:
make
./philo [number_of_philosophers] [time_to_die] [time_to_eat] [time_to_sleep] [number_of_times_each_philosopher_must_eat]
- number_of_philosophers: the number of philosophers and also the number of forks.
- time_to_die (in milliseconds): if a philosopher didn’t start eating time_to_die milliseconds since the beginning of their last meal or the beginning of the simulation, they die.
- time_to_eat (in milliseconds): the time it takes for a philosopher to eat. During that time, they will need to hold two forks.
- time_to_sleep (in milliseconds): the time a philosopher will spend sleeping.
- number_of_times_each_philosopher_must_eat (optional argument): if all philosophers have eaten at least number_of_times_each_philosopher_must_eat times, the simulation stops. If not specified, the simulation stops when a philosopher dies.
For example:
./philo 6 800 200 200 3
will let 6 philosophers eat 3 times.
Or:
./philo 4 310 200 200
will let the simulation run until 1 philosopher starves.
Example output of
./philo 4 2000 350 800 5
Any state change of a philosopher is formatted as follows: [current timestamp in milliseconds] [philosopher number] [state]
- Learned to create and join threads.
- Learned about what causes race conditions.
- Learned how to prevent race conditions using mutexes.
- Additionally I worked on making the output as easy to read as possible, adding colors and appropriate (error) messages.
See my profile page for ways to contact me!