Dining Philosophers Problem in Operating System
Edsger Wybe Dijkstra was a well-known Dutch scientist. He was the first person ever to present the Dining Philosophers Problem in the stream of Computer Science. The dining philosophers problem is a long-established problem and was introduced to illustrate high-weight problems in the programming of simultaneous or corresponding programming and long-established problems of computer science.
Dining Philosophers problem
There are five philosophers seated on chairs around a circular dining table along with five plates, five chopsticks, and a bowl of noodles, and their lives are stuck in a never-ending cycle of thinking and eating. All these philosophers are involved in two activities simultaneously: thinking and eating.
If any philosopher wants to eat, he requires two chopsticks, one from his left and another from his right. The left chopstick must be from his exact left, and the right chopstick also must be from his exact right. And this is a mandatory condition. If any philosopher wants to eat, he must pick two chopsticks, both belonging to the immediate and adjacent philosophers.
Suppose there is a scenario where the immediate left chopstick and immediate right chopsticks are occupied. In that case, the philosopher has to put down one of his chopsticks, and this choice depends totally on the philosopher. He can put down either the left chopstick or the right chopstick, and he again starts the thinking process.
For example: if philosopher number two wants to eat, he has to pick one chopstick from his left that is from philosopher number three and another
From his right, that is from philosopher number one.
The dining philosopher's problem indicates and illustrates a huge class of simultaneity command problems. Hence it is a long-established temporal event or synchronization problem. The problem is designing a protocol that allows the philosophers to eat without starving and causing a deadlock.
One possible solution to the Dining Philosopher's problem is to use a mutex (mutual exclusion) lock to control access to the chopsticks. A mutex lock allows only one philosopher to acquire both chopsticks simultaneously, ensuring that the philosophers do not try to grab the same chopstick simultaneously. This solution can prevent deadlock, but it may also lead to some philosophers having to wait longer to eat, resulting in inefficient resource usage.
Other solutions to the Dining Philosopher's problem include using a semaphore or a monitor to control access to the chopsticks or allowing philosophers to pick up chopsticks in a specific order to prevent deadlock.
Five philosophers sitting around the circular table:
Dining philosopher's problem
There is a code written below, which can be very helpful for you to understand the dining philosopher's problem. If you look carefully, there is an image right above this paragraph that is given for better understanding. All the philosophers are depicted as P1, P2, P3, P4, and P5, and all the chopsticks are simply depicted as numbers 1, 2, 3, 4, and 5.
Void Aphilosopher
{
while(1)
{
takes_achopstick[i];
takes_achopstick[ (i+1) % 5] ;
starts eating noodles
puts_achopstick[i] );
puts_achopstick[ (i+1) % 5] ;
starts thinking
}
Let us try to understand the above-written code:
Assume that philosopher number one wants to have food, then it will go to Aphilosopher() function and starts executing takes_achopstick[i];and by performing this, it carries 1 number chopstick, and after that, it starts executing takes_achopstick[ (i+1) % 5]; and by performing this, it carries 2 number chopstick( as i=1, hence(1+1)%5=2)
Similarly, assume now that philosopher number 2 wants to eat the food, so it will again enter in Aphilosopher() function and then starts executing takes_achopstick[i];and by performing this, it carries 2 number chopstick and after that, it starts executing takes_achopstick[ (i+2) % 5]; and by performing this, it carries 2 number chopstick( as i=2, hence(1+2)%5=3).
But, if you are looking carefully, you will notice that chopstick number 2 is not free as philosopher number 1 has already chosen that chopstick number 2, and there comes the actual problem, which is generated by the above-written code by creating a unique and rare situation.
The possible and required explanation of the dining philosophers problem:
To give this problem a solution, we are going to take the help of a semaphore to depict or represent the chopstick and trust me, and this will give us an explanation for the dining philosopher's problem.
There are two operations: wait and signal, which will be utilized to get the desired solution for the dining philosopher's problem. If anyone wants to pick a chopstick, then the wait operation will be performed, while if anyone wants to put down a chopstick, then the operation signal, which is a semaphore, will be performed.
What is a semaphore?
Semaphore is a variable of integer data type, which belongs to T, which can be accessed only by wait and signal, which are two uniform atomic operations rather than initialization. And these two operations are defined below:
wait( T )
{
while(T <= 0) ;
T--;
}
2. signal( T )
{
T++;
}
And from the description, as mentioned earlier of wait, it becomes very simple that the value of (T <= 0) then it will directly enter into a never-ending loop only because we have written a semicolon; after the while loop. Although the only work of the signal is to increase the value of T.
The chopstick is depicted as an array of semaphores. Its structure is mentioned right below:
semaphore Ch[5];
In the starting, every item of the semaphores Ch1, Ch2, Ch3, Ch4, and Ch5 are initialized to 1 as none of the philosophers has picked up any chopstick, and all of the chopsticks are kept on the table.
Now, we will change and improve the previous code of the dining philosophers problem with the help of two semaphore operations: wait and signal.
The code looks like this:
void Aphilosopher
{
while(1)
{
Wait( takes_achopstickCh[i] );
Wait( takes_achopstickCh[(i+1) % 5] ) ;
starts eating noodles
Signal( puts_schopstickCh[i] );
Signal( puts_achopstickCh[ (i+1) % 5] ) ;
starts thinking
}
}
In the upper-mentioned code, our first operation of wait is executed on takes_achopstickCh[i] and takes_achopstickCh[(i+1) % 5]. From this, we can see that philosopher i is holding two chopsticks from his right and left sides. And the function of consuming food is executed after that.
When the philosopher i is done with eating, the operation signal is executed on takes_achopstickCh[i] and takes_achopstickCh[(i+1) % 5]. From this, we can understand that philosopher i is done eating and releasing his left and right chopsticks. At last, the philosopher again starts to think.
Benefits of the solution of Dining Philosophers Problem:
The Dining Philosophers problem is a theoretical problem used to illustrate the challenges of managing shared resources in a concurrent system. Its solution can have several benefits, including:
- Improved resource utilization: By ensuring that the philosophers can eat efficiently, the solution can help to optimize the use of chopsticks and reduce waste.
- Improved system performance: By preventing deadlock, the solution can help ensure that the philosophers can eat in a timely manner, improving the system's overall performance.
- Increased reliability: By preventing deadlock, the solution can help ensure that the philosophers can eat without getting stuck, improving the system's reliability.
- Improved scalability: Using a flexible and adaptable solution, the system can be easily modified and expanded to accommodate additional philosophers or other resources.
The solution to the Dining Philosopher's problem can help to improve the efficiency, performance, reliability, and scalability of a concurrent system that involves the management of shared resources.