86
Chapter 2 Synchronization Algorithms and Concurrent Prog ramming Gadi Taubenfeld © 2007 1 Synchronization Algorithms Synchronization Algorithms and Concurrent Programming and Concurrent Programming Gadi Taubenfeld Gadi Taubenfeld Chapter 2 Mutual Exclusion using atomic registers: Basic Topics Version: November 2007

1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Embed Size (px)

Citation preview

Page 1: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

1

Synchronization Algorithms Synchronization Algorithms and Concurrent Programmingand Concurrent Programming

Gadi TaubenfeldGadi Taubenfeld

Chapter 2 Mutual Exclusion using atomic registers:

Basic Topics

Version: November 2007

Page 2: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

2

A note on the use of these ppt slides:I am making these slides freely available to all (faculty, students, readers). They are in PowerPoint form so you can add, modify, and delete slides and slide content to suit your needs. They obviously represent a lot of work on my part. In return for use, I only ask the following:

That you mention their source, after all, I would like people to use my

book!

That you note that they are adapted from (or perhaps identical to) my slides, and note my copyright of this material.

Thanks and enjoy! Gadi Taubenfeld

All material copyright 2007Gadi Taubenfeld, All Rights Reserved

A note on the use of these ppt slides:I am making these slides freely available to all (faculty, students, readers). They are in PowerPoint form so you can add, modify, and delete slides and slide content to suit your needs. They obviously represent a lot of work on my part. In return for use, I only ask the following:

That you mention their source, after all, I would like people to use my

book!

That you note that they are adapted from (or perhaps identical to) my slides, and note my copyright of this material.

Thanks and enjoy! Gadi Taubenfeld

All material copyright 2007Gadi Taubenfeld, All Rights Reserved

Synchronization Algorithms Synchronization Algorithms and Concurrent Programmingand Concurrent Programming

ISBN: 0131972596, 1ISBN: 0131972596, 1stst edition edition

To get the most updated version of these slides go to: http://www.faculty.idc.ac.il/gadi/book.htm

Page 3: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

3

2.1 Algorithms for Two Processes2.2 Tournament Algorithms2.3 A Fast Mutual Exclusion

Algorithms2.4 Starvation-free Algorithms2.5 Tight Space bounds2.6 Automatic Discovery of

algorithms

2.1 Algorithms for Two Processes2.2 Tournament Algorithms2.3 A Fast Mutual Exclusion

Algorithms2.4 Starvation-free Algorithms2.5 Tight Space bounds2.6 Automatic Discovery of

algorithms

Chapter 2 Chapter 2 Mutual Exclusion using atomic Mutual Exclusion using atomic

registers: Basic Topicsregisters: Basic Topics

Page 4: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

4

remainder coderemainder code

entry codeentry code

critical sectioncritical section

exit codeexit code

The problem is to design the entry and exit code in a way that guarantees that the mutual exclusion and deadlock-freedom properties are satisfied.

The mutual exclusion problemThe mutual exclusion problem

Page 5: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

5

The mutual exclusion problemThe mutual exclusion problem

Mutual Exclusion: No two processes are in their critical sections at the same time.

Deadlock-freedom: If a process is trying to enter its critical section, then some process, not necessarily the same one, eventually enters its critical section.

Starvation-freedom: If a process is trying to enter its critical section, then this process must eventually enter its critical section.

Mutual Exclusion: No two processes are in their critical sections at the same time.

Deadlock-freedom: If a process is trying to enter its critical section, then some process, not necessarily the same one, eventually enters its critical section.

Starvation-freedom: If a process is trying to enter its critical section, then this process must eventually enter its critical section.

entry code

exit code

criticalsection

remainder

Page 6: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

6

Question: true or false ?Question: true or false ?

A and B are deadlock-free C is deadlock-free.

A and B are starvation-free C is starvation-free.

A or B satisfies mutual exclusion C satisfies mutual exclusion.

A is deadlock-free and B is starvation-free C is starvation-free.

