From 60d4e7ea2d02dbb72e6075012f85da132f7284a2 Mon Sep 17 00:00:00 2001 From: Marco Realacci Date: Wed, 12 Mar 2025 10:54:27 +0100 Subject: [PATCH] vault backup: 2025-03-12 10:54:27 --- .obsidian/workspace.json | 2 +- Concurrent Systems/notes/4 - Semaphores.md | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index dba74c0..8236f1a 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -36,7 +36,7 @@ "file": "Concurrent Systems/slides/class 4.pdf", "page": 4, "left": -23, - "top": 342, + "top": 187, "zoom": 0.652019002375297 }, "icon": "lucide-file-text", diff --git a/Concurrent Systems/notes/4 - Semaphores.md b/Concurrent Systems/notes/4 - Semaphores.md index 81fe07e..599ffb0 100644 --- a/Concurrent Systems/notes/4 - Semaphores.md +++ b/Concurrent Systems/notes/4 - Semaphores.md @@ -71,4 +71,21 @@ Same as before but we use test&set to actually ensure MUTEX (of course we could It's a shared FIFO buffer of size k. - `BUF[0,…,k-1]`: generic registers (not even safe) accessed in MUTEX - `IN/OUT` : two variables pointing to locations in `BUF` to (circularly) insert/remove items, both initialized at 0 -- `FREE/BUSY`: two semaphores that count thew number of free/busy cells of BUF, initialized at k and 0 respectively. \ No newline at end of file +- `FREE/BUSY`: two semaphores that count thew number of free/busy cells of `BUF`, initialized at k and 0 respectively. + +``` +B.produce(v) := + FREE.down() # blocking if FREE becomes negative + BUF[IN] <- v + IN <- (IN+1) mod k # mod k since it is circular + BUSY.up() # "there is something to consume" + return + +B.consume() := + BUSY.down() # it blocks if there is nothing to consume + tmp <- BUF[OUT] + OUT <- (OUT+1) mod k + FREE.up() + return tmp +``` +