replication and consistency - tu kaiserslautern · the art of multiprocessor programming by maurice...

78
Replication and Consistency 02 Mutual Exclusion Annette Bieniusa AG Softech FB Informatik TU Kaiserslautern Winter Term 2019 Annette Bieniusa Replication and Consistency Winter Term 2019 1/ 62

Upload: others

Post on 12-Apr-2020

34 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Replication and Consistency02 Mutual Exclusion

Annette Bieniusa

AG SoftechFB Informatik

TU Kaiserslautern

Winter Term 2019Annette Bieniusa Replication and Consistency Winter Term 2019 1/ 62

Page 2: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Thank you!

These slides are based on companion material of the following books:

The Art of Multiprocessor Programming by Maurice Herlihyand Nir ShavitSynchronization Algorithms and Concurrent Programmingby Gadi Taubenfeld

Annette Bieniusa Replication and Consistency Winter Term 2019 2/ 62

Page 3: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Goals of this lecture

Formalize our understanding of mutual exclusionDiscuss protocols for 2 threads and extensions for N threads

FairnessInherent costs

Learn how to argue about and prove various properties in anasynchronous concurrent setting

Annette Bieniusa Replication and Consistency Winter Term 2019 3/ 62

Page 4: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

The History of the Mutual Exclusion ProblemFirst solution by DekkerFischer, Knuth, Lynch, Rabin, Rivest, . . .1974 Bakery algorithm by Lamport1981 Peterson’s algorithmHundreds of published solutions - not all correct!

Quelle: Wikipedia

In his 1965 paper E. W. Dijkstra wrote:Given in this paper is a solution to a problemwhich, to the knowledge of the author, has beenan open question since at least 1962, irrespectiveof the solvability. [. . . ] Although the setting ofthe problem might seem somewhat academicat first, the author trusts that anyone familiarwith the logical problems that arise in computercoupling will appreciate the significance of thefact that this problem indeed can be solved.

Annette Bieniusa Replication and Consistency Winter Term 2019 4/ 62

Page 5: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Warning!

You will never use these protocols!Get over it . . .

You are advised to understand themThe same issues show up everywhereExcept they will be hidden and more complex

Annette Bieniusa Replication and Consistency Winter Term 2019 5/ 62

Page 6: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Preliminaries

Annette Bieniusa Replication and Consistency Winter Term 2019 6/ 62

Page 7: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Time

Absolute, true and mathematical time, of itself and fromits own nature, flows equably without relation to anythingexternal.” (Isaac Newton, 1689)

Time is, like, Nature’s way of making sure that everythingdoesn’t happen all at once.” (Anonymous, circa 1968)

Annette Bieniusa Replication and Consistency Winter Term 2019 7/ 62

Page 8: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Formal system model

DefinitionA thread A is a sequence a0, a1, . . . of events.

A a0 a1 a2 . . . an. . .

“Trace” modelAn event a0 of thread A is

instantaneousat a unique point in time (no simultaneous events!)

Notation: a0 → a1 indicates order

Annette Bieniusa Replication and Consistency Winter Term 2019 8/ 62

Page 9: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Threads are State Machines

Thread State: Program counter + local variablesSystem state: Thread states + shared variablesEvents are state transitions

Assign value to shared variableAssign value to local variableRead value from shared/local variableInvoke methodReturn from method etc.

Annette Bieniusa Replication and Consistency Winter Term 2019 9/ 62

Page 10: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Modelling Concurrency via Interleaving

A a0 a1 a2 . . .

B b0 b1 b2. . .

A + B a0 a1 a2b0 b1 b2. . .

Annette Bieniusa Replication and Consistency Winter Term 2019 10/ 62

Page 11: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Modelling Concurrency via Interleaving

A a0 a1 a2 . . .

B b0 b1 b2. . .

A + B a0 a1 a2b0 b1 b2. . .

Annette Bieniusa Replication and Consistency Winter Term 2019 10/ 62

Page 12: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Intervals

An interval A0 = (a0, a1) is the time between events a0 and a1.

A a0 a1

A0

TaskGive definitions and examples of

Overlapping intervalsDisjoint intervals

Annette Bieniusa Replication and Consistency Winter Term 2019 11/ 62

Page 13: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Intervals

An interval A0 = (a0, a1) is the time between events a0 and a1.

A a0 a1

A0

