Dining-Philosophers Problem – N philosophers seated around a circular table
- There is one chopstick between each philosopher
- A philosopher must pick up its two nearest chopsticks in order to eat
- A philosopher must pick up first one chopstick, then the second one, not both at once
We need an algorithm for allocating these limited resources(chopsticks) among several processes(philosophers) such that solution is free from deadlock and free from starvation.
There exist some algorithm to solve Dining – Philosopher Problem, but they may have deadlock situation. Also, a deadlock-free solution is not necessarily starvation-free. Semaphores can result in deadlock due to programming errors. Monitors alone are not sufficiency to solve this, we need monitors with condition variables
Monitor-based Solution to Dining Philosophers
We illustrate monitor concepts by presenting a deadlock-free solution to the dining-philosophers problem. Monitor is used to control access to state variables and condition variables. It only tells when to enter and exit the segment. This solution imposes the restriction that a philosopher may pick up her chopsticks only if both of them are available.
To code this solution, we need to distinguish among three states in which we may find a philosopher. For this purpose, we introduce the following data structure:
THINKING – When philosopher doesn’t want to gain access to either fork.
HUNGRY – When philosopher wants to enter the critical section.
EATING – When philosopher has got both the forks, i.e., he has entered the section.
Philosopher i can set the variable state[i] = EATING only if her two neighbors are not eating
(state[(i+4) % 5] != EATING) and (state[(i+1) % 5] != EATING).
Above Program is a monitor solution to the dining-philosopher problem.
We also need to declare
This allows philosopher i to delay herself when she is hungry but is unable to obtain the chopsticks she needs. We are now in a position to describe our solution to the dining-philosophers problem. The distribution of the chopsticks is controlled by the monitor Dining Philosophers. Each philosopher, before starting to eat, must invoke the operation pickup(). This act may result in the suspension of the philosopher process. After the successful completion of the operation, the philosopher may eat. Following this, the philosopher invokes the putdown() operation. Thus, philosopher i must invoke the operations pickup() and putdown() in the following sequence:
DiningPhilosophers.pickup(i); ... eat ... DiningPhilosophers.putdown(i);
It is easy to show that this solution ensures that no two neighbors are eating simultaneously and that no deadlocks will occur. We note, however, that it is possible for a philosopher to starve to death.
This article is contributed by Mayank Rana. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Reader-Writers solution using Monitors
- Reader-Writer problem using Monitors (pthreads)
- Monitors in Process Synchronization
- Producer-Consumer solution using threads in Java
- Producer-Consumer solution using Semaphores in Java | Set 2
- Readers-Writers Problem | Set 1 (Introduction and Readers Preference Solution)
- Classical problems of Synchronization with Semaphore Solution
- C++ Program to concatenate two strings using Operator Overloading
- Dining Philosopher Problem Using Semaphores
- IPC using Message Queues
- Combinational circuits using Decoder
- Communication between two process using signals in C
- Producer Consumer Problem using Semaphores | Set 1
- Implementing Directory Management using Shell Script
- Finding Network ID of a Subnet (using Subnet Mask)
- Comparison on using Java for an Operating System instead of C
- Implementation of Non-Preemptive Shortest Job First using Priority Queue
- Parsing ambiguos grammars using LR parser
- Implementation of Least Recently Used (LRU) page replacement algorithm using Counters
- Shortest Job First (or SJF) CPU Scheduling Non-preemptive algorithm using Segment Tree
Improved By : Palak Jain 5