From 71114f1630f9fb996112f73ae95d4833e12e8c18 Mon Sep 17 00:00:00 2001 From: Marco Realacci Date: Wed, 12 Mar 2025 11:54:27 +0100 Subject: [PATCH] vault backup: 2025-03-12 11:54:27 --- Concurrent Systems/notes/4 - Semaphores.md | 32 ++++++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/Concurrent Systems/notes/4 - Semaphores.md b/Concurrent Systems/notes/4 - Semaphores.md index 2a00ca9..7a465ab 100644 --- a/Concurrent Systems/notes/4 - Semaphores.md +++ b/Concurrent Systems/notes/4 - Semaphores.md @@ -127,7 +127,32 @@ B.consume() := Thanks to the semaphores, we are sure that while loops will not go on forever! The loops starts only if there is at least a FREE / BUSY cell. #### (Multiple) Producers/Consumers - Wrong solution -EXERCISE - will do later +**EXERCISE** this example is wrong +``` +B.produce(v) := + FREE.down() + SP.down() + i <- IN + IN <- (IN + 1) mod k + EMPTY[in] <- ff + SP.up() + BUF[i] <- v + FULL[i] <- tt + BUSY.up() + return + +B.consume() := + BUSY.down() + SC.down() + o <- OUT + OUT <- (OUT+1) mod k + FULL[OUT] <- ff + SC.up() + tmp <- BUF[o] + EMPTY[o] <- tt + FREE.up() + return tmp +``` #### The Readers/Writers problem - Several processes want to access a file @@ -181,7 +206,7 @@ end_write() := GLOB_MUTEX.up() return ``` -If readers keeps arriving, they surpass writers. But when a writer terminates a +If readers keeps arriving, they surpass writers. But when a writer terminates it activates the first suspended process, which can be a reader or a writer. ##### Strong priority to Readers When a writer terminates, it activates the first reader, if there is any, or the first writer, otherwise. @@ -243,4 +268,5 @@ def end_write() := W_MUTEX.up() return ``` -This is prioritizing writers as if there are writers waiting, they will be waiting at `GLOB_MUTEX.down()`. This semaphore is upped before `PRIO_MUTEX` which is the one that blocks readers. \ No newline at end of file +This is prioritizing writers as if there are writers waiting, they will be waiting at `GLOB_MUTEX.down()`. This semaphore is upped before `PRIO_MUTEX` which is the one that blocks readers. +But writers won't be able to writer until there are no readers, if they keep coming, they will block the writers as `GLOB_MUTEX` will never be upped. \ No newline at end of file