Monitors: An Operating System Structuring Concept --- C.A.R. Hoare, 1974 Summary by Emre Kiciman et al. A monitor is a collection of data and procedures which are used for aquiring and releasing a resource. monitors are similar to objects The monitor variables are private to the monitor, and the monitor procedures cannot access any global variables. Only one thread could be executing a particular monitor's procedure at any time. This is the same as synchronized methods in Java. Operations needed: * wait - issued inside a resource-aquiring monitor procedure, causing a calling thread to wait. The waiting thread will release any mutual exclusion variables before it waits, to allow other threads into the monitor * signal - issued inside a resource-releasing monitor procedure to atomically wake exactly one thread waiting for the resource condition variable: to distinguish between different types of waits and signals. notation: condvar.wait; condvar.signal; condition var does not have any ``value'' as such. Note: I see the interaction between condition variables and monitors as one of the few tricky details of the paper. Shortly, wait and signal have to interact with the monitor lock that enforces mutual exclusion. It seems that the V(mutex) is only called when no process is blocked in signal or wait queues. Simple scheduling problem for a single resource: simulates a semaphore; hence monitor concept is at least as powerful as semaphores Fairness among multiple waiting threads desired Implementation of monitors using semaphores: - Each monitor procedure has a P(mutex) at its beginning and V(mutex) at the end - each monitor has a second semaphore, urgent (initialized to 0), on which signaling processes suspend themselves; - Each conditional variable in the monitor has a semaphore condsem associated with it condvar { semaphore condsem; int condcount; wait() { condcount++; if(urgentcount > 0) V(urgent) else V(mutex); P(condsem); condcount--; } signal() { urgentcount++; if(condcount > 0) {V(condsem); P(urgent)} urgentcount--; } } - basically the idea in the above implementation is that one thread passes on the ``baton'' to other threads, so that only one thread is executing any part of the monitor code - This mechanism is not just a recommended style of using semaphores. It is as different from semaphores as for loop is from jumps. - In practice the implementation can be made more efficient Bounded Buffer example Scheduled waits: include a thread priority in the conditional wait so that the thread with lowest priority can be awakened on a signal. cond.wait(p) Also cond.queue() which returns true if any thread is waiting on cond Alarm clock example, Disk head scheduler, readers and writers examplE