From ec1b4706d8b9fe106501e96ec6eedd1d3ccdb2b3 Mon Sep 17 00:00:00 2001 From: Marco Realacci Date: Wed, 12 Mar 2025 11:24:27 +0100 Subject: [PATCH] vault backup: 2025-03-12 11:24:27 --- .obsidian/workspace.json | 2 +- Concurrent Systems/notes/4 - Semaphores.md | 28 +++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index 9499de3..bfe8edc 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -36,7 +36,7 @@ "file": "Concurrent Systems/slides/class 4.pdf", "page": 8, "left": -23, - "top": 421, + "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 0d586b2..d66924b 100644 --- a/Concurrent Systems/notes/4 - Semaphores.md +++ b/Concurrent Systems/notes/4 - Semaphores.md @@ -153,4 +153,30 @@ conc_write() := ##### Weak priority to Readers - If a reader arrives during a read, it can surpass possible writers already suspended (risk of starvation for the writes) -- When a writer terminates, it activates the first suspended process, irrispectively of whether it is a reader or a writer (so, the priority to readers is said «weak») \ No newline at end of file +- When a writer terminates, it activates the first suspended process, irrispectively of whether it is a reader or a writer (so, the priority to readers is said «weak») + +``` +GLOB_MUTEX and R_MUTEX semaphores init. at 1 +R a shared register init. at 0 + +begin_read() := + R_MUTEX.down() + R++ # currently active readers + if R = 1 then GLOB_MUTEX.down() + R_MUTEX.up() + return + +end_read() := + R_MUTEX.down() + R-- + if R = 0 then GLOB_MUTEX.up() + R_MUTEX.up() + return + +begin_write() := + GLOB_MUTEX.down() + return + +end_write() := + GLOB_MUTEX.up() + return