Professional Documents
Culture Documents
Monitors: - Programming Language Construct That Supports Controlled Access To Shared Data
Monitors: - Programming Language Construct That Supports Controlled Access To Shared Data
Encapsulates
Shared data structures
Procedures/functions that operate on the data
Synchronization between processes calling those
procedures
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
Monitors
To allow a process to wait within the monitor,
a condition variable must be declared, as
condition x;
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.
(continued)
Monitors Condition
Variables
Monitors
monitor ProducerConsumer {
void producer() {
item i;
integer count = 0;
while (TRUE) {
/* produce item i */
/* function prototypes */
ProducerConsumer.insert(i);
}
}
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
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