vault backup: 2025-03-12 10:44:27

This commit is contained in:
Marco Realacci 2025-03-12 10:44:27 +01:00
parent fb56bcc453
commit 6547be1a32
2 changed files with 33 additions and 7 deletions

View file

@ -34,9 +34,9 @@
"type": "pdf",
"state": {
"file": "Concurrent Systems/slides/class 4.pdf",
"page": 2,
"page": 3,
"left": -23,
"top": 165,
"top": 298,
"zoom": 0.652019002375297
},
"icon": "lucide-file-text",
@ -217,16 +217,16 @@
},
"active": "6edd4157a160e462",
"lastOpenFiles": [
"Concurrent Systems/slides/class 4.pdf",
"Concurrent Systems/notes/4 - Semaphores.md",
"Concurrent Systems/notes/3a - Hardware primitives & Lamport Bakery algorithm.md",
"Concurrent Systems/notes/3b - Aravind's algorithm and improvements.md",
"Concurrent Systems/notes/4 - Semaphores.md",
"Concurrent Systems/slides/class 4.pdf",
"Concurrent Systems/notes/images/Pasted image 20250310172134.png",
"Concurrent Systems/notes/1b - Peterson algorithm.md",
"Concurrent Systems/slides/class 3.pdf",
"Concurrent Systems/notes/images/Pasted image 20250310103703.png",
"Concurrent Systems/notes/2 - Fast mutex by Lamport.md",
"Concurrent Systems/notes/1 - CS Basics.md",
"Concurrent Systems/notes/3a - Hardware primitives & Lamport Bakery algorithm.md",
"Concurrent Systems/notes/2b - Round Robin algorithm.md",
"Concurrent Systems/notes/1 - CS Basics2.md",
"HCIW/slides/Interface and Interaction for IoT.pdf",

View file

@ -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
@ -30,3 +30,29 @@ S.up() :=
if S.counter <= 0 then
activate a proc from S.queue
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