2019329621196麻宣政homework7 Version 0 |
|
👤 Author: by 1436830195qqcom 2021-12-29 09:56:57 |
Facing a table problem in my own case, there are 7 bowls and 7 chopsticks on the round table. When someone is hungry, he will try to use the chopsticks closest to him on the left and right. He can eat only when he has two chopsticks. After eating, put down your chopsticks and continue thinking. If seven people are hungry at the same time, and each person picks up the chopsticks on the left, this makes the chopsticks all 0. When they try to pick up the chopsticks on the right, they will wait indefinitely because they don’t have chopsticks. This may happen Lead to deadlock.
a. show that the four necessary conditions for deadlock indeed holds in this example.
1. Mutual exclusion: Each resource is either assigned to a process, or it is available.
2. Popularity and Waiting Conditions: The process of a resource can be requested to request new resources.
3. Unpububle conditions: The resource that has been assigned to a process cannot be mandatory, which can only be explicitly released in its process.
4. Loach Waiting Conditions: When the deadlock occurs, there must be a loop composed of two or more processes in the system, and each process in the loop is waiting for the resources occupied by the next process.
b. state a simple way for avoiding deadlocks in this case.
When everyone wants to eat, they choose to use chopsticks in one direction. Since there are only seven chopsticks, when faced with this situation, only 6 people may get the treatment of eating.
semaphore c[7]={1,1,1,1,1,1,1};
semaphore r=6;
void chioce(int i)
{
while(True)
{
think();
wait(r);
wait(c[i]);
wait(c[i+1]%7);
eat();
signal(c[i+1]%7);
signal(c[i]);
signal(r);
think()
}
}