280 likes | 298 Views
This chapter explores the use of semaphores for signaling, mutual exclusion, and synchronization in concurrent programming. It covers the definition and comparison of semaphores, binary semaphores, and counting semaphores. The producer/consumer problem and circular buffer implementation are also discussed. Additionally, the concept of monitors as a synchronization mechanism is introduced.
E N D
Concurrency: Mutual Exclusion and Synchronization Chapter 5 (Part 3)
Semaphores • Special variable called a semaphore is used for signaling • If a process is waiting for a signal, it is suspended until that signal is sent • Wait and signal operations cannot be interrupted • Queue is used to hold processes waiting on the semaphore
Definition of Semaphores (1) type semaphore = record count: integer; queue: list of process end; var s: 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?
Question 1 (2) Wait(s): if s.count > 0 then s.count := s.count - 1 else begin place this process in s.queue; block this process end; Signal(s): begin if there is at least one process suspended on semaphores then begin remove a process P from s.queue; place process P on ready list end else s.count := s.count + 1 end;
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/Consumer Problem (1) • One or more producers are generating date and placing these in a buffer • A single consumer is taking items out of the buffer one at time • Only one producer or consumer may access the buffer at any one time • Two semaphores are used • one to represent the amount of items in the buffer • one to signal that it is all right to use the buffer
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;
Infinite Buffer for Producer/Consumer Problem- Producer/Consumer Problem (4) . . . . b[2] b[3] b[4] b[1] b[5] out in Note: shade area indicates portion of buffer that is occupied
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*) beginbegin waitB(delay) repeatrepeat 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=0 then waitB(delay) foreverforever end; end;
Question 2 (1) Consider the solution to the infinite-buffer produce/consumer problem given before. Suppose we have the (common) case in which the producer and consumer are running at roughly the same speed. The scenario could be as follow: Producer: append; signal; produce; ...; append; signal; produce;.. Consumer: consume; ….; take; wait; consume; …; take; wait; … The producer always manages to append a new element to the buffer and signal during the consumption of the previous element by the consumer. The procedure is always appending to an empty buffer and the consumer is always taking the sole item in the buffer. Although the consumer never blocks on the semaphore, a large number of calls to the semaphore mechanism is made, creating considerable overhead. Construct a new program that will be more efficient under these circumstances.
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; beginbegin repeatrepeat produce; wait(n); wait(s); wait(s); append; take; signal(s) signal(s); signal(n) consume; forever forever end; end;
Producer with Circular Buffer producer: repeat produce item v; while ( (in + 1) mod n = out) do { nothing }; b[in] := v; in := (in + 1) mod n forever;
Consumer with Circular Buffer consumer repeat while in = out do { nothing }; w := b[out]; out := (out + 1) mod n; consume item w forever;
Monitors • Software procedures • Only one process may be executing in the monitor at a time. Other processes are suspended while waiting for the monitor • Processes may be suspended while in the monitor
Structure of a Monitor Queue of Entering Processes Monitor Waiting Area Entrance MONITOR condition c1 Local Data cwait(c1) Condition Variables …... Procedure 1 Condition cn …... cwait(cn) Procedure n Urget Queue csignal Initialization Code Exit
Message Passing • Enforce mutual exclusion • Exchange information send (destination, message) receive (source, message)
Message Passing - Synchronization • Sender and receiver may or may not be blocking (waiting for message) • Blocking send, blocking receive • both sender and receiver are blocked until message is delivered
Message Passing - Synchronization • Nonblocking send, blocking receive • sender continues processing such as sending messages as quickly as possible • receiver is blocked until the requested message arrives • Nonblocking send, nonblocking receive
Addressing • Direct addressing • send primitive includes a specific identifier of the destination process • receive primitive could know ahead of time which process a message is expected • receive primitive could use source parameter to return a value when the receive operation has been performed
Addressing • Indirect addressing • messages are sent to a shared data structure consisting of queues • queues are called mailboxes • one process sends a message to the mailbox and the other process picks up the message from the mailbox
General Message Format Message Type Destination ID Header Source ID Message Length Control Info. Message Contents Body
Queuing Discipline • The simplest queuing discipline is first-in-first-out (FIFO). • Pipe or named pipe can be use for queuing discipline.