Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 11

Monitors

Programming language construct that supports


controlled access to shared data
Compiler adds synchronization automatically
Enforced at runtime

Encapsulates
Shared data structures
Procedures/functions that operate on the data
Synchronization between processes calling those
procedures

Only one process active inside a monitor at any


instant
All procedures are part of critical section

Monitors
High-level synchronization allowing safe
sharing of an abstract data type among
concurrent processes.
monitor monitor-name
{
shared variable declarations
procedure body P1 () {
. . .
}
procedure body P2 () {
. . .
}
procedure body Pn () {
. . .
}
{
initialization code
}
}

Monitors

shared data

at most one
process in monitor
at a time

operations (procedures)

Monitors
Mutual exclusion
only one process can be executing inside at any time
if a second process tries to enter a monitor
procedure, it blocks until the first has relinquished
the monitor

Once inside a monitor, process may discover it


is not able to continue
condition variables provided within monitor
processes can wait or signal others to continue
condition variable can only be accessed from inside monitor
waiting process relinquishes monitor temporarily

Monitors
To allow a process to wait within the monitor,
a condition variable must be declared, as
condition x;

Condition variable can only be used with the


operations wait and signal.

The operation
wait(x);
means that the process invoking this operation is
suspended until another process invokes
signal(x);
The signal operation resumes exactly one
suspended process. If no process is suspended,
then the signal operation has no effect.

wait and signal

(continued)

When process invokes wait, it


relinquishes the monitor lock to allow
other processes in.
When process invokes signal, the
resumed process must reacquire
monitor lock before it can proceed
(inside the monitor)

Monitors Condition
Variables

Monitors
monitor ProducerConsumer {

void producer() {

condition full, empty;

item i;

integer count = 0;

while (TRUE) {
/* produce item i */

/* function prototypes */

ProducerConsumer.insert(i);

void insert(item i);


item remove();
}

}
}
void consumer() {

void producer();

item i;

void consumer();

while (TRUE) {
i =
ProducerConsumer.remove();
/* consume item i */
}
}
8

Monitors
void insert (item i) {
if (count == N) wait(full);
/* add item i */
count = count + 1;
if (count == 1) then signal(empty);
}
item remove () {
if (count == 0) wait(empty);
/* remove item into i */
count = count - 1;
if (count == N-1) signal(full);
return i;
}
9

Monitors variations
Hoare monitors: signal(c) means

run waiting process immediately (and acquires


monitor lock)
signaler blocks immediately (and releases lock)
condition guaranteed to hold when waiter runs
Mesa/Pilot monitors: signal(c) means
Waiting process is made ready, but signaler
continues
waiter competes for monitor lock when signaler leaves
monitor (or waits)
condition is not necessarily true when waiter runs again

being woken up is only a hint that something has


changed
must recheck conditional case
10

Monitors (Mesa)
void insert (item i) {
while (count == N) wait(full);
/* add item i */
count = count + 1;
if (count == 1) then signal(empty);
}
item remove () {
while (count == 0) wait(empty);
/* remove item into i */
count = count - 1;
if (count == N-1) signal(full);
return i;
}
11

You might also like