The Philosophers Project is a well-known concurrency and synchronization problem, used to teach about resource-sharing issues in computer science.
Imagine five philosophers seated around a circular table. In front of each philosopher, there is a single plate of spaghetti and a fork. Philosophers alternate between sleeping, thinking and eating.
A philosopher can only eat if they have two forks—one in each hand. Since there are only the same number of forks as of philosophers and each philosopher needs two to eat, they must coordinate with each other to avoid conflicts.
Avoid Deadlock: Ensure that the philosophers don't get stuck waiting for each other indefinitely.
Avoid Starvation: Every philosopher should eventually be able to eat, even if it’s not their turn yet.
Handle Concurrency Properly: Implement the solution with careful management of threads and processes, ensuring they can operate concurrently without issues.
The logic was implemented in C using threads and mutexes.
Each philosopher corresponds to a thread and synchronization primitives like mutexes are used to manage access to shared resources (the forks).
In this part, every philosopher will have a fork to his left and another to his right.
The logic was implemented in C using processes and semaphores.
Each philosopher corresponds to a process and synchronization primitives like semaphores are used to manage access to shared resources (the forks).
In this case, the forks are in the middle of the table instead of each being shared by only two philosophers.
- Clone repository
git clone git@github.com:mjorgecruz/42_philosophers.git
- Go inside project directory and run
make
cd 42_philosophers make
- Run
./philosophers {number_of_philosophers} {time_to_die} {time_to_eat} {time_to_sleep} {[number_of_times_each_philosopher_must_eat]}
./philosophers 5 400 100 200
- Go inside project directory and run
make bonus
cd 42_philosophers make bonus
- Run
./philosophers_bonus {number_of_philosophers} {time_to_die} {time_to_eat} {time_to_sleep} {[number_of_times_each_philosopher_must_eat]}
./philosophers_bonus 5 400 100 200 10