Kickstart Intro to Java
Download
Report
Transcript Kickstart Intro to Java
The Monitor
COMP346 - Operating Systems
Tutorial 7
Edition 1.2, June 15, 2002
June 11, 2002
Serguei A. Mokhov,
[email protected]
1
Topics
• Monitor Debrief
• Example
June 11, 2002
Serguei A. Mokhov,
[email protected]
2
The Monitor
• An abstract data structure
– Uses conditionals (data members)
– When a process runs within a monitor only one
function is accessed at a time
– Conditional variables represent critical regions
and functions are critical sections, thus must be
guaranteed to be atomic
– In analogy to the semaphore, it will have its
own Signal() and Wait() or several of them
June 11, 2002
Serguei A. Mokhov,
[email protected]
3
The Monitor: Advantages
• Reduce number of programmer’s errors
when use semaphores
• Internal implementation of the monitor may
change, but the clients using it shouldn’t be
rewritten
June 11, 2002
Serguei A. Mokhov,
[email protected]
4
The Monitor: Disadvantages
• Monitor allows to programmer:
– Release a resource, which was never acquired
– Request the same resource w/o releasing it first
June 11, 2002
Serguei A. Mokhov,
[email protected]
5
Condition Variables
• When a thread is running within the monitor
and certain condition is not met, it blocks
• Ownership is transferred to another process
• It can only be unblocked by other process
which satisfies the condition
• The variables used in conditions are called
condition variables
June 11, 2002
Serguei A. Mokhov,
[email protected]
6
Basic Operations
• queue() – how many processes a waiting
on a condition
• signal() – let others know that condition
doesn’t hold anymore
• wait() – block oneself on a condition and
release monitor to others
June 11, 2002
Serguei A. Mokhov,
[email protected]
7
Java’s Implementation of
Monitors
• synchronized makes functions atomic
• Java maintains internally object’s waiting queue
and does not provide queue() function
• Java’s equivalent to signal() – notify()
and notifyAll(); notify() wakes up
exactly one thread (is any) sleeping on the
condition; notifyAll() – everyone
respectively.
• Java’s equivalent to wait() is … well … wait()
June 11, 2002
Serguei A. Mokhov,
[email protected]
8
Restaurant Problem
• 10 places in the restaurant
• 3 people can wait in the waiting area if the
resto is full
• All extra people simply leave seeing that the
resto and the waiting area are full
• Once smb leaves the resto after eating, if
there’s smb in the waiting area, that
customer enters the resto.
June 11, 2002
Serguei A. Mokhov,
[email protected]
9
Restaurant Problem (2)
Monitor
{
condition customers = 0;
procedure bool eat()
{}
procedure void leave() {}
}
June 11, 2002
Serguei A. Mokhov,
[email protected]
10
Restaurant Problem (3)
procedure eat()
{
if(customers.queue() < 13)
{
if (customers.queue() < 10)
{
// allowed to eat
return true;
}
else
{
customers.wait();
return true;
}
}
return false;
}
June 11, 2002
procedure leave()
{
if(customers.queue() > 0)
{
customers.signal();
}
}
Serguei A. Mokhov,
[email protected]
11
Restaurant Problem (4)
customer
{
main()
{
if(monitor.eat())
{
// eating here
monitor.leave();
}
else
{
// leaving hungry
}
}
}
June 11, 2002
Serguei A. Mokhov,
[email protected]
12
Examples
• Semaphore.java
• SemRestaurant.java
• MonRestaurant.java
June 11, 2002
Serguei A. Mokhov,
[email protected]
13