vault backup: 2025-03-12 10:39:14
This commit is contained in:
parent
7691811801
commit
fb56bcc453
2 changed files with 28 additions and 4 deletions
|
@ -4,5 +4,29 @@
|
|||
**Semaphore:** is a shared counter S accessed via primitives $up$ and $down$ s.t.:
|
||||
- is initialized at s0 >= 0
|
||||
- it is alwayz >= 0
|
||||
- up atomically increases S
|
||||
- down atomically decreases S if it is not 0, otherwise the proce
|
||||
- *up* atomically increases S
|
||||
- *down* atomically decreases S if it is not 0, otherwise the calling process is blocked and waits.
|
||||
|
||||
Main use: **prevent busy waiting**: suspend processes that cannot perform *down*.
|
||||
|
||||
**Strong semaphore:** if uses a FIFO policy for blocking/unblocking, otherwise it's **weak**.
|
||||
**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 data structure (typically a queue), initially empty, to store suspended processes.
|
||||
|
||||
#### Ideal implementation
|
||||
```
|
||||
S.down() :=
|
||||
S.counter--
|
||||
if S.counter < 0 then
|
||||
enter into S.queue
|
||||
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