TaskGive definitions and examples of

Overlapping intervalsDisjoint intervals

Annette Bieniusa Replication and Consistency Winter Term 2019 11/ 62

Page 14: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Precedence

DefinitionInterval Ai precedes (happens before) interval Bj (Ai → Bj) if endevent of Ai is before start event of Bj .

QuestionPrecedence defines a partial order on intervals

Irreflexive: Never true that Ai → Ai

Antisymmetric: If Ai → Bj , then not true that Bj → Ai

Transitive: If Ai → Bj and Bj → Ck, then Ai → Ck

Why is precedence not a total order?

Annette Bieniusa Replication and Consistency Winter Term 2019 12/ 62

Page 15: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Repeated Events

while (...) {a0; a1;

}

ak0 denotes k-th occurrence of event a0, etc.

Ak0 denotes k-th occurrence of interval A0

Annette Bieniusa Replication and Consistency Winter Term 2019 13/ 62

Page 16: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Mutual exclusion

Annette Bieniusa Replication and Consistency Winter Term 2019 14/ 62

Page 17: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

The problemWant to guarantee mutually exclusive access to some sharedresource for several competing processesAvoid race conditions, i.e. flaws that occur when the timing orordering of events affects a program’s correctness

remainder code

entry code / lock

critical section

exit code / unlock

Annette Bieniusa Replication and Consistency Winter Term 2019 15/ 62

Page 18: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Formal propertiesMutual ExclusionCritical sections of different threads do not overlap.For threads A and B and integers j and k, either CSk

A → CSjB or

CSjB → CSk

A.

Deadlock FreedomIf some thread is trying to enter its critical section, then some thread(not necessarily the same one!) eventually enters its critical section.

Starvation FreedomIf a thread is trying to enter its critical section, then this thread musteventually enter its critical section.

Annette Bieniusa Replication and Consistency Winter Term 2019 16/ 62

Page 19: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Question

Which statement is correct?

Deadlock freedom implies starvation freedom.Starvation freedom implies deadlock freedom.

Annette Bieniusa Replication and Consistency Winter Term 2019 17/ 62

Page 20: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Assumptions

The remainder code does not influence the behavior of otherthreads.Shared objects used in entry/exit code may not be referred to inremainder code or critical section.Process cannot fail (i.e. stop) while in entry code, critical sectionor exit code.Process executes critical section and exit code in a finite numberof steps.

Annette Bieniusa Replication and Consistency Winter Term 2019 18/ 62

Page 21: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

QuestionThe following control flow graph sketches some algorithm C thatemploys algorithms A and B.

remaindercode (of C)

entry code of A

entry code of B

critical section

exit code of B

exit code of A

1 If both A and B are deadlock-free, then C is deadlock-free.2 If both A and B are starvation-free, then C is starvation-free.3 If either A or B satisfy mutual exclusion, then C satisfy mutual exclusion.4 If A is deadlock-free and B is starvation-free, then C is starvation-free.5 If A is starvation-free and B is deadlock-free, then C is starvation-free.Annette Bieniusa Replication and Consistency Winter Term 2019 19/ 62

Page 22: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Protocols for Mutual Exclusion

Annette Bieniusa Replication and Consistency Winter Term 2019 20/ 62

Page 23: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Two-Thread vs n-Thread Solutions

First: Two-thread solutionsIllustrate most basic ideasAlgorithms fit on one slide

Then: n-Thread solutions

Annette Bieniusa Replication and Consistency Winter Term 2019 21/ 62

Page 24: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Protocol LockOneInitially: turn = 0

Thread 0:while (true) {

remainder codeturn = 0while (turn == 1)

{skip;}critical section

}

Thread 1:while (true) {

remainder codeturn = 1while (turn == 0)

{skip;}critical section

}

QuestionDoes it solve the mutual exclusion problem?

Under sequential execution, threads cannot proceed.⇒ Mutual exclusion, but not deadlock-freedom

Annette Bieniusa Replication and Consistency Winter Term 2019 22/ 62

Page 25: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Protocol LockOneInitially: turn = 0

Thread 0:while (true) {

remainder codeturn = 0while (turn == 1)

{skip;}critical section

}

Thread 1:while (true) {

remainder codeturn = 1while (turn == 0)

{skip;}critical section

}

