Enforcing Mutual Exclusion Using Monitors
Mutual Exclusion Requirements Mutually exclusive access to critical section Progress. If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely. Bounded Waiting. A bound must exist on the # of times that other processes are allowed to enter their critical sections after a process has requested to enter its critical section and before that request is granted. Assume each process executes at a nonzero speed No assumption concerning relative speed of n processes. 2
Enforcing Mutual Exclusion funtion(){ <non- critical section> enter_critical_section(); <critical section> exit_critical_section(); <remainder section> 3
Four different approaches Hardware support Software- defined approaches Support from the OS Support from the programming language 4
Programming Language Sync. Construct Semaphores are error prone Hard to detect timing errors Obscure code (widely separated synchronization pairs) A monitor is a higher level (programming language) synchronization construct Semantics Only 1 process at a time can be active in a monitor A monitor variable can only be accessed within the monitor Signaling between processes is done through condition variables in a monitor 5
Monitor High- level synchronization construct that allows the safe sharing of an abstract data (ADT) type among concurrent processes. monitor monitor- name { shared variable declarations condition variable declarations procedure body P1 ( ) {... procedure body P2 ( ) {... procedure body Pn ( ) {... { initialization code Q1 6
Monitors To allow a process to wait within the monitor, a condition variable must be declared, as condition x, y; Condition variable can only be used with the operations wait and signal. The operation x.wait(); means that the process invoking this operation is blocked until another process invokes x.signal(); The x.signal operation resumes exactly one process blocked on x. If no process is blocked, then the signal operation has no effect. It is lost. Q2, 3 7
Schema@c View of a Monitor 8
Monitor With Condi@on Variables 9
Monitor summary Programming language construct that guarantees mutual exclusion A software module that consists of procedures, an initialization routine and local data. Local data are accessible only by the procedures of the monitor. A process can enter a monitor by invoking one of its procedures. Only one process may be executing in the monitor at any time. Other processes that would like to invoke a monitor procedure will be blocked. 10
Mutual Exclusion Mutual exclusion is guaranteed and automatic: only one process allowed in the monitor place the shared data structure in a monitor. Recall that monitor is a programming language construct (supported by some languages) Compiler provides support for mutually exclusive access to monitor procedures Q4 11
Classical Synchroniza@on Problems Bounded- Buffer Problem Readers and Writers Problem Dining- Philosophers Problem 12
Readers/Writers problem Many readers Many writers Any number of readers can be allowed to read simultaneously While a writer is writing, no others can write. No readers should read either. 13
Monitors Readers/Writers problem Monitor ReaderWriter; /* The following can only be accessed by monitor functions. */ int readers = 0; boolean writelock = false; Condition canwrite; Condition canread; 14
Reader(){ Writer(){ while(true){ beginread(); Read(); endread(); while(true){ beginwrite(); Write(); endwrite(); 15
The Reader: Hoare s model void beginread(){ if(writelock queue(canwrite)){ cwait(canread); readers++; csignal(canread); void endread(){ readers- - ; if(readers == 0){ csignal(canwrite); 16
The Writer: Hoare s model void beginwrite(){ if(readers > 0 writelock){ cwait(canwrite); writelock = true; void endwrite(){ writelock = false; if(queue(canread) csignal(canread); else if (queue(canwrite)) csignal(canwrite); 17
Hoare s model Hoare s model requires that a process signals at the end of the procedure and leaves the monitor. If signal() is not the last operation, then the process making the signal must block and instead of being placed on any of the conditions queues or the entry queue, will be placed in the urgent queue. The signal will wake the first process in the queue for that condition. If no process is waiting, the signal is lost. 18
19
Disadvantages of Hoare s model If the process that signaled is not done with the monitor, then unnecessary process switches: One to block the process One to resume it Process scheduling must be very strictly enforced. A process from the corresponding condition queue must be scheduled immediately Scheduler must ensure that no other process enters monitor before activation Reason: condition under which process was activated could change 20
Reader(){ Writer(){ while(true){ beginread(); Read(); endread(); while(true){ beginwrite(); Write(); endwrite(); 21
Reader 1: void beginread(){ if(writelock queue(canwrite)) { cwait(canread); readers++; signal(canread); Writer 1: void endwrite(){ writelock = false; if(queue(canread) else Reader 1 is blocked on canread. Writer 1 signals canread. csignal(canread); if (queue(canwrite)) csignal(canwrite); Reader 1 is un-blocked and is ready, but is not immediately scheduled. Writer 2 is scheduled sets writelock to true and is ready to write - IS WRITING when timed out. Reader 1 gets scheduled to run Will increment readers, signals to any waiting readers and then PROCEED TO READ. Writer 2: void beginwrite(){ if(readers > 0 writelock){ cwait(canwrite); writelock = true; Q5 22
Lampson and Redell modifica@on cnotify() instead of csignal(): Process will notify the first process waiting on condition. Notified process need not be scheduled immediately Notifying process need not quit or block. Code must change, such that any process that has to continue will check the condition again. Q6 23
Reader : Lampson and Redell s model void beginread(){ while(writelock queue(canwrite)){ cwait(canread); readers++; cnotify(canread); void endread(){ readers- - ; if(readers == 0){ cnotify(canwrite); 24
Writer: Lampson and Redell s model void beginwrite(){ while(readers > 0 writelock){ cwait(canwrite); writelock = true; void endwrite(){ writelock = false; if(queue(canread) cnotify(canread); else if (queue(canwrite)) cnotify(canwrite); 25
Modifica@on allows cbroadcast() cbroadcast(c) Wake all processes that are waiting on a condition. Example, if n processes are waiting on space in memory. If j bytes were available and now k more bytes become free. The memory manager does not know which processes waiting can run with k + j bytes. Using cbroadcast(), it can wake all the processes that are waiting and the process that can run with k + j bytes will run. Q7 26
Monitor solu@on Producer Consumer Checkout the JavaMonitor project from your svn repository Study and run the code Java uses wait() / notify(), synchronized methods, and locks to implement monitors 27