90 likes | 263 Views
Monitors and Inter-Process Communication. Basic Concept. Consists of a lock and zero or more condition variables A queue. Monitors. Look like C++ "classes" encapsulation private mutex (semaphore) variable public interfaces public interfaces use P, V for protection
E N D
Basic Concept • Consists of a lock and zero or more condition variables • A queue
Monitors • Look like C++ "classes" • encapsulation • private mutex (semaphore) variable • public interfaces • public interfaces use P, V for protection • acts like a "critical section"
Condition Variables (cv's) • Private to monitor • Access via: • wait() – suspends process • signal() – lets ONE process resume • queue() – TRUE if #waiters on cv > 0 • 2 approaches • Hoare – p1 waiting, p0 signals, p1 starts now • Mesa (Hansen) – p1 waiting, p0 signals and continues to run, p1 re-checks when p0 ends • fewer context switches
Comparison • Hoare semantics if (R is held) R.wait(); //proceed • Mesa semantics while (R held) R.wait(); // forces re-try //proceed
Mesa vs. Hoare • Mesa: signaler keeps lock, waking thread must wait on acquire • Hoare: signaler releases lock, waking thread acquires and runs
IPC • Pipes • anonymous • limited to parent-child due to file reference • child inherits pipe-end as an open file • named • pipe is opened with a name • names are system-wide • managed like files • Message passing • send() & receive() • synchronous & asynchronous
Notes on Java • The JVM • uses monitors for mutual exclusion • provides wait and notify for cooperation