Professional Documents
Culture Documents
Chapter2 Mutex BasicTopics
Chapter2 Mutex BasicTopics
Chapter2 Mutex BasicTopics
q That you mention their source, after all, I would like people to use my book!
q That you note that they are adapted from (or perhaps identical to)
my slides, and note my copyright of this material.
remainder code
entry code
critical section
exit 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.
remainder
q Deadlock-freedom: If a process is trying
to enter its critical section, then some
process, not necessarily the same one, entry code
eventually enters its critical section.
critical
q Starvation-freedom: If a process is section
trying to enter its critical section, then
this process must eventually enter its
exit code
critical section.
entry code of A
Thread 0 Thread 1
while (true} { while (true} {
remainder code remainder code
entry
code while (turn = 1) {skip} while (turn = 0) {skip}
critical section critical section
exit
code turn = 1 turn = 0
} }
turn
ümutual exclusion
0/1 C deadlock-freedom
Does it work?
Chapter 2 Synchronization Algorithms and Concurrent Programming 9
Gadi Taubenfeld © 2014
Convention
Thread 0 Thread 1
while (true} { while (true} {
remainder code remainder code
entry code entry code
critical section critical section
exit code exit code
} }
Thread 0 Thread 1
Thread 0 Thread 1
flag[0] = true flag[1] = true
while (flag[1]) {skip} while (flag[0]) {skip}
critical section critical section
flag[0] = false flag[1] = false
flag
0 false
ümutual exclusion
1 false C deadlock-freedom
Does it work?
Chapter 2 Synchronization Algorithms and Concurrent Programming 12
Gadi Taubenfeld © 2014
Proposed solution 3
Thread 0 Thread 1
while (flag[1]) {skip} while (flag[0]) {skip}
flag[0] = true flag[1] = true
critical section critical section
flag[0] = false flag[1] = false
flag
0 false C mutual exclusion
1 false üDeadlock-freedom
Does it work?
Peterson’s algorithm
Thread 0 Thread 1
flag[0] = true flag[1] = true
turn = 1 turn = 0
while (flag[1] and turn = 1) while (flag[0] and turn = 0)
{skip} {skip}
critical section critical section
flag[0] = false flag[1] = false
flag
0 false
1 false
turn 0/1
flag
0 false
true
1 false
true
turn 0/1
0
1
Indicate contending
b[i] := true
Barrier
turn := i
no / maybe
exit code
b[i] = false ?
Question
Using only single-writer registers
Tournament Algorithms
Assume that the base two-
Yes
process algorithm satisfies
starvation-freedom. Does the
tournament algorithm also
satisfies starvation-freedom ?
1 2 3 4 5 6 7 8
C C M M
M M
q Time complexity
m Fast
m Adaptive
q Fairness
m FIFO, ...
q Fault-tolerance
q Local spinning
q Space complexity
q Communication primitives
q Counting steps
m contention-free time complexity
m process time complexity
m process step complexity
q Counting time units
m system response time
m process response time
q Counting communications
m distributed shared memory
m coherent caching
q Space complexity
Thread 0 Thread 1
flag[0] = true flag[1] = true
turn = 1 turn = 0
while (flag[1] and turn = 1) while (flag[0] and turn = 0)
{skip} {skip}
critical section critical section
flag[0] = false flag[1] = false
Chapter 2 A single
Synchronization Algorithms and Concurrent Programming run 27
Gadi Taubenfeld © 2014
Process time complexity
What is the process time/step complexity?
Thread 0 Thread 1
flag[0] = true flag[1] = true
turn = 1 turn = 0
while (flag[1] and turn = 1) while (flag[0] and turn = 0)
{skip} {skip}
critical section critical section
flag[0] = false flag[1] = false
one
time
unit time
A single run
Chapter 2 Synchronization Algorithms and Concurrent Programming 29
Gadi Taubenfeld © 2014
System response time
P1 P2
q Informal: The longest black interval,
counting time units.
Chapter 2 A single
Synchronization Algorithms and Concurrent Programming run 30
Gadi Taubenfeld © 2014
How many time units
P1 P2
3
time
4
6
7
A single run
Chapter 2 Synchronization Algorithms and Concurrent Programming 31
Gadi Taubenfeld © 2014
How many time units
P1 P2
1
2
3
4
6
time
7
10
A single run
Chapter 2 Synchronization Algorithms and Concurrent Programming 32
Gadi Taubenfeld © 2014
What is the system response time?
Show a run of Peterson’s algorithm where the
system response time is 5 time units.
P1 P2
Thread 0 Thread 1 1
2
flag[0] = true flag[1] = true
3
turn = 1 turn = 0
4
while (flag[1] and turn = 1) while (flag[0] and turn = 0)
5
{skip} {skip}
critical section critical section
flag[0] = false flag[1] = false
1 2 3 4 5
1 2 3 4 5
q At most n-1 can move right
n
q At most n-1 can move down
q At most 1 can win 1 n-1
win right
q In solo run è 1 win
n-1
down
x=i
if y ¹ 0 then go right fi x
y=1
if x ¹ i then go down fi y 0
win
Chapter 2 Synchronization Algorithms and Concurrent Programming 36
Gadi Taubenfeld © 2014
Notation
Example:
await (x=5) == wait until x=5
x y 0
Chapter 2 Synchronization Algorithms and Concurrent Programming 39
Gadi Taubenfeld © 2014
Fast Mutual exclusion Algorithm
code of process i , i Î {1 ,..., n}
1 2 n
x y 0 b false false false false
Yes
Chapter 2 Synchronization Algorithms and Concurrent Programming 41
Gadi Taubenfeld © 2014
Can we remove await y=0 in line 4 ?
Yes
Chapter 2 Synchronization Algorithms and Concurrent Programming 43
Gadi Taubenfeld © 2014
Would this work ?
Contention? yes
y=0? Wait until CS is released
no
Barrier The last to cross
the barrier!
turn := i
q Mutual Exclusion
remainder
q Deadlock-freedom
doorway
waiting entry code
q Starvation-freedom
critical
q FIFO section
exit code
remainder 0 0 0 0 0 0
doorway 1 2
3 2
4
entry
waiting 1 2
3 2
4
CS 1 2 2 time
exit 1 2 2
1 2 3 4 n
number 0 0 0 0 0 0 integer
remainder 0 0 0 0 0 0
doorway 1 2 2
entry
waiting 1 2 2
CS 1 deadlock time
exit 1
1 2 3 4 n
number 0 0 0 0 0 0 integer
1 2 3 4 n
number 0 0 0 0 0 0 integer
1 2 3 4 5 n
remainder 0 0 0 0 0 0
2
doorway 0
1 0
2 0
2
entry
waiting 1 2 2
CS 1 2 2 time
exit 1
choosing[i] = true
number[i] = 1 + max {number[j] | (1 £ j £ n)}
choosing[i] = false
for 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 false false false false false false bits
number 0 0 0 0 0 0 integer
choosing[i] = true
number[i] = 1 + max {number[j] | (1 £ j £ n)}
choosing[i] = false
for 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 false false false false false false bits
number 0 0 0 0 0 0 integer
1 2 3 4 5 n
remainder 0 0 0 0 0 0
1
doorway 0
1 0
2 0
1
entry
waiting 1 2 1
CS 1 2 1 time
exit 1
number[i] = -1 Yes
number[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 Can we replace £ with = ?
number[i] = 0
1 2 3 4 n
choosing false false false false false false bits
number 0 0 0 0 0 0 integer
local1 = 0
for local2 = 1 to n do
local3 = number[local2]
if local1 < local3 then local1 = local3 fi
od
number[i] = 1 + local1
1 2 3 4 n
number 0 0 0 0 0 0 integer
local1 = i
for local2 = 1 to n do
if number[local1] < number[local2] then local1 = local2 fi
od
number[i] = 1 + number[local1]
1 2 3 4 n
number 0 0 0 0 0 0 integer
1 2 3 4 5 n
remainder 0 0 0 0 0 0
doorway ?1 1 1
entry
Passed process 1
Waiting for process 2
waiting 1 1 1
CS 1 1 1 time
exit 1
local1 = i
for local2 = 1 to n do
if number[local1] £ number[local2] then local1 = local2 fi
od
number[i] = 1 + number[local1]
1 2 3 4 5 n
remainder 0 0 0 0 0 0
doorway 0
1 0
2
1 0
2
entry
waiting 1 2
1 2
CS 1 2
1 2 time
exit 1 2
1 2
1 2 3 4 n
choosing bits
mycolor bits
number {0,1,...,n}
choosing[i] = true
mycolor[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
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.
Bakery
Black-White Bakery
Answer: No.
run x run y
• p reads 5 from r • p reads 5 from r
• q writes 6 to r • p writes 7 to r
• p writes 7 to r • q writes 6 to r
• q writes 8 to r • q reads 6 from r
• p reads 8 from r • q writes 8 to r
• q write 6 to r1 • p reads 8 from r
r 68 r 8
• 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
• p reads 5 from r
• p reads 5 from r
• q reads 5 from r p covers r at
• p writes 7 to r these three
points
• q writes 8 to r
• p reads 8 from r
• p writes 2 to r
p is in its then, p is
critical not hidden
section
5 w is in its
r remainder ü w is hidden è remove w
• w reads 5
q is in its ü important: q is still hidden !!!
• p reads 5 remainder
• q reads 5 ü q is hidden è remove q
• p writes 7
• q writes 8
Formal proof: By induction on the
• q reads 8 number of steps of processes
• w writes 3 other than p in x ...
• w reads 3
• p write 9
• p in its critical section
• p exits p is in its
remainder
all the
processes
are Then, for every
hidden p there exists z
x
only p takes
steps
z
p covers unique
register
x looks like y to p
p covers unique
register z p is in its critical
section
p is in its critical
section
Chapter 2 Synchronization Algorithms and Concurrent Programming 83
Gadi Taubenfeld © 2014
Lemma 2.21
(main lemma)
all the
processes
are in their Then, for every set of
remainder processes P there exists z
x
only P take
steps
z
P are hidden and
cover |P| distinct
registers
W1 W2 Wi Wj
x y1 y2 ... yi ... yj ...
only p takes
steps
P are hidden and P are hidden? Yes
cover |P| distinct P cover W1 ? Yes
zregisters
1 z2 p covers w1?zYes
i
... zj
p is hidden? Maybe
w1 w2 wi equals wj
p covers a unique P are hidden? Yes
register w1 ÏW1 P cover Wj ? Yes
(Lemma 2.20) This completes the lower bound proof
p covers wj? Yes
p is hidden? Yes!
• 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 this write does
not change the
• p writes 8 to r value of r!
r 8
An Upper Bound
1 2 3 4 n
b false false false
true false
true false false
true bits
critical section
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
od
until b[i] = true
for j = i+1 to n do await b[j] = false od
critical section
b[i] = false
1 2 3 4 n
b false false false false false false bits
Question
Computing with Infinitely Many Process
1 2 3 4 5 6 7 8 9 10
owner 0 0 10 0
1 0 0
1 0 0 0 0 …
other 10 10 10 10 0 0 0 0 0 0 …
loser 0 0 10 0 0 0 0 0 0 0 …
winner 0 0 0 0
1 0 10 0 0 0 0 …
eureka
correct
Algorithm algorithms Algorithm algorithms
Generator Verifier
parameters
(# of: processes, bits, lines of code)
* This run was stopped after few solutions were found. Not all algorithms were tested.
-- Gadi