A is starvation-free and B is deadlock-free C is starvation-free.

A and B are deadlock-free C is deadlock-free.

A and B are starvation-free C is starvation-free.

A or B satisfies mutual exclusion C satisfies mutual exclusion.

A is deadlock-free and B is starvation-free C is starvation-free.

A is starvation-free and B is deadlock-free C is starvation-free.

entry code of A

exit code of A

criticalsection

remainder

entry code of B

exit code of B

Algorithm C

Page 7: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

7

Does it work?

Atomic read/write registersAtomic read/write registersProposed solution 1Proposed solution 1

Thread 0while (true} {remainder codewhile (turn = 1) {skip}critical sectionturn = 1}

Thread 1while (true} {remainder codewhile (turn = 0) {skip}critical sectionturn = 0}

entry code

exit code

0/1

turn mutual exclusion deadlock-freedom

Page 8: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

8

Proposed solution 2Proposed solution 2

Thread 0

flag[0] = truewhile (flag[1]) {skip}critical sectionflag[0] = false

Thread 1

flag[1] = truewhile (flag[0]) {skip}critical sectionflag[1] = false

false

flag

false

0

1

Does it work?

mutual exclusion deadlock-freedom

Page 9: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

9

Proposed solution 3Proposed solution 3

Thread 0

while (flag[1])

{skip}flag[0] = truecritical sectionflag[0] = false

Thread 1

while (flag[0])

{skip}flag[1] = truecritical sectionflag[1] = false

falseflag

false01

Does it work?

mutual exclusionDeadlock-freedom

Page 10: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

10

Peterson’s algorithmPeterson’s algorithm

Thread 0flag[0] = trueturn = 1while (flag[1] and turn = 1) {skip}critical sectionflag[0] = false

Thread 1flag[1] = trueturn = 0while (flag[0] and turn = 0) {skip}critical sectionflag[1] = false

false

flag

false

0

1

0/1turn

Section 2.1.1

Page 11: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

11

0/10/1

A variant of Peterson’s algorithmA variant of Peterson’s algorithm

false

flag

false

0

1

turn

Is it correct ?

true

true

10

Thread 0turn = 1flag[0] = truewhile (flag[1] and turn = 1) {skip}critical sectionflag[0] = false

Thread 1turn = 0flag[1] = truewhile (flag[0] and turn = 0) {skip}critical sectionflag[1] = false

Page 12: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

12

Indicate contendingb[i] := true

Indicate contendingb[i] := true

Barrierturn := iBarrier

turn := i

Contention?b[i] = true ?Contention?b[i] = true ?

critical sectioncritical section

exit codeb[i] = false ?

exit codeb[i] = false ?

First to cross the barrier?turn = j ?

First to cross the barrier?turn = j ?

yes

yes

no / maybe

no

Schematic for Peterson’s algorithmSchematic for Peterson’s algorithm

Page 13: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

13

Properties of Peterson’s SolutionProperties of Peterson’s Solution Satisfies mutual exclusion and starvation-

freedom Memory accesses are assumed to be atomic Solution for two processes only

Page 14: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

14

QuestionQuestion

Replace the multi-writer register turn with two single-writer registers. What is new algorithm?

Hint: use Solution #4 for the too-much-milk problem.

Answer (Kessels’ Alg.)turn = 0 turn[0] =

turn[1]turn = 1 turn[0]

turn[1]

Using only single-writer registersUsing only single-writer registers

Section 2.1.2

Page 15: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

15

Solutions for Many ProcessesSolutions for Many Processes

How can we use a two-process algorithm to construct an algorithm for many processes?

How can we use a two-process algorithm to construct an algorithm for many processes?

Page 16: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

16

Tournament AlgorithmsTournament Algorithms

1 2 3 4 5 6 7 8

Section 2.2

Page 17: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

17

QuestionQuestion

Assume that the base two-process algorithm satisfies starvation-freedom. Does the tournament algorithm also satisfy starvation-freedom?

