diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index 035da25..17dc80e 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -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", diff --git a/Concurrent Systems/notes/4 - Semaphores.md b/Concurrent Systems/notes/4 - Semaphores.md index 99f5001..b758a9b 100644 --- a/Concurrent Systems/notes/4 - Semaphores.md +++ b/Concurrent Systems/notes/4 - Semaphores.md @@ -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 \ No newline at end of file + 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