From Operating System Concepts by Abraham Silberschatz, Peter B. Galvin, Greg Gagne
5.8.3 Implementing a Monitor Using Semaphores
We now consider a possible implementation of the monitor mechanism using semaphores. For each monitor, a semaphore
mutex(initialized to 1) is provided. A process must execute
wait(mutex)before entering the monitor and must execute
signal(mutex)after leaving the monitor.
Since a signaling process must wait until the resumed process either leaves or waits, an additional semaphore,
next, is introduced, initialized to 0. The signaling processes can use
nextto suspend themselves. An integer variable
next countis also provided to count the number of processes suspended on
next. Thus, each external function
Fis replaced by
wait(mutex); ... body of F ... if (next count > 0) signal(next); else signal(mutex);
Mutual exclusion within a monitor is ensured.
Since the semaphore
mutexis initialized to 1, is it used for limiting at most one process can be inside the monitor?
nextmeans the number of processes suspended on
next. Are these processes suspended on
nextall inside a monitor?
If the answers to the above two questions are yes, do they contradict each other?
If no, how does a monitor work actually?