Assume that the base two-process algorithm satisfies starvation-freedom. Does the tournament algorithm also satisfy starvation-freedom?

YesYes

Page 18: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

18

Properties & complexity Properties & complexity

Time complexity Fast Adaptive

Fairness FIFO, ...

Fault-tolerance Local spinning Space complexity Communication primitives

Time complexity Fast Adaptive

Fairness FIFO, ...

Fault-tolerance Local spinning Space complexity Communication primitives

Page 19: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

19

Complexity Measures Complexity Measures

Counting steps process step complexity process time complexity contention-free time

complexity Counting time units

system response time process response time

Counting communications distributed shared memory coherent cashing

Space complexity

Counting steps process step complexity process time complexity contention-free time

complexity Counting time units

system response time process response time

Counting communications distributed shared memory coherent cashing

Space complexity

Page 20: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

20

Thread 0flag[0] = trueturn = 1while (flag[1] and turn = 1) {skip}critical sectionflag[0] = false

Thread 1flag[1] = trueturn = 0while (flag[0] and turn = 0) {skip}critical sectionflag[1] = false

What is the contention-free time complexity?

44

Page 21: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

21

Thread 0flag[0] = trueturn = 1while (flag[1] and turn = 1) {skip}critical sectionflag[0] = false

Thread 1flag[1] = trueturn = 0while (flag[0] and turn = 0) {skip}critical sectionflag[1] = false

What is the process step complexity?

Theorem: There is no two process mutual exclusion algorithm with an upper bound on the process time/step complexity. [1992]

Proof: see Section 3.2.5 .

Theorem: There is no two process mutual exclusion algorithm with an upper bound on the process time/step complexity. [1992]

Proof: see Section 3.2.5 .

Page 22: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

22

Show a run of Peterson’s algorithm where thesystem response time is 5 time units.

What is the system response time?

1 2 3 4 5

Thread 0flag[0] = trueturn = 1while (flag[1] and turn = 1) {skip}critical sectionflag[0] = false

Thread 1flag[1] = trueturn = 0while (flag[0] and turn = 0) {skip}critical sectionflag[1] = false

Page 23: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

23

Fast Mutual exclusion AlgorithmFast Mutual exclusion Algorithm

Mutual exclusion and deadlock-freedom Starvation of individual processes is

possible fast access

in the absence of contention, only 7 accesses to shared memory are needed

With contention Even if only 2 processes contend, the

winner may need to check all the 0(n) shared registers

System response time is of order n time units

n+2 shared registers are used

Mutual exclusion and deadlock-freedom Starvation of individual processes is

possible fast access

in the absence of contention, only 7 accesses to shared memory are needed

With contention Even if only 2 processes contend, the

winner may need to check all the 0(n) shared registers

System response time is of order n time units

n+2 shared registers are used

Section 2.3

Page 24: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

24

At most n-1 can move

right

At most n-1 can move

down

At most 1 can win

In solo run 1 win

Splitter Splitter

rightwin

down

n

1 n-1

n-1

right

Page 25: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

25

The code of the splitter The code of the splitter

x = i if y = 1 then go right fi y = 1 if x i then go down fi win

x

0y

win

down

n

1 n-1

n-1

right

Page 26: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

26

NotationNotation

await (condition) == while (not condition) do skip od

Page 27: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

27

Fast Mutual exclusion AlgorithmFast Mutual exclusion Algorithmcode of process i , i {1 ,..., n}

start: b[i] = true

x = i if y 0 then b[i] = false await y = 0 goto start fi y = i if x i then b[i] = false for j = 1 to n do await b[j] = false od if y i then await y = 0 goto start fi fi critical section y = 0 b[i] = false

1 2 n

b false false falsefalseyx 0

Page 28: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

28

Can we switch the order of the last two statements?

start: b[i] = true

x = i if y 0 then b[i] = false await y = 0 goto start fi y = i if x i then b[i] = false for j = 1 to n do await b[j] = false od if y i then await y = 0 goto start fi fi critical section b[i] = false y = 0

