Sleep and Wakeup using Monitors

The problem with the semaphores described in the previous page is that they require the programmer to use low-level system calls. It is easy to put these system calls in the wrong order, resulting in a deadlock. A higher level solution would make implementing mutual exclusion and syncronization a little easier. Monitors do just that. They are a high level construct found in some programming languages which contain variables, procedures, and data structures. Monitors closely resemble the protected classes and modules which are found in languages like C++ and Ada, in that they only allow access to the variables they contain through the functions they contain. Only one process may execute a function within a monitor at any one time. This is what allows monitors to enforce mutual exclusion.

How do monitors handle blocking?

What happens when a process calls a function inside a monitor that is already occupied by another process? Monitors use condition variables and two operations called WAIT and SIGNAL. When a process calls a function in a monitor that is already occupied, it performs a WAIT operation on some condition variable. This will cause the process to block, until the other process, which currently occupies the monitor, leaves the monitor and performs a SIGNAL on the condition variable.

Look at the following example, in which two processes interact with one monitor.


               
IPC Home    Subway    Solution   Sleep &   Message
  Page       Map       Types     Wake Up   Passing