Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

Operating Systems Lecture Notes

Lecture 14
Monitors
Martin C. Rinard
Monitors: A high-level data abstraction tool that automatically generates atomic
operations on a given data structure. A monitor has:
o Shared data.
o A set of atomic operations on that data.
o A set of condition variables.
Monitors can be imbedded in a programming language: Mesa/Cedar from Xerox A!C.
"ypical implementation: each monitor has one loc#. Ac$uire loc# %hen begin a monitor
operation& and !elease loc# %hen operation finishes. 'ptimi(ation: reader/%riter loc#s.
Statically identify operations that only read data& then allo% these read-only operations to
go concurrently. )riters get mutual exclusion %ith respect to other %riters and to readers.
Standard synchroni(ation mechanism for accessing shared data.
Advantages: reduces probability of error *never forget to Ac$uire or !elease the loc#+&
biases programmer to thin# about the system in a certain %ay *is not ideologically
neutral+. "rend is a%ay from encapsulated high-level operations such as monitors to%ard
more general purpose but lo%er level synchroni(ation operations.
,ounded buffer using monitors and signals
o Shared State data[10] - a buffer holding produced data. num - tells ho% many
produced data items there are in the buffer.
o Atomic Operations Produce(v) called %hen producer produces data item v.
Consume(v) called %hen consumer is ready to consume a data item. Consumed
item put into v.
o Condition Variables bufferAvail - signalled %hen a buffer becomes available.
dataAvail - signalled %hen data becomes available.
monitor {
Condition *bufferAvail, *dataAvail;
int num = 0;
int data[10];

Produce(v) {
!ile (num == 10) { "* #esa semantics *"
bufferAvail$%&ait();
'
(ut v into data arra)
num**;
dataAvail$%+i,nal(); "* must ala)s do t!is- *"
"* can re(lace it! broadcast- *"
'
Consume(v) {
!ile (num == 0) { "* #esa +emantics *"
dataAvail$%&ait();
'
(ut ne.t data arra) value into v
num$$;
bufferAvail$%+i,nal(); "* must ala)s do t!is- *"
"* can re(lace it! broadcast- *"
'
'
"he best %ay to understand monitors is that there is a syntactic transformation that inserts
the loc# operations.
Condition *bufferAvail, *dataAvail;
int num = 0;
int data[10];
/oc0 *monitor/oc0;

Produce(v) {
monitor/oc0$%Ac1uire(); "* Ac1uire monitor loc0 $ ma0es o(eration
atomic *"
!ile (num == 10) { "* #esa semantics *"
bufferAvail$%&ait(monitor/oc0);
'
(ut v into data arra)
num**;
dataAvail$%+i,nal(monitor/oc0); "* must ala)s do t!is- *"
"* can re(lace it! broadcast- *"
monitor/oc0$%2elease(); "* 2elease monitor loc0 after (erform
o(eration *"
'
Consume(v) {
monitor/oc0$%Ac1uire(); "* Ac1uire monitor loc0 $ ma0es o(eration
atomic *"
!ile (num == 0) { "* #esa +emantics *"
dataAvail$%&ait(monitor/oc0);
'
(ut ne.t data arra) value into v
num$$;
bufferAvail$%+i,nal(monitor/oc0); "* must ala)s do t!is- *"
"* can re(lace it! broadcast- *"
monitor/oc0$%2elease(); "* 2elease monitor loc0 after (erform
o(eration *"
'
'

You might also like