yx 0

What is the maximum number of processes that can be in their CS at the same time?

Page 29: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

29

Can we replace y=0 in line 12 with ... ?

start: b[i] = true

x = i if y 0 then b[i] = false await y = 0 goto start fi y = i if x i then b[i] = false for j = 1 to n do await b[j] = false od if y i then await y = 0 goto start fi fi critical section y = 0 replace with: if y = i then y = 0 fi b[i] = false

Yes

Page 30: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

30

Can we remove await y=0 in line 4 ?

start: b[i] = true

x = i if y 0 then b[i] = false await y = 0 goto start fi y = i if x i then b[i] = false for j = 1 to n do await b[j] = false od if y i then await y = 0 goto start fi fi critical section y = 0 b[i] = false

No. Deadlock is possible

remove ?

Page 31: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

31

Can we remove await y=0 in line 9 ?

start: b[i] = true

x = i if y 0 then b[i] = false await y = 0 goto start fi y = i if x i then b[i] = false for j = 1 to n do await b[j] = false od if y i then await y = 0 goto start fi fi critical section y = 0 b[i] = false

Yes

remove ?

Page 32: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

32

Indicate contendingb[i] := true

Indicate contendingb[i] := true

Barrierturn := iBarrier

turn := i

Contention?x i ?

Contention?x i ?

critical sectioncritical section

exit codeexit code

Last to cross the barrier?y = i ?

Last to cross the barrier?y = i ?

yes

yes

Contention?y = 0 ?

Contention?y = 0 ?

Continue only after it is guaranteed that no one cancross the barrier

Continue only after it is guaranteed that no one cancross the barrier

no

Wait until CS is releasedWait until CS is released

Wait until CS is releasedWait until CS is releasedyes

The last to crossthe barrier!

no

no

Schematic for the fast algorithmSchematic for the fast algorithm

Page 33: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

33

4.1 Basic Definitions (i.e., FIFO) 4.2 The Bakery Algorithm4.3 The Black-White Bakery

Algorithm Algorithm

4.1 Basic Definitions (i.e., FIFO) 4.2 The Bakery Algorithm4.3 The Black-White Bakery

Algorithm Algorithm

Starvation-free AlgorithmsStarvation-free Algorithms

Section 2.4

Page 34: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

34

The mutual exclusion problemThe mutual exclusion problem

entry code

exit code

criticalsection

remainder Mutual Exclusion

Deadlock-freedom

Starvation-freedom

FIFO

doorway

waiting

Page 35: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

35

The Bakery AlgorithmThe Bakery Algorithm

Section 2.4.2

Page 36: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

36

time

The Bakery AlgorithmThe Bakery Algorithm

0 0 0 0 0 0

doorway

1 2 3 4 5 n

CS

exit

1

1

2 2

2 2

1

1

0

2

2

0

3

3

2

2

0

4

4waiting

en

try

remainder

Section 2.4.2

Page 37: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

37

Implementation 1Implementation 1code of process i , i {1 ,..., n}

number[i] = 1 + max {number[j] | (1 j n)}for j = 1 to n { await (number[j] = 0) (number[j] > number[i])}critical sectionnumber[i] = 0

1 2 3 4 n

number integer0 0 0 0 0 0

Answer: No! can deadlock

Page 38: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

38

time

Implementation 1: deadlockImplementation 1: deadlock

0 0 0 0 0 0

doorway

1 2 3 4 5 n

CS

exit

1

1

2 2

2 2

1

1

0

waiting

en

try

remainder

deadlock

Page 39: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

39

Implementation 1Implementation 1code of process i , i {1 ,..., n}

number[i] = 1 + max {number[j] | (1 j n)}for j = 1 to n { await (number[j] = 0) (number[j] > number[i])}critical sectionnumber[i] = 0

1 2 3 4 n

number integer0 0 0 0 0 0

Answer: does not satisy mutual exclusion

What if we replace > with ?

Page 40: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

40

