0% found this document useful (0 votes)
31 views6 pages

Monitors

Uploaded by

Sharlin Lins L
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views6 pages

Monitors

Uploaded by

Sharlin Lins L
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY

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 (…) { … }
}
}

2.9.1 Monitor Usage


• A monitor is essentially a class, in which all data is private, and with the
special restriction that only one method within any given monitor object may be active at
the same time. An additional restriction is that monitor methods may only access the shared
data within the monitor and any data passed to them as parameters. I.e. they cannot access
any data external to the monitor.

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.

Monitor with condition variables


• But now there is a potential problem - If process P within the monitor issues a signal that
would wake up process Q also within the monitor, then there would be two processes running
simultaneously within the monitor, violating the exclusion requirement. Accordingly there are
two possible solutions to this dilemma:

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.

2.9.2 Dining-Philosophers Solution Using Monitors


• This solution to the dining philosophers uses monitors, and the restriction that
a philosopher may only pick up chopsticks when both are available. There are also two key
data structures in use in this solution:
1.enum { THINKING, HUNGRY,EATING } state[ 5 ]; A philosopher may only set
their state to eating when neither of their adjacent neighbors is eating. ( state[ ( i + 1 ) % 5 ]
!= EATING && state[ ( i + 4 ) % 5 ] != EATING ).
2.Conditionself[ 5 ]; This condition is used to delay a hungry philosopher who is
unable to acquire chopsticks.
In the following solution philosophers share a monitor, DiningPhilosophers, and eat using the
following sequence of operations:
3.DiningPhilosophers.pickup( ) - Acquires chopsticks, which may block the
process.
4.eat
5.DiningPhilosophers.putdown( ) - Releases the chopsticks.
monitorDiningPhilosophers
{
enum { THINKING; HUNGRY, EATING) state [5] ;
condition self [5];
void pickup (inti) { state[i] = HUNGRY;
test(i);
if (state[i] != EATING) self[i].wait; }
void putdown (inti)
{ state[i] = THINKING;

CS8493-OPERATING SYSTEMS
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY

// test left and right neighborstest((i + 4) % 5);


test((i + 1) % 5);
}
void test (inti) { if ((state[(i + 4) % 5] != EATING) && (state[i] == HUNGRY) &&
(state[(i + 1) % 5] != EATING) )
{ state[i] = EATING ;
self[i].signal () ;
} } initialization_code()
{ for (inti = 0; i< 5; i++)
state[i] = THINKING;
}
}
2.9.3 Implementing a Monitor Using Semaphores
• One possible implementation of a monitor uses a semaphore "mutex" to control
mutual exclusionary access to the monitor, and a counting semaphore "next" on which
processes can suspend themselves after they are already "inside" the monitor ( in conjunction
with condition variables, see below. ) The integer next_count keeps track of how many
processes are waiting in the next queue. Externally accessible monitor processes are then
implemented as:
• Condition variables can be implemented using semaphores as well. For a condition x,
a semaphore "x_sem" and an integer "x_count" are introduced, both initialized to zero. The
wait and signal methods are then implemented as follows. ( This approach to the condition
implements the signal-and-wait option described above for ensuring that only one process at
a time is active inside the monitor. )
Variables semaphoremutex;
// (initially = 1) semaphore next;
// (initially = 0) intnext_count = 0;
Each procedure F will be replaced by wait(mutex);

body of F;
… if (next_count> 0) signal(next) else signal(mutex);

CS8493-OPERATING SYSTEMS
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY

Mutual exclusion within a monitor is ensured


For each condition variable x, we have: semaphore x_sem; // (initially = 0) intx_count = 0;
The operation x.waitcan be implemented as:
x_count++; if (next_count> 0) signal(next); else
signal(mutex); wait(x_sem); x_count--;
The operation x.signalcan be implemented as:
if (x_count> 0) { next_count++;
signal(x_sem); wait(next);
next_count--;
}

2.9.4 Resuming Processes Within a Monitor


• When there are multiple processes waiting on the same condition within a monitor,
how does one decide which one to wake up in response to a signal on that condition? One
obvious approach is FCFS, and this may be suitable in many cases.
• Another alternative is to assign ( integer ) priorities, and to wake up the process with
the smallest ( best ) priority.
• Figure illustrates the use of such a condition within a monitor used for resource
allocation. Processes wishing to access this resource must specify the time they expect to use
it using the acquire( time ) method, and must call the release( ) method when they are done
with the resource.
monitorResourceAllocator
{ boolean busy; condition x; void acquire(int time) { if (busy)
x.wait(time); busy = TRUE;
} void release() { busy = FALSE;
x.signal();
} initialization code() { busy = FALSE;
}
}
A monitor to allocate a single resource.
Unfortunately the use of monitors to restrict access to resources still only works if
programmers make the requisite acquire and release calls properly. One option would be to

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

You might also like