Monitors
Monitors
2.9 MONITORS
• Semaphores can be very useful for solving concurrency problems, but only if
programmers use them properly. If even one process fails to abide by the proper use of
semaphores, either accidentally or deliberately, then the whole system breaks down. ( And
since concurrency problems are by definition rare events, the problem code may easily go
unnoticed and/or be heinous to debug. )
• For this reason a higher-level language construct has been developed, called
monitors. monitor monitor-name
{
// shared variable declarations procedure P1 (…) { …. } procedure Pn (…) {……}
Initialization code (…) { … }
}
}
CS8493-OPERATING SYSTEMS
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
Figure shows a schematic of a monitor, with an entry queue of processes waiting their turn
to execute monitor operations ( methods. )
• In order to fully realize the potential of monitors, we need to introduce one additional
new data type, known as a condition.
• A variable of type condition has only two legal operations, wait and signal. I.e. if X was
defined as type condition, then legal operations would be X.wait( ) and X.signal( )
• The wait operation blocks a process until some other process calls signal, and adds the
blocked process onto a list associated with that condition.
• The signal process does nothing if there are no processes waiting on that condition.
Otherwise it wakes up exactly one process from the condition's list of waiting processes. (
Contrast this with counting semaphores, which always affect the semaphore on a signal call.
)
• Figure below illustrates a monitor that includes condition variables within its data
space. Note that the condition variables, along with the list of processes currently waiting
for the conditions, are in the data space of the monitor - The processes on these lists are not
"in" the monitor, in the sense that they are not executing any code in the monitor.
CS8493-OPERATING SYSTEMS
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
Signal and wait - When process P issues the signal to wake up process Q, P then waits, either
for Q to leave the monitor or on some other condition.
Signal and continue - When P issues the signal, Q waits, either for P to exit the monitor or for
some other condition.
There are arguments for and against either choice. Concurrent Pascal offers a third alternative
- The signal call causes the signalling process to immediately exit the monitor, so that the
waiting process can then wake up and proceed.
CS8493-OPERATING SYSTEMS
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
CS8493-OPERATING SYSTEMS
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
CS8493-OPERATING SYSTEMS
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
place the resource allocation code into the monitor, thereby eliminating the option for
programmers to bypass or ignore the monitor, but then that would substitute the monitor's
scheduling algorithms for whatever other scheduling algorithms may have been chosen for
that particular resource.
CS8493-OPERATING SYSTEMS