You are on page 1of 363

M.

Ben-Ari
Principles of Concurrent and Distributed Programming
Second Edition

Addison-Wesley, 2006

c Mordechai Ben-Ari 2006

Computer Time


0

100

200

300

400

500

time (nanoseconds)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 1.1

Human Time


0

100

200

300

400

500

time (seconds)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 1.2

Concurrency in an Operating System

I/O

Computation
6
start I/O

6
end I/O

time

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 1.3

Interleaving as Choosing Among Processes

p3, . . .
6
cpp

p1, r1, p2, q1

q2, . . .

6
cpq
I
@
@
@ r2, . . .
6
cpr

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.1

Possible Interleavings

p1q1p2q2,
p1q1q2p2,
p1p2q1q2,
q1p1q2p2,
q1p1p2q2,
q1q2p1p2.

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.2

Algorithm 2.1: Trivial concurrent program


integer n 0
p
q
integer k1 1
integer k2 2
p1: n k1
q1: n k2

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.3

Algorithm 2.2: Trivial sequential program


integer n 0
integer k1 1
integer k2 2
p1: n k1
p2: n k2

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.4

State Diagram for a Sequential Program

'

$'

$'

p1: n k1
k1 = 1, k2 = 2
n=0
&

p2: n k2
k1 = 1, k2 = 2
n=1
%&

(end)
k1 = 1, k2 = 2
n=2
%&

s-

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.5

State Diagram for a Concurrent Program


r
' ?
p1: n k1
q1: n k2
k1 = 1, k2 = 2
n=0
&
'


(end)
q1: n k2
k1 = 1, k2 = 2
&n = 1

'

?
(end)
(end)
k1 = 1, k2 = 2
&n = 2

@
%
@
@
R
'@
p1: n k1
(end)
k1 = 1, k2 = 2
&n = 2

'

?
(end)
(end)
k1 = 1, k2 = 2
&n = 1

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

%
$

%
Slide 2.6

Scenario for a Concurrent Program

Process p

Process q

k1

k2

p1: nk1

q1: nk2

(end)

q1: nk2

(end)

(end)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.7

Multitasking System

R Operating R
R
R
e
e Program 1 e Program 2 e
System
g
g
g
g
XXX
*



XXX

XXX


XXX

XX
z 
R
e
g

Program 3

R
e
g

Program 4

CPU

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.8

Multiprocessor Computer

Global
Memory

CPU

CPU

CPU

Local
Memory

Local
Memory

Local
Memory

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.9

Inconsistency Caused by Overlapped Execution

Global memory
0000 0000 0000 0011
*


YH
H

0000 0000 0000 0001

0000 0000 0000 0010

Local memory

Local memory

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.10

Distributed Systems Architecture

Node 

Node

6@
6
I@@

@ @
@ @
@ @
R ?

?
@

Node
- Node

Node 

Node
6

?
Node

- Node

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.11

Algorithm 2.3: Atomic assignment statements


integer n 0
p
q
p1: n n + 1
q1: n n + 1

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.12

Scenario for Atomic Assignment Statements

Process p

Process q

Process p

Process q

p1: nn+1

q1: nn+1

p1: nn+1

q1: nn+1

(end)

q1: nn+1

p1: nn+1

(end)

(end)

(end)

(end)

(end)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.13

Algorithm 2.4: Assignment statements with one global reference


integer n 0
p
q
integer temp
integer temp
p1: temp n
q1: temp n
p2: n temp + 1
q2: n temp + 1

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.14

Correct Scenario for Assignment Statements

Process p

Process q

p.temp

q.temp

p1: tempn

q1: tempn

p2: ntemp+1

q1: tempn

(end)

q1: tempn

(end)

q2: ntemp+1

(end)

(end)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.15

Incorrect Scenario for Assignment Statements

Process p

Process q

p.temp

q.temp

p1: tempn

q1: tempn

p2: ntemp+1

q1: tempn

p2: ntemp+1

q2: ntemp+1

(end)

q2: ntemp+1

(end)

(end)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.16

Algorithm 2.5: Stop the loop A


integer n 0
boolean flag false
p
p1: while flag = false
p2:
n1n

q
q1:

flag true

q2:

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.17

Algorithm 2.6: Assignment statement for a register machine


integer n 0
p
q
p1: load R1,n
q1: load R1,n
p2: add R1,#1
q2: add R1,#1
p3: store R1,n
q3: store R1,n

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.18

Register Machine

Memory

Memory

Load

Memory

6Store
?
0

Registers

Registers

Registers

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.19

Scenario for a Register Machine

Process p

Process q

p.R1

q.R1

p1: load R1,n

q1: load R1,n

p2: add R1,#1

q1: load R1,n

p2: add R1,#1

q2: add R1,#1

p3: store R1,n

q2: add R1,#1

p3: store R1,n

q3: store R1,n

(end)

q3: store R1,n

(end)

(end)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.20

p1:
p2:
p3:
p4:

Algorithm 2.7: Assignment statement for a stack machine


integer n 0
p
q
push n
q1: push n
push #1
q2: push #1
add
q3: add
pop n
q4: pop n

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.21

Stack Machine

Memory

Memory

Memory

A Pop
K
A

Push A
AU

0
Stack

1
Stack

1
Stack

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.22

Algorithm 2.8: Volatile variables


integer n 0

p1:
p2:
p3:
p4:
p5:

p
integer local1, local2
n some expression
computation not using n
local1 (n + 5) 7
local2 n + 5
n local1 * local2

q
integer local
q1: local n + 6
q2:
q3:
q4:
q5:

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.23

Algorithm 2.9: Concurrent counting algorithm


integer n 0
p
q
integer temp
integer temp
p1: do 10 times
q1: do 10 times
p2:
temp n
q2:
temp n
p3:
n temp + 1
q3:
n temp + 1

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.24

Concurrent Program in Pascal


1

program count;

var n: integer := 0;

3
4

procedure p;

var temp, i : integer;

begin

for i := 1 to 10 do

begin

temp := n;

10

n := temp + 1

11

end

12

end;

13
14
15
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.25

Concurrent Program in Pascal


16

procedure q;

17

var temp, i : integer;

18

begin

19

for i := 1 to 10 do

20

begin

21

temp := n;

22

n := temp + 1

23

end

24

end;

25
26

begin

27

cobegin p; q coend;

28

writeln( The value of n is , n)

29

end.

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.26

Concurrent Program in C
1

int n = 0;

2
3

