diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index 53fa912..01f0c24 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -34,9 +34,9 @@ "type": "pdf", "state": { "file": "Concurrent Systems/slides/class 4.pdf", - "page": 14, + "page": 16, "left": -27, - "top": 94, + "top": 130, "zoom": 0.5754156769596199 }, "icon": "lucide-file-text", diff --git a/Concurrent Systems/notes/4b -.md b/Concurrent Systems/notes/4b -.md index d11cef5..2b63763 100644 --- a/Concurrent Systems/notes/4b -.md +++ b/Concurrent Systems/notes/4b -.md @@ -88,4 +88,72 @@ operation end_write() := else CW.signal() -``` \ No newline at end of file +``` + +#### Readers/Writers problem, strong priority to Writers +``` +monitor RW_READERS := + AR, WR, AW, WW init at 0 + condition CR, CW + +operation begin_read() := + if WW + AW != 0 then + CR.wait() + CR.signal() + AR++ + +operation end_read() := + AR-- + if AR = 0 then + CW.signal() + +operation begin_write() := + WW++ + if (AR+AW != 0) then + CW.wait() + AW++ + WW-- + +operation end_write() := + AW-- + if WW > 0 then + CW.signal() + else + CR.signal() +``` + +#### Readers/Writers problem, a fair solution +- after a write, all waiting readers are enabled +- during a read, new readers must wait if writers are waiting +``` +monitor RW_READERS := + AR, WR, AW, WW init at 0 + condition CR, CW + +operation begin_read() := + WR++ + if WW + AW != 0 then + CR.wait() + CR.signal() + AR++ + WR-- + +operation end_read() := + AR-- + if AR = 0 then + CW.signal() + +operation begin_write() := + WW++ + if (AR+AW != 0) then + CW.wait() + AW++ + WW-- + +operation end_write() := + AW-- + if WW > 0 then + CW.signal() + else + CR.signal() +```