vault backup: 2025-03-17 10:19:23
This commit is contained in:
parent
d0a1c05d86
commit
29465e9a2c
2 changed files with 31 additions and 3 deletions
|
@ -67,4 +67,32 @@ In this case we break the symmetry by letting an odd number of philosophers sit
|
|||
|
||||
#### Solution 4 - Monitors!
|
||||
Pick up 2 chopsticks only if both are free
|
||||
a philosopher moves to his/her eating state only if both neighbors are not in their eating states à need to define a state for each philosopher • if one of my neighbors is eating, and I’m hungry, ask them to signal me when they’re done à thus, states of each philosopher are: thinking, hungry, eating à need condition variables to signal waiting hungry philosopher(s) This solutoin very well fits with the features of monitors!
|
||||
- a philosopher moves to his/her eating state only if both neighbors are not in their eating states
|
||||
- need to define a state for each philosopher
|
||||
- if one of my neighbors is eating, and I’m hungry, ask them to signal me when they’re done
|
||||
- thus, states of each philosopher are: thinking, hungry, eating
|
||||
- need condition variables to signal waiting hungry philosopher(s)
|
||||
|
||||
```
|
||||
monitor DP
|
||||
status state[N] all initialized at thinking;
|
||||
condition self[N];
|
||||
|
||||
Pickup(i) :=
|
||||
state[i] = hungry;
|
||||
test(i);
|
||||
if (state[i] != eating) then
|
||||
self[i].wait;
|
||||
|
||||
Putdown(i) :=
|
||||
state[i] = thinking;
|
||||
test((i+1)%N);
|
||||
test((i-1)%N);
|
||||
|
||||
test(i) :=
|
||||
if (state[(i+1)%N] != eating
|
||||
&& state[(i-1)%N] != eating
|
||||
&& state[i] == hungry) then
|
||||
state[i] = eating;
|
||||
self[i].signal();
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue