concurrent algorithms 2018 · problem 2 –register-swap •task: write an algorithm that...

43
Concurrent Algorithms 2018 Midterm Exam Solutions 1

Upload: others

Post on 21-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Concurrent Algorithms 2018Midterm Exam Solutions

1

Page 2: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 1

• Task: Write an algorithm that implements a MRSW atomic M-valued register using (any number of) SRSW regular M-valued registers.

• Solution:

SRSW regular M-valued → SRSW atomic M-valued → MRSW atomic M-valued

(see lecture slides)

2

Page 3: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 2 – register-swap

• Task: Write an algorithm that implements wait-free consensus for n processes in this setting.

Variables:Shared MWMR atomic registers A and B.

procedure register-swap(A, B)tempA = AtempB = BA = tempBB = tempA

3

Page 4: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 2 – register-swap

• R[1, …, N] = {⊥, …, ⊥}

• Winner[1, …. N] = {⊥, …, ⊥}

• Decided = won

procedure propose(v)R[i] = vregister-swap(Winner[i], Decider)j = unique index in Winner with Winner[j] = wonreturn R[j]

4

Page 5: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 2 – register-swap

• R[1, …, N] = {⊥, …, ⊥}

• Winner[1, …. N] = {⊥, …, ⊥}

• Decided = won

procedure propose(v)R[i] = vregister-swap(Winner[i], Decider)j = unique index in Winner with Winner[j] = wonreturn R[j]

First processes that does the swap “wins” the consensus

5

Page 6: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

Variables:

V=0 (binary register)

procedure test-and-set()

temp = V

if temp = 0 then

V = 1

return temp

6

Page 7: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

R[2] = {⊥, ⊥}X // test-and-set object

procedure proposei(v) // i in {0, 1}R[i] = vresult = x.test-and-set()if (result == 0)

return R[i]else

return R[1 – i]

test-and-setsolves consensusfor 2 processes.

7

Page 8: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

R[2] = {⊥, ⊥}X // test-and-set object

procedure proposei(v) // i in {0, 1}R[i] = vresult = x.test-and-set()if (result == 0)

return R[i]else

return R[1 – i]

test-and-setsolves consensusfor 2 processes.

But not for 3 processes.

8

Page 9: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S0

State: state of all the processes and of the shared objects

9

Page 10: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S0 S1

Process p5 takes a step

10

A step corresponds to the access (read or modify) of some shared object.

Page 11: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S0 S1

Process p5 takes a step

S2

11

Page 12: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S0 S1

Process p5 takes a step

S2 …

12

Page 13: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S0

A state is bivalent if the decision is not yet fixed.Processes could decide 0 or 1.

S4 S5…

S2 S3…

Processesdecide 0

Processesdecide 1

13

Page 14: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S0

A state is bivalent if the decision is not yet fixed.Processes could decide 0 or 1.

S4 S5…

S2 S3…

Processesdecide 0

Processesdecide 1

S0 is bivalent

14

Page 15: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S0

A state is univalent if the decision is fixed.

S4 S5…

S2 S3…

Processesdecide 0

Processesdecide 1

S5 is univalent.All processes startingfrom S5 decide on onespecific value.

15

Page 16: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S0

p0 p1propose(0) propose(1)

will decide 0.

solo run

16

Page 17: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S0

p0 p1propose(0) propose(1)

will decide 1.

solo run

17

Page 18: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S0

Every consensus algorithm has an initial bivalent state.

p0 p1propose(0) propose(1)

will decide 1.

solo run

18

Page 19: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

Every consensus algorithm has a state that:• is bivalent;• if any process takes a step, the new state is univalent.

Also known as a critical state.

19

Page 20: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set S9

Every consensus algorithm has a state that:• is bivalent;• if any process takes a step, the new state is univalent.

Also known as a critical state.

Suppose not. As long as a process can take stepswithout reaching a univalent state, let that processtake steps.

p2 takes steps

20

Page 21: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set S9

Every consensus algorithm has a state that:• is bivalent;• if any process takes a step, the new state is univalent.

Also known as a critical state.

S10

Suppose not. As long as a process can take stepswithout reaching a univalent state, let that processtake steps.

p2 takes steps

21

Page 22: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set S9

Every consensus algorithm has a state that:• is bivalent;• if any process takes a step, the new state is univalent.

Also known as a critical state.

S10

Suppose not. As long as a process can take stepswithout reaching a univalent state, let that processtake steps.

p2 takes steps

p2 takes steps…

S∞

22

Page 23: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set S9

Every consensus algorithm has a state that:• is bivalent;• if any process takes a step, the new state is univalent.

Also known as a critical state.

S10