QuestionDoes it solve the mutual exclusion problem?Under sequential execution, threads cannot proceed.⇒ Mutual exclusion, but not deadlock-freedom

Annette Bieniusa Replication and Consistency Winter Term 2019 22/ 62

Page 26: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Convention

Initially: ...

Thread 0:while (true) {

remainder codeentry codecritical sectionexit code

}

Thread 1:while (true) {

remainder codeentry codecritical sectionexit code

}

Annette Bieniusa Replication and Consistency Winter Term 2019 23/ 62

Page 27: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Convention

Initially: ...

Thread 0:

entry codecritical sectionexit code

Thread 1:

entry codecritical sectionexit code

Annette Bieniusa Replication and Consistency Winter Term 2019 24/ 62

Page 28: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Protocol LockTwoInitially: flag[0] = flag[1] = false

Thread 0:while (true) {

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

}

Thread 1:while (true) {

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

}

QuestionDoes it solve the mutual exclusion problem?

If each thread sets its flag to true and waits for the other, they willwait forever.⇒ Mutual exclusion, but not deadlock-freedom

Annette Bieniusa Replication and Consistency Winter Term 2019 25/ 62

Page 29: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Protocol LockTwoInitially: flag[0] = flag[1] = false

Thread 0:while (true) {

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

}

Thread 1:while (true) {

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

}

QuestionDoes it solve the mutual exclusion problem?If each thread sets its flag to true and waits for the other, they willwait forever.⇒ Mutual exclusion, but not deadlock-freedom

Annette Bieniusa Replication and Consistency Winter Term 2019 25/ 62

Page 30: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Protocol LockThree

Initially: flag[0] = flag[1] = false

Thread 0:while (flag[1]) {skip;}flag[0] = truecritical sectionflag[0] = false

Thread 1:while (flag[0]) {skip;}flag[1] = truecritical sectionflag[1] = false

QuestionDoes it solve the mutual exclusion problem?

If each thread pass the while-loop at the same time and set their flagto true, they both enter the critical section.⇒ Deadlock-freedom, but no mutual-exclusion

Annette Bieniusa Replication and Consistency Winter Term 2019 26/ 62

Page 31: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Protocol LockThree

Initially: flag[0] = flag[1] = false

Thread 0:while (flag[1]) {skip;}flag[0] = truecritical sectionflag[0] = false

Thread 1:while (flag[0]) {skip;}flag[1] = truecritical sectionflag[1] = false

QuestionDoes it solve the mutual exclusion problem?If each thread pass the while-loop at the same time and set their flagto true, they both enter the critical section.⇒ Deadlock-freedom, but no mutual-exclusion

Annette Bieniusa Replication and Consistency Winter Term 2019 26/ 62

Page 32: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Peterson’s Algorithm

Initially: flag[0] = flag[1] = false, turn = 0

Thread 0:flag[0] = trueturn = 1

while (flag[1] && turn == 1) {skip;

}critical section

flag[0] = false

Thread 1:flag[1] = trueturn = 0

while (flag[0] && turn == 0) {skip;

}critical sectionflag[1] = false

Annette Bieniusa Replication and Consistency Winter Term 2019 27/ 62

Page 33: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

In detail// Announce interestflag[i] = true

// Defer to the otherturn = j

// Wait while other is interested and not my turnwhile (flag[j] && turn == j) {skip;}

critical section

// no longer interestedflag[i] = false

Does it matter if we replace the order of line 1 and 2?

Does not satisfy mutual exclusion anymore!

Annette Bieniusa Replication and Consistency Winter Term 2019 28/ 62

Page 34: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

In detail// Announce interestflag[i] = true

// Defer to the otherturn = j

// Wait while other is interested and not my turnwhile (flag[j] && turn == j) {skip;}

critical section

// no longer interestedflag[i] = false

Does it matter if we replace the order of line 1 and 2?

Does not satisfy mutual exclusion anymore!

Annette Bieniusa Replication and Consistency Winter Term 2019 28/ 62

Page 35: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

In detail// Announce interestflag[i] = true

// Defer to the otherturn = j

// Wait while other is interested and not my turnwhile (flag[j] && turn == j) {skip;}

critical section

// no longer interestedflag[i] = false

Does it matter if we replace the order of line 1 and 2?

Does not satisfy mutual exclusion anymore!

Annette Bieniusa Replication and Consistency Winter Term 2019 28/ 62

