Monitor
Introduction
Monitor 是 high-level synchronization construct,把 shared data 與操作它的 functions 包在同一個 ADT,並保證同一時間只有一個 process active inside the monitor。
它把 synchronization rule 放進 language / ADT,減少手寫 semaphore protocol 時的順序錯誤。
Condition Variable
Monitor 的 mutual exclusion 只保證同時只有一個 process 在 monitor 內;若 process 需要等待某個 condition,使用 condition variable。
condition x, y;主要操作:
x.wait();
x.signal();x.wait():呼叫者 suspend,讓出 monitorx.signal():resume 一個等待x的 process;若沒人等待,沒有任何效果
Condition variable 的 signal 不會累積;semaphore 的 signal 會改變 value。
Signal Semantics
若 P 在 monitor 內 x.signal(),而 Q 被喚醒,monitor 不能讓 P 和 Q 同時 active。
- Signal and wait:P 先等,Q 立刻恢復
- Signal and continue:P 繼續跑,Q 等 P 離開 monitor 後恢復
Implementations and Resume Order
Monitor Implementation with Semaphores
D-OS-Ch06ga-Monitor_Implementation_with_Semaphores