A Multithreaded Solution to the Dining Philosophers Problem
philosophers
is a multithreaded implementation of the classic Dining Philosophers problem.
Problem:
- The Dining Philosophers problem is a classic concurrency problem dealing with synchronization.
- It involves one or more philosophers sitting at a round table.
- The philosophers alternatively eat, think, or sleep.
- A fork is placed between each pair of adjacent philosophers. Each philosopher has one fork to the left and one fork to the right.
- As spaghetti is difficult to serve and eat with a single fork, it is assumed that a philosopher must eat with two forks.
- The challenge is to design a protocol that allows each philosopher to periodically stop thinking and eat without causing a deadlock.
Solution:
- Each philosopher is represented by a separate thread.
- The forks are represented by mutexes that control access to the shared resources (the forks).
- A philosopher picks up the fork on their right, then the fork on their left, before they start eating.
- After eating, a philosopher puts down both forks, then starts thinking.
- The simulation stops when a philosopher dies or when each philosopher has eaten a certain number of times.
gcc
orclang
make
- Clone the repository:
git clone https://github.com/lkilpela/philosophers.git
- Navigate into the project directory:
cd philosophers/philo
- Compile the project:
make
- Run the program with five parameters:
num_of_philos
: The number of philosophers.time_to_die
: The time in milliseconds a philosopher will die if they don't start eating.time_to_eat
: The time in milliseconds a philosopher spends eating.time_to_sleep
: The time in milliseconds a philosopher spends sleeping.num_times_to_eat
(optional): The number of times each philosopher must eat before the simulation ends.
- Example:
- Do not test with more than 200 philosophers.
- Do not test with time_to_die or time_to_eat or time_to_sleep set to values lower than 60 ms.
- Test
1 800 200 200
. The philosopher should not eat and should die. - Test
5 800 200 200
. No philosopher should die. - Test
5 800 200 200 7
. No philosopher should die and the simulation should stop when every philosopher has eaten at least 7 times. - Test
4 410 200 200
. No philosopher should die. - Test
4 310 200 100
. One philosopher should die. - Test with 2 philosophers and check the different times: a death delayed by more than 10 ms is unacceptable.
- Test with any values of your choice to verify all the requirements. Ensure philosophers die at the right time, that they don't steal forks, and so forth.
The philosophers
project will be evaluated based on the following criteria:
-
Correctness: The program should correctly implement the Dining Philosophers problem. No philosopher should be able to eat with only one fork, and no two philosophers should be able to use the same fork at the same time.
-
Deadlock Prevention: The program should prevent deadlocks. At no point should the program get stuck because each philosopher is holding one fork and waiting for the other.
-
Starvation Prevention: The program should prevent starvation. Each philosopher should be able to eat periodically, and no philosopher should die because they can't get access to the forks.
-
Concurrency: The program should run the philosophers concurrently. Each philosopher should be represented by a separate thread.
-
Output: The program should output the status of each philosopher (thinking, eating, sleeping, died) with timestamps in milliseconds.
-
Code Quality: The code should be well-organized and easy to read. It should be free of memory leaks and undefined behavior.
Please note that these are general guidelines and the actual evaluation criteria may vary.
Here are some of the comments received during the peer evaluation of the philosophers
project:
Peer 1: "..."
Peer 2: "..."
Peer 3: "..."
This project is licensed under the MIT License.