number[i] = 1 + max {number[j] | (1 j n)}for j = 1 to n { await (number[j] = 0) (number[j],j) number[i],i)

// lexicographical order

}critical sectionnumber[i] = 0

1 2 3 4 n

number integer0 0 0 0 0 0

Answer: does not satisfy mutual exclusion

Implementation 2Implementation 2code of process i , i {1 ,..., n}

Page 41: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

41

time

Implementation 2: no mutual exclusionImplementation 2: no mutual exclusion

0 0 0 0 0

doorway

1 2 3 4 5 n

CS

exit

0

1

0 0

2 2

1

1

0

2 2

waiting

en

try

remainder

1 2 2

0

2

Page 42: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

42

The Bakery AlgorithmThe Bakery Algorithmcode of process i , i {1 ,..., n}

choosing[i] = true

number[i] = 1 + max {number[j] | (1 j n)}choosing[i] = falsefor j = 1 to n { await choosing[j] = false await (number[j] = 0) (number[j],j) (number[i],i)}critical sectionnumber[i] = 0

1 2 3 4 n

choosing bitsfalse

number integer0 0 0 0 0 0

false false false false false

Page 43: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

44

Is the following version correct?Is the following version correct?code of process i , i {1 ,..., n}

choosing[i] = true

number[i] = 1 + max {number[j] | (1 j n)}choosing[i] = falsefor j = 1 to n { if i > j { await choosing[j] = false } await (number[j] = 0) (number[j],j) (number[i],i)}critical sectionnumber[i] = 0

1 2 3 4 n

choosing bitsfalse

number integer0 0 0 0 0 0

false false false false false

Page 44: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

45

time

NoNo

0 0 0 0 0

doorway

1 2 3 4 5 n

CS

exit

0

1

0 0

2 1

1

1

0

2 1

waiting

en

try

remainder

1 2 1

0

1

Page 45: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

46

Is the following version correct?Is the following version correct?code of process i , i {1 ,..., n}

number[i] = -1number[i] = 1 + max {number[j] | (1 j n) , 0}for j = 1 to n { await number[j] -1 await (number[j] 0) (number[j],j) (number[i],i)}critical sectionnumber[i] = 0

1 2 3 4 n

choosing bitsfalse

number integer0 0 0 0 0 0

false false false false false

Yes

Can we replace with = ?

Page 46: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

47

Computing the Maximum #1Computing the Maximum #1code of process i , i {1 ,..., n}

Correct implementation

local1 = 0for local2 = 1 to n do local3 = number[local2] if local1 < local3 then local1 = local3 fi odnumber[i] = 1 + local1

1 2 3 4 nnumber integer0 0 0 0 0 0

number[i] = 1 + max {number[j] | (1 j n)}

Page 47: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

48

Computing the Maximum #2Computing the Maximum #2code of process i , i {1 ,..., n}

Is the following implementation correct ?

local1 = ifor local2 = 1 to n do if number[local1] < number[local2] then local1 = local2 fi odnumber[i] = 1 + number[local1]

1 2 3 4 nnumber integer0 0 0 0 0 0

number[i] = 1 + max {number[j] | (1 j n)}

Page 48: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

49

time

0 0 0 0

doorway

1 2 3 4 5 n

CS

exit

1 1

1 1

1

1

0

1

waiting

en

try

remainder 00

?

1

1

?local1 2

1

NoNo

Passed process 1Waiting for process 2

Page 49: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

50

Computing the Maximum #3Computing the Maximum #3code of process i , i {1 ,..., n}

Is the following implementation correct ?

local1 = ifor local2 = 1 to n do if number[local1] number[local2] then local1 = local2 fi odnumber[i] = 1 + number[local1]

A difficult question !!!

This is Problem 2.39

Page 50: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

51

Properties of the Bakery AlgorithmProperties of the Bakery Algorithm

Satisfies mutex & FIFO.

The size of number[i] is unbounded.

Safe registers: reads which are concurrent

with writes may return arbitrary value.

