Semaphores using Monitors
Monitor Semaphore
int
s;
condition s_c;
init(value) { s = value }
P () { s--; if (s<0) s_c.wait() }
V() { S++; if (s<=0) c_s.signal()
}
End Monitor
Monitors using Semaphores
Monitor M; // the monitor
Entry func(..)
{…
… ..
c.wait()
… ..
c.signal()
..
}
End Monitor;
semaphore mutex, next, x_sem;
int
next_count, x_count;
func()
{
P(mutex) // entry section
….
….
x_count ++ // “wait call”
if (next_count > 0)
V(next)
else V(mutex)
P(x_sem)
X_count--;
…..
…..
if(x_count > 0) { //
“signal” call
next_count++;
V(x_sem)
P(next)
next_count--;
}
… ..
… ..
if (next_count > 0) V(next) // “exit”
section
else V(mutex)
}