Monitors

The monitor is a programming-language construct that provides equivalent functionality to that of semaphores and that is easier to control.

It has also been implemented as a program library. This allows programmers to put a monitor lock on any object. In particular, for something like a linked list, you may want to lock all linked lists with one lock, or have one lock for each list, or have one lock for each element of each list.

Monitor with Signal

The chief characteristics of a monitor are the following:

  • Local data variables are accessible only by the monitor (⇒ shared data in monitor is “safe”)
  • Process enters monitor by invoking one of its procedures (⇒ controlled entry)
  • Only one process may be executing in the monitor at a time (⇒ mutex)

A monitor supports synchronization by the use of condition variables that are contained within the monitor and accessible only within the monitor. They are operated on by two functions:

  • : Suspend execution of the calling process on condition . The monitor is now available for use by another process
  • : Resume execution of some process blocked after a on the same condition. If there are several such processes, choose one of them; if there is no such process, do nothing.

Different from semaphore

Note that monitor wait and signal operations are different from those for the semaphore. If a process in a monitor signals and no task is waiting on the condition variable, the signal is lost.

Monitors on Producer and Consumer Problem:

A producer can add characters to the buffer only by means of the procedure append inside the monitor; the producer does not have direct access to buffer. The procedure first checks the condition notfull to determine if there is space available in the buffer. If not, the process executing the monitor is blocked on that condition. Some other process (producer or consumer) may now enter the monitor. Later, when the buffer is no longer full, the blocked process may be removed from the queue, reactivated, and resume processing. After placing a character in the buffer, the process signals the notempty condition. A similar description can be made of the consumer function.

Solution coded in Figure 5.16 of textbook

Difference between monitors and semaphores

This example points out the division of responsibility with monitors compared to semaphores.

In the case of monitors, the monitor construct itself enforces mutual exclusion: It is not possible for both a producer and a consumer simultaneously to access the buffer. However, the programmer must place the appropriate cwait and csignal primitives inside the monitor to prevent processes from depositing items in a full buffer or removing them from an empty one. In the case of semaphores, both mutual exclusion and synchronization are the responsibility of the programmer.

Advantage that monitors have over semaphores is that all of the synchronization functions are confined to the monitor. Thus, easier to verity that the synchronization has been done correctly and to detect bugs.