Suppose not. As long as a process can take stepswithout reaching a univalent state, let that processtake steps.

p2 takes steps

p2 takes steps…

S∞

not wait-free23

Page 24: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S15S15 is a critical state.In other words:• S15 is bivalent• Any process that takes a step

reaches a univalent state S16S17

p0p1

24

Page 25: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

Assume there is a consensus algorithm for 3 processes p0, p1, and p2

that only uses read/write and test-and-set objects.

There should be a critical state.

25

Page 26: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S15

S16S17

p0 p1

S18

p2

0-valent 0-valent

1-valent

26

Page 27: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S15

S16S17

p0 p1

S18

p2

0-valent 0-valent

1-valent

27

Can the steps be reads?

Page 28: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S15

S16S17

p0 p1

S18

p2

0-valent 0-valent

1-valent

28

Can the steps be reads?

No!

Page 29: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S15

S16S17

p0 p1

S18

p2

0-valent 0-valent

1-valent

29

Can the steps be writes?

Page 30: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S15

S16S17

p0 p1

S18

p2

0-valent 0-valent

1-valent

30

Can the steps be writes?

No!

Page 31: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S15

S16S17

p0 p1

S18

p2

0-valent 0-valent

1-valent

31

So steps need to betest-and-sets

Page 32: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S15

S16S17

P0 x.t&s() P1 y.t&s()

0-valent

1-valent

32

Can it be test-and-setson different objects?

S18

P2 z.t&s()

0-valent

Page 33: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S15

S16S17

P0 x.t&s() P1 y.t&s()

0-valent

1-valent

33

P1 y.t&s() P0 x.t&s()

A contradiction.

S18 S19

S18

P2 z.t&s()

0-valent

Can it be test-and-setson different objects?

P2 cannot distinguish between S18 and S19!

Page 34: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S15

S16S17

P0 x.t&s() P1 x.t&s()

0-valent

1-valent

34

So, all processes use the same test-and-set object.

S18

P2 x.t&s()

0-valent

S18 S19

Page 35: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S15

S16S17

P0 x.t&s() P1 x.t&s()

0-valent

1-valent

35

So, all processes use the same test-and-set object.

S18

P2 x.t&s()

0-valent

P2 x.t&s() P2 x.t&s()

S18 S19

Page 36: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S15

S16S17

P0 x.t&s() P1 x.t&s()

0-valent

1-valent

36

So, all processes use the same test-and-set object.

S18

P2 x.t&s()

0-valent

P2 x.t&s() P2 x.t&s()

P2 cannot distinguish between S18 and S19!S18 S19

Page 37: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S15

S16S17

P0 x.t&s() P1 x.t&s()

0-valent

1-valent

37

So, all processes use the same test-and-set object.

S18

P2 x.t&s()

0-valent

P2 x.t&s() P2 x.t&s()

Let P2 run. It will decide 0.

Let P2 run. It will decide 1.

P2 cannot distinguish between S18 and S19!S18 S19

Page 38: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

S15

S16S17

P0 x.t&s() P1 x.t&s()

0-valent

1-valent

38

So, all processes use the same test-and-set object.

S18

P2 x.t&s()

0-valent

P2 x.t&s() P2 x.t&s()

P2 cannot distinguish between S18 and S19!

A contradiction.

Let P2 run. It will decide 0.

Let P2 run. It will decide 1.

S18 S19

Page 39: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 3 – test-and-set

39

In other words, the consensusnumber of test-and-set is 2.

Page 40: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 4 – queue

40

Double-ended queue with a total of 3 peek operations.

procedure peek(end)if peeks_invoked == 3

return ⊥peeks_invoked=peeks_invoked+1if end = HEAD

return list.first()else

return list.last()

Page 41: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 4 – queue

41

Double-ended queue with a total of 3 peek operations.

procedure peek(end)if peeks_invoked == 3

return ⊥peeks_invoked=peeks_invoked+1if end = HEAD

return list.first()else

return list.last()

Task: Solve consensus for 4 processes.

Page 42: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 4 – queue

42

Double-ended queue with a total of 3 peek operations.

procedure propose(v)deque.enqueue(HEAD, v)winner = deque.peek(TAIL)if winner != ⊥

return winnerelse

return deque.dequeue(TAIL)

Page 43: Concurrent Algorithms 2018 · Problem 2 –register-swap •Task: Write an algorithm that implements wait-free consensus for n processes in this setting. Variables: Shared MWMR atomic

Problem 4 – queue

43

Double-ended queue with a total of 3 peek operations.

procedure propose(v)deque.enqueue(HEAD, v)winner = deque.peek(TAIL)if winner != ⊥

return winnerelse

return deque.dequeue(TAIL) At most 1 process would dequeue.