Page 36: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Proof Idea: Mutual Exclusion

If thread 0 in critical section: flag[0] = true, turn = 0

If thread 1 in critical section: flag[1] = true, turn = 1

⇒ Cannot both be true

Annette Bieniusa Replication and Consistency Winter Term 2019 29/ 62

Page 37: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Proof Idea: Mutual Exclusion

If thread 0 in critical section: flag[0] = true, turn = 0

If thread 1 in critical section: flag[1] = true, turn = 1

⇒ Cannot both be true

Annette Bieniusa Replication and Consistency Winter Term 2019 29/ 62

Page 38: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Proof Idea: Deadlock Freedom

In entry code for thread j:while (flag[i] && turn == i) {};

Thread blocked

only at while looponly if it is not its turn

⇒ Only one thread will have its value in turn!

Annette Bieniusa Replication and Consistency Winter Term 2019 30/ 62

Page 39: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Proof Idea: Starvation Freedom

Thread i blocked only if j repeatedly re-enters so thatflag[j] && turn == j

When thread j re-enters (i.e. calls again the entry code), it setsturn to i.Therefore, i eventually gets in.

Annette Bieniusa Replication and Consistency Winter Term 2019 31/ 62

Page 40: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Extension for N-Threads: Tournament Algorithms

0 1 2 3 4 5 6 7

0 2 1 3

0 1

0

The winner

processes

level 0

level 1

level 2

Annette Bieniusa Replication and Consistency Winter Term 2019 32/ 62

Page 41: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Properties

For Tournament Algorithm based on Peterson’s Algorithm:

Satisfies mutual exclusion and starvation freedomContention-free time complexity is 4 log n accesses to sharedmemoryUses 3(n− 1) shared registers, three for each node (= lock)One process can enter its critical section arbitrarily many timesahead of another slower process from a different subtree

⇒ Want stronger fairness guarantee!

Annette Bieniusa Replication and Consistency Winter Term 2019 33/ 62

Page 42: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Bounded Waiting

Divide entry code into two parts:Doorway interval DA

Always finished in finite stepsWaiting interval WA

May take unbounded number of steps

Annette Bieniusa Replication and Consistency Winter Term 2019 34/ 62

Page 43: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

r-Bounded Waiting

For threads A and B:

If DkA → Dj

B, then CSkA → CSj+r

B

B cannot overtake A by more than r timesFirst-come-first-served (FIFO) means r = 0

For Tournament Algorithm from before:

No one starvesBut very weak fairness: Not r-bounded for any r!That is pretty lame. . .

Annette Bieniusa Replication and Consistency Winter Term 2019 35/ 62

Page 44: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

r-Bounded Waiting

For threads A and B:

If DkA → Dj

B, then CSkA → CSj+r

B

B cannot overtake A by more than r timesFirst-come-first-served (FIFO) means r = 0

For Tournament Algorithm from before:

No one starvesBut very weak fairness: Not r-bounded for any r!That is pretty lame. . .

Annette Bieniusa Replication and Consistency Winter Term 2019 35/ 62

Page 45: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Bakery Algorithm

Provides FIFOIdea:

Take a numberWait until lower numbers have been served

For symmetry breaking, we use lexicographic order on tuples:

(a, i) < (b, j) if a < b or a = b and i < j

Annette Bieniusa Replication and Consistency Winter Term 2019 36/ 62

Page 46: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Bakery Algorithm

Initially: For all i = 1,. . . ,n: number[i] = 0, choosing[i] = false

