vault backup: 2025-03-12 10:44:27
This commit is contained in:
parent
fb56bcc453
commit
6547be1a32
2 changed files with 33 additions and 7 deletions
|
@ -13,7 +13,7 @@ Main use: **prevent busy waiting**: suspend processes that cannot perform *down*
|
|||
**Binary semaphore:** if it is at most 1 (also *up* are blocking).
|
||||
|
||||
A semaphore needs two underlying objects:
|
||||
- a counter initialized at s0 that can also become negative
|
||||
- a counter initialized at s0 that can also become negative (see the implementation below to understand)
|
||||
- a data structure (typically a queue), initially empty, to store suspended processes.
|
||||
|
||||
#### Ideal implementation
|
||||
|
@ -29,4 +29,30 @@ S.up() :=
|
|||
S.counter++
|
||||
if S.counter <= 0 then
|
||||
activate a proc from S.queue
|
||||
return
|
||||
return
|
||||
```
|
||||
>[!note] note
|
||||
>if S.counter ≥ 0, then this is the value of the semaphore; otherwise, S.counter tells you how many processes are suspended in S
|
||||
>
|
||||
>all operations are in MUTEX
|
||||
|
||||
|
||||
#### Actual implementation
|
||||
```
|
||||
Let t be a test&set register initialized at 0
|
||||
|
||||
S.down() :=
|
||||
Disable interrupts
|
||||
wait S.t.test&set() = 0
|
||||
S.counter--
|
||||
if S.counter < 0 then
|
||||
enter into S.queue
|
||||
S.t <- 0
|
||||
SUSPEND
|
||||
return
|
||||
|
||||
S.up() :=
|
||||
S.counter++
|
||||
if S.counter <= 0 then
|
||||
activate a proc from S.queue
|
||||
return
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue