Introduction to multi-threading in C
This project is a multithreaded simulation of the Dining Philosophers problem. The problem is a classic multi-process synchronization problem which was originally formulated in 1965 by Edsger Dijkstra as a student exam exercise.
The problem consists of N philosophers sitting at a round table who do only three things: eat, think, and sleep. There is a bowl of spaghetti on the table, but philosophers need two forks to eat it. Each philosopher can only use the forks to their immediate right and left. The philosophers must find a solution that prevents them from starving.
The project is structured as follows:
philo/srcs/
: This directory contains all the source code for the simulation that uses mutexes and threads.philo_bonus/srcs/
: This directory contains all the source code for the simulation that uses Semaphores and processes.Makefile
: This is used to compile the project.
To run the project, follow these steps:
- Clone the repository
- Navigate into the cloned repository:
cd <repository_directory>
- Compile the project:
make
make will compile 2 executables, phil and phil_bonus, the first one is based on threads and mutexes, the latter uses semaphores and multi-process instead. both take the same arguments which are:
NUMBER_OF_PHILOSOPHERS
defines both how many philosophers there are as well as how many forksTIME_TO_DIE
defines how much time (milliseconds) it takes for a philosopher to starveTIME_TO_EAT
defines how much time (milliseconds) it takes for a philosopher to eatTIME_TO_SLEEP
defines how much time (milliseconds) it takes for a philosopher to sleep- {OPTIONAL}
NUMBER_OF_MEALS
defines how many meals must each philosopher eat before the simulation ends
example : ./philo 5 800 200 200
(in this example the simulation should not stop)