The question of the Philosopher's meal:
(a) Question:
5 philosophers sit around a round table, each of the two philosophers has a chopstick, philosophers usually think, only when they are hungry, only pick up chopsticks to eat.
It is stipulated that each philosopher can only take his left chopstick and then take his right chopsticks before eating.
(ii) Analysis:
Each chopstick is a critical resource, setting 5 mutually exclusive semaphores.
Semaphore stcik[5]={1,1,1,1,1}
Because: only the left chopsticks-"occupy the right Chopsticks-" eat
So P (left chopsticks)-"p (right chopsticks)-" eat
(iii) Realization of:
Main () {
Philosopher (0);//Note that loops cannot be used because this is a 4-side process.
Philosopher (1);
Philosopher (2);
Philosopher (3);
Philosopher (4);
}
Philosopher (int i) {
while (1) {
Thinking
P (Stick[i]);//Take Left chopsticks
P (stick[i+1]);//Take Right chopsticks
Meal
V (Stick[i]);
V (stick[i+1]);
}
}
(iv) Questions:
If 5 philosophers pick up their left chopsticks at the same time, a deadlock will occur.
(v) Methods of preventing deadlocks:
(1) Method one:
After getting the chopsticks on the left, check that the chopsticks on the right are available. If it is not available, first put down the left hand chopsticks, wait a while to repeat the whole process.
Problem: the occurrence of hunger;
If you pick up the chopsticks at the same time, then the right chopsticks are not available, then put down, and then pick up, ... , then no one can eat,
(2) Method two:
A maximum of four philosophers are allowed to dine at the same time to ensure that at least one philosopher can dine and eventually
Release the two chopsticks he used so that more philosophers could dine.
Pseudo code:
Semaphore chopstick[5]={1,1,1,1,1};
Semaphore room=4;
void philosopher (int i)
{
while (true)
{
Think ();
Wait (guest); Request to enter the room to dine
Wait (Chopstick[i]); Request chopsticks on the left hand side
Wait (chopstick[(i+1)%5]); Request chopsticks on the right hand side
Eat ();
Signal (chopstick[(i+1)%5]); Release the chopsticks on the right hand side
Signal (Chopstick[i]); Release the chopsticks on the left hand side
Signal (guest); Exit the room to release the semaphore rooms
}
}
(3) will take the left chopsticks, and take the right chopsticks as an atomic operation: (that is, only when the chopsticks can be obtained, only take chopsticks. )
Method One:
Using the and-type semaphore mechanism: According to the course, in one primitive, a piece of code needs to be
Multiple critical resources are either allocated to it, or none are allocated, so there is no deadlock situation.
Pseudo code:
Semaphore chopstick[5]={1,1,1,1,1};
void philosopher (int I)
{
while (true)
{
Think ();
P (chopstick[(i+1)]%5,chopstick[i]);
Eat ();
V (chopstick[(i+1)]%5,chopstick[i]);
}
}
Method Two:
The protection mechanism of the semaphore is used to realize. The left and right chopsticks are taken before the Eat () by the semaphore mutex
The operation of the child is protected so that it becomes an atomic operation, which prevents the deadlock from appearing.
Pseudo code:
semaphore mutex = 1;
Semaphore chopstick[5]={1,1,1,1,1};
void philosopher (int I)
{
while (true)
{
Think ();
P (mutex);
P (chopstick[(i+1)]%5);
P (Chopstick[i]);
V (mutex);
Eat ();
V (chopstick[(i+1)]%5);
V (Chopstick[i]);
}
}
(4) The philosopher who prescribes the odd number first picks up his left chopstick and then goes to the chopsticks on his right, while the even number of philosophers is the opposite.
Pseudo code:
Semaphore chopstick[5]={1,1,1,1,1};
void philosopher (int i)
{
while (true)
{
Think ();
if (i%2 = = 0)//even philosopher, first right rear left.
{
P (chopstick[i + 1] mod 5);
P (chopstick[i]);
Eat ();
V (chopstick[i + 1] mod 5);
V (chopstick[i]);
}
Else//Odd philosopher, first left and right.
{
P (chopstick[i]);
P (chopstick[i + 1] mod 5);
Eat ();
V (chopstick[i]);
V (chopstick[i + 1] mod 5);
}
}