Chapter3 Mutex AdvancedTopics
Chapter3 Mutex AdvancedTopics
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.
P1 P2 P3 P4
Entry
Critical Section
Exit
P1 P2 P3 P4
Shared memory
P1 P2 P3 P4
C1 C2 C3 C4
Shared memory
P1 P2 P3 P4
M1 M2 M3 M4
P1 P2 P3 P4
M1 M2 M3 M4
Answer: No.
Answer: Yes.
Bakery
Black-White Bakery
Local-spinning
Local-spinning
choosing[i] = true
number[i] = 1 + max {number[j] | (1 £ j £ n)}
choosing[i] = false
for j = 1 to n {
await choosing[j] = false 1. Wait for j to choose a number
await (number[j] = 0) Ú (number[j],j)
2. Wait for j³to(number[i],i)
finish its critical section
}
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 {spin.ch[j,i] = false}
for j = 1 to n {
spin.ch[i,j] = true
if choosing[i] = true then await spin.ch[i,j] = false fi
spin.nu[i,j] = true
if (number[j] = 0) Ú (number[j],j) ³ (number[i],i)
then skip else await spin.nu[i,j] = false fi
}
critical section
number[i] = 0
for j = 1 to n {spin.nu[i,j] = false}
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
1. await choosing[j] = false 1. Wait for j to choose a number
if mycolor[j] = mycolor[i]
then await (number[j] = 0) Ú (number[j],j) ³ (number[i],i) Ú
2. (mycolor[j] ¹ mycolor[i])
2. Wait for j to finish its critical section
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
T1
p1
T2
p2
T3
p3
time
Algorithm #1
A simple adaptive algorithm
for unbounded concurrency
x=i
if y = 1 then go right fi n
y=1
if x ¹ i then go down fi 1 n-1
win right
win
x n-1
down
y 0
y=1 £ n-1
x=i right
(and all
y¹1
latecomer)
£1 x=i
win y=1
x¹1
down
n’
1 n’-1
win right
n’-1
down
x=i
if y = 1 then b = 1; go right fi
x
y=1
if x = i then z = 1 y 0
if b = 0 then go win else go down fi
else await b = 1 or z = 1 z 0
if z = 1 then go right else go down fi
fi
b 0
win right
down
b=1 1 n-1
win z=1 MA-
b=1 right
splitter
n-1
b¹1 z=1
await (b = 1 or z = 1)
z¹1
down
Algorithm #2
An Adaptive Tournament Algorithm
Algorithm #2
An Adaptive Tournament Algorithm
critical
start here section
2 get 3 compete
name
4 5 6 7
Algorithm #2
An Adaptive Tournament Algorithm
p q
2-process
mutex
O(log n)
height
Bakery
Black-White Bakery
Adaptive
Chapter 3 Synchronization Algorithms and Concurrent Programming 32
Gadi Taubenfeld © 2014
Active set
• join(S)
• leave(S) P1 P2 P3
• Get-set(S)
P1 P2
1 2 3 4 n
S 0 0 0 0 0 0 bits
Black-White Bakery
FIFO
Bounded space
Local-spinning Adaptive Adaptive
Local-spinning
Black-White Bakery
Single-writer
Local-spinning bits
Adaptive
Yes No
All
Chapter 3 Synchronization Algorithms and Concurrent Programming 38
Gadi Taubenfeld © 2014
Section 3.2.5
Impossibility
v v
1,2 1,2
e1 e2 e1 e2
v1 v2 v1 v2
1 2 2 1
r1 r2 r1 r2
v12 v21 v12 v21
1 2 2 1
2 1
1 2 infinite infinite
path path
p1 is in CS p2 is in CS
Immediate failures
Continuous failures
Katseff’s fault-tolerant algorithm
Self-Stabilization
Self-Stabilization
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
repeat
b[i] = true; j = 1;
while (b[i] = true) and (j < i) do
if b[j] = true then b[i] = false;
repeat
if b[i] = true then b[i] = false fi
until b[j] = false
fi
j = j+1
od
until b[i] = true
for j = i+1 to n do
repeat
if b[i] = true then b[i] = false fi
until b[j] = false
od
critical section
b[i] = false
Chapter 3 Synchronization Algorithms and Concurrent Programming 44
Gadi Taubenfeld © 2014
Symmetric Algorithms
Section 3.4
Definitions
Symmetric deadlock-free algorithms
Symmetric starvation-free algorithms
-- Gadi