Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld Chapter 2 Mutual Exclusion using atomic registers Basic Topics Version June 2014 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld 2014 ID: 809958
Download The PPT/PDF document "Chapter 2 Synchronization Algorithms and..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Synchronization Algorithms and Concurrent Programming
Gadi Taubenfeld
Chapter 2 Mutual Exclusion using atomic registers: Basic Topics
Version:
June 2014
Slide2Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
A note on the use of these power-point 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 2014Gadi Taubenfeld, All Rights Reserved
Synchronization Algorithms and Concurrent Programming
ISBN: 0131972596, 1
st
edition
To get the most updated version of these slides go to:
http://www.faculty.idc.ac.il/gadi/book.htm
Slide3Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
2.1
Algorithms for Two Processes2.2 Tournament Algorithms2.3 A Fast Mutual Exclusion Algorithms2.4 Starvation-free Algorithms2.5 Tight Space bounds
2.6 Automatic Discovery of algorithms Chapter 2 Mutual Exclusion using atomic registers: Basic Topics
Slide4The Mutual Exclusion Problem
Basic definitions
Chapter 1
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Slide5Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
remainder code
entry code
critical sectionexit 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 problem
Slide6Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
The 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.
entry code
exit code
critical
section
remainder
Slide7Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Question: true or false ?
entry code of A
exit code of A
critical
section
remainder
entry code of B
exit code of B
Algorithm C
entry code
exit code
critical
section
remainder
Algorithm A
Algorithm B
Slide8Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
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.
entry code of A
exit code of A
critical
section
remainder
entry code of B
exit code of B
Algorithm C
Slide9Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Does it work?
Atomic read/write registers
Proposed solution 1Thread 0
while (true} {remainder code
while (turn = 1) {
skip
}
critical section
turn = 1
}
Thread 1
while (true} {
remainder code
while
(turn = 0)
{
skip
}
critical section
turn = 0
}
entry
code
exit
code
0/1
turn
mutual exclusion
deadlock-freedom
Slide10Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Convention
Thread 0
while (true} {remainder code
}
Thread 1
while (true} {
remainder code
}
entry code
exit code
critical section
entry code
exit code
critical section
Slide11Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Convention
Thread 0
Thread 1
entry code
exit code
critical section
entry code
exit code
critical section
Slide12Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Proposed solution 2
Thread 0
flag[0] = truewhile (flag[1])
{skip}critical section
flag[0] = false
Thread 1
flag[1] = true
while
(flag[0])
{
skip
}
critical section
flag[1] = false
false
flag
false
0
1
Does it work?
mutual exclusion
deadlock-freedom
Slide13Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Proposed solution 3
Thread 0
while (flag[1]) {
skip}flag[0] = true
critical sectionflag[0] = false
Thread 1
while
(flag[0])
{
skip
}
flag[1] = true
critical section
flag[1] = false
false
flag
false
0
1
Does it work?
mutual exclusion
Deadlock-freedom
Slide14Algorithms for Two Processes
Section 2.1
Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Slide15Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Peterson’s algorithm
Thread 0
flag[0] = trueturn = 1
while (flag[1] and turn = 1)
{skip
}
critical section
flag[0] = false
Thread 1
flag[1] = true
turn = 0
while
(flag[0]
and
turn = 0)
{
skip
}
critical section
flag[1] = false
false
flag
false
0
1
0/1
turn
Section 2.1.1
Slide16Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
0/1
0/1
A variant of Peterson’s algorithm
false
flag
false
0
1
turn
Is it correct ?
true
true
1
0
Thread 0
turn = 1
flag[0] = true
while
(flag[1]
and
turn = 1)
{
skip
}
critical section
flag[0] = false
Thread 1
turn = 0
flag[1] = true
while
(flag[0]
and
turn = 0)
{
skip
}
critical section
flag[1] = false
Slide17Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Indicate contending
b[i] := trueBarrier
turn := iContention?b[i] = true ?
critical sectionexit codeb
[i] = false ?
First to cross
the barrier?
turn
=
j
?
yes
yes
no / maybe
no
Schematic for Peterson’s algorithm
Slide18Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Properties of Peterson’s Solution
Satisfies mutual exclusion and starvation-freedomMemory accesses are assumed to be atomicSolution for two processes only
Slide19Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Question
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 registers
Section 2.1.2
Slide20Algorithms for Many Processes
Section 2.2
Chapter 2Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
How can we use two-process algorithm to construct an algorithm for many processes?
Slide21Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Tournament Algorithms
1
2
3
4
5
6
7
8
Section 2.2
Yes
Assume that the base two-process algorithm satisfies starvation-freedom. Does the tournament algorithm also satisfies starvation-freedom ?
Slide22Models, Properties & Complexity
Basic definitions and notations
Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Slide23Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
. . .
M
. . .
M
M
C
. . .
M
C
P
P
P
P
P
P
Shared Memory Models
Coherent Caching
Distributed Shared Memory
Simple Shared Memory
Slide24Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Properties & complexity
Time complexityFastAdaptiveFairnessFIFO, ...Fault-toleranceLocal spinning Space complexityCommunication primitives
Slide25Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Complexity Measures
Counting stepscontention-free time complexityprocess time complexityprocess step complexityCounting time unitssystem response timeprocess response timeCounting communicationsdistributed shared memorycoherent cachingSpace complexity
Slide26Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Thread 0
flag[0] = true
turn = 1while (flag[1] and turn = 1)
{skip}
critical section
flag[0] = false
Thread 1
flag[1] = true
turn = 0
while
(flag[0]
and
turn = 0)
{
skip
}
critical section
flag[1] = false
What is the contention-free time complexity?
4
Contention-free time complexity
(3 in the entry + 1 in the exit)
Slide27Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Process time complexity
time
P1
P2
A single run
Formal:
The max # of (shared memory) steps a winning process need to take in its entry/exit sections since the last time some process released its critical section.
Given an algorithm divide it steps …
A – entry/exit steps;
B – critical section steps;
C – other steps (remainder).
Informal:
The longest
black
interval, counting steps.
Process step complexity:
# of steps, since it has started…
Slide28Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
∞
Thread 0
flag[0] = trueturn = 1while
(flag[1] and turn = 1)
{skip}
critical section
flag[0] = false
Thread 1
flag[1] = true
turn = 0
while
(flag[0]
and
turn = 0)
{
skip
}
critical section
flag[1] = false
What is the process time/step complexity?
Theorem:
There is no two-process mutual exclusion with an upper bound on the process time/step complexity. [1992]
Proof:
see Section 3.2.5 .
Process time complexity
Slide29Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
System response time
time
P1
P2
A single run
one
time
unit
Slide30Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
System response time
time
P1
P2
A single run
Formal:
The longest time interval where some process is in its entry code while no process is in its CS, assuming an
upper bound of one time unit
for step time, and no lower bound.
Given an algorithm divide it steps …
A – entry/exit steps;
B – critical section steps;
C – other steps (remainder).
Informal:
The longest
black
interval, counting time units.
Slide31Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
How many time units
time
P1
P2
A single run
1
2
3
4
5
6
7
Slide32Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
How many time units
time
P1
P2
1
2
6
7
8
9
10
3
4
5
A single run
Slide33Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Show a run of Peterson’s algorithm where the
system response time is 5 time units.
What is the system response time?
1
2
3
4
5
Thread 0
flag[0] = true
turn = 1
while
(flag[1]
and
turn = 1)
{
skip
}
critical section
flag[0] = false
Thread 1
flag[1] = true
turn = 0
while
(flag[0]
and
turn = 0)
{
skip
}
critical section
flag[1] = false
P1
P2
1
2
3
4
5
Slide34A Fast Mutual exclusion Algorithm
Section 2.3
Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Slide35Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
A Fast Mutual exclusion Algorithm
Mutual exclusion and deadlock-freedomStarvation of individual processes is possiblefast accessin the absence of contention, only 7 accesses to shared memory are neededWith 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 unitsn+2 shared registers are used
Slide36Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
At most
n-1 can move rightAt most n-1 can move downAt most 1 can winIn solo run
1 winSplitter
right
win
down
n
1
n-
1
n-
1
right
1
2
3
4
5
x = i
if
y
0
then
go right
fi
y = 1
if
x
i
then
go down
fi
win
x
0
y
Slide37Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Notation
await (condition) == while
(not condition) do skip od
Example:await (x=5) == wait until x=5
Slide38Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Fast Mutual exclusion Algorithm
code 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
false
false
y
x
0
right
down
win
Slide39Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
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
y
x
0
What is the
maximum
number of processes that can be in their CS at the same time?
Slide40Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Fast Mutual exclusion Algorithm
code 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
false
false
y
x
0
Can we replace the order of these two statements ?
No. Mutual exclusion is not satisfied.
Slide41Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
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
Slide42Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
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 ?
Slide43Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
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 ?
Slide44Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Would this work ?
start: b[i] = true x = i
if y 0 and y =i 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
remove
add
and
No. Mutual exclusion is not satisfied.
Slide45Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Indicate contending
b[i] := true
Barrierturn := iContention?x i ?
critical sectionexit code
Last to cross the barrier?y = i
?
yes
yes
Contention?
y
=
0
?
Continue only after it is
guaranteed that no one can
cross the barrier
no
Wait until CS is released
Wait until CS is released
yes
The last to cross
the barrier!
no
no
Schematic for the fast algorithm
Slide46Starvation-free
Algorithms
Section 2.4Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
2.4.1
Basic Definitions (i.e., FIFO) 2.4.2 The Bakery Algorithm2.4.3 The Black-White Bakery Algorithm
Slide47Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
The mutual exclusion problem
entry code
exit code
critical
section
remainder
Mutual Exclusion
Deadlock-freedom
Starvation-freedom
FIFO
doorway
waiting
Slide48Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
time
The 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
4
waiting
entry
remainder
Section 2.4.2
Slide49Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Implementation 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
integer
0
0
0
0
0
0
Answer: No! can deadlock
Slide50Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
time
Implementation 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
entry
remainder
deadlock
Slide51Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Implementation 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
integer
0
0
0
0
0
0
Answer: does not satisfy mutual exclusion
What if we replace
>
with
?
Slide52Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
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
integer
0
0
0
0
0
0
Answer: does not satisfy mutual exclusion
Implementation 2
code of process i , i
{1 ,..., n}
Slide53Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
time
Implementation 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
entry
remainder
1
2
2
0
2
Slide54Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
The Bakery Algorithmcode of process i , i
{1 ,..., n}choosing[i] = tru
enumber[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 section
number[
i
] = 0
1
2
3
4
n
choosing
bits
false
number
integer
0
0
0
0
0
0
false
false
false
false
false
Slide55Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Properties 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.
Slide56Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
time
The 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
4
waiting
entry
remainder
Slide57Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Is the following version correct?
code of process i , i {1 ,..., n}choosing[i] = truenumber[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 section
number[i] = 0
1
2
3
4
n
choosing
bits
false
number
integer
0
0
0
0
0
0
false
false
false
false
false
Slide58Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
time
No
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
entry
remainder
1
2
1
0
1
Slide59Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
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 section
number[i] = 0
1
2
3
4
n
choosing
bits
false
number
integer
0
0
0
0
0
0
false
false
false
false
false
Yes
Can we replace
with
=
?
Slide60Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Computing the Maximum #1
code 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
n
number
integer
0
0
0
0
0
0
number[i] = 1 +
max {
number[j]
|
(1
j n
)
}
Slide61Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Computing the Maximum #2
code 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
n
number
integer
0
0
0
0
0
0
number[i] = 1 +
max {
number[j]
|
(1
j n
)
}
Slide62Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
time
0
0
0
0
doorway
1
2
3
4
5
n
CS
exit
1
1
1
1
1
1
0
1
waiting
entry
remainder
0
0
?
1
1
?
local1
2
1
No
Passed process 1
Waiting for process 2
Slide63Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Computing the Maximum #3
code 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
Slide64Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
The Black-White Bakery Algorithm
Section 2.4.3
Bounding the space of the Bakery Algorithm
Bakery (FIFO, unbounded)
The Black-White Bakery Algorithm
FIFO
Bounded space
+ one bit
Slide65Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
time
The 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
2
waiting
entry
remainder
1
2
0
2
0
1
2
1
1
0
0
color bit
Slide66Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
The Black-White Bakery Algorithm
1
23
4
n
choosing
Data Structures
mycolor
number
color bit
bits
bits
{0,1,...,n}
{black,white}
Slide67Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
The Black-White Bakery Algorithm
code of process i , i
{1 ,..., n}
choosing[i] = truemycolor[i] = color
number[i] = 1 + max{number[j] | (1
j n
)
(mycolor[j] = mycolor[i])
}
choosing[i] = false
for
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 od
critical section
if
mycolor[i] = black
then
color =
white
else
color = black
fi
number[i] = 0
Slide68Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
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?
Question
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.
Slide69Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Does it matter if we change the order of the last two statements (in the exit code) of the Black-White Bakery Algorithm?
Question
Answer: Yes, it matters.
Slide70Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Bakery
Black-White Bakery
Use single-writer bits only!
Question
Replace the multi-writer bit
color
with n single-writer bits.
Answer: See page 57.
Slide71Tight Space Bounds
Section 2.5
Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Slide72Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
A Simple Observation
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)
Slide73Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Tight Space Bounds
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)
Slide74Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Definitions
run – a sequence of eventsrun x looks like run y to process pprocess p is hidden in run xprocess p covers register r in run x
Slide75Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Example
run x looks like run y to process p
run xp reads 5 from rq writes 6 to rp writes 7 to rq writes 8 to rp reads 8 from r
run yp reads 5 from r
p writes 7 to rq writes 6 to rq reads 6 from r
q writes 8 to r
p reads 8 from r
8
r
8
r
q write 6 to r1
6
The values of the shared registers must also be the same
Slide76Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Example
p reads 5 from rq reads 5 from r
p writes 7 to rq writes 8 to rp reads 8 from rq 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
Slide77Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Example
p reads 5 from rp reads 5 from r
q reads 5 from rp writes 7 to rq writes 8 to rp reads 8 from rp 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
Slide78Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Lemma 2.17
(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) …
Slide79Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Lemma 2.18
(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 …
Slide80Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Lemma 2.19
x
all the processes are hidden
all processes, except maybe 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 ...
Slide81Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Lemma 2.19: Example
w reads 5
p reads 5q reads 5p writes 7q writes 8q reads 8w writes 3w reads 3
p write 9p in its critical sectionp exits
5
r
w is hidden
remove w
w 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 ...
Slide82Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Lemma 2.20
(simple)
xall the processes are hidden
only p takes steps
z
p covers unique register
Then, for every
p there exists z
Slide83Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Lemma 2.20: Proof
x
all the processes are hidden
all processes, except maybe 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 …
Slide84Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Lemma 2.21
(main lemma)
xall the processes are in their remainder
z
only P take steps
P are hidden and cover |P| distinct registers
Then, for every set of
processes P there exists z
Proof: By induction on k the size of P.
Slide85Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Lemma 2.21
(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 registers
Slide86Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Lemma 2.21
x
y
1
W
1
...
...
...
y
2
y
i
y
j
W
2
W
i
W
j
only P take steps
P are hidden and cover |P| distinct registers
By the induction hypothesis:
z
1
w
1
z
2
z
i
z
j
w
2
w
i
w
j
...
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
w
1
W
1
(Lemma 2.20)
P are hidden? Yes
P cover
W
1
? Yes
p covers
w
1
? Yes
p is hidden?
Maybe
P are hidden? Yes
P cover
W
j
? Yes
p covers
w
j
? Yes
p is hidden?
Yes!
Slide87Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Question
p reads 5 from rq reads 5 to r
p writes 7 to rq writes 8 to rp reads 8 from rp writes 8 to r
8
r
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!
Slide88Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Tight Space Bounds
Theorem: There is a deadlock-free mutual exclusion algorithm for n processes that uses n shared bits.
Section 2.5.2
An Upper Bound
Slide89Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
The One-Bit Algorithmcode of process i , i
{1 ,..., n}1
2
34
n
b
bits
false
false
false
false
false
false
true
true
false
false
true
true
false
critical section
false
p
3
writes true
p
3
writes false
p
n
writes true
p
n
writes false
Slide90Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
The 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 od
critical section
b[i] = false
1
2
3
4
n
b
bits
false
false
false
false
false
false
Slide91Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Does the correctness of the One-bit algorithm depend on the order in which the bits are read within the loops ?
Question
Slide92Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Properties 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?
Slide93Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Question
Does it follow from the lower bound that there is no solution, using atomic registers, when the number of processes is not known in advance or when the number is infinite ?
Computing with Infinitely Many ProcessSection 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.
Slide94Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Computing with Infinitely Many Process: The algorithm
Section 2.5.3
0
0
0
0
0
0
0
0
0
0
owner
0
0
0
0
0
0
0
0
0
0
other
0
0
0
0
0
0
0
0
0
0
loser
0
0
0
0
0
0
0
0
0
0
winner
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 …
…
…
…
…
Slide95Automatic Discovery of Algorithms
Section 2.6
Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Slide96Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Automatic 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 ...)
Slide97Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Automatic Discovery
Algorithm GeneratorAlgorithm Verifier
algorithms
correctalgorithms
Slide98Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
System Overview
Algorithm GeneratorAlgorithm Verifier
User Interface
algorithms
verification
results
correct
algorithms
parameters
(# of: processes, bits, lines of code)
Slide99Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Results
Shared
bits
2
23344456
Entry
6
7
4
6
4
5
6
5
5
Exit
1
1
1
1
1
1
1
1
1
Complex
conditions
Yes
Yes
Yes
Starvation
freedom
YesYesYesYes
Tested algorithms 7,196,536,269846,712,05925,221,3891,838,128,995129,542,873129,190,403*900,000,000*22,000,000*70,000,000
Correct
alg.
0
66
105
10
480
56
80
106
96
appx.
running
hours
216
39
0.4
47
1
1
12
0.4
1
User-defined parameters Results
*
This run was stopped after few solutions were found. Not all algorithms were tested.
Slide100Chapter 2
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014
Next Chapter
In Chapter 3 we will consider more advanced solutions to the mutual exclusion problem using atomic registers. -- Gadi