Docsity

Synchronization - Operating Systems and Architecture - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Synchronization - Operating Systems and Architecture - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Concurrency: Mutual

Exclusion and

Synchronization

Chapter 5 (Part 3)

Semaphores

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

Definition of Semaphores (2)

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;

Question 1 (1)

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?

Definition of Binary Semaphores (1)

type binary semaphore = record

count: (0, 1); queue: list of process end; var s: binary semaphore;

Definition of Binary Semaphores (2)

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 Function

  • Producer/Consumer Problem (2)

producer: repeat produce item v; b[in] := v; in := in + 1 forever;

Consumer Function

  • Producer/Consumer Problem (3)

consumer: repeat while in <= out do { nothing }; w := b[out]; out := out + 1; consume item w forever;

Using Binary Semaphores

  • Producer/Consumer Problem (5)

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

  • Producer/Consumer Problem (6) procedure producer procedure consumer; var m:integer;(local var) begin begin waitB(delay) repeat repeat produce; waitB(s); waitB(s); take; append; n:=n-1; n:=n+1; m:=n; if n=1 signalB(s); then signalB(delay); consume; signalB(s) if m= then waitB(delay) forever forever end ; end ; Docsity.com

Using Counting Semaphores (1)

program producerconsumer; var n: semaphore (:=0); s: semaphore (:=1);

... begin (*main program *) parbegin producer; consumer parend end.

Using Counting Semaphores (2)

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 with Circular

Buffer

consumer repeat while in = out do { nothing }; w := b[out]; out := (out + 1) mod n; consume item w forever;

Monitors

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