Monitor Semaphore: Wait and Signal
E N D
Presentation Transcript
Wait/Signal in Monitor MonitorSemaphoreWait Always suspends Changes semaphore’s state Suspends or not depends on stateSignal Resumes a waiting process Resumes a waiting process = No-op if no process waiting Changes semaphore’s state • To avoid confusions, some texts stick to using P and V for semaphore’s wait and signal. POS-A
Monitor Implementation x Condition variables and their queues y Entry queue Entry procedures Initialization code POS-A
Two Choices • Suppose a process (W) is waiting on x. • Another process (S) then executes x.signal. • Should we suspend S and let W run? • Yes the implementation we saw • No (S continues to execute) danger is that the logical condition for which W was waiting may no longer hold POS-A
Guarantees • Execution of procedures must be mutually exclusive. • A wait must block the current process on the corresponding condition. • When a process exits or is blocked on a condition and there are processes waiting to enter or reenter the monitor, one must be selected. • If there is a process suspended as the result of executing a signal operation, then it is selected; otherwise, one of the processes from the initial queue of entering processes is selected (processes blocked on conditions remain suspended) • A signal must determine if any process is waiting on the corresponding condition. If this is the case, the current process is suspended and one of these waiting processes is reactivated; otherwise, the current process continues. POS-A
The Implementation mutex a semaphore initialized to 1; used to guard mutual access to procedures inside the monitor next a semaphore initialized to 0; used to suspend a process when executing a signal operation (the Yes choice) x-sem a semaphore initialized to 0; used to suspend a process executing a wait on x next-count preset to 0; contains the number of processes suspended as a result of a signal operation x-count preset to 0; contains the number of processes suspended as a result of a wait operation on x POS-A