In computer science, the dining philosophers problem [1] is an example problem often used in concurrent algorithm design to illustrate synchronization issues and techniques for resolving them. It was originally formulated in 1965 by Edsger Dijkstra as a student exam exercise, presented in terms of computers competing for access to tape drive peripherals.
Five philosophers dine together at the same table. Each philosopher has their own place at the table. There is a fork between each plate. The dish served is a kind of spaghetti which has to be eaten with two forks. Each philosopher can only alternately think and eat. Moreover, a philosopher can only eat their spaghetti when they have both a left and right fork. Thus two forks will only be available when their two nearest neighbors are thinking, not eating. After an individual philosopher finishes eating, they will put down both forks. The problem is how to design a regimen (a concurrent algorithm) such that no philosopher will starve; i.e., each can forever continue to alternate between eating and thinking, assuming that no philosopher can know when others may want to eat or think.
The problem was designed to illustrate the challenges of avoiding deadlock, a system state in which no progress is possible. To see that a proper solution to this problem is not obvious, consider a proposal in which each philosopher is instructed to behave as follows:
- think until the left fork is available; when it is, pick it up;
- think until the right fork is available; when it is, pick it up;
- when both forks are held, eat for a fixed amount of time;
- put the left fork down;
- put the right fork down;
- repeat from the beginning.
However, they each will think for an undetermined amount of time and may end up holding a left fork thinking, staring at the right side of the plate, unable to eat because there is no right fork, until they starve. Therefore, a valid process synchronization solution has to handle such deadlock cases as well as tackling other sequence and access problems
For more details, please read the report provided in folder docs
.
- Nguyễn Tống Minh (Email: minh.nt204885@sis.hust.edu.vn)
- Nguyễn Công Đạt (Email: dat.nc200137@sis.hust.edu.vn)
- Nguyễn Trung Hiếu (Email: hieu.nt204877@sis.hust.edu.vn)
- Ngô Thị Thu Huyền (Email: huyen.ntt200289@sis.hust.edu.vn)
dining_philosophers # source code
-- ./gui/ # UI and event handler
-- ./solutions/ # backends - solutions' implementation
-- ./forks.py # implemented for fork objects in the problem statement
-- ./philosophers.py # implemented for philosopher objects in the problem statement
-- ./table.py # problem mode
docs # project report
README.md
To run the program (through main.py
(terminal version) or app.py
(GUI version)), Python 3
is required.
- Install
Python 3
(above$3.6$ ). - Change the working directory to the project root.
- Run the program (suggested):
python -m app