vault backup: 2025-03-03 09:47:47
This commit is contained in:
parent
6fc3d042b8
commit
7e23b3a8d5
1 changed files with 20 additions and 4 deletions
|
@ -116,7 +116,7 @@ Atomic R/W registers are storage units that can be accessed through two operatio
|
|||
### Peterson algorithm (for two processes)
|
||||
Let's try to enforce MUTEX with just 2 processes.
|
||||
|
||||
1st attempt:
|
||||
##### 1st attempt:
|
||||
```
|
||||
lock(i) :=
|
||||
AFTER_YOU <- i
|
||||
|
@ -128,14 +128,30 @@ unlock(i) :=
|
|||
```
|
||||
This protocol satisfies MUTEX, but suffers from deadlock (if one process never locks).
|
||||
|
||||
2nd attempt:
|
||||
##### 2nd attempt:
|
||||
```
|
||||
Initialize FLAG[0] and FLAG[1] to down
|
||||
lock(i) :=
|
||||
AFTER_YOU <- i
|
||||
wait AFTER_YOU != i
|
||||
FLAG[i] <- up
|
||||
wait FLAG[1-i] = down
|
||||
return
|
||||
|
||||
unlock(i) :=
|
||||
FLAG[i] <- down
|
||||
return
|
||||
```
|
||||
Still suffers from deadlock if both processes simultaneously raise their flag.
|
||||
|
||||
##### Correct solution:
|
||||
```
|
||||
Initialize FLAG[0] and FLAG[1] to down
|
||||
lock(i) :=
|
||||
FLAG[i] <- up
|
||||
AFTER_YOU <- i
|
||||
wait FLAG[1-i] = down OR AFTER_YOU != i
|
||||
return
|
||||
|
||||
unlock(i) :=
|
||||
FLAG[i] <- down
|
||||
return
|
||||
```
|
Loading…
Reference in a new issue