Interprocess Sync & Comm: Producer/Consumer Implementation & Message Passing, Study notes of Operating Systems

The code and explanation of two common interprocess synchronization and communication methods: producer/consumer implementation with a shared buffer and message passing. The producer/consumer implementation uses a shared buffer and a counter to ensure proper synchronization between producing and consuming processes. Message passing uses the send and receive functions to enable communication and synchronization between processes.

Typology: Study notes

Pre 2010

Uploaded on 08/31/2009

koofers-user-ib8
koofers-user-ib8 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Interprocess Synchronization
and Communication
Steve Goddard
http://www.cse.unl.edu/~goddard/Courses/CSCE351
CSCE 351
Operating System Kernels
2
Producer/Consumer
Implementation
process
Producer
var
c
: char
begin
loop
<produce a character “c”>
while
nextIn
+
1
mod
n
=
nextOut
do
NOOP
end while
buf
[
nextIn
] :=
c
nextIn
:=
nextIn
+
1
mod
n
end loop
end
Producer
process
Consumer
var
c
: char
begin
loop
while
nextIn
=
nextOut
do
NOOP
end while
c
:=
buf
[
nextOut
]
nextOut
:=
nextOut
+1 mod
n
<consume a character “c”>
end loop
end
Consumer
globals
buf
: array [
0
..
n-1
] of char;
nextIn
,
nextOut
:
0
..
n-1
:=
0
n
-1
n
-2
nextIn nextOut
0 1 2 ...
pf3
pf4
pf5

Partial preview of the text

Download Interprocess Sync & Comm: Producer/Consumer Implementation & Message Passing and more Study notes Operating Systems in PDF only on Docsity!

1

Interprocess Synchronization

and Communication

Steve Goddard

[email protected]

http://www.cse.unl.edu/~goddard/Courses/CSCE

CSCE 351

Operating System Kernels

Producer/Consumer

Implementation

processProducer varc : char begin loop < produce a character “c” > whilenextIn+1 modn =nextOut do NOOP end while buf[nextIn] :=c nextIn :=nextIn+ 1 modn end loop end Producer

processConsumer varc : char begin loop whilenextIn =nextOut do NOOP end while c :=buf[nextOut] nextOut :=nextOut+1 modn < consume a character “c” > end loop endConsumer

globals buf : array [0..n-1] of char; n-1 nextIn,nextOut :0..n-1 := 0 n-

nextIn nextOut

3

processProducer varc : char begin loop < produce a character “c” > whilecount =n do NOOP end while buf[nextIn] :=c nextIn :=nextIn+ 1 modn count :=count + 1 end loop end Producer

processConsumer varc : char begin loop whilecount = 0 do NOOP end while c :=buf[nextOut] nextOut :=nextOut+1 mod n count :=count - 1 < consume a character “c” > end loop endConsumer

globals buf : array [0..n-1] of char; nextIn,nextOut : 0..n-1 := 0 count : integer := 0

n- n-

nextIn nextOut

Producer/Consumer Implementation

with a shared counter

The Critical Section Problem

u One implementation of the shared counter

processProducer

begin

<count := count + 1>

MOV R1, @count

ADD R1, 1

MOV @count, R

end Producer

processConsumer

begin

<count := count - 1>

MOV R2, @count

SUB R2, 1

MOV @count, R

endConsumer

7

Message Passing

u Two fundamental communication & synchronization

paradigms

» Shared memory

v Efficient, familiar

v Not always available

v Potentially insecure

» Message passing

v Awkward, less standardized

v Extensible to communication in distributed systems

v Syntax:

send(process : process_id,message : string)

receive(process : process_id, var message : string)

Message Passing Example

Ye Olde Producer/Consumer System

process producer begin loop < produce a char “c” > send(consumer, c) end loop end producer

process consumer begin loop receive(producer,mesg) < consume message “mesg” > end loop end consumer

OS Kernel

9

Issues

Synchronization semantics

u When does a send/receive operation terminate?

» Blocking

v sender waits until its message is received

v receiver waits if no message is available

» Non-blocking

v send operation “immediately” returns

v receive operation returns if no message is

available

» Variants

v send()/receive() with timeout

OS Kernel

Sender Receiver

OS Kernel

Sender Receiver

Send message to recvr.

Wait until message is

accepted.

Broadcast message to all

receivers. Wait until

message is accepted by all.

Send message to recvr.

Broadcast message to all

receivers.

Blocking Nonblocking

Explicit

Implicit

Synchronization

Naming

Semantics of Message Passing

send(recvr,mesg)