This is a C implementation of the Dining Philosophers problem, which is a classic example of a concurrency problem.
The problem involves a group of philosophers who are seated at a circular table with a fork between each pair of adjacent philosophers.
Each philosopher alternates between thinking and eating, and to eat, they must pick up the two forks on either side of them.
However, since there are only as many forks as there are philosophers, deadlock can occur if each philosopher picks up one fork and waits for the other.
This program simulates the behavior of the philosophers using threads and mutexes to avoid deadlock.
The program takes the following command-line arguments:
./philo num_philosophers time_to_die time_to_eat time_to_sleep [num_times_each_philosopher_must_eat]
num_philosophers: the number of philosophers at the table
time_to_die: the time in milliseconds after which a philosopher dies if they have not started eating
time_to_eat: the time in milliseconds it takes for a philosopher to eat
time_to_sleep: the time in milliseconds it takes for a philosopher to sleep
num_times_each_philosopher_must_eat (optional): the number of times each philosopher must eat before the simulation ends
The output of the program is a log of the actions of each philosopher, along with a timestamp in milliseconds.
git clone [email protected]:Clivassy/Forked_Philosophers.git
cd Forked_Philosophers
make
To compile the program, run make. This will create an executable called philo.
To run the program, pass in the desired command-line arguments:
./philo 5 800 200 200
This will simulate a table with 5 philosophers, where each philosopher dies after 800 milliseconds of inactivity, it takes 200 milliseconds to eat and sleep.
Optionally, you can specify the number of times each philosopher must eat before the simulation ends:
./philo 5 800 200 200 3
This will end the simulation after each philosopher has eaten 3 times.
The Dining Philosophers Problem :
https://www.youtube.com/watch?v=FYUi-u7UWgw
https://www.youtube.com/watch?v=NbwbQQB7xNQ
Threads :
https://www.youtube.com/watch?v=o_GbRujGCnM
https://www.tutorialspoint.com/multithreading-in-c
https://www.geeksforgeeks.org/multithreading-c-2/
Datas Race:
https://www.youtube.com/watch?v=oq29KUy29iQ&list=PLfqABt5AS4FmuQf70psXrsMLEDQXNkLq2&index=4