




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





















Chapter 5 (Part 3)
z Special variable called a semaphore is used for signaling z If a process is waiting for a signal, it is suspended until that signal is sent z Wait and signal operations cannot be interrupted z Queue is used to hold processes waiting on the semaphore
Wait(s): s.count := s.count -1; if s.count < 0 then begin place this process in s.queue; block this process end;
Signal(s): s.count := s.count + 1; if s.count <= 0 then begin remove a process P from s.queue; place process P on ready list end;
Consider the new definition of semaphores (next slide). Compare this set of definitions with that of previous (old) definition. Note one difference: with the new definition, a semaphore can never take on a negation value. Is there any difference in the effect of the two sets of definitions when used in program. That is, could you substitute one set for the other without altering the meaning of the program?
count: (0, 1); queue: list of process end; var s: binary semaphore;
WaitB(s): if s.value = 1 then s.count := 0; else begin place this process in s.queue; block this process end;
SignalB(s): if s.queue is empty then s.value := 1; else begin remove a process P from s.queue; place process P on ready list end;
producer: repeat produce item v; b[in] := v; in := in + 1 forever;
consumer: repeat while in <= out do { nothing }; w := b[out]; out := out + 1; consume item w forever;
program producerconsumer; var n: integer; s: (binary) semaphore (:=1); delay: (binary) semaphore(:=0); … ... begin (*main program *) n:= 0; parbegin producer; consumer parend end.
Using Binary Semaphores
program producerconsumer; var n: semaphore (:=0); s: semaphore (:=1);
... begin (*main program *) parbegin producer; consumer parend end.
procedure producer procedure consumer;
begin begin
repeat repeat produce; wait(n); wait(s); wait(s); append; take; signal(s) signal(s); signal(n) consume; forever forever end ; end ;
consumer repeat while in = out do { nothing }; w := b[out]; out := (out + 1) mod n; consume item w forever;
z Software procedures z Only one process may be executing in the monitor at a time. Other processes are suspended while waiting for the monitor z Processes may be suspended while in the monitor