Presentation is loading. Please wait.

Presentation is loading. Please wait.

June 11, 2002Serguei A. Mokhov, 1 The Monitor COMP346 - Operating Systems Tutorial 7 Edition 1.2, June 15, 2002.

Similar presentations


Presentation on theme: "June 11, 2002Serguei A. Mokhov, 1 The Monitor COMP346 - Operating Systems Tutorial 7 Edition 1.2, June 15, 2002."— Presentation transcript:

1 June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 The Monitor COMP346 - Operating Systems Tutorial 7 Edition 1.2, June 15, 2002

2 June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 2 Topics Monitor Debrief Example

3 June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 3 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

4 June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 4 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

5 June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 5 The Monitor: Disadvantages Monitor allows to programmer: –Release a resource, which was never acquired –Request the same resource w/o releasing it first

6 June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 6 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

7 June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 7 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

8 June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 8 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()

9 June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 9 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.

10 June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 10 Restaurant Problem (2) Monitor { condition customers = 0; procedure bool eat() {} procedure void leave() {} }

11 June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 11 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; } procedure leave() { if(customers.queue() > 0) { customers.signal(); }

12 June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 12 Restaurant Problem (4) customer { main() { if(monitor.eat()) { // eating here monitor.leave(); } else { // leaving hungry }

13 June 11, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 13 Examples Semaphore.java SemRestaurant.java MonRestaurant.java


Download ppt "June 11, 2002Serguei A. Mokhov, 1 The Monitor COMP346 - Operating Systems Tutorial 7 Edition 1.2, June 15, 2002."

Similar presentations


Ads by Google