monitors monitors
play

Monitors Monitors 1 What What Is What What Is Is a Monitor? - PowerPoint PPT Presentation

Monitors Monitors 1 What What Is What What Is Is a Monitor? Is a Monitor? Monitor? - Basics onitor? - Basics Basics Basics Monitor is a highly structured programming Monitor is a highly structured programming language construct.


  1. Monitors Monitors 1

  2. What What Is What What Is Is a Monitor? Is a Monitor? Monitor? - Basics onitor? - Basics Basics Basics � Monitor is a highly structured programming � Monitor is a highly structured programming language construct. It consists of � Private variables and private procedures that � Private variables and private procedures that can only be used within a monitor. � Constructors that initialize the monitor. � C t t th t i iti li th it � A number of (public) monitor procedures that can be invoked by users. � Note that monitors have no public data. p � A monitor is a mini-OS with monitor procedures as system calls. as system calls. 2

  3. Monitor: Mutual Exclusion Monitor: Mutual Exclusion 1/2 1/2 � N � No more than one process can be executing in a th b ti i monitor. Thus, mutual exclusion is automatically guaranteed in a monitor. t ti ll t d i it � When a process calls a monitor procedure and enters the monitor successfully, it is the only process executing in the monitor. � When a process calls a monitor procedure and the monitor has a process running, the caller is p g blocked outside of the monitor. 3

  4. Monitor: Monitor: Monitor: Mutual Monitor: Mutual Mutual Exclusion Mutual Exclusion Exclusion 2/2 Exclusion 2/2 2/2 2/2 � If there is a process � If there is a process processes waiting processes waiting to enter monitor executing in a monitor, any monitor, any process that calls a monitor procedure monitor procedure is blocked outside of the monitor. the monitor. � When the monitor has no executing has no executing process, one process will be let in will be let in. 4

  5. Monitor: Monitor: Monitor: Syntax Monitor: Syntax Syntax Syntax � All variables are private. � All variables are private monitor Monitor-Name monitor Monitor-Name { Why? Exercise! local variable declarations; � Monitor procedures are � Monitor procedures are public ; however, some Procedure1(…) { // statements }; { // statements }; procedures can be made procedures can be made Procedure2(…) private so that they can { // statements }; only be used within a y // other procedures monitor. { � Initialization procedures z p // initialization } ( i.e ., constructors) } execute only once when the monitor is created. 5

  6. Monitor: Monitor: Monitor: A Very Monitor: A Very Very Simple ery Simple Simple Example imple Example Example xample monitor IncDec monitor IncDec process Increment process Increment { while (1) { int count; // do something // do something void Increase(void) IncDec.Increase(); { count++; } cout << IncDec.GetData(); void Decrease(void) // do something { count { count--; } ; } } } int GetData(void) { return count; } initialization initialization { { count = 0; } t 0 } } 6

  7. Condition Variables Condition Variables � Mutual exclusion is an easy task with monitors. � While a process is executing in a monitor, a � While a process is executing in a monitor, a process may have to wait until an event occurs. � Each programmer-defined event is artificially � Each programmer-defined event is artificially associated with a condition variable . � A condition variable, or a condition, has a waiting � A diti i bl diti h iti list, and two methods: signal and wait . � Note that a condition variable has no value and cannot be modified 7

  8. Condition Condition w ait w ait � Let cv be a condition variable. The use of methods signal and wait on cv are cv.signal() and cv.wait() . � Condition wait and condition signal can only be used in a monitor . � A process that executes a condition wait blocks immediately and is put into the waiting list of that condition variable. � This means that this process is waiting for the indicated event to occur. 8

  9. C C Con onditi d diti dition on signa i i gnal l l � Condition signal is used to indicate an event � Condition signal is used to indicate an event has occurred. � If there are processes waiting on the signaled � If there are processes waiting on the signaled condition variable, one of them will be released. � If there is no waiting process waiting on the � If there is no waiting process waiting on the signaled condition variable, this signal is lost as if it never occurs it never occurs. � Consider the released process (from the signaled condition) and the process that signals There condition) and the process that signals. There are two processes executing in the monitor, and mutual exclusion is violated! 9

  10. T T Tw o w o Types o T T ypes of Mon f M onit it itors ors � After a signal the released process and the � After a signal, the released process and the signaling process may be executing in the monitor. � There are two common and popular approaches to � There are two common and popular approaches to address this issue: � Hoare Type (proposed by C A R Hoare): The � Hoare Type (proposed by C.A.R.Hoare): The released process takes the monitor and the signaling process waits somewhere signaling process waits somewhere. � Mesa Type (proposed by Lampson and Redell): The released process waits somewhere and the The released process waits somewhere and the signaling process continues to use the monitor. 10

  11. What Do You Mean b What Do You Mean by “Waiting Somew here Waiting Somew here”? ”? � The signaling process (Hoare type) or the released � The signaling process (Hoare type) or the released process (Mesa type) must wait somewhere. � You could consider there is a waiting bench for � You could consider there is a waiting bench for these processes to wait. � As a result, each process that involves in a monitor � As a result each process that involves in a monitor call may be in one of the four states: � Active : The running one � Active : The running one � Entering : Those blocked by the monitor � W i i � Waiting : Those waiting on a condition variable Th i i di i i bl � Inactive : Those waiting on the waiting bench 11

  12. Monitor Monitor Monitor w ith Monitor w ith w ith Condition ith Condition Condition Variables ondition Variables Variables Variables � Processes suspended due to signal/wait are in i l/ it i the Re-entry list ( i e waiting ( i.e ., waiting bench). � When the � When the monitor is free, a process is process is released from either entry or re- y entry . 12

  13. What What What is What is is the is the the major the major major difference? ajor difference? difference? difference? Condition UntilHappen; Condition UntilHappen; With Hoare type, once a signal arrives, the signaler // Hoare Type // yp yields the monitor to the yields the monitor to the if (!event) released process and the UntilHappen.wait(); condition is not changed. g Thus, a if is sufficient. // Mesa Type With Mesa type, the released With Mesa type, the released while (!event) while (!event) process may be suspended for a UntilHappen.wait(); while before it runs. During this period, other processes may be in the monitor and change the condition It is better to check the condition. It is better to check the condition again with a while ! 13

  14. Monitor: Dinin Monitor: Dining Philoso g g Philosophers p p hers Revisited Revisited � Instead of picking up � Instead of picking up chopsticks one by one, we insist that a philosopher insist that a philosopher can eat only if he can pick up both simultaneously up both simultaneously . � Can we use a semaphore t to protect chopsticks 0 t t h ti k 0 and 1, another for 1 and 2 2, and so on? No, no, no. d ? N � Race condition!!!!! 14

  15. Monitor Definition Monitor Definition monitor Control int CanEat(int i) { { { { bool used[5]; if (!Used[i] && condition self[5]; [ ]; !Used[(i+1)%5]) [( ) ]) private: return TRUE; int CanEat(int); else return FALSE; procedure GET(int); } procedure PUT(int); procedure PUT(int); { { // initialization Function CanEat() returns () for (i=0;i<5;i++) TRUE if both chops for used[i] = FALSE; Philosopher i are available. } } 15

  16. Monitor: Monitor: GET GET() () and () () and PUT PUT() () () () Why a while rather than a if even with a Hoare monitor ? void PUT(int i) i i i void GET(int i) { { Used[i] = FALSE; Used[i] = FALSE; while (!CanEat(i)) hil (!C E t(i)) Used[(i+1)%5] self[i].wait(); = FALSE; Used[i] = TRUE; Used[i] TRUE; for (i=0;i<5;i++) Used[(i+1)%5] = TRUE; self[i].signal(); } } � In fact, PUT() only requires to signal self[(i+1)%5] and self[(i+4)%5] , the two neighbors of philosopher i . � Does it really matter? Why? How about Deadlock? 16

  17. Monitor: Monitor: Producer/Consumer Monitor: Monitor: Producer/Consumer Producer/Consumer Producer/Consumer monitor ProdCons { int count, in, out; int Buf[SIZE]; condition condition UntilFull, UntilEmpty; p y; procedure PUT(int); procedure GET(int *); { count = 0} bounded-buffer } } 17

  18. M Mon M onit itor: i it or: PUT() PUT() PUT() PUT() an and GET() d GET() GET() GET() void PUT(int X) i i void GET(int *X) i i { { if (count == SIZE) if (count == SIZE) if (count == 0) if (count == 0) UntilEmpty.wait(); UntilFull.wait(); Buf[in] = X; *X = Buf[out]; in = (in+1)%SIZE; out=(out+1)%SIZE; count++; count--; if if (count == 1) if if (count == SIZE-1) UntilFull.signal(); UntilEmpty.signal(); } } } } 18

Recommend


More recommend