choosing[i] = truenumber[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

Annette Bieniusa Replication and Consistency Winter Term 2019 37/ 62

Page 47: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Computing the Maximum

local1 = 0for local2 = 1 to n do

local3 = number[local2]if local1 < local3 then local1 = local3

number[i] = 1 + local1

QuestionIs this version also correct?local1 = ifor local2 = 1 to n do

if number[local1] < number[local2] then local1 = local2number[i] = 1 + number[local1]

Annette Bieniusa Replication and Consistency Winter Term 2019 38/ 62

Page 48: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Computing the Maximum

local1 = 0for local2 = 1 to n do

local3 = number[local2]if local1 < local3 then local1 = local3

number[i] = 1 + local1

QuestionIs this version also correct?local1 = ifor local2 = 1 to n do

if number[local1] < number[local2] then local1 = local2number[i] = 1 + number[local1]

Annette Bieniusa Replication and Consistency Winter Term 2019 38/ 62

Page 49: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Properties of the Bakery Algorithm

Satisfies mutual exclusion and FIFOWorks with safe registers: Reads which are concurrent withwrites may return arbitrary value

Annette Bieniusa Replication and Consistency Winter Term 2019 39/ 62

Page 50: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Proof idea: FIFO

If DA → DB, then A’s number is smaller than B’swriteA(number[A]) → readB(number[A]) → writeB(number[B])→ readB(choosing[A])So B is locked out while choosing[A] is true

Annette Bieniusa Replication and Consistency Winter Term 2019 40/ 62

Page 51: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Question

The Bakery Algorithm is succinct, elegant, and fair.

So why isn’t it practical?

The size of number[i] is unboundedBut variants with bounded space exist where numbers are re-used

Well, you have to read N distinct variables

Annette Bieniusa Replication and Consistency Winter Term 2019 41/ 62

Page 52: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Question

The Bakery Algorithm is succinct, elegant, and fair.

So why isn’t it practical?

The size of number[i] is unboundedBut variants with bounded space exist where numbers are re-used

Well, you have to read N distinct variables

Annette Bieniusa Replication and Consistency Winter Term 2019 41/ 62

Page 53: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Classification of registers

Shared read/write memory locations called registers (historicalreasons)Different flavors

SRSW = Single-Reader-Single-WriterMRSW = Multi-Reader-Single-Writer (like flag[])MRMW = Multi-Reader-Multi-Writer (like number[] or turn)[Not that interesting: SRMW]

Annette Bieniusa Replication and Consistency Winter Term 2019 42/ 62

Page 54: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Observation

Any deadlock-free mutual exclusion algorithm for N threads using onlySWMR registers must use at least N such registers.

Proof: Before entering its critical section a thread must write at leastonce. . . .

Annette Bieniusa Replication and Consistency Winter Term 2019 43/ 62

Page 55: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Observation

Any deadlock-free mutual exclusion algorithm for N threads using onlySWMR registers must use at least N such registers.

Proof: Before entering its critical section a thread must write at leastonce. . . .

Annette Bieniusa Replication and Consistency Winter Term 2019 43/ 62

Page 56: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Can we do better using MWMR registers ?

Annette Bieniusa Replication and Consistency Winter Term 2019 44/ 62

Page 57: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Theorem (Lower Bound)

Any deadlock-free mutual exclusion algorithm for N threads must useat least N shared (MWMR) registers.

Proof: Tricky!(Burns and Lynch 1993)

⇒ Let’s have a look at the case for two threads!

Annette Bieniusa Replication and Consistency Winter Term 2019 45/ 62

Page 58: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Proving Algorithmic Impossibility

To show no algorithm exists:

Assume by way of contradiction one does existShow a bad execution that violates assumed properties

In our case, assume an algorithm for deadlock-free mutual exclusionusing < N registers and show how several threads can reach the CS atthe same time.

Annette Bieniusa Replication and Consistency Winter Term 2019 46/ 62

Page 59: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Theorem (Lower Bound) for Two Threads

Any deadlock-free mutual exclusion algorithm for 2 threads must use atleast 2 shared MWMR registers.

Proof: Assume one register suffices and derive a contradiction

Annette Bieniusa Replication and Consistency Winter Term 2019 47/ 62

Page 60: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Theorem (Lower Bound) for Two Threads

Any deadlock-free mutual exclusion algorithm for 2 threads must use atleast 2 shared MWMR registers.

Proof: Assume one register suffices and derive a contradiction

Annette Bieniusa Replication and Consistency Winter Term 2019 47/ 62

Page 61: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Proof (1): Two-thread executions

Threads run, reading and writing register RDeadlock-freedom ⇒ at least one thread gets in

Annette Bieniusa Replication and Consistency Winter Term 2019 48/ 62

Page 62: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Proof (2): Covering State for One Register Always Exists

In any protocol, B has to write to R before entering CSStop it just before

Annette Bieniusa Replication and Consistency Winter Term 2019 49/ 62

Page 63: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Proof (3): While B is covering R

A runs, possibly writes to R and enters CS

Annette Bieniusa Replication and Consistency Winter Term 2019 50/ 62

Page 64: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Proof (4): Now B (over)writes

B Runs, first obliterating any trace of A, then also enters CS

⇒ Mutual exclusion violated!

Annette Bieniusa Replication and Consistency Winter Term 2019 51/ 62

Page 65: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Proof (4): Now B (over)writes

B Runs, first obliterating any trace of A, then also enters CS

⇒ Mutual exclusion violated!

Annette Bieniusa Replication and Consistency Winter Term 2019 51/ 62

Page 66: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Theorem (Lower Bound) for Three Threads

Any deadlock-free mutual exclusion algorithm for 3 threads must use atleast 3 shared MWMR registers.

Annette Bieniusa Replication and Consistency Winter Term 2019 52/ 62

Page 67: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Proof (1)

Assume covering state for 2 threads

Annette Bieniusa Replication and Consistency Winter Term 2019 53/ 62

Page 68: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Proof (2)

Now A runs, write to one or both registers, enters CS

Annette Bieniusa Replication and Consistency Winter Term 2019 54/ 62

Page 69: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Proof (3)

Other threads obliterate evidence that A entered CS

Annette Bieniusa Replication and Consistency Winter Term 2019 55/ 62

Page 70: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Proof (4)

Other thread gets in because situation cannot be distinguished

Annette Bieniusa Replication and Consistency Winter Term 2019 56/ 62

Page 71: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Proof (5): How do we reach this covering state?

Start in covering state of B for register RB

If we run B through CS 3 times, B must return twice to coversome register → RB

Pigeon-hole principleRun system until A is about to write to (uncovered) RA

Must exist!

Annette Bieniusa Replication and Consistency Winter Term 2019 57/ 62

Page 72: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Proof (6): Are we done?

No! A could have written to RB

So, CS no longer looks emptyObservable by thread C

Annette Bieniusa Replication and Consistency Winter Term 2019 58/ 62

Page 73: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Proof (6): Are we done?

No! A could have written to RB

So, CS no longer looks emptyObservable by thread C

Annette Bieniusa Replication and Consistency Winter Term 2019 58/ 62

Page 74: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Proof (7): One more round

Run B to obliterate traces of A in RB

Run B again till it is about to write to RB

Now we are done

Annette Bieniusa Replication and Consistency Winter Term 2019 59/ 62

Page 75: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

From 3 to N threads

Proof by inductionThere is a covering state where k threads not in CS cover kdistinct registersProof follows when k = N − 1

Annette Bieniusa Replication and Consistency Winter Term 2019 60/ 62

Page 76: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Summary

In the 1960’s many incorrect solutions to starvation-free mutualexclusion using RW-registers were published . . .Today, we know how to solve FIFO with N -thread mutualexclusion using 2N RW-Registers

N RW-Registers inefficientReason: Writes “cover” older writes

Need stronger hardware operations that do not have the “coveringproblem”Following lectures: Understand what these operations are!

Annette Bieniusa Replication and Consistency Winter Term 2019 61/ 62

Page 77: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Summary

In the 1960’s many incorrect solutions to starvation-free mutualexclusion using RW-registers were published . . .Today, we know how to solve FIFO with N -thread mutualexclusion using 2N RW-RegistersN RW-Registers inefficient

Reason: Writes “cover” older writesNeed stronger hardware operations that do not have the “coveringproblem”Following lectures: Understand what these operations are!

Annette Bieniusa Replication and Consistency Winter Term 2019 61/ 62

Page 78: Replication and Consistency - TU Kaiserslautern · The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit Synchronization Algorithms and Concurrent Programming by

Copyright

This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. You are free:

to Share — to copy, distribute and transmit the workto Remix — to adapt the work

Under the following conditions:

Attribution. You must attribute the work to “The Art of Multiprocessor Programming” and “SynchronizationAlgorithms and Concurrent Programming” (but not in any way that suggests that the authors endorse you oryour use of the work).Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only underthe same, similar or a compatible license.

For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this iswith a link to http://creativecommons.org/licenses/by-sa/3.0/. Any of the above conditions can be waived if you getpermission from the copyright holder. Nothing in this license impairs or restricts the author’s moral rights.

Burns, James E., and Nancy A. Lynch. 1993. “Bounds on Shared Memory for Mutual Exclusion.” Inf. Comput. 107 (2):171–84. https://doi.org/10.1006/inco.1993.1065.

Annette Bieniusa Replication and Consistency Winter Term 2019 62/ 62