Presentation is loading. Please wait.

Presentation is loading. Please wait.

Concurrency: Mutual Exclusion and Synchronization

Similar presentations


Presentation on theme: "Concurrency: Mutual Exclusion and Synchronization"— Presentation transcript:

1 Concurrency: Mutual Exclusion and Synchronization
Chapter 5 (Part 3)

2 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

3 Definition of Semaphores (1)
type semaphore = record count: integer; queue: list of process end; var s: semaphore;

4 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 remove a process P from s.queue; place process P on ready list

5 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?

6 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

7 Definition of Binary Semaphores (1)
type binary semaphore = record count: (0, 1); queue: list of process end; var s: binary semaphore;

8 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; remove a process P from s.queue; place process P on ready list

9 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

10 Producer Function - Producer/Consumer Problem (2)
repeat produce item v; b[in] := v; in := in + 1 forever;

11 Consumer Function - Producer/Consumer Problem (3)
repeat while in <= out do { nothing }; w := b[out]; out := out + 1; consume item w forever;

12 Infinite Buffer for Producer/Consumer Problem - Producer/Consumer Problem (4)
b[1] b[5] out in Note: shade area indicates portion of buffer that is occupied

13 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.

14 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= signalB(s); then signalB(delay); consume; signalB(s) if m=0 then waitB(delay) forever forever end; end;

15 Question (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.

16 Using Counting Semaphores (1)
program producerconsumer; var n: semaphore (:=0); s: semaphore (:=1); ... begin (*main program *) parbegin producer; consumer parend end.

17 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;

18 Producer with Circular Buffer
repeat produce item v; while ( (in + 1) mod n = out) do { nothing }; b[in] := v; in := (in + 1) mod n forever;

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

20 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

21 Structure of a Monitor Queue of Entering Processes
Monitor Waiting Area Entrance MONITOR condition c Local Data cwait(c1) Condition Variables … Procedure 1 Condition cn …... cwait(cn) Procedure n Urget Queue csignal Initialization Code Exit

22 Message Passing Enforce mutual exclusion Exchange information
send (destination, message) receive (source, message)

23 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

24 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

25 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

26 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

27 General Message Format
Message Type Destination ID Header Source ID Message Length Control Info. Message Contents Body

28 Queuing Discipline The simplest queuing discipline is first-in-first-out (FIFO). Pipe or named pipe can be use for queuing discipline.


Download ppt "Concurrency: Mutual Exclusion and Synchronization"

Similar presentations


Ads by Google