Note Taker: Don Le CSE 451 Lecture Notes for 4/28/03 --- READ Chapters 9 and 10 --- --- POINT OF LECTURE --- The point of the lecture is that a monitor is a structure built to address the specific problems with the semaphore. By combining the desirable properties of the semaphore with modularity, the monitor was created to made programming easier. --- SEMAPHORE --- 1. We can use them in lots of ways: mutual exclusion, scheduling, etc. Implication: We can easily misuse them. 2. goto of synchronization Like in the use of goto statements, there are lots of ways to screw up a semaphore. (Ex: Put P's and V's in wrong order) The problem is that there are no rules. --- BRIEF HISTORY OF PROGRAMMING --- In 70's there was excitement in the compiler world. - Could convert high level code to low level. - Transformed from having no rules, to creating a more structured foundation - goto --> structured program (procedures) - Instead of if(x==2), you could have if(x equals 2()) Not only could procedures be collected together, we could build modules: - Each module can have data that is only visible within module - Module has procedures that can access the data - Only some procedures are visible to user - Foundation of object-oriented programming MESA was one of first languages to used compiler based services to implement modules. --- MONITOR VS SEMAPHORE --- We have compiler support for isolation and separation and we have the semaphore. Is there any way to combine these pieces so that the programmer can think less? - people who invented MESA introduced synchronization into the modular programming environment. Monitor: Addresses the specific problems of semaphore: 1. PROBLEM: Uses global variable - bad, no firewalls around shared structures. SOLUTION: Get rid of explicit notion of semaphore, so no global variable. 2. PROBLEM: No logical enforced connection between P and V. SOLUTION: Can implicitly enforce connection between P and V since semaphore not there. 3. PROBLEM: No connection between semaphore and "global data" it was managing. SOLUTION: Granularity of synchronized access is in module 4. PROBLEM: Used for both mutual exclusion and scheduling. SOLUTION: Introduce separate mechanism for scheduling. --- MONITOR STRUCTURE --- A monitor is a module with locks. Has: - Collection of private data. - Some private routines. - Some public routines. - Entry into every public routine is blocked by a shared lock. - Only one thread can be active in a monitor at a time. If another one tries to enter, it is blocked. - Monitor is not sufficient for demands of scheduling. sample structure: t(){ p(l); // takes care of problems with having to remember to call P and V ... // Allows mututally exclusive access to data v(l); } --- SAMPLE PSEUDOCODE FOR MONITOR --- Monitor Christophe int x; /* a condtion variable is just a queue that waits for something to change in the monitor */ condition var c; // c is like corner where people agree to meet for pizza bool frozen=false; public int read (){ return x; } public inc (){ while(frozen) // get out of monitor and hope it unfreezes cwait(c); // just wait in corner until ready x++; } /* Resposibility for condition to be met is separate from the queue */ public int freeze (){ // frozen variable is atomic because monitor is built on sephamore, // which is built on test and set which is atomic frozen = true; return x; } public unfreeze() { frozen = false; // if there is 1 thread on the queue, wake it up // make ready one loser from c's queue csignal(c); // condition specified } cwait(c){ enqueue self on c release lock scheduler(c) // Note: the following lock can't be acquired until thread is being // executed again, because code can't be run if thread is not executing acquire lock return; What's the bug in our code? We can have multiple people on the wait queue that can never get off, because the wait queue is only dequeued on unfreeze(). Solution: Need variable n that keeps track of how many threads on wait queue while(n) signal n-- OR use broadcast(c) Broadcast means for each thread on conditional, wake it up. Different semantics for signal: MESA semantics: Signal is a hint. The condition might still not be ready when timeto execute again. while (not ready) wait(...) Hoare Semantics Signal is guarantee. Ties invariant and semantics tightly. It puts more pressure on the scheduler. It is rarely ever used. if (not ready) wait(...)