void p() {

int temp, i ;

for (i = 0; i < 10; i ++) {

temp = n;

n = temp + 1;
}

8
9

10
11
12
13
14
15
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.27

Concurrent Program in C

16

void q() {

17

int temp, i ;

18

for (i = 0; i < 10; i ++) {

19

temp = n;

20

n = temp + 1;
}

21
22

23
24

void main() {

25

cobegin { p(); q(); }

26

cout << "The value of n is " << n << "\n";

27

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.28

Concurrent Program in Ada


1

with Ada.Text_IO; use Ada.Text_IO;

procedure Count is

N: Integer := 0;

pragma Volatile(N);

5
6

task type Count_Task;

task body Count_Task is

8
9
10

Temp: Integer;
begin
for I in 1..10 loop

11

Temp := N;

12

N := Temp + 1;

13
14

end loop;
end Count_Task;

15
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.29

Concurrent Program in Ada

16
17
18
19
20

begin
declare
P, Q: Count_Task;
begin
null ;

21

end;

22

Put_Line("The value of N is " & Integer Image(N));

23

end Count;

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.30

Concurrent Program in Java


1
2

class Count extends Thread {


static volatile int n = 0;

3
4

public void run() {

int temp;

for (int i = 0; i < 10; i ++) {

temp = n;

n = temp + 1;
}

9
10

11
12
13
14
15
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.31

Concurrent Program in Java


public static void main(String[] args ) {

16
17

Count p = new Count();

18

Count q = new Count();

19

p. start ();

20

q. start ();

21

try {

22

p. join ();

23

q. join ();

24

25

catch (InterruptedException e) { }

26

System.out.println ("The value of n is " + n);


}

27
28

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.32

Concurrent Program in Promela


1

#include "for.h"

#define TIMES 10

byte

n = 0;

4
5

proctype P() {

byte temp;

for (i ,1, TIMES)

temp = n;

n = temp + 1
rof (i )

10
11

12
13
14
15
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.33

Concurrent Program in Promela

16

init {
atomic {

17
18

run P();

19

run P()

20

21

(_nr_pr == 1);

22

printf ("MSC: The value is %d\n", n)

23

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.34

Frog Puzzle


M
M
M
M
F
F
F
F


c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.35

One Step of the Frog Puzzle


M
M
M
M
F
F
F
F


c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.36

Final State of the Frog Puzzle


F
F
F
F
M
M
M
M


c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.37

(Partial) State Diagram for the Frog Puzzle




MM t FF 

XXX


H

z

HH X




j
H


9 





M t MFF 
t MMFF  

 ? 
MFM t F 

HH


HH
j






MF t MF 
MFMF t 


HH


HH
j





  ? 
MFFM t  
MF t FM 
t FMMF 












 


 ?

F t MMF 
MFF t M  
t FMFM 



 ? 
F t MFM 

 ? 
FFM t M 

 ? 
FF t MM 


c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.38

Algorithm 2.10: Incrementing and decrementing


integer n 0
p
q
integer temp
integer temp
p1: do K times
q1: do K times
p2:
temp n
q2:
temp n
p3:
n temp + 1
q3:
n temp 1

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.39

Algorithm 2.11: Zero A


boolean found
p
p1:
p2:
p3:
p4:

integer i 0
found false
while not found
ii+1
found f(i) = 0

q
q1:
q2:
q3:
q4:

integer j 1
found false
while not found
jj1
found f(j) = 0

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.40

Algorithm 2.12: Zero B


boolean found false
p
integer i 0
p1: while not found
p2:
ii+1
p3:
found f(i) = 0

q
integer j 1
q1: while not found
q2:
jj1
q3:
found f(j) = 0

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.41

Algorithm 2.13: Zero C


boolean found false
p
p1:
p2:
p3:
p4:

integer i 0
while not found
ii+1
if f(i) = 0
found true

q
q1:
q2:
q3:
q4:

integer j 1
while not found
jj1
if f(j) = 0
found true

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.42

Algorithm 2.14: Zero D


boolean found false
integer turn 1
p
p1:
p2:
p3:
p4:
p5:

integer i 0
while not found
await turn = 1
turn 2
ii+1
if f(i) = 0
found true

q
q1:
q2:
q3:
q4:
q5:

integer j 1
while not found
await turn = 2
turn 1
jj1
if f(j) = 0
found true

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.43

Algorithm 2.15: Zero E


boolean found false
integer turn 1
p
p1:
p2:
p3:
p4:
p5:
p6:

integer i 0
while not found
await turn = 1
turn 2
ii+1
if f(i) = 0
found true
turn 2

q
q1:
q2:
q3:
q4:
q5:
q6:

integer j 1
while not found
await turn = 2
turn 1
jj1
if f(j) = 0
found true
turn 1

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.44

Algorithm 2.16: Concurrent algorithm A


integer array [1..10] C ten distinct initial values
integer array [1..10] D
integer myNumber, count
p1: myNumber C[i]
p2: count number of elements of C less than myNumber
p3: D[count + 1] myNumber

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.45

while n < 2
p2:
write(n)
p1:

Algorithm 2.17: Concurrent algorithm B


integer n 0
p
q
q1: n n + 1
q2: n n + 1

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.46

Algorithm 2.18: Concurrent algorithm C


integer n 1
p
q
p1: while n < 1
q1: while n >= 0
p2:
nn+1
q2:
nn1

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.47

Algorithm 2.19: Stop the loop B


integer n 0
boolean flag false
p
p1: while flag = false
p2:
n1n
p3:

q
q1: while flag = false
q2:
if n = 0
q3:
flag true

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.48

Algorithm 2.20: Stop the loop C


integer n 0
boolean flag false
p
p1: while flag = false
p2:
n1n

q
q1: while n = 0 // Do nothing
q2: flag true

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.49

Algorithm 2.21: Welfare crook problem


integer array[0..N] a, b, c . . . (as required)
integer i 0, j 0, k 0
p1:
p2:
p3:
p4:
p5:
p6:

loop
if condition-1
ii+1
else if condition-2
jj+1
else if condition-3
kk+1
else exit loop

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 2.50

Algorithm 3.1: Critical section problem


global variables
p
q
local variables
local variables
loop forever
loop forever
non-critical section
non-critical section
preprotocol
preprotocol
critical section
critical section
postprotocol
postprotocol

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.1

Critical Section

fff
HH
H
f

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.2

Algorithm 3.2: First attempt


integer turn 1
p
p1:
p2:
p3:
p4:

loop forever
non-critical section
await turn = 1
critical section
turn 2

q
q1:
q2:
q3:
q4:

loop forever
non-critical section
await turn = 2
critical section
turn 1

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.3

Algorithm 3.3: History in a sequential algorithm


integer a 1, b 2
p1: Millions of statements
p2: a (a+b)*5
p3: . . .

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.4

Algorithm 3.4: History in a concurrent algorithm


integer a 1, b 2
p
q
p1: Millions of statements
q1: Millions of statements
p2: a (a+b)*5
q2: b (a+b)*5
p3: . . .
q3: . . .

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.5

First States of the State Diagram


- p1,q1,1


?

p2,q1,1










- p1,q2,1  


 
?


- p2,q2,1  


 
?

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.6

State Diagram for the First Attempt



r -
p1,q1,1 






? 



- p2,q1,1 H
p1,q2,1 


H
H
? 
j ? 
H
p3,q1,1 H
p2,q2,1 


H
H
? 
j ? 
H
p3,q2,1 
p4,q1,1 H


HH
? 
j ? 
H
p1,q1,2 H
p4,q2,1 


HH
? 
j ? 
H
p2,q1,2
p1,q2,2





? 
?


p2,q2,2
p1,q3,2






?
? 


p2,q3,2
p1,q4,2






?


p2,q4,2

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.7

Alternate Layout for the First Attempt (Not in the Book)


s
? ?

p1,q1,1





- p1,q2,1 




?


p1,q1,2



?


- p1,q2,2



? ?

p2,q1,1



?
?




- p2,q2,1  
- p2,q1,2




 

 ?
p1,q3,2



?


p3,q1,1



?
? ?



- p3,q2,1  
- p2,q2,2




 

 ?
p1,q4,2



?


p4,q1,1



?
? ?



- p4,q2,1  
- p2,q3,2




 

?


- p2,q4,2





c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.8

Algorithm 3.5: First attempt (abbreviated)


integer turn 1
p
q
loop forever
loop forever
p1:
await turn = 1
q1:
await turn = 2
p2:
turn 2
q2:
turn 1

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.9

State Diagram for the Abbreviated First Attempt

t
' ?

p1: await turn=1,


- q1: await turn=2,
'
turn = 2
&
%
&

&

p1: await turn=1,


'
q2: turn1,
turn = 2
&
%
%

p1: await turn=1,


$
q1: await turn=2, 
turn = 1
&
%

'

&

' ? ?

?
'

p2: turn2,
$
q1: await turn=2, 
turn = 1
&
%
&

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

%
Slide 3.10

Fragment of the State Diagram for the First Attempt

'

'

p1: NCS,
q1: NCS,
turn = 2
&

p1: NCS,
q2: await turn=2,
turn = 2
&
%

?
'

?
'

p2: await turn=1,


'
q1: NCS,
turn = 2
&
%

p2: await turn=1,


- q2: await turn=2,
'
turn = 2
&
%

&

&

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.11

p1:
p2:
p3:
p4:
p5:

Algorithm 3.6: Second attempt


boolean wantp false, wantq false
p
q
loop forever
loop forever
non-critical section
q1:
non-critical section
await wantq = false
q2:
await wantp = false
wantp true
q3:
wantq true
critical section
q4:
critical section
wantp false
q5:
wantq false

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.12

Algorithm 3.7: Second attempt (abbreviated)


boolean wantp false, wantq false
p
q
loop forever
loop forever
p1:
await wantq = false
q1:
await wantp = false
p2:
wantp true
q2:
wantq true
p3:
wantp false
q3:
wantq false

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.13

Fragment of the State Diagram for the Second Attempt

'
'
$
$
$
- p2: wantptrue,
p2: wantptrue,
p1: await !wantq,
- q2: wantqtrue,
q1: await !wantp,
q1: await !wantp,
false,false
false,false
false,false
&
%
&
%
&
%
6
r

'

'

?
$
'

p3: wantpfalse,
p3: wantpfalse,
q2: wantqtrue,
q3: wantqfalse, 
true,true
true,false
&
%
&
%

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.14

Scenario Showing that Mutual Exclusion Does Not Hold

Process p

Process q

wantp

wantq

p1: await wantq=false

q1: await wantp=false

false

false

p2: wantptrue

q1: await wantp=false

false

false

p2: wantptrue

q2: wantqtrue

false

false

p3: wantpfalse

q3: wantqtrue

true

false

p3: wantpfalse

q3: wantqfalse

true

true

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.15

p1:
p2:
p3:
p4:
p5:

Algorithm 3.8: Third attempt


boolean wantp false, wantq false
p
q
loop forever
loop forever
non-critical section
q1:
non-critical section
wantp true
q2:
wantq true
await wantq = false
q3:
await wantp = false
critical section
q4:
critical section
wantp false
q5:
wantq false

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.16

Scenario Showing Deadlock in the Third Attempt

Process p

Process q

wantp

wantq

p1: non-critical section

q1: non-critical section

false

false

p2: wantptrue

q1: non-critical section

false

false

p2: wantptrue

q2: wantqtrue

false

false

p3: await wantq=false

q2: wantqtrue

true

false

p3: await wantq=false

q3: await wantp=false

true

true

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.17

Fragment of the State Diagram Showing Deadlock

'
$
'
$
$
- p3: await !wantq,
p2: wantptrue,
p3: await !wantq,
- q3: await !wantp,
q2: wantqtrue,
q2: wantqtrue,
 

false,false
true,true
true,false
&
%
&
%
&
%
6
 
 
r
'

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.18

p1:
p2:
p3:
p4:
p5:
p6:
p7:

Algorithm 3.9: Fourth attempt


boolean wantp false, wantq false
p
q
loop forever
loop forever
non-critical section
q1:
non-critical section
wantp true
q2:
wantq true
while wantq
q3:
while wantp
wantp false
q4:
wantq false
wantp true
q5:
wantq true
critical section
q6:
critical section
wantp false
q7:
wantq false

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.19

Cycle in the State Diagram for the Fourth Attempt

r
'?
$ '
$ '
$
p3: while wantq,
p3: while wantq,
p4: wantpfalse,
- q4: wantqfalse,
- q4: wantqfalse,
q3: while wantp,
true,true
true,true
true,true
&
% &
% &
%
6

'
$ '
$ '?
$
p5: wantptrue, 
p5: wantptrue, 
p4: wantpfalse,
q5: wantqtrue,
q3: while wantp,
q5: wantqtrue,
false,false
false,true
true,false
&
% &
% &
%

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.20

p1:
p2:
p3:
p4:
p5:
p6:
p7:
p8:
p9:
p10:

Algorithm 3.10: Dekkers algorithm


boolean wantp false, wantq false
integer turn 1
p
q
loop forever
loop forever
non-critical section
q1:
non-critical section
wantp true
q2:
wantq true
while wantq
q3:
while wantp
if turn = 2
q4:
if turn = 1
wantp false
q5:
wantq false
await turn = 1
q6:
await turn = 2
wantp true
q7:
wantq true
critical section
q8:
critical section
turn 2
q9:
turn 1
wantp false
q10:
wantq false

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.21

p1:
p2:
p3:
p4:
p5:

Algorithm 3.11: Critical section problem with test-and-set


integer common 0
p
q
integer local1
integer local2
loop forever
loop forever
non-critical section
q1:
non-critical section
repeat
repeat
test-and-set(
q2:
test-and-set(
common, local1)
common, local2)
until local1 = 0
q3:
until local2 = 0
critical section
q4:
critical section
common 0
q5:
common 0

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.22

p1:
p2:
p3:
p4:
p5:

Algorithm 3.12: Critical section problem with exchange


integer common 1
p
q
integer local1 0
integer local2 0
loop forever
loop forever
non-critical section
q1:
non-critical section
repeat
repeat
exchange(common, local1)
q2:
exchange(common, local2)
until local1 = 1
q3:
until local2 = 1
critical section
q4:
critical section
exchange(common, local1)
q5:
exchange(common, local2)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.23

p1:
p2:
p3:
p4:
p5:
p6:

Algorithm 3.13: Petersons algorithm


boolean wantp false, wantq false
integer last 1
p
q
loop forever
loop forever
non-critical section
q1:
non-critical section
wantp true
q2:
wantq true
last 1
q3:
last 2
await wantq = false or
q4:
await wantp = false or
last = 2
last = 1
critical section
q5:
critical section
wantp false
q6:
wantq false

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.24

p1:
p2:

p3:
p4:
p5:

Algorithm 3.14: Manna-Pnueli algorithm


integer wantp 0, wantq 0
p
q
loop forever
loop forever
non-critical section
q1:
non-critical section
if wantq = 1
q2:
if wantp = 1
wantp 1
wantq 1
else wantp 1
else wantq 1
await wantq 6= wantp
q3:
await wantp 6= wantq
critical section
q4:
critical section
wantp 0
q5:
wantq 0

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.25

p1:
p2:
p3:
p4:
p5:
p6:
p7:
p8:
p9:
p10:
p11:

Algorithm 3.15: Doran-Thomas algorithm


boolean wantp false, wantq false
integer turn 1
p
q
loop forever
loop forever
non-critical section
q1:
non-critical section
wantp true
q2:
wantq true
if wantq
q3:
if wantp
if turn = 2
q4:
if turn = 1
wantp false
q5:
wantq false
await turn = 1
q6:
await turn = 2
wantp true
q7:
wantq true
await wantq = false
q8:
await wantp = false
critical section
q9:
critical section
wantp false
q10:
wantq false
turn 2
q11:
turn 1

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 3.26

p1:
p2:
p3:
p4:
p5:

Algorithm 4.1: Third attempt


boolean wantp false, wantq false
p
q
loop forever
loop forever
non-critical section
q1:
non-critical section
wantp true
q2:
wantq true
await wantq = false
q3:
await wantp = false
critical section
q4:
critical section
wantp false
q5:
wantq false

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 4.1

2A

A
true
false

6
i

time

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 4.2

3A

A
true
false

6
i

time

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 4.3

Duality: 2A

A
true
false

6
i

time

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 4.4

Duality: 3A

A
true
false

6
i

time

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 4.5

32A

A
true
false

6
i

time

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 4.6

23A

A
true
false

6
i

time

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 4.7

AU B

A, B
true
false

6
i

time

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 4.8

32A1 32A2

s k2

A1,A2

true

s k1
?

false

6
i

time

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 4.9

23A1 23A2

A1,A2
true
false

6
i

time

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 4.10

Overtaking: tryp ( csq ) W (csq ) W ( csq ) W (csp )

p, q
cs
try
ncs
0

10

11

12

13

time

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 4.11

p1:
p2:
p3:
p4:
p5:
p6:
p7:
p8:
p9:
p10:

Algorithm 4.2: Dekkers algorithm


boolean wantp false, wantq false
integer turn 1
p
q
loop forever
loop forever
non-critical section
q1:
non-critical section
wantp true
q2:
wantq true
while wantq
q3:
while wantp
if turn = 2
q4:
if turn = 1
wantp false
q5:
wantq false
await turn = 1
q6:
await turn = 2
wantp true
q7:
wantq true
critical section
q8:
critical section
turn 2
q9:
turn 1
wantp false
q10:
wantq false

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 4.12

Dekkers Algorithm in Promela


1

bool wantp = false, wantq = false; byte turn = 1;

active proctype p() {

do :: wantp = true;
do :: ! wantq > break;

:: else >

if :: (turn == 1)

:: (turn == 2) >

wantp = false; (turn == 1); wantp = true

fi

9
10

od;

11

printf ("MSC: p in CS\n") ;

12

turn = 2; wantp = false

13
14

od
}

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 4.13

Specifying Correctness in Promela


1

byte critical

= 0;

2
3

bool PinCS = false;

#define nostarve PinCS / LTL claim <> nostarve /

5
6

active proctype p() {


do ::

7
8

/ preprotocol /

critical ++;

10

assert(critical <= 1);

11

PinCS = true;

12

critical ;

13

/ postprotocol /
od

14
15

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 4.14

LTL Translation to Never Claims


1

never {

/ !(<>nostarve) /

accept_init :

T0_init:

if

:: (! (( nostarve ))) > goto T0_init

fi ;

8
9
10
11
12
13
14
15
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 4.15

LTL Translation to Never Claims

16

never {

17

T0_init:

/ !([]<>nostarve) /

18

if

19

:: (! (( nostarve ))) > goto accept_S4

20

:: (1) > goto T0_init

21

fi ;

22

accept_S4:

23

if

24

:: (! (( nostarve ))) > goto accept_S4

25

fi ;

26

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 4.16

p1:
p2:
p3:
p4:
p5:

Algorithm 5.1: Bakery algorithm (two processes)


integer np 0, nq 0
p
q
loop forever
loop forever
non-critical section
q1:
non-critical section
np nq + 1
q2:
nq np + 1
await nq = 0 or np nq
q3:
await np = 0 or nq < np
critical section
q4:
critical section
np 0
q5:
nq 0

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 5.1

p1:
p2:
p3:
p4:
p5:
p6:

Algorithm 5.2: Bakery algorithm (N processes)


integer array[1..n] number [0,. . . ,0]
loop forever
non-critical section
number[i] 1 + max(number)
for all other processes j
await (number[j] = 0) or (number[i] number[j])
critical section
number[i] 0

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 5.2

p1:
p2:
p3:
p4:
p5:
p6:
p7:
p8:
p9:

Algorithm 5.3: Bakery algorithm without atomic assignment


boolean array[1..n] choosing [false,. . . ,false]
integer array[1..n] number [0,. . . ,0]
loop forever
non-critical section
choosing[i] true
number[i] 1 + max(number)
choosing[i] false
for all other processes j
await choosing[j] = false
await (number[j] = 0) or (number[i] number[j])
critical section
number[i] 0

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 5.3

p1:
p2:
p3:
p4:
p5:
p6:

Algorithm 5.4: Fast algorithm for two processes (outline)


integer gate1 0, gate2 0
p
q
loop forever
loop forever
non-critical section
non-critical section
gate1 p
q1:
gate1 q
if gate2 6= 0 goto p1
q2:
if gate2 6= 0 goto q1
gate2 p
q3:
gate2 q
if gate1 6= p
q4:
if gate1 6= q
if gate2 6= p goto p1
q5:
if gate2 6= q goto q1
critical section
critical section
gate2 0
q6:
gate2 0

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 5.4

Fast Algorithm - No Contention (1)

pi

p
-

pi@

@
(a)

pi

(b)

 pi

(c)

p
-

(d)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 5.5

Fast Algorithm - No Contention (2)

(e)

pi

pi

(f)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 5.6

Fast Algorithm - Contention At Gate 2

pi

pi-

p
-

@
(a)

p


pi

(b)

pi

(c)

q
-

(d)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 5.7

Fast Algorithm - Contention At Gate 1 (1)

pi

p
-

pi@

@
(a)

pi

(b)

 pi

(c)

(d)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 5.8

Fast Algorithm - Contention At Gate 1 (2)

pi@
@
(e)

q
-

pi-

@
@
(f)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 5.9

p1:
p2:
p3:
p4:
p5:
p6:

Algorithm 5.5: Fast algorithm for two processes (outline)


integer gate1 0, gate2 0
p
q
loop forever
loop forever
non-critical section
non-critical section
gate1 p
q1:
gate1 q
if gate2 6= 0 goto p1
q2:
if gate2 6= 0 goto q1
gate2 p
q3:
gate2 q
if gate1 6= p
q4:
if gate1 6= q
if gate2 6= p goto p1
q5:
if gate2 6= q goto q1
critical section
critical section
gate2 0
q6:
gate2 0

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 5.10

p1:
p2:

p3:
p4:

p5:

p6:

Algorithm 5.6: Fast algorithm for two processes


integer gate1 0, gate2 0
boolean wantp false, wantq false
p
q
gate1 p
q1:
gate1 q
wantp true
wantq true
if gate2 6= 0
q2:
if gate2 6= 0
wantp false
wantq false
goto p1
goto q1
gate2 p
q3:
gate2 q
if gate1 6= p
q4:
if gate1 6= q
wantp false
wantq false
await wantq = false
await wantp = false
if gate2 6= p goto p1
q5:
if gate2 6= q goto q1
else wantp true
else wantq true
critical section
critical section
gate2 0
q6:
gate2 0
wantp false
wantq false

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 5.11

Algorithm 5.7: Fishers algorithm


integer gate 0

p1:
p2:
p3:
p4:
p5:

loop forever
non-critical section
loop
await gate = 0
gate i
delay
until gate = i
critical section
gate 0

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 5.12

Algorithm 5.8: Lamports one-bit algorithm


boolean array[1..n] want [false,. . . ,false]

p1:
p2:
p3:
p4:
p5:
p6:
p7:
p8:

loop forever
non-critical section
want[i] true
for all processes j < i
if want[j]
want[i] false
await not want[j]
goto p1
for all processes j > i
await not want[j]
critical section
want[i] false

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 5.13

p1:
p2:
p3:

p4:
p5:
p6:
p7:

Algorithm 5.9: Manna-Pnueli central server algorithm


integer request 0, respond 0
client process i
loop forever
non-critical section
while respond 6= i
request i
critical section
respond 0
server process
loop forever
await request 6= 0
respond request
await respond = 0
request 0

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 5.14

State Changes of a Process

inactive

ready

- running

- completed

YH
H

HH

?
HH
H blocked

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.1

p1:
p2:
p3:
p4:

Algorithm 6.1: Critical section with semaphores (two processes)


binary semaphore S (1, )
p
q
loop forever
loop forever
non-critical section
q1:
non-critical section
wait(S)
q2:
wait(S)
critical section
q3:
critical section
signal(S)
q4:
signal(S)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.2

Algorithm 6.2: Critical section with semaphores (two proc., abbrev.)


binary semaphore S (1, )
p
q
loop forever
loop forever
p1:
wait(S)
q1:
wait(S)
p2:
signal(S)
q2:
signal(S)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.3

State Diagram for the Semaphore Solution

'

$'

$'

p1: wait(S),
- p2: signal(S),
q1: wait(S), 
q1: wait(S),
(0, )
(1, )
I
@
&
%
&
@@
@@
@@'
@@
R p1: wait(S),
@

p2: signal(S),
- q1: blocked,
(0, {q})
p&
I
@
%
%
@
@
@'
$
$
q@

p1: blocked,
- q2: signal(S),
q2: signal(S),
(0, {p})
(0, )
&
%&
%

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.4

p1:
p2:
p3:
p4:

Algorithm 6.3: Critical section with semaphores (N proc.)


binary semaphore S (1, )
loop forever
non-critical section
wait(S)
critical section
signal(S)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.5

Algorithm 6.4: Critical section with semaphores (N proc., abbrev.)


binary semaphore S (1, )
loop forever
p1:
wait(S)
p2:
signal(S)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.6

Scenario for Starvation

Process p

Process q

Process r

p1: wait(S)

q1: wait(S)

r1: wait(S)

(1, )

p2: signal(S)

q1: wait(S)

r1: wait(S)

(0, )

p2: signal(S)

q1: blocked

r1: wait(S)

(0, {q})

p1: signal(S)

q1: blocked

r1: blocked

(0, {q, r})

p1: wait(S)

q1: blocked

r2: signal(S)

(0, {q})

p1: blocked

q1: blocked

r2: signal(S)

(0, {p, q})

p2: signal(S)

q1: blocked

r1: wait(S)

(0, {q})

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.7

Algorithm 6.5: Mergesort


integer array A
binary semaphore S1 (0, )
binary semaphore S2 (0, )
sort1
sort2
merge
p1: sort 1st half of A
q1: sort 2nd half of A
r1: wait(S1)
p2: signal(S1)
q2: signal(S2)
r2: wait(S2)
p3:
q3:
r3: merge halves of A

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.8

Algorithm 6.6: Producer-consumer (infinite buffer)


infinite queue of dataType buffer empty queue
semaphore notEmpty (0, )
producer
consumer
dataType d
dataType d
loop forever
loop forever
p1:
d produce
q1:
wait(notEmpty)
p2:
append(d, buffer)
q2:
d take(buffer)
p3:
signal(notEmpty)
q3:
consume(d)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.9

Partial State Diagram for Producer-Consumer with Infinite Buffer

'

$'

$'

p1: append,
p2: signal(S),
p1: append,
- q1: wait(S),
- q1: wait(S),
q1: wait(S),
(0, ), [ ]
(1, ), [x]
(0, ), [x]
&
%&
%&
%
6
'?

$ '?

$ '?

p1: append,
p1: append,
p2: signal(S),
- q1: blocked,
q2: take,
q1: blocked,
(0, {con}), [ ]
(0, ), [x]
(0, {con}), [x]
&
%&
%&
%

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.10

Algorithm 6.7: Producer-consumer (infinite buffer, abbreviated)


infinite queue of dataType buffer empty queue
semaphore notEmpty (0, )
producer
consumer
dataType d
dataType d
loop forever
loop forever
p1:
append(d, buffer)
q1:
wait(notEmpty)
p2:
signal(notEmpty)
q2:
d take(buffer)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.11

p1:
p2:
p3:
p4:

Algorithm 6.8: Producer-consumer (finite buffer, semaphores)


finite queue of dataType buffer empty queue
semaphore notEmpty (0, )
semaphore notFull (N, )
producer
consumer
dataType d
dataType d
loop forever
loop forever
d produce
q1:
wait(notEmpty)
wait(notFull)
q2:
d take(buffer)
append(d, buffer)
q3:
signal(notFull)
signal(notEmpty)
q4:
consume(d)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.12

Scenario with Busy Waiting

Process p

Process q

p1: wait(S)

q1: wait(S)

p2: signal(S)

q1: wait(S)

p2: signal(S)

q1: wait(S)

p1: wait(S)

q1: wait(S)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.13

Algorithm 6.9: Dining philosophers (outline)

p1:
p2:
p3:
p4:

loop forever
think
preprotocol
eat
postprotocol

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.14

The Dining Philosophers

.........................................................
.
.
.
.
.
.
.
.
.
.
.
..........
........
......................
.
.
........
.
.
.
.
.
.
.
.
.
.
.
.
.......
.
.
.
.
.
.
.
.
.
.
.
.
.....
.
.
.
.
.
.
.
.
.
.
.....
.
.
.
.
... phil4 ...
...
....
.
.
.
.
.
....
.
.
.
.
.
....
H

.
....
.. .
.
.
.
.
.
H

.
.
.
.
.
.
.
.
.
.
.
.............
fork4 H
j
 fork3 .....

.....
............. .....
.
.
.
.
.
.
.
.
.
.... .............................
.
.... ..
...
...
.
... ...
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.... ...
.
.
........
.
.
.
.
.
.
.
.
... ...
.
.
.
.
.....
.
....
.
.
.
phil3
. ....
..... ..... phil5 .....
.
...
.
.
.
.
.
.
...
...
...
...
.
..
..
..
.
.
.
.....
...
.
.
.
.
.
.
.
.
.
.
.
.
.....
.
.....................
....................
.
.
.
.
... Spaghetti ..
...
..
.
.
.
...
...
..
..
...
.
.
...
.
.
....
...
..
..
*

Y
H
.
.
.
.
.
.
.
.
.
...
.........

HH ..
........................
...

..
.
... fork5 .......................
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
...... fork2 ..
.....
...
......
..
....
.
.
.
.
..
...
...
.
.
.
.
.
.
.
.
...
..
..... phil2 ....
..... phil1 ....
.
....
.
6 ...
.
....
..
..
..
.
.
.
.
.... ......
.
.
.
.
.....
..... ......... ...........
.... ........
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
......
.....
.......
.
.
.
.
.
.........
......
.
.
fork1
.
...........
.
.
.
.
.
.................
.....
..........................................

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.15

p1:
p2:
p3:
p4:
p5:
p6:

Algorithm 6.10: Dining philosophers (first attempt)


semaphore array [0..4] fork [1,1,1,1,1]
loop forever
think
wait(fork[i])
wait(fork[i+1])
eat
signal(fork[i])
signal(fork[i+1])

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.16

p1:
p2:
p3:
p4:
p5:
p6:
p7:
p8:

Algorithm 6.11: Dining philosophers (second attempt)


semaphore array [0..4] fork [1,1,1,1,1]
semaphore room 4
loop forever
think
wait(room)
wait(fork[i])
wait(fork[i+1])
eat
signal(fork[i])
signal(fork[i+1])
signal(room)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.17

p1:
p2:
p3:
p4:
p5:
p6:

Algorithm 6.12: Dining philosophers (third attempt)


semaphore array [0..4] fork [1,1,1,1,1]
philosopher 4
loop forever
think
wait(fork[0])
wait(fork[4])
eat
signal(fork[0])
signal(fork[4])

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.18

p1:
p2:
p3:
p4:
p5:
p6:
p7:
p8:
p9:
p10:
p11:

Algorithm 6.13: Barzs algorithm for simulating general semaphores


binary semaphore S 1
binary semaphore gate 1
integer count k
loop forever
non-critical section
wait(gate)
wait(S)
// Simulated wait
count count 1
if count > 0 then
signal(gate)
signal(S)
critical section
wait(S)
// Simulated signal
count count + 1
if count = 1 then
signal(gate)
signal(S)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.19

p1:
p2:
p3:
p4:
p5:
p6:
p7:
p8:
p9:
p10:
p11:
p12:
p13:

Algorithm 6.14: Uddings starvation-free algorithm


semaphore gate1 1, gate2 0
integer numGate1 0, numGate2 0
wait(gate1)
numGate1 numGate1 + 1
signal(gate1)
wait(gate1)
numGate2 numGate2 + 1
numGate1 numGate1 1
// Statement is missing in the book
if numGate1 > 0
signal(gate1)
else signal(gate2)
wait(gate2)
numGate2 numGate2 1
critical section
if numGate2 > 0
signal(gate2)
else signal(gate1)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.20

Uddings Starvation-Free Algorithm

numGate1

gate1

numGate2

m
m
m

m
m

@
@
@
@
@
@

@
@
@
@

@
@
@
@
@
@

@
@
@
@

gate2

CS

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.21

Scenario for Starvation in Uddings Algorithm

Process p

Process q

gate1

gate2

nGate1

nGate2

p4: wait(g1)

q4: wait(g1)

p9: wait(g2)

q9: wait(g2)

CS

q9: wait(g2)

p12: signal(g2)

q9: wait(g2)

p1: wait(g1)

CS

p1: wait(g1)

q13: signal(g1)

p1: blocked

q13: signal(g1)

p4: wait(g1)

q1: wait(g1)

p4: wait(g1)

q4: wait(g1)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.22

Semaphores in Java
1

import java.util . concurrent. Semaphore;

class CountSem extends Thread {

static volatile int n = 0;

static Semaphore s = new Semaphore(1);

5
6

public void run() {

int temp;

for (int i = 0; i < 10; i ++) {

try {
s. acquire ();

10
11

12

catch (InterruptedException e) {}

13
14
15
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.23

Semaphores in Java

16

temp = n;

17

n = temp + 1;

18

s. release ();
}

19

20
21

public static void main(String[] args ) {

22

/ As before /

23

24
25

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.24

Semaphores in Ada
1

protected type Semaphore(Initial : Natural) is

entry Wait;

procedure Signal;

4
5
6

private
Count: Natural := Initial ;
end Semaphore;

7
8
9
10
11
12
13
14
15
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.25

Semaphores in Ada

16

protected body Semaphore is

17

entry Wait when Count > 0 is

18

begin

19
20

Count := Count 1;
end Wait;

21
22

procedure Signal is

23

begin

24

Count := Count + 1;

25

end Signal;

26

end Semaphore;

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.26

Busy-Wait Semaphores in Promela

/ Copyright (C) 2006 M. BenAri. See copyright.txt /

/ Definition of busywait semaphores /

inline wait( s ) {
atomic { s > 0 ; s }

4
5

6
7

inline signal ( s ) { s++ }

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.27

Weak Semaphores in Promela (three processes)


1

/ Copyright (C) 2006 M. BenAri. See copyright.txt /

/ Weak semaphore /

/ NPROCS the number of processes must be defined. /

/ THIS VERSION is specialized for exactly THREE processes /

5
6

/ A semaphore is a count plus an array of blocked processes /

typedef Semaphore {

byte count;

bool blocked[NPROCS];

10

};

11
12

/ Initialize

13

inline initSem(S, n) {

14
15

semaphore to n /

S.count = n
}

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.28

Weak Semaphores in Promela (three processes)


16

/ Wait operation: /

17

/ If count is zero, set blocked and wait for unblocked /

18

inline wait(S) {
atomic {

19
20

if

21

:: S.count >= 1 > S.count

22

:: else > S.blocked[_pid1] = true; !S.blocked[_pid1]

23

fi
}

24
25

26
27

/ Signal operation : /

28

/ If there are blocked processes , remove one nondeterministically

29

inline signal (S) {

30

atomic {

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.29

Weak Semaphores in Promela (three processes)

31

if

32

:: S.blocked[0] > S.blocked[0] = false

33

:: S.blocked[1] > S.blocked[1] = false

34

:: S.blocked[2] > S.blocked[2] = false

35

:: else > S.count++

36

fi
}

37
38

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.30

Weak Semaphores in Promela (N processes)


1

/ Copyright (C) 2006 M. BenAri. See copyright.txt /

/ Weak semaphore /

/ NPROCS the number of processes must be defined. /

4
5

/ A semaphore is a count plus an array of blocked processes /

typedef Semaphore {

byte count;

bool blocked[NPROCS];
byte i , choice ;

9
10

};

11
12

/ Initialize

13

inline initSem(S, n) {

14
15

semaphore to n /

S.count = n
}

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.31

Weak Semaphores in Promela (N processes)


16

/ Wait operation: /

17

/ If count is zero, set blocked and wait for unblocked /

18

inline wait(S) {
atomic {

19
20

if

21

:: S.count >= 1 > S.count

22

:: else > S.blocked[_pid1] = true; !S.blocked[_pid1]

23

fi
}

24
25

26
27

/ Signal operation : /

28

/ If there are blocked processes , remove each one and /

29

/ nondeterministically

30

/ or exit the operation . /

decide whether to replace it in the channel /

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.32

Weak Semaphores in Promela (N processes)


31
32

inline signal (S) {


atomic {

33

S.i = 0;

34

S.choice = 255;

35

do

36

:: (S.i == NPROCS) > break

37

:: (S.i < NPROCS) && !S.blocked[S.i] > S.i++

38

:: else >

39

if

40

:: (S.choice == 255) > S.choice = S.i

41

:: (S.choice != 255) > S.choice = S.i

42

:: (S.choice != 255) >

43

fi ;

44

S.i ++

45

od;

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.33

Weak Semaphores in Promela (N processes)

46

if

47

:: S.choice == 255 > S.count++

48

:: else > S.blocked[S.choice] = false

49

fi
}

50
51

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.34

Barzs Algorithm in Promela


1

#define NPROCS 3

#define K

byte gate = 1;

int count = K;

byte critical

active [ NPROCS] proctype P () {

= 0;

do ::

atomic { gate > 0; gate; }

d_step {

10

count;

11

if

12

:: count > 0 > gate++

13

:: else

14

fi

15

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.35

Barzs Algorithm in Promela

critical ++;

16

assert (critical

17
18

critical ;

19

d_step {

<= 1);

20

count++;

21

if

22

:: count == 1 > gate++

23

:: else

24

fi
}

25

od

26
27

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.36

wait(S)
p2: write("p")
p3: signal(T)
p1:

Algorithm 6.15: Semaphore algorithm A


semaphore S 1, semaphore T 0
p
q
q1: wait(T)
q2: write("q")
q3: signal(S)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.37

p
p1: write("p")
p2: signal(S1)
p3: signal(S2)

Algorithm 6.16: Semaphore algorithm B


semaphore S1 0, S2 0
q
r
q1: wait(S1)
r1: wait(S2)
q2: write("q")
r2: write("r")
q3:

r3:

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.38

p1:
p2:
p3:
p4:

Algorithm 6.17: Semaphore algorithm with a loop


semaphore S 1
boolean B false
p
q
wait(S)
q1: wait(S)
B true
q2: while not B
signal(S)
q3:
write("*")
q4: signal(S)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.39

Algorithm 6.18: Critical section problem (k out of N processes)


binary semaphore S 1, delay 0
integer count k

p1:
p2:
p3:
p4:
p5:
p6:
p7:
p8:
p9:
p10:
p11:

integer m
loop forever
non-critical section
wait(S)
count count 1
m count
signal(S)
if m 1 wait(delay)
critical section
wait(S)
count count + 1
if count 0 signal(delay)
signal(S)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.40

Circular Buffer


     
     


 
 
6
out

6
in

     
     
6
out

6
in

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.41

p1:
p2:
p3:
p4:
p5:

Algorithm 6.19: Producer-consumer (circular buffer)


dataType array [0..N] buffer
integer in, out 0
semaphore notEmpty (0, )
semaphore notFull (N, )
producer
consumer
dataType d
dataType d
loop forever
loop forever
d produce
q1:
wait(notEmpty)
wait(notFull)
q2:
d buffer[out]
buffer[in] d
q3:
out (out+1) modulo N
in (in+1) modulo N
q4:
signal(notFull)
signal(notEmpty)
q5:
consume(d)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.42

Algorithm 6.20: Simulating general semaphores


binary semaphore S 1, gate 0
integer count 0
wait
p1: wait(S)
p2: count count 1
p3: if count < 0
p4:
signal(S)
p5:
wait(gate)
p6: else signal(S)
signal
p7: wait(S)
p8: count count + 1
p9: if count 0
p10:
signal(gate)
p11: signal(S)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.43

Weak Semaphores in Promela with Channels


1

/ Weak semaphore /

/ NPROCS the number of processes must be defined. /

3
4

/ A semaphore is a count plus a channel /

/ plus a couple of local variables /

typedef Semaphore {

byte count;

chan ch = [NPROCS] of { pid };

byte temp, i ;

10

};

11
12

/ Initialize

13

inline initSem(S, n) {

14
15

semaphore to n /

S.count = n
}

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.44

Weak Semaphores in Promela with Channels


16

/ Wait operation: /

17

/ If count is zero, place your _pid in the channel /

18

19

inline wait(S) {

and block until it is removed. /


atomic {

20
21

if

22

:: S.count >= 1 > S.count;

23

:: else > S.ch ! _pid; !( S.ch ?? [ eval(_pid)])

24

fi
}

25
26

27

/ Signal operation : /

28

/ If there are blocked processes , remove each one and /

29

/ nondeterministically

30

/ or exit the operation . /

decide whether to replace it in the channel /

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.45

Weak Semaphores in Promela with Channels


31

inline signal (S) {


atomic {

32
33

S.i = len(S.ch);

34

if

35

:: S.i == 0 > S.count++ /No blocked process, increment count/

36

:: else >
do

37
38

:: S.i == 1 > S.ch ? _; break /Remove only blocked process/

39

:: else > S.i;

40

S.ch ? S.temp;

41

if :: break :: S.ch ! S.temp fi


od

42

fi

43

44
45

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.46

Algorithm 6.21: Readers and writers with semaphores


semaphore readerSem 0, writerSem 0
integer delayedReaders 0, delayedWriters 0
semaphore entry 1
integer readers 0, writers 0
SignalProcess
if writers = 0 or delayedReaders > 0
delayedReaders delayedReaders 1
signal(readerSem)
else if readers = 0 and writers = 0 and delayedWriters > 0
delayedWriters delayedWriters 1
signal(writerSem)
else signal(entry)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.47

Algorithm 6.21: Readers and writers with semaphores


StartRead
p1: wait(entry)
p2: if writers > 0
p3:
delayedReaders delayedReaders + 1
p4:
signal(entry)
p5:
wait(readerSem)
p6: readers readers + 1
p7: SignalProcess
EndRead
p8: wait(entry)
p9: readers readers 1
p10: SignalProcess

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.48

Algorithm 6.21: Readers and writers with semaphores


StartWrite
p11: wait(entry)
p12: if writers > 0 or readers > 0
p13:
delayedWriters delayedWriters + 1
p14:
signal(entry)
p15:
wait(writerSem)
p16: writers writers + 1
p17: SignalProcess
EndWrite
p18: wait(entry)
p19: writers writers 1
p20: SignalProcess

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 6.49

Algorithm 7.1: Atomicity of monitor operations


monitor CS
integer n 0
operation increment
integer temp
temp n
n temp + 1
p
p1:
CS.increment

q1:

q
CS.increment

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.1

Executing a Monitor Operation

fff
HH
H
monitor CS
f
n

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.2

Algorithm 7.2: Semaphore simulated with a monitor


monitor Sem
integer s k
condition notZero
operation wait
if s = 0
waitC(notZero)
ss1
operation signal
ss+1
signalC(notZero)
p
loop forever
non-critical section
p1:
Sem.wait
critical section
p2:
Sem.signal

q
loop forever
non-critical section
q1:
Sem.wait
critical section
q2:
Sem.signal

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.3

Condition Variable in a Monitor

fff
HH
H
monitor Sem
f
s

notZero



fff

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.4

State Diagram for the Semaphore Simulation

#
#
#
p1: Sem.wait,
p2: Sem.signal,
- p2: Sem.signal,
q1: Sem.wait, 
q1: Sem.wait,
blocked,
0, < q >
1, <>
0, <>

 "
"
!
"
!
!


6
6 






 #

# ?


9

p1: Sem.wait,
blocked,
- q2: Sem.signal
q2: Sem.signal,
0, < p >
0, <>
"
!
"
!

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.5

Algorithm 7.3: Producer-consumer (finite buffer, monitor)


monitor PC
bufferType buffer empty
condition notEmpty
condition notFull
operation append(datatype V)
if buffer is full
waitC(notFull)
append(V, buffer)
signalC(notEmpty)
operation take()
datatype W
if buffer is empty
waitC(notEmpty)
W head(buffer)
signalC(notFull)
return W

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.6

Algorithm 7.3: Producer-consumer (finite buffer, monitor) (continued)


producer
datatype D
loop forever
p1:
D produce
p2:
PC.append(D)

consumer
datatype D
loop forever
q1:
D PC.take
q2:
consume(D)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.7

The Immediate Resumption Requirement

fff
HH
H

condition 1
fff

waiting


monitor
AA



ff

f
signaling

condition 2
ff


AA



c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.8

Algorithm 7.4: Readers and writers with a monitor


monitor RW
integer readers 0
integer writers 0
condition OKtoRead, OKtoWrite
operation StartRead
if writers 6= 0 or not empty(OKtoWrite)
waitC(OKtoRead)
readers readers + 1
signalC(OKtoRead)
operation EndRead
readers readers 1
if readers = 0
signalC(OKtoWrite)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.9

Algorithm 7.4: Readers and writers with a monitor (continued)


operation StartWrite
if writers 6= 0 or readers 6= 0
waitC(OKtoWrite)
writers writers + 1
operation EndWrite
writers writers 1
if empty(OKtoRead)
then signalC(OKtoWrite)
else signalC(OKtoRead)
reader
p1: RW.StartRead
p2: read the database
p3: RW.EndRead

writer
q1: RW.StartWrite
q2: write to the database
q3: RW.EndWrite

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.10

Algorithm 7.5: Dining philosophers with a monitor


monitor ForkMonitor
integer array[0..4] fork [2, . . . , 2]
condition array[0..4] OKtoEat
operation takeForks(integer i)
if fork[i] 6= 2
waitC(OKtoEat[i])
fork[i+1] fork[i+1] 1
fork[i1] fork[i1] 1
operation releaseForks(integer i)
fork[i+1] fork[i+1] + 1
fork[i1] fork[i1] + 1
if fork[i+1] = 2
signalC(OKtoEat[i+1])
if fork[i1] = 2
signalC(OKtoEat[i1])
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.11

Algorithm 7.5: Dining philosophers with a monitor (continued)


philosopher i
p1:
p2:
p3:
p4:

loop forever
think
takeForks(i)
eat
releaseForks(i)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.12

Scenario for Starvation of Philosopher 2

phil1

phil2

phil3

take(1)

take(2)

release(1)

release(1)

f0

f1

f2

f3

f4

take(3)

take(2)

take(3)

take(2) and

release(3)

waitC(OK[2])
4

release(1)

(blocked)

release(3)

take(1)

(blocked)

release(3)

release(1)

(blocked)

release(3)

release(1)

(blocked)

take(3)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.13

Readers and Writers in C


1

monitor RW {

int readers = 0, writing = 1;

condition OKtoRead, OKtoWrite;

4
5

void StartRead() {
if (writing || ! empty(OKtoWrite))

waitc(OKtoRead);

7
8

readers = readers + 1;

signalc(OKtoRead);

10

11

void EndRead() {

12

readers = readers 1;

13

if (readers == 0)
signalc(OKtoWrite);

14
15

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.14

Readers and Writers in C


void StartWrite () {

16

if (writing || (readers != 0))

17

waitc(OKtoWrite);

18

writing = 1;

19

20
21

void EndWrite() {

22
23

writing = 0;

24

if (empty(OKtoRead))
signalc(OKtoWrite);

25

else

26

signalc(OKtoRead);

27

28
29

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.15

Algorithm 7.6: Readers and writers with a protected object


protected object RW
integer readers 0
boolean writing false
operation StartRead when not writing
readers readers + 1
operation EndRead
readers readers 1
operation StartWrite when not writing and readers = 0
writing true
operation EndWrite
writing false
reader
loop forever
p1:
RW.StartRead
p2:
read the database
p3:
RW.EndRead

writer
loop forever
q1:
RW.StartWrite
q2:
write to the database
q3:
RW.EndWrite

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.16

Context Switches in a Monitor

Process reader

Process writer

waitC(OKtoRead)

operation EndWrite

(blocked)

writing false

(blocked)

signalC(OKtoRead)

readers readers + 1

return from EndWrite

signalC(OKtoRead)

return from EndWrite

read the data

return from EndWrite

read the data

...

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.17

Context Switches in a Protected Object

Process reader

Process writer

when not writing

operation EndWrite

(blocked)

writing false

(blocked)

when not writing

(blocked)

readers readers + 1

read the data

...

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.18

Simple Readers and Writers in Ada


1

protected RW is

procedure Write(I: Integer );

function Read return Integer ;

4
5
6

private
N: Integer := 0;
end RW;

7
8
9
10
11
12
13
14
15
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.19

Simple Readers and Writers in Ada

16

protected body RW is

17

procedure Write(I: Integer ) is

18

begin

19

N := I;

20

end Write;

21

function Read return Integer is

22

begin

23
24
25

return N;
end Read;
end RW;

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.20

Readers and Writers in Ada


1

protected RW is

entry StartRead;

procedure EndRead;

entry Startwrite ;

procedure EndWrite;

private

Readers: Natural :=0;

Writing: Boolean := false ;

end RW;

10
11
12
13
14
15
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.21

Readers and Writers in Ada


16

protected body RW is

17

entry StartRead

18
19
20
21

when not Writing is


begin
Readers := Readers + 1;
end StartRead;

22
23

procedure EndRead is

24

begin

25
26

Readers := Readers 1;
end EndRead;

27
28
29
30
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.22

Readers and Writers in Ada

31
32
33
34
35

entry StartWrite
when not Writing and Readers = 0 is
begin
Writing := true;
end StartWrite;

36
37

procedure EndWrite is

38

begin

39
40
41

Writing := false ;
end EndWrite;
end RW;

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.23

Producer-Consumer in Java
1

class PCMonitor {

final int N = 5;

int Oldest = 0, Newest = 0;

volatile int Count = 0;

int Buffer [] = new int[N];

synchronized void Append(int V) {


while (Count == N)

try {

wait();

} catch (InterruptedException e) {}

10
11

Buffer [ Newest] = V;

12

Newest = (Newest + 1) % N;

13

Count = Count + 1;

14

notifyAll ();

15

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.24

Producer-Consumer in Java
synchronized int Take() {

16
17

int temp;

18

while (Count == 0)
try {

19

wait();

20

} catch (InterruptedException e) {}

21
22

temp = Buffer[Oldest];

23

Oldest = (Oldest + 1) % N;

24

Count = Count 1;

25

notifyAll ();

26

return temp;
}

27
28

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.25

A Monitor in Java With notifyAll

fff
I
HH @
H @
f
object

@

@

ff
f


waiting

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.26

Java Monitor for Readers and Writers


1

class RWMonitor {

volatile int readers = 0;

volatile boolean writing = false;

synchronized void StartRead() {


while (writing )

try {

wait();

} catch (InterruptedException e) {}

readers = readers + 1;

notifyAll ();

10
11

12

synchronized void EndRead() {

13

readers = readers 1;

14

if (readers == 0) notifyAll ();

15

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.27

Java Monitor for Readers and Writers

synchronized void StartWrite() {

16

while (writing || (readers != 0))

17

try {

18

wait();

19

} catch (InterruptedException e) {}

20

writing = true;

21
22

23

synchronized void EndWrite() {

24

writing = false;

25

notifyAll ();
}

26
27

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.28

Simulating Monitors in Promela


1

/ Copyright (C) 2006 M. BenAri. See copyright.txt /

/ Definitions for monitor /

bool lock = false;

4
5

typedef Condition {

bool gate;

byte waiting ;

9
10

inline enterMon() {

11

atomic {

12

! lock;

13

lock = true;
}

14
15

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.29

Simulating Monitors in Promela


16
17

inline leaveMon() {

18

lock = false;

19

20
21

inline waitC(C) {
atomic {

22
23

C.waiting ++;

24

lock = false;

/ Exit monitor /

25

C.gate;

/ Wait for gate /

26

lock = true;

/ IRR /

27

C.gate = false;

/ Reset gate /

28

C.waiting ;
}

29
30

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.30

Simulating Monitors in Promela


31
32

inline signalC (C) {


atomic {

33

if

34

/ Signal only if waiting /

35

:: (C.waiting > 0) >

36
37

C.gate = true;

38

! lock ;

39

lock = true; / Take lock again /

40

:: else

41

fi ;
}

42
43

/ IRR wait for released lock /

44
45

#define emptyC(C) (C.waiting == 0)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.31

Readers and Writers in Ada (1)


1

protected RW is

2
3

entry Start_Read;

procedure End_Read;

entry Start_Write;

procedure End_Write;

7
8
9

private
Waiting_To_Read : integer := 0;

10

Readers : Natural := 0;

11

Writing : Boolean := false ;

12
13

end RW;

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.32

Readers and Writers in Ada (2)


1

protected RW is

2
3

entry StartRead;

procedure EndRead;

entry Startwrite ;

procedure EndWrite;

function NumberReaders return Natural;

8
9

private

10

entry ReadGate;

11

entry WriteGate;

12

Readers: Natural :=0;

13

Writing: Boolean := false ;

14
15

end RW;

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 7.33

Algorithm 8.1: Producer-consumer (channels)


channel of integer ch
producer
consumer
integer x
integer y
loop forever
loop forever
p1:
x produce
q1:
ch y
p2:
ch x
q2:
consume(y)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 8.1

p1:
p2:
p3:
p4:
p5:
p6:
p7:
p8:

Algorithm 8.2: Conways problem


constant integer MAX 9
constant integer K 4
channel of integer inC, pipe, outC
compress
output
char c, previous 0
char c
integer n 0
integer m 0
inC previous
loop forever
loop forever
inC c
q1:
pipe c
if (c = previous) and
q2:
outC c
(n < MAX 1)
nn+1
q3:
mm+1
else
if n > 0
q4:
if m >= K
pipe intToChar(n+1)
q5:
outC newline
n0
q6:
m0
pipe previous
q7:
previous c
q8:

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 8.2

Conways Problem

inC compress

pipe
-

output

outC -

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 8.3

Process Array for Matrix Multiplication

4,2,6
Result 

Source

Source

Source

2
0
1
?

2
1
0
?

0
0
1
?

3,2,4


2
0
1
?
10,5,18
Result 

3,0,0


2
1
0
?
6,5,10


2
0
1
?
16,8,30
Result 

0,0,0


Zero

0,0,0


Zero

0,0,0


Zero

0
0
1
?
6,0,0


2
1
0
?
9,8,16


6
0
0
1
?

9,0,0


2
0
1
?

2
1
0
?

0
0
1
?

Sink

Sink

Sink

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 8.4

Computation of One Element

30

Result

 16

 0

 0

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Zero

Slide 8.5

p1:
p2:
p3:
p4:
p5:

Algorithm 8.3: Multiplier process with channels


integer FirstElement
channel of integer North, East, South, West
integer Sum, integer SecondElement
loop forever
North SecondElement
East Sum
Sum Sum + FirstElement SecondElement
South SecondElement
West Sum

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 8.6

p1:
p2:
p3:
p4:
p5:
p6:
p7:

Algorithm 8.4: Multiplier with channels and selective input


integer FirstElement
channel of integer North, East, South, West
integer Sum, integer SecondElement
loop forever
either
North SecondElement
East Sum
or
East Sum
North SecondElement
South SecondElement
Sum Sum + FirstElement SecondElement
West Sum

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 8.7

p1:
p2:
p3:
p4:
p5:
p6:

Algorithm 8.5: Dining philosophers with channels


channel of boolean forks[5]
philosopher i
fork i
boolean dummy
boolean dummy
loop forever
loop forever
think
q1:
forks[i] true
forks[i] dummy
q2:
forks[i] dummy
forks[i+1] dummy
q3:
eat
q4:
forks[i] true
q5:
forks[i+1] true
q6:

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 8.8

Conways Problem in Promela


1

#define N 9

#define K 4

chan inC, pipe, outC = [0] of { byte };

4
5

active proctype Compress() {

byte previous , c, count = 0;

inC ? previous ;

do

:: inC ? c >

10

if

11

:: (c == previous) && (count < N1) > count++

12

:: else >

13
14
15
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 8.9

Conways Problem in Promela


16

if

17

:: count > 0 >

18

pipe ! count+1;

19

count = 0

20

:: else

21

fi ;

22

pipe ! previous ;

23

previous = c;
fi

24

od

25
26

27
28
29
30
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 8.10

Conways Problem in Promela


31

active proctype Output() {

32

byte c, count = 0;

33

do

34

:: pipe ? c;

35

outC ! c;

36

count++;

37

if

38

:: count >= K >

39

outC ! \n;

40

count = 0

41

:: else

42

fi
od

43
44

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 8.11

Multiplier Process in Promela

proctype Multiplier (byte Coeff;


chan North; chan East; chan South; chan West) {

2
3

byte Sum, X;

for (i ,0, SIZE1)


if :: North ? X > East ? Sum;

:: East ? Sum > North ? X;

6
7

fi ;

South ! X;

Sum = Sum + XCoeff;


West ! Sum;

10

rof (i )

11
12

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 8.12

Algorithm 8.6: Rendezvous


client
integer parm, result
loop forever
p1:
parm . . .
p2:
server.service(parm, result)
p3:
use(result)

server
integer p, r
loop forever
q1:
q2:
q3:

accept service(p, r)
r do the service(p)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 8.13

Timing Diagram for a Rendezvous

t1

t2

t3

calling
6
parameters

accepting

results

time

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 8.14

Bounded Buffer in Ada


1

task body Buffer is

B: Buffer_Array;

In_Ptr, Out_Ptr, Count: Index := 0;

4
5
6
7

begin
loop
select
when Count < IndexLast =>

accept Append(I: in Integer ) do

B(In_Ptr) := I;

10

end Append;

11

Count := Count + 1; In_Ptr := In_Ptr + 1;

12
13

or

14
15
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 8.15

Bounded Buffer in Ada

when Count > 0 =>

16

accept Take(I: out Integer ) do

17

I := B(Out_Ptr);

18

end Take;

19

Count := Count 1; Out_Ptr := Out_Ptr + 1;

20
21
22
23
24
25

or
terminate;
end select;
end loop;
end Buffer;

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 8.16

Remote Procedure Call

Remote
interface

Client program

Server program

Remote
interface

6
?
Sending stub

Receiving stub
6

?
Communications

- Communications

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 8.17

Pipeline Sort

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 8.18

Hoares Game

@
@
@
y

@
@
@

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 8.19

A Space







(a,10,20)

(a,30)



(a,false,40)




(a,true,50)






c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 9.1

Algorithm 9.1: Critical section problem in Linda

p1:
p2:
p3:
p4:

loop forever
non-critical section
removenote(s)
critical section
postnote(s)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 9.2

Algorithm 9.2: Client-server algorithm in Linda


client
constant integer me . . .
serviceType service
dataType result, parm
p1: service // Service requested
p2: postnote(S, me, service, parm)
p3: removenote(R, me, result)

server
integer client
serviceType s
dataType r, p
q1: removenote(S, client, s, p)
q2: r do (s, p)
q3: postnote(R, client, r)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 9.3

Algorithm 9.3: Specific service


client
constant integer me . . .
serviceType service
dataType result, parm
p1: service // Service requested
p2: postnote(S, me, service, parm)
p3:
p4:

q1:
q2:
q3:

removenote(R, me, result)

q4:

server
integer client
serviceType s
dataType r, p
s // Service provided
removenote(S, client, s=, p)
r do (s, p)
postnote(R, client, r)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 9.4

Algorithm 9.4: Buffering in a space


producer
integer count 0
integer v
loop forever
p1:
v produce
p2:
postnote(B, count, v)
p3:
count count + 1

consumer
integer count 0
integer v
loop forever
q1:
removenote(B, count=, v)
q2:
consume(v)
q3:
count count + 1

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 9.5

p1:
p2:
p3:
p4:
p5:

Algorithm 9.5: Multiplier process with channels in Linda


parameters: integer FirstElement
parameters: integer North, East, South, West
integer Sum, integer SecondElement
integer Sum, integer SecondElement
loop forever
removenote(E, North=, SecondElement)
removenote(S, East=, Sum)
Sum Sum + FirstElement SecondElement
postnote(E, South, SecondElement)
postnote(S, West, Sum)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 9.6

p1:
p2:
p3:
p4:
p5:
p6:
p7:

Algorithm 9.6: Matrix multiplication in Linda


constant integer n . . .
master
worker
integer i, j, result
integer r, c, result
integer r, c
integer array[1..n] vec1, vec2
loop forever
for i from 1 to n
q1:
removenote(T, r, c)
for j from 1 to n
q2:
readnote(A, r=, vec1)
postnote(T, i, j)
q3:
readnote(B, c=, vec2)
for i from 1 to n
q4:
result vec1 vec2
for j from 1 to n
q5:
postnote(R, r, c, result)
removenote(R, r, c, result)
q6:
print r, c, result
q7:

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 9.7

p1:
p2:
p3:
p4:
p5:
p6:
p7:

Algorithm 9.7: Matrix multiplication in Linda with granularity


constant integer n . . .
constant integer chunk . . .
master
worker
integer i, j, result
integer r, c, k, result
integer r, c
integer array[1..n] vec1, vec2
loop forever
for i from 1 to n
q1:
removenote(T, r, k)
for j from 1 to n step by chunk
q2:
readnote(A, r=, vec1)
postnote(T, i, j)
q3:
for c from k to k+chunk-1
for i from 1 to n
q4:
readnote(B, c=, vec2)
for j from 1 to n
q5:
result vec1 vec2
removenote(R, r, c, result)
q6:
postnote(R, r, c, result)
print r, c, result
q7:

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 9.8

Definition of Notes in Java


1

public class Note {

public String id ;

public Object[] p;

4
5

// Constructor for an array of objects

public Note (String id , Object[] p) {

this . id = id;

if (p != null) this . p = p.clone();

10
11

// Constructor for a single integer

12

public Note (String id , int p1) {


this (id , new Object[]{new Integer(p1)});

13
14

15
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 9.9

Definition of Notes in Java

16

// Accessor for a single integer value

17

public int get(int i ) {


return (( Integer )p[i ]). intValue ();

18

19
20

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 9.10

Matrix Multiplication in Java


1

private class Worker extends Thread {


public void run() {

2
3

Note task = new Note("task");

while (true) {

Note t = space.removenote(task);

int row = t.get(0), col = t.get(1);

Note r = space.readnote(match("a", row));

Note c = space.readnote(match("b", col));

int ip = 0;
for (int i = 1; i <= SIZE; i++)

10

ip = ip + r.get(i )c. get(i );

11

space. postnote(new Note("result", row, col , ip ));

12

13

14
15

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 9.11

Matrix Multiplication in Promela

chan space = [25] of { byte, short, short, short, short };

2
3

active[ WORKERS] proctype Worker() {

short row, col , ip , r1, r2, r3, c1, c2, c3;

do

:: space ?? t ,

row, col , _, _;

space ?? <a, eval(row), r1, r2, r3>;

space ?? <b, eval(col ), c1, c2, c3>;

ip = r1c1 + r2c2 + r3c3;


space !

10

od;

11
12

r , row, col , ip , 0;

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 9.12

p1:
p2:
p3:
p4:
p5:
p6:
p7:
p8:
p9:

Algorithm 9.8: Matrix multiplication in Linda (exercise)


constant integer n . . .
master
worker
integer i, j, result
integer i, r, c, result
integer r, c
integer array[1..n] vec1, vec2
loop forever
postnote(T, 0)
q1:
removenote(T i)
q2:
if i < (n n) 1
q3:
postnote(T, i+1)
q4:
r (i / n) + 1
q5:
c (i modulo n) + 1
for i from 1 to n
q6:
readnote(A, r=, vec1)
for j from 1 to n
q7:
readnote(B, c=, vec2)
removenote(R, r, c, result)
q8:
result vec1 vec2
print r, c, result
q9:
postnote(R, r, c, result)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 9.13

Sending and Receiving Messages

node 5
integer k 20
send(request, 3, k, 30)

node 3
integer m, n
receive(request, m, n)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.1

Sending a Message and Expecting a Reply

node 5
send(request, 3, myID)

node 3
integer source
receive(request, source)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.2

Algorithm 10.1: Ricart-Agrawala algorithm (outline)


integer myNum 0
set of node IDs deferred empty set
p1:
p2:
p3:
p4:
p5:
p6:
p7:
p8:
p9:

p10:
p11:
p12:
p13:

main
non-critical section
myNum chooseNumber
for all other nodes N
send(request, N, myID, myNum)
await replys from all other nodes
critical section
for all nodes N in deferred
remove N from deferred
send(reply, N, myID)
receive
integer source, reqNum
receive(request, source, reqNum)
if reqNum < myNum
send(reply,source,myID)
else add source to deferred

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.3

RA Algorithm (1)

10

Aaron


req

req

Becky

 req
req

@@ @
I
req
@req
@
@
R
@ @
Chloe

15

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.4

RA Algorithm (2)

Aaron

10

Chloe
@
I
@ reply
@
@

reply

Becky

reply


Chloe

15

Aaron, Chloe

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.5

Virtual Queue in the RA Algorithm

Becky

Aaron

Chloe

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.6

RA Algorithm (3)

Aaron

10

Chloe
reply
Becky

reply
-

Chloe

15

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.7

RA Algorithm (4)

Aaron

10

@ reply
@
R
@

Becky

Chloe

15

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.8

Equal Ticket Numbers

Becky

req
req

Becky
Aaron

Aaron

Aaron

Becky

Note: This figure is not in the book.

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.9

Choosing Ticket Numbers (1)

Becky

req
req

Becky

Aaron

10

Aaron

10

Aaron

10


reply

Aaron

Becky

5
reply

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.10

Choosing Ticket Numbers (2)

Becky

8
req

Becky

Aaron

10

Aaron

10


reply

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.11

Quiescent Nodes

Becky

5
req

Becky

Aaron

Aaron

Becky

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.12

Algorithm 10.2: Ricart-Agrawala algorithm


integer myNum 0
set of node IDs deferred empty set
integer highestNum 0
boolean requestCS false

p1:
p2:
p3:
p4:
p5:
p6:
p7:
p8:
p9:
p10:
p11:

Main
loop forever
non-critical section
requestCS true
myNum highestNum + 1
for all other nodes N
send(request, N, myID, myNum)
await replys from all other nodes
critical section
requestCS false
for all nodes N in deferred
remove N from deferred
send(reply, N, myID)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.13

Algorithm 10.2: Ricart-Agrawala algorithm (continued)

p1:
p2:
p3:
p4:
p5:

Receive
integer source, requestedNum
loop forever
receive(request, source, requestedNum)
highestNum max(highestNum, requestedNum)
if not requestCS or requestedNum myNum
send(reply, source, myID)
else add source to deferred

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.14

Correct of the RA Algorithm (Case 1)

choose

- send
request
- receive
request

- reply

- choose

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.15

Correct of the RA Algorithm (Case 2)

i main

choose

- send
request

i receive

j main
j receive

choose

- receive
request

- reply

- receive
request

- reply

- send
request

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.16

Channels in RA Algorithm in Promela

node i H
HH ch[id]
H
- node id




node j 

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.17

RA Algorithm in Promela Main Process


1
2
3

proctype Main( byte myID ) {


do ::
atomic {

requestCS[myID] = true ;

myNum[myID] = highestNum[myID] + 1 ;

7
8
9
10

for (J,0, NPROCS1)


if
:: J != myID >
ch[J] ! request , myID, myNum[myID];

11
12

:: else

13

fi

14

rof (J);

15
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.18

RA Algorithm in Promela Main Process


16

for (K,0,NPROCS2)

17

ch[myID] ?? reply , _ , _;

18
19

rof (K);

20

critical_section ();

21

requestCS[myID] = false;

22
23

byte N;

24

do

25

:: empty(deferred[myID]) > break;

26

:: deferred [ myID] ? N > ch[N] ! reply, 0, 0


od

27

od

28
29

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.19

RA Algorithm in Promela Receive Process


1

proctype Receive( byte myID ) {

byte reqNum, source;

do ::

ch[myID] ?? request, source, reqNum;

highestNum[myID] =

(( reqNum > highestNum[myID]) >


reqNum : highestNum[myID]);

7
8

atomic {

if

10

:: requestCS[myID] &&

11

( (myNum[myID] < reqNum) ||

12

( (myNum[myID] == reqNum) &&

13
14
15

(myID < source)


) ) >
deferred [ myID] ! source

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.20

RA Algorithm in Promela Receive Process

:: else >

16

ch[source] ! reply , 0, 0

17

fi

18

19

od

20
21

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.21

Algorithm 10.3: Ricart-Agrawala token-passing algorithm


boolean haveToken true in node 0, false in others
integer array[NODES] requested [0,. . . ,0]
integer array[NODES] granted [0,. . . ,0]
integer myNum 0
boolean inCS false
sendToken
if exists N such that requested[N] > granted[N]
for some such N
send(token, N, granted)
haveToken false

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.22

Algorithm 10.3: Ricart-Agrawala token-passing algorithm (continued)

p1:
p2:
p3:
p4:
p5:
p6:
p7:
p8:
p9:
p10:
p11:
p12:

Main
loop forever
non-critical section
if not haveToken
myNum myNum + 1
for all other nodes N
send(request, N, myID, myNum)
receive(token, granted)
haveToken true
inCS true
critical section
granted[myID] myNum
inCS false
sendToken

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.23

Algorithm 10.3: Ricart-Agrawala token-passing algorithm (continued)

p13:
p14:
p15:
p16:

Receive
integer source, reqNum
loop forever
receive(request, source, reqNum)
requested[source] max(requested[source], reqNum)
if haveToken and not inCS
sendToken

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.24

Data Structures for RA Token-Passing Algorithm

requested

granted

Aaron

Becky

Chloe

Danielle

Evan

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.25

Distributed System for Neilsen-Mizuno Algorithm

- Danielle
6


I@
@
@
@
R@
@


?
Aaron


Chloe

?
Evan
6


I
@@
@
@
@
?
R
@
- Becky

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.26

Spanning Tree in Neilsen-Mizuno Algorithm

Danielle 

Evan

@
R
@
Chloe

Aaron

@
I
@
-

Becky

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.27

Neilsen-Mizuno Algorithm (1)

Becky

Aaron

Chloe

Danielle 

Evan

Aaron

Becky

Chloe

Danielle 

Evan

?
Aaron

Becky 

Chloe

Danielle 

Evan

?
Aaron

Becky

Chloe

- Danielle

Evan

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.28

Neilsen-Mizuno Algorithm (2)

?
Aaron

- Becky

Chloe

- Danielle

Evan
6

Aaron

- Becky

Chloe

- Danielle

Evan
6

Aaron

- Becky

Chloe

- Danielle

Evan

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.29

Algorithm 10.4: Neilsen-Mizuno token-passing algorithm


integer parent (initialized to form a tree)
integer deferred 0
boolean holding true in the root, false in others

p1:
p2:
p3:
p4:
p5:
p6:
p7:
p8:
p9:
p10:
p11:

Main
loop forever
non-critical section
if not holding
send(request, parent, myID, myID)
parent 0
receive(token)
holding false
critical section
if deferred 6= 0
send(token, deferred)
deferred 0
else holding true

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.30

Algorithm 10.4: Neilsen-Mizuno token-passing algorithm (continued)

p12:
p13:
p14:
p15:
p16:
p17:
p18:
p19:

Receive
integer source, originator
loop forever
receive(request, source, originator)
if parent = 0
if holding
send(token, originator)
holding false
else deferred originator
else send(request, parent, myID, originator)
parent source

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 10.31

Distributed System with an Environment Node

node2

node1
HH


*



HH
H
j
H

?
node3

HH






HH
j
H
node4




c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 11.1

Back Edges



 *







node1
YH
H
HH
H H
HH
HH
j
HH
H

node2
6

Y
H
HHH
HHH
6
HH
HH
H
j
H

node4
*


?
? 



node3 

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 11.2

Algorithm 11.1: Dijkstra-Scholten algorithm (preliminary)


integer array[incoming] inDeficit [0,. . . ,0]
integer inDeficit 0, integer outDeficit 0
send message
p1: send(message, destination, myID)
p2: increment outDeficit
receive message
p3: receive(message, source)
p4: increment inDeficit[source] and inDeficit
send signal
p5: when inDeficit > 1 or
(inDeficit = 1 and isTerminated and outDeficit = 0)
p6:
E some edge E with inDeficit[E] 6= 0
p7:
send(signal, E, myID)
p8:
decrement inDeficit[E] and inDeficit
receive signal
p9: receive(signal, _)
p10: decrement outDeficit
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 11.3

p1:
p2:
p3:
p4:
p5:

p6:
p7:

Algorithm 11.2: Dijkstra-Scholten algorithm (env., preliminary)


integer outDeficit 0
computation
for all outgoing edges E
send(message, E, myID)
increment outDeficit
await outDeficit = 0
announce system termination
receive signal
receive(signal, source)
decrement outDeficit

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 11.4

The Preliminary DS Algorithm is Unsafe

node2

*

 e2


node1
HH

HHe3
H
j
H

?
node3

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 11.5

Spanning Tree

node2

node1


*



HH

HH
H
j
H
node4

?
node3

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 11.6

p1:
p2:
p3:

p4:
p5:
p6:
p7:

Algorithm 11.3: Dijkstra-Scholten algorithm


integer array[incoming] inDeficit [0,. . . ,0]
integer inDeficit 0
integer outDeficit 0
integer parent 1
send message
when parent 6= 1
// Only active nodes send messages
send(message, destination, myID)
increment outDeficit
receive message
receive(message,source)
if parent = 1
parent source
increment inDeficit[source] and inDeficit

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 11.7

Algorithm 11.3: Dijkstra-Scholten algorithm (continued)


send signal
p8: when inDeficit > 1
p9:
E some edge E for which
(inDeficit[E] > 1) or (inDeficit[E] = 1 and E 6= parent)
p10:
send(signal, E, myID)
p11:
decrement inDeficit[E] and inDeficit
p12: or when inDeficit = 1 and isTerminated and outDeficit = 0
p13:
send(signal, parent, myID)
p14:
inDeficit[parent] 0
p15:
inDeficit 0
p16:
parent 1
receive signal
p17: receive(signal, _)
p18: decrement outDeficit

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 11.8

Partial Scenario for DS Algorithm

Action

node1

node2

node3

node4

12

(-1,[ ],0)

(-1,[0,0],0)

(-1,[0,0,0],0)

(-1,[0],0)

24

(-1,[ ],1)

(1,[1,0],0)

(-1,[0,0,0],0)

(-1,[0],0)

23

(-1,[ ],1)

(1,[1,0],1)

(-1,[0,0,0],0)

(2,[1],0)

24

(-1,[ ],1)

(1,[1,0],2)

(2,[0,1,0],0)

(2,[1],0)

13

(-1,[ ],1)

(1,[1,0],3)

(2,[0,1,0],0)

(2,[2],0)

32

(-1,[ ],2)

(1,[1,0],3)

(2,[1,1,0],0)

(2,[2],0)

43

(-1,[ ],2)

(1,[1,1],3)

(2,[1,1,0],1)

(2,[2],0)

(-1,[ ],2)

(1,[1,1],3)

(2,[1,1,1],1)

(2,[2],1)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 11.9

Data Structures After Completion of Partial Scenario

node2 (3) H
1
*

HH


6
HH 2
1

j
H

node1 (2)
node4 (1)

HH

1

HH
?


H

j node3 (1) 
H
1
1

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 11.10

p1:
p2:
p3:
p4:
p5:

p6:
p7:

Algorithm 11.4: Credit-recovery algorithm (environment node)


float weight 1.0
computation
for all outgoing edges E
weight weight / 2.0
send(message, E, weight)
await weight = 1.0
announce system termination
receive signal
receive(signal, w)
weight weight + w

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 11.11

Algorithm 11.5: Credit-recovery algorithm (non-environment node)


constant integer parent 0 // Environment node
boolean active false
float weight 0.0
send message
p1: if active
// Only active nodes send messages
p2:
weight weight / 2.0
p3:
send(message, destination, myID, weight)
receive message
p4: receive(message, source, w)
p5: active true
p6: weight weight + w
send signal
p7: when terminated
p8:
send(signal, parent, weight)
p9:
weight 0.0
p10:
active false

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 11.12

Messages on a Channel

node1

m14, m13, m12, m11, m10


-

node2

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 11.13

Sending a Marker

node1

m14, m13, m12, marker, m11, m10


-

node2

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 11.14

p1:
p2:

p3:
p4:

Algorithm 11.6: Chandy-Lamport algorithm for global snapshots


integer array[outgoing] lastSent [0, . . . , 0]
integer array[incoming] lastReceived [0, . . . , 0]
integer array[outgoing] stateAtRecord [1, . . . , 1]
integer array[incoming] messageAtRecord [1, . . . , 1]
integer array[incoming] messageAtMarker [1, . . . , 1]
send message
send(message, destination, myID)
lastSent[destination] message
receive message
receive(message,source)
lastReceived[source] message

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 11.15

Algorithm 11.6: Chandy-Lamport algorithm for global snapshots (continued)


receive marker
p6: receive(marker, source)
p7: messageAtMarker[source] lastReceived[source]
p8: if stateAtRecord = [1,. . . ,1]
// Not yet recorded
p9:
stateAtRecord lastSent
p10:
messageAtRecord lastReceived
p11:
for all outgoing edges E
p12:
send(marker, E, myID)
record state
p13: await markers received on all incoming edges
p14: recordState

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 11.16

Messages and Markers for a Scenario

node2
M,3,2,1

node1

M,3,2,1

@ M,3,2,1
@
R
@
- node3

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 11.17

Scenario for CL Algorithm (1)

Action

node1
ls

lr

st

[3,3]

node2
rc

mk

ls

lr

[3]

[3]

1M2

[3,3]

[3,3]

[3]

[3]

1M3

[3,3]

[3,3]

[3]

[3]

21M

[3,3]

[3,3]

[3]

[3]

2M3

[3,3]

[3,3]

[3]

[3]

st

rc

mk

[3]

[3]

[3]

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 11.18

Scenario for CL Algorithm (2)

Action

node3
ls

lr

st

rc

mk

32
32

[0,1]

32

[0,2]

32M

[0,3]

31

[0,3]

[0,3]

[0,3]

31

[1,3]

[0,3]

[0,3]

31

[2,3]

[0,3]

[0,3]

31M

[3,3]

[0,3]

[0,3]

[3,3]

[0,3]

[3,3]

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 11.19

Architecture for a Reliable System

Temperature



Q
JQ





J Q

s
J
Q

J 
3
Pressure


 J



^
J






CPU

HH

CPU

HH
j

- Comparator


*



CPU

Throttle





c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.1

p1:
p2:
p3:
p4:
p5:
p6:

Algorithm 12.1: Consensus - one-round algorithm


planType finalPlan
planType array[generals] plan
plan[myID] chooseAttackOrRetreat
for all other generals G
send(G, myID, plan[myID])
for all other generals G
receive(G, plan[G])
finalPlan majority(plan)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.2

Messages Sent in a One-Round Algorithm

Basil A
I
@



Zoe A


A

@ @
A@ @ R
@ @
R @
@
R
- Leo R
A

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.3

Data Structures in a One-Round Algorithm

Leo
general

Zoe
plan

general

plans

Basil

Basil

Leo

Leo

Zoe

Zoe

majority

majority

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.4

Algorithm 12.2: Consensus - Byzantine Generals algorithm


planType finalPlan
planType array[generals] plan, majorityPlan
planType array[generals, generals] reportedPlan
p1: plan[myID] chooseAttackOrRetreat
p2: for all other generals G
// First round
p3:
send(G, myID, plan[myID])
p4: for all other generals G
p5:
receive(G, plan[G])
p6: for all other generals G
// Second round
p7:
for all other generals G except G
p8:
send(G, myID, G, plan[G])
p9: for all other generals G
p10:
for all other generals G except G
p11:
receive(G, G, reportedPlan[G, G])
p12: for all other generals G
// First vote
p13:
majorityPlan[G] majority(plan[G] reportedPlan[*, G])
p14: majorityPlan[myID] plan[myID]
// Second vote
p15: finalPlan majority(majorityPlan)
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.5

Data Structure for Crash Failure - First Scenario (Leo)

Leo
general

plan

reported by
Basil

Basil

Leo

Zoe

majority

majority

Zoe

A
R

A
A

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.6

Data Structure for Crash Failure - First Scenario (Zoe)

Zoe
general

plan

reported by
Basil

Basil

Leo

Zoe

majority

Leo
A

majority

A
R
A
A

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.7

Data Structure for Crash Failure - Second Scenario (Leo)

Leo
general

plan

reported by
Basil

Basil

Leo

Zoe

majority

majority

Zoe
A

A
R

A
A

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.8

Data Structure for Crash Failure - Second Scenario (Zoe)

Zoe
general

plan

reported by
Basil

Basil

Leo

Zoe

majority

Leo
A

majority

A
R
A
A

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.9

Knowledge Tree about Basil for Crash Failure - First Scenario

Basil A




HH

HH

Zoe A

Leo A

Leo A

Zoe A

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.10

Knowledge Tree about Basil for Crash Failure - Second Scenario

Basil X
HH
H

H
Leo X

Zoe X

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.11

Knowledge Tree about Leo for Crash Failure

Leo X



Zoe X

HH

HH
Basil X

Basil X

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.12

Messages Sent for Byzantine Failure with Three Generals

Basil
I
@
R


Zoe A


A

@ @
A@ @ R
@ @
R @
@
R
- Leo R
A

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.13

Data Stuctures for Leo and Zoe After First Round

Leo
general

Zoe
plans

general

plans

Basil

Basil

Leo

Leo

Zoe

Zoe

majority

majority

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.14

Data Stuctures for Leo After Second Round

Leo
general

plans

reported by
Basil

Basil

Leo

Zoe

majority

majority

Zoe
A

A
R

R
R

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.15

Data Stuctures for Zoe After Second Round

Zoe
general

plans

reported by
Basil

Basil

Leo

Zoe

majority

Leo
A

majority

A
R
A
A

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.16

Knowledge Tree About Zoe

Zoe A




HH

HH

Leo A

Basil A

Basil A

Leo R

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.17

Four Generals: Data Structure of Basil (1)

Basil
general

plan

reported by
John

Basil

John

Leo

Zoe

majority

Leo

majority

Zoe
A

R
?
?

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.18

Four Generals: Data Structure of Basil (2)

Basil
general

plans

reported by
John

Basil

John

Leo

Zoe

Leo

majority

Zoe
A

R
R
R

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.19

Knowledge Tree About Loyal General Leo

Leo X








Basil X


John X

HH
HH



HH
HH

John X
A
A
Zoe X



Basil X

Zoe X
A
A
Zoe X



Basil Y

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

A
A
John Z

Slide 12.20

Knowledge Tree About Traitor Zoe

Zoe








Basil X


John X

HH
HH



HH
HH

John Y
A
A
Leo X



Basil Y

Leo Z
A
A
Leo Y



Basil Z

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

A
A
John Z

Slide 12.21

Complexity of the Byzantine Generals Algorithm

traitors

generals

messages

36

392

10

1790

13

5408

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.22

p1:
p2:
p3:
p4:
p5:
p6:
p7:

Algorithm 12.3: Consensus - flooding algorithm


planType finalPlan
set of planType plan { chooseAttackOrRetreat }
set of planType receivedPlan
do t + 1 times
for all other generals G
send(G, plan)
for all other generals G
receive(G, receivedPlan)
plan plan receivedPlan
finalPlan majority(plan)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.23

Flooding Algorithm with No Crash: Knowledge Tree About Leo

Leo X

?
Zoe X

Basil X


Zoe X

Zoe X

Zoe X

John X

@
@
R
@

John X

@
@
R
@

Zoe X

Basil X

Zoe X

Zoe X

Zoe X

Zoe X

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.24

Flooding Algorithm with Crash: Knowledge Tree About Leo (1)

Leo X

?
Basil X
@

Zoe X

@
R
@
John X

Zoe X

Zoe X

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.25

Flooding Algorithm with Crash: Knowledge Tree About Leo (2)

Leo X

?
Basil X
@
@
R
@
John X

?
Zoe X

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.26

Algorithm 12.4: Consensus - King algorithm


planType finalPlan, myMajority, kingPlan
planType array[generals] plan
integer votesMajority
p1: plan[myID] chooseAttackOrRetreat
p2:
p3:
p4:
p5:
p6:
p7:
p8:

do two times
for all other generals G
// First and third rounds
send(G, myID, plan[myID])
for all other generals G
receive(G, plan[G])
myMajority majority(plan)
votesMajority number of votes for myMajority

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.27

Algorithm 12.4: Consensus - King algorithm (continued)


p9:
p10:
p11:
p12:
p13:
p14:
p15:
p16:
p17:

if my turn to be king
// Second and fourth rounds
for all other generals G
send(G, myID, myMajority)
plan[myID] myMajority
else
receive(kingID, kingPlan)
if votesMajority > 3
plan[myID] myMajority
else
plan[myID] kingPlan
finalPlan plan[myID]

// Final decision

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.28

Scenario for King Algorithm - First King Loyal General Zoe (1)
Basil
Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

kingPlan

John
Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

kingPlan

Leo
Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

kingPlan

Zoe
Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

kingPlan

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.29

Scenario for King Algorithm - First King Loyal General Zoe (2)
Basil
Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

kingPlan

R
John

Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

kingPlan

R
Leo

Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

kingPlan

R
Zoe

Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

kingPlan

R
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.30

Scenario for King Algorithm - First King Loyal General Zoe (3)
Basil
Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

45

kingPlan

John
Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

45

kingPlan

Leo
Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

45

kingPlan

Zoe
Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

45

kingPlan

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.31

Scenario for King Algorithm - First King Traitor Mike (1)


Basil
Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

kingPlan

R
John

Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

kingPlan

A
Leo

Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

kingPlan

A
Zoe

Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

kingPlan

R
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

R
Slide 12.32

Scenario for King Algorithm - First King Traitor Mike (2)


Basil
Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

kingPlan

John
Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

kingPlan

Leo
Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

kingPlan

Zoe
Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

kingPlan

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.33

Scenario for King Algorithm - First King Traitor Mike (3)


Basil
Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

kingPlan

A
John

Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

kingPlan

A
Leo

Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

kingPlan

A
Zoe

Basil

John

Leo

Mike

Zoe

myMajority

votesMajority

kingPlan

A
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.34

Complexity of Byzantine Generals and King Algorithms

traitors

generals

messages

traitors

generals

messages

36

48

392

240

10

1790

13

672

13

5408

17

1440

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.35

Impossibility with Three Generals (1)

Zoe X

Leo Y
@
@

@
@
@

Leo
x1 , . . . , xn

John

Leo
u1 , . . . , um

@
Zoe
y1 , . . . , yn

John

Zoe
v1 , . . . , vm

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.36

Impossibility with Three Generals (2)

John
@
@
Leo
x1 , . . . , xn

@
Zoe
y1 , . . . , yn

Zoe
x1 , . . . , xn

Leo
y1 , . . . , yn

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.37

Exercise for Byzantine Generals Algorithm

Zoe
general

plan

reported by
Basil

Basil

John

Leo

Zoe

majority

John

Leo

?
A
?

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 12.38

Release Time, Execution Time and Relative Deadline


r

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.1

Periodic Task


r

-
r

-

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.2

Deadline is a Multiple of the Period


r

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.3

Architecture of Ariane Control System

Sensors

INS

Main
Computer

Actuators

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.4

Synchronization Window in the Space Shuttle

225 240

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

1000

Slide 13.5

Synchronous System

Sample
Compute
Control
T1

T2

T1

T2

Telemetry
Self-test
0

10

11

12

13

14

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

15

Slide 13.6

Synchronous System Scheduling Table

Sample

Compute

Control

Telemetry 1

Self-test

Sample

Compute

Control

Telemetry 2

Telemetry 1

10

11

12

13

14

Sample

Compute

Control

Telemetry 2

Self-test

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.7

Algorithm 13.1: Synchronous scheduler


taskAddressType array[0..numberFrames-1] tasks
[task address,. . . ,task address]
integer currentFrame 0
loop
p2:
await beginning of frame
p3:
invoke tasks[currentFrame]
p4:
increment currentFrame modulo numberFrames
p1:

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.8

Algorithm 13.2:
queue
sample
dataType d
p1: d sample
p2: append(d, buffer1)
p3:

Producer-consumer (synchronous system)


of dataType buffer1, buffer2
compute
control
dataType d1, d2
dataType d
q1: d1 take(buffer1)
r1: d take(buffer2)
q2: d2 compute(d1)
r2: control(d)
q3: append(d2, buffer2)
r3:

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.9

Asynchronous System

Data management

Communications

Telemetry
0

10

11

12

13

14

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

15

Slide 13.10

Algorithm 13.3: Asynchronous scheduler


queue of taskAddressType readyQueue . . .
taskAddressType currentTask
loop forever
p1:
await readyQueue not empty
p2:
currentTask take head of readyQueue
p3:
invoke currentTask

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.11

Algorithm 13.4: Preemptive scheduler


queue of taskAddressType readyQueue . . .
taskAddressType currentTask
p1:
p2:
p3:
p4:
p5:
p6:
p7:
p8:
p9:
p10:

loop forever
await a scheduling event
if currentTask.priority < highest priority of a task on readyQueue
save partial computation of currentTask and place on readyQueue
currentTask take task of highest priority from readyQueue
invoke currentTask
else if currentTasks timeslice is past and
currentTask.priority = priority of some task on readyQueue
save partial computation of currentTask and place on readyQueue
currentTask take a task of the same priority from readyQueue
invoke currentTask
else resume currentTask

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.12

Preemptive Scheduling

Watchdog

Data management

Communications

Telemetry
0

10

11

12

13

14

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

15

Slide 13.13

p1:
p2:
p3:
p4:
p5:

Algorithm 13.5: Watchdog supervision of response time


boolean ran false
data management
watchdog
loop forever
loop forever
do data management
q1:
await ninth frame
ran true
q2:
if ran is false
rejoin readyQueue
q3:
notify response-time overflow
q4:
ran false
q5:
rejoin readyQueue

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.14

Algorithm 13.6: Real-time buffering - throw away new data


queue of dataType buffer empty queue
sample
compute
dataType d
dataType d
loop forever
loop forever
p1:
d sample
q1:
await buffer not empty
p2:
if buffer is full do nothing
q2:
d take(buffer)
p3:
else append(d,buffer)
q3:
compute(d)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.15

Algorithm 13.7: Real-time buffering - overwrite old data


queue of dataType buffer empty queue
sample
compute
dataType d
dataType d
loop forever
loop forever
p1:
d sample
q1:
await buffer not empty
p2:
append(d, buffer)
q2:
d take(buffer)
p3:
q3:
compute(d)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.16

Interrupt Overflow on Apollo 11

Watchdog

Counter increments

Main task
0

10

11

12

13

14

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

15

Slide 13.17

Priority Inversion (1)

CS

Data management

CS

Telemetry

10

11

12

13

14

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

15

Slide 13.18

Priority Inversion (2)

CS

Data management

Communications
CS

Telemetry

10

11

12

13

14

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

15

Slide 13.19

Priority Inheritance

CS

Data management
Telemetry
Communications
CS

Telemetry

10

11

12

13

14

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

15

Slide 13.20

Priority Inversion in Promela (1)


1

mtype = { idle, blocked, nonCS, CS, long };

mtype data = idle, comm = idle, telem = idle;

#define ready(p) (p != idle && p != blocked)

4
5

active proctype Data() {

do

::

data = nonCS;

enterCS(data);

exitCS(data);
data = idle ;

10

od

11
12

13
14
15
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.21

Priority Inversion in Promela (1)


16

active proctype Comm() provided (!ready(data)) {

17

do

18

::

comm = long;
comm = idle;

19

od

20
21

22

active proctype Telem()


provided (!ready(data) && !ready(comm)) {

23
24

do

25

::

telem = nonCS;

26

enterCS(telem);

27

exitCS(telem);

28

telem = idle ;
od

29
30

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.22

Priority Inversion in Promela (2)


1

bit sem = 1;

2
3

inline enterCS(state ) {

atomic {

if

:: sem == 0 >

state = blocked;

sem != 0;
:: else >

9
10

fi ;

11

sem = 0;

12

state = CS;
}

13
14

15
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.23

Priority Inversion in Promela (2)

16

inline exitCS(state ) {
atomic {

17
18

sem = 1;

19

state = idle
}

20
21

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.24

Priority Inheritance in Promela


1

#define inherit (p) (p == CS)

active proctype Data() {

do

::

data = nonCS;

assert( ! (telem == CS && comm == long) );

enterCS(data); exitCS(data);

data = idle ;
od

8
9
10
11
12
13
14
15

}
active proctype Comm()
provided (!ready(data) && !inherit (telem))
{ ... }
active proctype Telem()
provided (! ready(data) && !ready(comm) || inherit (telem))
{ ... }

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.25

Data Structures in Simpsons Algorithm

lastWrittenPair

lastReadPair
@
@
R
@

1
I
@


@
@
@
0
@
@

currentSlot

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.26

Algorithm 13.8: Simpsons four-slot algorithm


dataType array[0..1,0..1] data default initial values
bit array[0..1] currentSlot { 0, 0 }
bit lastWrittenPair 1, lastReadPair 1

p1:
p2:
p3:
p4:
p5:
p6:

writer
bit writePair, writeSlot
dataType item
loop forever
item produce
writePair 1 lastReadPair
writeSlot 1 currentSlot[writePair]
data[writePair, writeSlot] item
currentSlot[writePair] writeSlot
lastWrittenPair writePair

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.27

Algorithm 13.8: Simpsons four-slot algorithm (continued)

p7:
p8:
p9:
p10:
p11:

reader
bit readPair, readSlot
dataType item
loop forever
readPair lastWrittenPair
lastReadPair readPair
readSlot currentSlot[readPair]
item data[readPair, readSlot]
consume(item)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.28

Algorithm 13.9: Event signaling


binary semaphore s 0
p
p1: if decision is to wait for event
p2:
wait(s)

q
q1: do something to cause event
q2: signal(s)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.29

Suspension Objects in Ada

package Ada.Synchronous_Task_Control is

type Suspension_Object is limited private;

procedure Set_True(S : in out Suspension_Object);

procedure Set_False(S : in out Suspension_Object);

function Current_State(S : Suspension_Object)

6
7
8
9

return Boolean;
procedure Suspend_Until_True(
S : in out Suspension_Object);
private

10

not specified by the language

11

end Ada.Synchronous_Task_Control;

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.30

Algorithm 13.10: Suspension object - event signaling


Suspension_Object SO (false by default)
p
q
p1: if decision is to wait for event
q1: do something to cause event
p2:
Suspend_Until_True(SO)
q2: Set_True(SO)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.31

Transition in UPPAAL




clk >= 12, ch ?, n := n + 1




c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.32

Feasible Priority Assignment

P1
P2
0

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.33

Infeasible Priority Assignment

P2
P1
0

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.34

Algorithm 13.11: Periodic task


constant integer period . . .
integer next currentTime
loop forever
p1:
delay next currentTime
p2:
compute
p3:
next next + period

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide 13.35

Semantics of Propositional Operators

v(A1 )

v(A2 )

A1

A1

A1 A2

A1 A2

otherwise

A1 A2

A1 A2

otherwise

A1 A2

A1 A2

otherwise

A1 A2

v(A1 ) = v(A2 )

A1 A2

v(A1 ) 6= v(A2 )

v(A)

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide B.1

Wason Selection Task

p3

p5

flag = 1

flag = 0

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide B.2

Algorithm B.1: Verification example


integer x1, integer x2
integer y1 0, integer y2 0, integer y3
read(x1,x2)
p2: y3 x1
p3: while y3 6= 0
p4:
if y2+1 = x2
p5:
y1 y1 + 1
p6:
y2 0
p7:
else
p8:
y2 y2 + 1
p9:
y3 y3 1
p10: write(y1,y2)
p1:

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide B.3

Spark Program for Integer Division


1

# main_program;

procedure Divide(X1,X2: in Integer ; Q,R : out Integer )

# derives Q, R from X1,X2;

# pre (X1 >= 0) and (X2 > 0);

# post (X1 = Q X2 + R) and (X2 > R) and (R >= 0);

is

N: Integer ;

8
9
10
11
12
13
14
15
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide B.4

Spark Program for Integer Division


16

begin

17

Q := 0; R := 0; N := X1;

18

while N /= 0

19

# assert (X1 = QX2+R+N) and (X2 > R) and (R >= 0);

20

loop

21
22
23
24

if R+1 = X2 then
Q := Q + 1; R := 0;
else
R := R + 1;

25

end if ;

26

N := N 1;

27
28

end loop;
end Divide;

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide B.5

Integer Division
1
2
3

procedure Divide(X1,X2: in Integer ; Q,R : out Integer ) is


N: Integer ;
begin

pre (X1 >= 0) and (X2 > 0);

Q := 0; R := 0; N := X1;

while N /= 0

assert (X1 = QX2+R+N) and (X2 > R) and (R >= 0);

loop

if R+1 = X2 then Q := Q + 1; R := 0;

10

else R := R + 1;

11

end if ;

12

N := N 1;

13

end loop;

14

post (X1 = Q X2 + R) and (X2 > R) and (R >= 0);

15

end Divide;

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide B.6

Verification Conditions for Integer Division


Precondition to assertion:
(X1 0) (X2 > 0)
(X1 = Q X2 + R + N) (X2 > R) (R 0).
Assertion to postcondition:
(X1 = Q X2 + R + N) (X2 > R) (R 0) (N = 0)
(X1 = Q X2 + R) (X2 > R) (R 0).
Assertion to assertion by then branch:
(X1 = Q X2 + R + N) (X2 > R) (R 0) (R + 1 = X2)
(X1 = Q0 X2 + R0 + N 0 ) (X2 > R0 ) (R0 0).
Assertion to assertion by else branch:
(X1 = Q X2 + R + N) (X2 > R) (R 0) (R + 1 6= X2)
(X1 = Q0 X2 + R0 + N 0 ) (X2 > R0 ) (R0 0).
c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide B.7

The Sleeping Barber

producer

consumer

Buffer

notEmpty

append(d, Buffer)

wait(notEmpty)

[]

signal(notEmpty)

wait(notEmpty)

[1]

append(d, Buffer)

wait(notEmpty)

[1]

append(d, Buffer)

d take(Buffer)

[1]

append(d, Buffer)

wait(notEmpty)

[]

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide C.1

Synchronizing Precedence

node2

node1
HH


*



HH
H
j
H

HH

?
node3






HH
j
H
node4




c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide C.2

Algorithm C.1: Barrier synchronization


global variables for synchronization
loop forever
p1:
wait to be released
p2:
computation
p3:
wait for all processes to finish their computation

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide C.3

The Stable Marriage Problem

Man

List of women

Woman

List of men

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide C.4

p1:
p2:
p3:
p4:
p5:
p6:
p7:
p8:

Algorithm C.2: Gale-Shapley algorithm for stable marriage


integer list freeMen {1,. . . ,n}
integer list freeWomen {1,. . . ,n}
integer pair-list matched
integer array[1..n, 1..n] menPrefs . . .
integer array[1..n, 1..n] womenPrefs . . .
integer array[1..n] next 1
while freeMen 6= , choose some m from freeMen
w menPrefs[m, next[m]]
next[m] next[m] + 1
if w in freeWomen
add (m,w) to matched, and remove w from freeWomen
else if w prefers m to m // where (m,w) in matched
replace (m,w) in matched by (m,w), and remove m from freeMen
else // w rejects m, and nothing is changed

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide C.5

The n-Queens Problem

Q
Q

2
Q

4
Q

7
Q

8
1

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide C.6

The Architecture of BACI

Pascal
source

Pascal
compiler


Editor
@
R
@

C
source

C
compiler

@
R
@

P-code

- Interpreter

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide D.1

The Architecture of Spin

Promela
source


6
LTL
translator

Editor

Parser/
Generator

Trail
6

?
Verifier
C source

Computer

@
@

6
R
@
LTL
formula

?
C
Compiler

Verifier
Executable

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide D.2

Cycles in a State Diagram

'

criticalp = 0
criticalq = 0
&

'

- criticalp = 0
criticalq = 1
% &

'

criticalp = 0
criticalq = 0
&


%

'

criticalp = 0
criticalq = 1
&

'

- criticalp = 0
criticalq = 0
% &

?
?
'

criticalp = 0
criticalq = 0
&

$

%

c M. Ben-Ari 2006
M. Ben-Ari. Principles of Concurrent and Distributed Programming, Second edition

Slide D.3

You might also like