Page 51: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

52

The Black-White Bakery AlgorithmThe Black-White Bakery Algorithm

Section 2.4.3

Bounding the space of the Bakery Algorithm

Bakery (FIFO, unbounded)

The Black-White Bakery Algorithm

FIFOBounded space+ one bit

Page 52: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

53

time

The Black-White Bakery AlgorithmThe Black-White Bakery Algorithm

0 0 0 0 0

doorway

1 2 3 4 5 n

CS

exit

0

1

0 0

2 2

1

1

0

2

2

0

1

2

2

0

2waiting

en

try

remainder

1 20 201 2

1

1

00

color bit

Page 53: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

54

The Black-White Bakery AlgorithmThe Black-White Bakery Algorithm

1 2 3 4 n

choosing

Data StructuresData Structures

mycolor

number

color bit

bits

bits

{0,1,...,n}

{black,white}

Page 54: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

55

The Black-White Bakery AlgorithmThe Black-White Bakery Algorithmcode of process i , i {1 ,..., n}

choosing[i] = truemycolor[i] = colornumber[i] = 1 + max{number[j] | (1 j n) (mycolor[j] = mycolor[i])}choosing[i] = falsefor j = 0 to n do await choosing[j] = false if mycolor[j] = mycolor[i] then await (number[j] = 0) (number[j],j) (number[i],i) (mycolor[j] mycolor[i]) else await (number[j] = 0) (mycolor[i] color) (mycolor[j] = mycolor[i]) fi odcritical sectionif mycolor[i] = black then color = white else color = black finumber[i] = 0

Page 55: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

56

What happens if in the algorithm each process chooses its identifier instead of choosing 1 + max ? Would the algorithm be correct? What properties would it satisfy?

QuestionQuestion

Answer: Incorrect.Run process i first until it enters its CS; Now run a process with a smaller id until it also enters its CS.

Page 56: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

57

Does it matter if we change the order of the last two statements (in the exit code) of the Black-White Bakery Algorithm?

QuestionQuestion

Answer: Yes, it matters.

Page 57: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

58

Bakery

Black-White Bakery

Use single-writer bits only!

QuestionQuestion

Replace the multi-writer bit color with n single-writer bits.

Answer: See page 57.

Page 58: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

59

• A lower bound (Section 2.5.1)• An Upper Bound (Section 2.5.2)

Tight space bounds for mutual Tight space bounds for mutual exclusion using atomic registersexclusion using atomic registers

Page 59: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

60

A Simple ObservationA Simple Observation

Theorem: Any deadlock-free mutual exclusion algorithm for n processes using only SWMR registers must use at least n such registers.

Theorem: Any deadlock-free mutual exclusion algorithm for n processes using only SWMR registers must use at least n such registers.

A very simple Lower Bound

Proof: Before entering its critical section a process must write at least once …

Question: Can we do better using MWMR registers ?

Answer: No.

(SWMR == Single Writer Multi Reader)

Page 60: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

61

Tight Space BoundsTight Space Bounds

Theorem: Any deadlock-free mutual exclusion algorithm for n processes must use at least n shared registers.

Theorem: Any deadlock-free mutual exclusion algorithm for n processes must use at least n shared registers.

Section 2.5.1

A Lower Bound (very difficult to prove!)

Proof: (see next few slides)

Page 61: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

62

DefinitionsDefinitions

run – a sequence of events run x looks like run y to process p process p is hidden in run x process p covers register r in run x

Page 62: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

63

ExampleExamplerun x looks like run y to process p

run x• p reads 5 from r• q writes 6 to r• p writes 7 to r• q writes 8 to r• p reads 8 from r

run y• p reads 5 from r• p writes 7 to r• q writes 6 to r• q reads 6 from r• q writes 8 to r• p reads 8 from r

8r 8r

• q write 6 to r1

6

The values of the shared registers must also be the same

Page 63: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

64

ExampleExample

• p reads 5 from r• q reads 5 from r• p writes 7 to r• q writes 8 to r• p reads 8 from r• q writes 6 from r

