Top Banner
Concurrency: Mutual Exclusion and Synchronization Chapter 5 (Part 3)
28

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

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Concurrency: Mutual Exclusion and Synchronization Chapter 5 (Part 3)

Concurrency: Mutual Exclusion and Synchronization

Chapter 5 (Part 3)

Page 2: 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

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

Definition of Semaphores (1)

type semaphore = record count: integer;

queue: list of processend;

var s: semaphore;

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

Definition of Semaphores (2)Wait(s):

s.count := s.count -1;if s.count < 0

then beginplace this process in s.queue;block this process

end;

Signal(s):s.count := s.count + 1;if s.count <= 0

then beginremove a process P from s.queue;place process P on ready list

end;

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

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?

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

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):

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

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

Definition of Binary Semaphores (1)

type binary semaphore = record count: (0, 1);

queue: list of processend;

var s: binary semaphore;

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

Definition of Binary Semaphores (2)WaitB(s): if s.value = 1 then s.count := 0;

else beginplace 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;

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

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

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

Producer Function - Producer/Consumer Problem (2)

producer:

repeat

produce item v;

b[in] := v;

in := in + 1

forever;

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

Consumer Function - Producer/Consumer Problem (3)

consumer:

repeat

while in <= out do { nothing };

w := b[out];

out := out + 1;

consume item w

forever;

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

Infinite Buffer for Producer/Consumer Problem - Producer/Consumer Problem (4)

b[2] b[3] b[4]

out in

b[1] b[5] . . . .

Note: shade area indicates portion of buffer that is occupied

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

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

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

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=0

then waitB(delay)forever forever

end; end;

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

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.

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

Using Counting Semaphores (1)

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

s: semaphore (:=1);

...begin (*main program *)

parbegin producer; consumer parendend.

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

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;

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

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;

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

Consumer with Circular Buffer

consumer

repeat

while in = out do { nothing };

w := b[out];

out := (out + 1) mod n;

consume item w

forever;

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

Monitors

Software proceduresOnly 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

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

Structure of a Monitor Queue of Entering

ProcessesMonitor 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

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

Message Passing

Enforce mutual exclusionExchange information

send (destination, message)

receive (source, message)

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

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

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

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

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

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

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

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

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

General Message Format

Message Contents

Header

Body

Message TypeDestination ID

Source ID

Message LengthControl Info.

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

Queuing Discipline

The simplest queuing discipline is first-in-first-out (FIFO).

Pipe or named pipe can be use for queuing discipline.