28
CIS 720 Message Passing

CIS 720

  • Upload
    awen

  • View
    26

  • Download
    0

Embed Size (px)

DESCRIPTION

CIS 720. Message Passing. Message passing systems. Send statements Receive statements. Process naming. P ! m ( para_list ): send message m with parameters para_list to P. send(m( para_list , P) - PowerPoint PPT Presentation

Citation preview

CIS 720

Message Passing

Message passing systems

• Send statements• Receive statements

Process naming

• P ! m(para_list): send message m with parameters para_list to P.

• send(m(para_list, P) • P ? m(para_list): receive message m from P,

and assign the received parameters to variables in para_list.

• receive(m(para_list), P)

Channel naming

• send(m(para_list), ch) or ch!m(para_list)

• receive(m(para_list), ch) or ch?m(para_list)

A: amount of synchronization and Buffering

• Minimal synchronization: 1• Non-blocking send: 1 + 6• Blocking send: 1 + 2 + 6• Reliable blocking send: 1 + 2 + 5 + 6• Synchronous send: 1 + 2 + 3 + 4 + 5 + 6

Synchronous communication

• Send statement: – A ! m(expr): the send statement blocks until the

receive statement is also enabled. • Receive statement

– B ? m(var): the receive statement blocks until the send statement is also enabled.

• When matching send and receive statements are enabled, then var is assigned the value of expr

• A pair of send and receive statements are matching if– The send statement appears in the process named

in the receive statement.– The receive statement appears in the process

named in the send statement.– var = expr is a valid assignment statement– The type of the messages match

• P1: P2:y := 2 z := 1P2 ! y P1 ? w

P2 ? y P1 ! w + z

• P1: P2: P3:y := 2 z := 1 x = 1P2 ! y P1 ? W P2 ? x

P3 ? y P3 ! w + z P1 ! x

• P1: P2:y := 2 z := 1P2 ! y P1 ! w

P2 ? y P1 ? w + z

Deadlock

Array copying program

A[0..N-1] B[0..N-1]

i = 0; P1: P2:

do do i < n P2 ! A[i]; i < n P1 ? B[i]

i = i + 1

od od

Array copying program

A[0..N-1] B[0..N-1]

i = 0; P1: P2:

do do i < n P2 ! A[i]; i < n P1 ? B[i]

i = i + 1

od od

This could deadlock because when i = n-1, P2 could check “i < n” before i is incremented.

Array copying program

A[0..N-1] B[0..N-1]

i = 0; j = 0P1: P2:

do do i < n P2 ! A[i]; j < n P1 ? B[j];

i = i + 1 j = j + 1

od od

Satisfaction condition

• one must show the following satisfaction condition for each pair of matching send and receive statements:

P1 P2

: : {P} {Q} P2!expr P1?var {U} {V} : :

• P1: P2:

y := 2 z := 1

P2 ! y P1 ? w

P2 ? y P1 ! w + z

• P1: P2:

{true} {true}y := 2 z := 1 {y = 2} {z = 1}P2 ! y P1 ? w

{w = 2} {z = 1 /\ w = 2}

P2 ? y P1 ! w + z

{y = 3} {y = 3}

{ y = 2 /\ z = 1}w = y { w = 2 /\ z = 1}

{ w = 2 /\ z = 1}y = w + z { y = 3}

Guarded Communication

• P ? x /\ bool action• Guard evaluates to true if

– bool is true– executing P ? x will not cause delay

• Guard evaluates to false if bool is false• Guard blocks if bool is true but executing P ? x

will cause delay

Mutual exclusion

• C: P1: P2:do do do

P1?req P1?rel C!req; C!req [] cs cs

P2?req P2?rel C!rel; C!rel od od od

Example….

• P1: P2: v1 = 1; v2 = ? v3 = ?; v4 = 2

if if

fi fi

Example….

• P1: P2: v1 = 1; v2 = ? v3 = ?; v4 = 2

if ifP2 ! v1 P2 ? v2 P1 ! v4 P1 ? v3

[] [] P2 ? v2 P2 ! v1 P1 ? v3 P1 ! v4 fi fi

Computing the minimum valueD: num=0; m = Max; A: B: C:

do A ? v m = min(m,v);num++ D!a; D!b D!c

[] D?a D?b D?c B ? v m = min(m,v);num++ [] C ? v m = min(m,v);num++ [] num = 3 A!m; B!m; C!m od

Computing the minimum valueA: a=initial value; num1 = 0 do B ? v a = min(a,v);num1++ [] C ? v a = min(a,v);num1++

[] B ! a skip; num1++ [] C ! a skip; num1++ [] num1 = 4 exit;

od

B: b=initial value; num2 = 0 do A ? v b = min(b,v);num2++ [] C ? v b = min(b,v);num2++

[] A ! a skip; num2++ [] C ! a skip; num2++ [] num2 = 4 exit;

od

Centralized Semaphore

• C: A: B:do do do

A ? p A ? v C!p; C!p [] cs cs

B ? p B ? v C!v; C!v od od od

Centralized Semaphore

• C: sem =1 A: B:do do do

sem =1; A ? p sem-- C!p; C!p

[] cs cs

sem=1; B ? p sem-- C!v; C!v

[] od od

sem=0; A ? v sem++ []

sem=0; B ? v sem++

od

Dining philosophers problem

P0: do hungry acquire left and right fork eat release forks sleep od

! hungry