r

writes must be overwritten before any other process has read the value written

process p is hidden in run x

Page 64: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

65

ExampleExample

• p reads 5 from r• p reads 5 from r• q reads 5 from r• p writes 7 to r• q writes 8 to r• p reads 8 from r• p writes 2 to r

r

writes must be overwritten before any other process has read the value written

process p covers register r in run x

p covers r at these

three points

Page 65: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

66

Lemma 2.17Lemma 2.17 (very simple)(very simple)

x y

run x looks like run y to processes in P

z

P events only

then, this is also a

run

Proof: By induction on the number of events in (z-x) …

Page 66: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

67

Lemma 2.18Lemma 2.18 (very simple)(very simple)

If p is in its critical section in run z then p is not hidden in z.

z

p is in its

critical section

then, p is not hidden

Proof: Assume to the contrary … by Lemma 2.17 …

Page 67: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

68

Lemma 2.19Lemma 2.19

x

all the processes

are hidden

all processes except may p are

in their remainders

y

Then, for any p there exists y such that x looks like y to p

Proof: By induction on the number of steps of processes other than p in x ...

Page 68: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

69

Lemma 2.19Lemma 2.19: Example: Example

• w reads 5• p reads 5• q reads 5• p writes 7• q writes 8• q reads 8• w writes 3• w reads 3• p write 9• p in its critical section• p exits

5 r w is hidden remove ww is in its remainder

q is in its remainder

important: q is still hidden !!!

q is hidden remove q

p is in its remainder

Formal proof: By induction on the number of steps of processes other than p in x ...

Page 69: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

70

Lemma 2.20Lemma 2.20 (simple)(simple)

x

all the processe

s are hidden

only p takes steps

zp covers unique register

Then, for every p there exists z

Page 70: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

71

Lemma 2.20: ProofLemma 2.20: Proof

x

all the processes are hidden

all processes except may p are

in their remainders

x looks like y to p

p is in its critical section

p is in its critical section

By Lemma 2.17 …

… p is hidden … imp. by Lemma 2.18

p covers unique register

z

By Lemma 2.19: there exists y …

By the deadlock-freedom assumption …

Page 71: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

72

Lemma 2.21Lemma 2.21 (main lemma)(main lemma)

x

all the processes are in their remainder

z

only P take steps

P are hidden and cover |P|

distinct register

Then, for every set of processes P there exists z

Proof: By induction on k the size of P.

Page 72: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

73

Lemma 2.21Lemma 2.21 (main lemma)(main lemma)

Proof: By induction on k the size of P. Base: k = 0 is trivial. Induction hypothesis: Assume that it holds for k 0. Step: We prove that it holds for k+1.

let P be a set of processes where |P|= k; let p be a process not in P. We show that:

z

only P+p take steps

P+p are hidden and cover |P+p| distinct register

Page 73: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

74

Lemma 2.21Lemma 2.21

x y1

W1

... ... ...y2 yi

yj

W2 Wi Wj

only P take steps

P are hidden and cover |P|

distinct registers

By the induction hypothesis:

z1

w1

z2 zi zj

w2 wi wj

...

each process in P takes one step first; all in their remainders ; ...

equals

This completes the lower bound proof

only p takes steps

p covers a unique register

w1 W1(Lemma 2.20)

P are hidden? YesP cover W1 ? Yesp covers w1? Yesp is hidden? Maybe

P are hidden? YesP cover Wj ? Yesp covers wj? Yesp is hidden? Yes!

Page 74: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

75

QuestionQuestion

• p reads 5 from r• q reads 5 to r• p writes 7 to r• q writes 8 to r• p reads 8 from r• p writes 8 to r

8r

What breaks in the lower bound proof if we change the definition of “process p is hidden in run x” as follows:

this write does not

change the value of r!

Page 75: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

76

Tight Space BoundsTight Space Bounds

Theorem: There is a deadlock-free mutual exclusion algorithm for n processes that uses n shared bits.

