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
4
.obsidian/workspace.json
vendored
4
.obsidian/workspace.json
vendored
|
@ -34,9 +34,9 @@
|
||||||
"type": "pdf",
|
"type": "pdf",
|
||||||
"state": {
|
"state": {
|
||||||
"file": "Concurrent Systems/slides/class 4.pdf",
|
"file": "Concurrent Systems/slides/class 4.pdf",
|
||||||
"page": 23,
|
"page": 24,
|
||||||
"left": -26,
|
"left": -26,
|
||||||
"top": 359,
|
"top": 340,
|
||||||
"zoom": 0.57541567695962
|
"zoom": 0.57541567695962
|
||||||
},
|
},
|
||||||
"icon": "lucide-file-text",
|
"icon": "lucide-file-text",
|
||||||
|
|
|
@ -67,4 +67,32 @@ In this case we break the symmetry by letting an odd number of philosophers sit
|
||||||
|
|
||||||
#### Solution 4 - Monitors!
|
#### Solution 4 - Monitors!
|
||||||
Pick up 2 chopsticks only if both are free
|
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