Monitors and Inter-Process Communication © 2004, D. J. Foreman
Basic Concept Consists of a lock (a mutex) and zero or more condition variables A queue © 2004, D. J. Foreman
Monitors Look like C++ "classes" encapsulation private mutex (semaphore) variable public interfaces public interfaces use P, V for protection acts like a "critical section" © 2004, D. J. Foreman
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 © 2004, D. J. Foreman
Comparison Hoare semantics if (R is held) R.wait(); //proceed Mesa semantics while (R held) R.wait(); // forces re-try © 2004, D. J. Foreman
Mesa vs. Hoare Hoare: signaler releases lock, waking thread acquires and runs Mesa: signaler keeps lock, waking thread must wait on acquire © 2004, D. J. Foreman
IPC Pipes Message passing anonymous named send() & receive() 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 © 2004, D. J. Foreman
Notes on Java The JVM uses monitors for mutual exclusion provides wait and notify for cooperation © 2004, D. J. Foreman
Condition Variables vs Semaphores CV's Semaphores Only in monitors Anywhere BUT monitors Wait always blocks Wait blocks if count>0 Signal releases a blocked thread or does nothing Signal releases a blocked thread or increments count Either caller or released thread continues (Hoare vs Mesa) Both continue © 2004, D. J. Foreman