Theorem: There is a deadlock-free mutual exclusion algorithm for n processes that uses n shared bits.

Section 2.5.2

An Upper Bound

Page 76: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

77

The One-Bit AlgorithmThe One-Bit Algorithmcode of process i , i {1 ,..., n}

1 2 3 4 n

b bits false false false false false false true true false false true true false

critical section

false

p3 writes true

p3 writes false

pn writes true

pn writes false

Page 77: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

78

The One-Bit AlgorithmThe One-Bit Algorithmcode of process i , i {1 ,..., n}

repeat b[i] = true; j = 1; while (b[i] = true) and (j < i) do if b[j] = true then b[i] = false; await b[j] =false fi j = j+1 oduntil b[i] = truefor j = i+1 to n do await b[j] = false odcritical sectionb[i] = false

1 2 3 4 n

b bitsfalse false false false false false

Page 78: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

79

Does the correctness of the One-bit algorithm depend on the order in which the bits are read in the loops ?

QuestionQuestion

Page 79: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

80

Properties of the One-Bit AlgorithmProperties of the One-Bit Algorithm

• Satisfies mutual exclusion and deadlock-freedom• Starvation is possible• It is not fast• It is not symmetric• It uses only n shared bits and hence it is space optimal

• Would it work with safe bits?

• Satisfies mutual exclusion and deadlock-freedom• Starvation is possible• It is not fast• It is not symmetric• It uses only n shared bits and hence it is space optimal

• Would it work with safe bits?

Page 80: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

81

QuestionQuestion

Does it follow from the lower bound that there is no solution when the number of processes is not known in advance or when the number is infinite?

Computing with Infinitely Many ProcessComputing with Infinitely Many Process

Section 2.5.3

No. It follows that in such a case infinitely many registers are needed. A simple algorithm for infinitely many processes is presented in Section 2.5.3.

Page 81: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

82

Computing with Infinitely Many Process: The algorithmComputing with Infinitely Many Process: The algorithm

Section 2.5.3

0 0 0 0 0 0 0 0 0 0owner

0 0 0 0 0 0 0 0 0 0other

0 0 0 0 0 0 0 0 0 0loser

0 0 0 0 0 0 0 0 0 0winner

1 2 3 4 5 6 7 8 9 10

1

1

1 1 1 1

process 4 runs alone …

process 4 is the winner

process 3 runs ...

1

1

process 3 lost

process 6 runs ...

1

1

1 1 1 1

process 3 lost

This is just one game, we need infinitely many …

Page 82: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

83

Automatic Discovery of AlgorithmsAutomatic Discovery of Algorithms

How do we invent algorithms?

Section 2.6

eureka

This is one way

See next slide for another way ...(maybe they are the same ...)

Page 83: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

84

Automatic DiscoveryAutomatic Discovery

Algorithm Generator

Algorithm Generator

Algorithm Verifier

Algorithm Verifier

algorithmscorrectalgorithms

Page 84: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

85

System OverviewSystem Overview

Algorithm Generator

Algorithm Generator

Algorithm Verifier

Algorithm Verifier

User InterfaceUser Interface

algorithms

verificationresults

correctalgorithms

parameters(# of: processes, bits, lines of code)

Page 85: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

86

ResultsResults

Sharedbits

223344456

Entry

674645655

Exit

111111111

Complexconditions

Yes

Yes

Yes

Starvationfreedom

YesYesYes

Yes

Tested algorithms

7,196,536,269846,712,05925,221,389

1,838,128,995129,542,873129,190,403

*900,000,000*22,000,000*70,000,000

Correct alg.

066

10510

4805680

10696

appx. runninghours

216390.44711

120.4

1

User-defined parameters Results

* This run was stopped after few solutions were found. Not all algorithms were tested.

Page 86: 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld

Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007

87

Next ChapterNext Chapter

In Chapter 3 we will consider more advanced solutions to the mutual exclusion problem using atomic registers.

-- Gadi