The ninth project of 42's curriculum asks students to solve the famous Dijkstra's synchronization problem called The Dining Philosophers Problem. This is a introduction to multithreads, multiprocesses, mutexes and semaphores.
git clone [email protected]:ygor-sena/42cursus-philosophers.git
To compile the mandatory project, execute the following command in your terminal:
cd philo/ && make
Otherwise, if you want to compile the bonus version, execute:
cd philo_bonus/ && make
Both the mandatory and bonus program take at least 5 arguments. The first parameter is the program's binary file and the last one is optional. Each of them stands for:
#2 | #3 | #4 | #5 | #6 (optional) |
---|---|---|---|---|
5 | 800 | 200 | 200 | 5 |
number_of_philosophers |
time_to_die |
time_to_eat |
time_to_sleep |
number_of_times_each_must_eat |
So, to start a dinning simulation where there are 5 philosophers that must eat 7 times each and the time to die, to eat and to sleep is 800, 200 and 200 respectively, we should execute the following command:
./philo 5 800 200 200 7
Run the program with the valgrind's flags below to check for memory leaks:
valgrind -q --leak-check=full --show-leak-kinds=all --track-origins=yes
Also, run the program with valgrind's tools DRD (Data Race Detector) and Helgrind separately to search for data race conditions and typical syncronization problems such as deadlock:
valgrind --tool=drd
valgrind --tool=helgrind
There are cases where a philosopher can die because the scheduler priorized some threads/processes instead of giving all of them the same priority. To avoid this, it is necessary to implement a function that always will make the hungriest philosopher dinner first. It is possible to implement this in the mandatory project according the rules of the subject. For the bonus project, we would need shared memory to share information between the child processes, but its use is not allowed by the subject.
-
General references:
- Concorrência e Paralelismo (Parte 1) | Entendendo Back-End para Iniciantes by Fábio Akita.
- Ansel Sermersheim: “Multithreading is the answer. What is the question? (part 1 of 2) by CppCon 2017.
- Ansel Sermersheim: “Multithreading is the answer. What is the question? (part 2 of 2) by CppCon 2017.
- Unix Threads in C by CodeVault.
-
About the Dining Philosophers Problem:
- TANENBAUM, Andrew; BOS, Herbert. Modern Operating System. 4th edition. 2014.
- The Dining Philosophers Problem With Ron Swanson by Aditya Bhargava.
-
About threads and mutexes:
-
About time conversion:
- How to get the current time in milliseconds from C in Linux? by StackOverFlow
-
About Helgrind and DRD (Data Race Detector):
-
Interesting contents by fellow 42's students:
- Web visualizer for Philosophers project by nafuka.
- Visual summary of the Philosophers project made with Miro.
- Acelera: Philosophers by rodsmade.
- Summary of the concepts of Philosophers project made with Notion.
I want to thank Marcelo Magalhães, also a student at 42SP for his support throughout the project when I needed. Please, check out his interesting projects at GitHub here.