diff --git a/Concurrent Systems/notes/4 - Semaphores.md b/Concurrent Systems/notes/4 - Semaphores.md index 7d4d5a3..4a66f7b 100644 --- a/Concurrent Systems/notes/4 - Semaphores.md +++ b/Concurrent Systems/notes/4 - Semaphores.md @@ -205,7 +205,8 @@ end_write() := ##### Weak priority to Writers ``` -GLOB_MUTEX, PRIO_MUTEX, R_MUTEX and W_MUTEX semaphores init. at 1 R and W shared registers init. at 0 +GLOB_MUTEX, PRIO_MUTEX (to prioritize the writers), R_MUTEX and W_MUTEX semaphores init. at 1 +R and W shared registers init. at 0 begin_read() := PRIO_MUTEX.down() @@ -217,5 +218,27 @@ begin_read() := PRIO_MUTEX.up() return -end_read() := - +end_read() := # like weak priority to readers + R_MUTEX.down() + R-- + if R = 0 then GLOB_MUTEX.up() + R_MUTEX.up() + return + +def begin_write() := + W_MUTEX.down() + W++ + if W = 1 then + PRIO_MUTEX.down() + W_MUTEX.up() + GLOB_MUTEX.down() + return + +def end_write() := + GLOB_MUTEX.up() + W_MUTEX.down() + W-- + if W = 0 then + PRIO_MUTEX.up() + W_MUTEX.up() + return \ No newline at end of file