Chapter2 Mutex BasicTopics

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 99

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 1
Gadi Taubenfeld © 2014
Synchronization Algorithms
and Concurrent Programming
ISBN: 0131972596, 1st edition

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:

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.

Thanks and enjoy!


Gadi Taubenfeld
All material copyright 2014
Gadi Taubenfeld, All Rights Reserved

To get the most updated version of these slides go to:


http://www.faculty.idc.ac.il/gadi/book.htm
Chapter 2 Synchronization Algorithms and Concurrent Programming 2
Gadi Taubenfeld © 2014
Chapter 2
Mutual Exclusion using atomic
registers: Basic Topics

2.1 Algorithms for Two Processes


2.2 Tournament Algorithms
2.3 A Fast Mutual Exclusion
Algorithms
2.4 Starvation-free Algorithms
2.5 Tight Space bounds
2.6 Automatic Discovery of
algorithms

Chapter 2 Synchronization Algorithms and Concurrent Programming 3


Gadi Taubenfeld © 2014
The Mutual Exclusion Problem
Basic definitions

Chapter 1 Synchronization Algorithms and Concurrent Programming 4


Gadi Taubenfeld © 2014
The mutual exclusion problem

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.

Chapter 2 Synchronization Algorithms and Concurrent Programming 5


Gadi Taubenfeld © 2014
The mutual exclusion problem
q Mutual Exclusion: No two processes are in
their critical sections at the same time.

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.

Chapter 2 Synchronization Algorithms and Concurrent Programming 6


Gadi Taubenfeld © 2014
Question: true or false ? Algorithm C

Algorithm A Algorithm B remainder

entry code of A

remainder entry code of B

entry code critical


section
critical exit code of B
section
exit code exit code of A

Chapter 2 Synchronization Algorithms and Concurrent Programming 7


Gadi Taubenfeld © 2014
Question: true or false ? Algorithm C

q A and B are deadlock-free è C is


deadlock-free. remainder
q A and B are starvation-free è C is
starvation-free. entry code of A
q A or B satisfies mutual exclusion è
C satisfies mutual exclusion. entry code of B
q A is deadlock-free and B is critical
starvation-free è C is starvation- section
free. exit code of B
q A is starvation-free and B is
deadlock-free è C is starvation- exit code of A
free.

Chapter 2 Synchronization Algorithms and Concurrent Programming 8


Gadi Taubenfeld © 2014
Atomic read/write registers
Proposed solution 1

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
} }

Chapter 2 Synchronization Algorithms and Concurrent Programming 10


Gadi Taubenfeld © 2014
Convention

Thread 0 Thread 1

entry code entry code


critical section critical section
exit code exit code

Chapter 2 Synchronization Algorithms and Concurrent Programming 11


Gadi Taubenfeld © 2014
Proposed solution 2

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?

Chapter 2 Synchronization Algorithms and Concurrent Programming 13


Gadi Taubenfeld © 2014
Algorithms for Two Processes
Section 2.1

Chapter 2 Synchronization Algorithms and Concurrent Programming 14


Gadi Taubenfeld © 2014
Section 2.1.1

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

Chapter 2 Synchronization Algorithms and Concurrent Programming 15


Gadi Taubenfeld © 2014
A variant of Peterson’s algorithm
Is it correct ?
Thread 0 Thread 1
turn = 1 turn = 0
flag[0] = true flag[1] = true
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
true
1 false
true
turn 0/1
0
1

Chapter 2 Synchronization Algorithms and Concurrent Programming 16


Gadi Taubenfeld © 2014
Schematic for Peterson’s algorithm

Indicate contending
b[i] := true

Barrier
turn := i
no / maybe

yes First to cross


Contention? the barrier?
b[i] = true ? turn = j ?
no
yes
critical section

exit code
b[i] = false ?

Chapter 2 Synchronization Algorithms and Concurrent Programming 17


Gadi Taubenfeld © 2014
Properties of Peterson’s Solution
q Satisfies mutual exclusion and starvation-freedom
q Memory accesses are assumed to be atomic
q Solution for two processes only

Chapter 2 Synchronization Algorithms and Concurrent Programming 18


Gadi Taubenfeld © 2014
Section 2.1.2

Question
Using only single-writer registers

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]

Chapter 2 Synchronization Algorithms and Concurrent Programming 19


Gadi Taubenfeld © 2014
Algorithms for Many Processes
Section 2.2

How can we use two-process


algorithm to construct an
algorithm for many processes?

Chapter 2 Synchronization Algorithms and Concurrent Programming 20


Gadi Taubenfeld © 2014
Section 2.2

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

Chapter 2 Synchronization Algorithms and Concurrent Programming 21


Gadi Taubenfeld © 2014
Models, Properties & Complexity
Basic definitions and notations

Chapter 2 Synchronization Algorithms and Concurrent Programming 22


Gadi Taubenfeld © 2014
Shared Memory Models

P ... P P ... P P ... P

C C M M

M M

Simple Coherent Distributed


Shared Caching Shared
Memory Memory

Chapter 2 Synchronization Algorithms and Concurrent Programming 23


Gadi Taubenfeld © 2014
Properties & complexity

q Time complexity
m Fast
m Adaptive
q Fairness
m FIFO, ...
q Fault-tolerance
q Local spinning
q Space complexity
q Communication primitives

Chapter 2 Synchronization Algorithms and Concurrent Programming 24


Gadi Taubenfeld © 2014
Complexity Measures

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

Chapter 2 Synchronization Algorithms and Concurrent Programming 25


Gadi Taubenfeld © 2014
Contention-free time complexity
What is the contention-free time 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

(3 in the entry + 1 in the exit)


Chapter 2 Synchronization Algorithms and Concurrent Programming 26
Gadi Taubenfeld © 2014
Process time complexity
P1 P2
q Informal: The longest black interval,
counting steps.

q 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. time

q Process step complexity: # of steps,


since it has started…

Given an algorithm divide it steps …


• A – entry/exit steps;
• B – critical section steps;
• C – other steps (remainder).

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

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 .

Chapter 2 Synchronization Algorithms and Concurrent Programming 28


Gadi Taubenfeld © 2014
System response time
P1 P2

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.

q 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 time
step time, and no lower bound.

Given an algorithm divide it steps …


• A – entry/exit steps;
• B – critical section steps;
• C – other steps (remainder).

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

Chapter 2 Synchronization Algorithms and Concurrent Programming 33


Gadi Taubenfeld © 2014
A Fast Mutual exclusion Algorithm
Section 2.3

Chapter 2 Synchronization Algorithms and Concurrent Programming 34


Gadi Taubenfeld © 2014
A Fast Mutual exclusion Algorithm

q Mutual exclusion and deadlock-freedom


q Starvation of individual processes is possible
q fast access
m in the absence of contention, only 7
accesses to shared memory are needed
q With contention
m Even if only 2 processes contend, the
winner may need to check all the 0(n)
shared registers
m System response time is of order n time
units
q n+2 shared registers are used

Chapter 2 Synchronization Algorithms and Concurrent Programming 35


Gadi Taubenfeld © 2014
Splitter

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

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

Example:
await (x=5) == wait until x=5

Chapter 2 Synchronization Algorithms and Concurrent Programming 37


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

start: b[i] = true


x=i
right
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
win if y ¹ i then await y = 0
goto start fi fi
critical section down
y=0
b[i] = false
Chapter 2 Synchronization Algorithms and Concurrent Programming 38
Gadi Taubenfeld © 2014
Can we switch the order of the
last two statements?
What is the maximum
start: b[i] = true number of processes that
x=i can be in their CS at the
if y ¹ 0 then b[i] = false same time?
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

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

start: b[i] = true


x=i
if y ¹ 0 then b[i] = false Can we replace the
order of these two
await y = 0
statements ?
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 No. Mutual exclusion
b[i] = false is not satisfied.
Chapter 2 Synchronization Algorithms and Concurrent Programming 40
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
Chapter 2 Synchronization Algorithms and Concurrent Programming 41
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 remove ?
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


Chapter 2 Synchronization Algorithms and Concurrent Programming 42
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
remove ?
goto start fi fi
critical section
y=0
b[i] = false

Yes
Chapter 2 Synchronization Algorithms and Concurrent Programming 43
Gadi Taubenfeld © 2014
Would this work ?

start: b[i] = true add


x=i and
if y ¹ 0 and y =i then b[i] = false
await y = 0
remove
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
No. Mutual exclusion
b[i] = false
is not satisfied.

Chapter 2 Synchronization Algorithms and Concurrent Programming 44


Gadi Taubenfeld © 2014
Schematic for the fast algorithm
Indicate contending
b[i] := true

Contention? yes
y=0? Wait until CS is released

no
Barrier The last to cross
the barrier!
turn := i

yes Continue only after it is


Contention?
x¹i? guaranteed that no one can
cross the barrier
no
yes Last to cross the barrier?
critical section y=i?
no

exit code Wait until CS is released

Chapter 2 Synchronization Algorithms and Concurrent Programming 45


Gadi Taubenfeld © 2014
Starvation-free Algorithms
Section 2.4

2.4.1 Basic Definitions (i.e., FIFO)


2.4.2 The Bakery Algorithm
2.4.3 The Black-White Bakery Algorithm

Chapter 2 Synchronization Algorithms and Concurrent Programming 46


Gadi Taubenfeld © 2014
The mutual exclusion problem

q Mutual Exclusion
remainder
q Deadlock-freedom
doorway
waiting entry code
q Starvation-freedom

critical
q FIFO section

exit code

Chapter 2 Synchronization Algorithms and Concurrent Programming 47


Gadi Taubenfeld © 2014
Section 2.4.2

The Bakery Algorithm


1 2 3 4 5 n

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

Chapter 2 Synchronization Algorithms and Concurrent Programming 48


Gadi Taubenfeld © 2014
Implementation 1
code 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 section
number[i] = 0

1 2 3 4 n
number 0 0 0 0 0 0 integer

Answer: No! can deadlock

Chapter 2 Synchronization Algorithms and Concurrent Programming 49


Gadi Taubenfeld © 2014
Implementation 1: deadlock
1 2 3 4 5 n

remainder 0 0 0 0 0 0

doorway 1 2 2
entry

waiting 1 2 2

CS 1 deadlock time

exit 1

Chapter 2 Synchronization Algorithms and Concurrent Programming 50


Gadi Taubenfeld © 2014
Implementation 1
code 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 section
What if we replace > with ³ ?
number[i] = 0

1 2 3 4 n
number 0 0 0 0 0 0 integer

Answer: does not satisfy mutual exclusion

Chapter 2 Synchronization Algorithms and Concurrent Programming 51


Gadi Taubenfeld © 2014
Implementation 2
code 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],j) ³ number[i],i)
// lexicographical order
}
critical section
number[i] = 0

1 2 3 4 n
number 0 0 0 0 0 0 integer

Answer: does not satisfy mutual exclusion

Chapter 2 Synchronization Algorithms and Concurrent Programming 52


Gadi Taubenfeld © 2014
Implementation 2: no mutual exclusion

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

Chapter 2 Synchronization Algorithms and Concurrent Programming 53


Gadi Taubenfeld © 2014
The Bakery Algorithm
code of process i , i Î {1 ,..., n}

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

Chapter 2 Synchronization Algorithms and Concurrent Programming 54


Gadi Taubenfeld © 2014
Properties of the Bakery Algorithm

q Satisfies mutex & FIFO.


q The size of number[i] is unbounded.

q Safe registers: reads which are concurrent with


writes may return arbitrary value.

Chapter 2 Synchronization Algorithms and Concurrent Programming 55


Gadi Taubenfeld © 2014
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] = 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

Chapter 2 Synchronization Algorithms and Concurrent Programming 57


Gadi Taubenfeld © 2014
No

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

Chapter 2 Synchronization Algorithms and Concurrent Programming 58


Gadi Taubenfeld © 2014
Is the following version correct?
code of process i , i Î {1 ,..., n}

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

Chapter 2 Synchronization Algorithms and Concurrent Programming 59


Gadi Taubenfeld © 2014
Computing the Maximum #1
code of process i , i Î {1 ,..., n}
Correct implementation

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

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

Chapter 2 Synchronization Algorithms and Concurrent Programming 60


Gadi Taubenfeld © 2014
Computing the Maximum #2
code of process i , i Î {1 ,..., n}
Is the following implementation correct ?

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

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

Chapter 2 Synchronization Algorithms and Concurrent Programming 61


Gadi Taubenfeld © 2014
No
local1 2
?

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

Chapter 2 Synchronization Algorithms and Concurrent Programming 62


Gadi Taubenfeld © 2014
Computing the Maximum #3
code of process i , i Î {1 ,..., n}
Is the following implementation correct ?

A difficult question !!!

local1 = i
for local2 = 1 to n do
if number[local1] £ number[local2] then local1 = local2 fi
od
number[i] = 1 + number[local1]

This is Problem 2.39

Chapter 2 Synchronization Algorithms and Concurrent Programming 63


Gadi Taubenfeld © 2014
Section 2.4.3

The Black-White Bakery Algorithm


Bounding the space of the Bakery Algorithm

Bakery (FIFO, unbounded)

The Black-White Bakery Algorithm


FIFO
Bounded space
+ one bit

Chapter 2 Synchronization Algorithms and Concurrent Programming 64


Gadi Taubenfeld © 2014
The Black-White Bakery Algorithm
color bit

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

Chapter 2 Synchronization Algorithms and Concurrent Programming 65


Gadi Taubenfeld © 2014
The Black-White Bakery Algorithm
Data Structures

1 2 3 4 n
choosing bits

mycolor bits
number {0,1,...,n}

color bit {black,white}

Chapter 2 Synchronization Algorithms and Concurrent Programming 66


Gadi Taubenfeld © 2014
The Black-White Bakery Algorithm
code of process i , i Î {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

Chapter 2 Synchronization Algorithms and Concurrent Programming 67


Gadi Taubenfeld © 2014
Question

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?

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.

Chapter 2 Synchronization Algorithms and Concurrent Programming 68


Gadi Taubenfeld © 2014
Question

Does it matter if we change the order of the last two


statements (in the exit code) of the Black-White
Bakery Algorithm?

Answer: Yes, it matters.

Chapter 2 Synchronization Algorithms and Concurrent Programming 69


Gadi Taubenfeld © 2014
Question
Replace the multi-writer bit color
with n single-writer bits.

Bakery

Black-White Bakery

Use single-writer bits only!

Answer: See page 57.

Chapter 2 Synchronization Algorithms and Concurrent Programming 70


Gadi Taubenfeld © 2014
Tight Space Bounds
Section 2.5

Chapter 2 Synchronization Algorithms and Concurrent Programming 71


Gadi Taubenfeld © 2014
A Simple Observation
A very simple Lower Bound

Theorem: Any deadlock-free mutual exclusion


algorithm for n processes using only SWMR
registers must use at least n such registers.

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)


Chapter 2 Synchronization Algorithms and Concurrent Programming 72
Gadi Taubenfeld © 2014
Tight Space Bounds
Section 2.5.1

A Lower Bound (very difficult to prove!)

Theorem: Any deadlock-free mutual exclusion


algorithm for n processes must use at least n
shared registers.

Proof: è (see next few slides)

Chapter 2 Synchronization Algorithms and Concurrent Programming 73


Gadi Taubenfeld © 2014
Definitions

q run – a sequence of events


q run x looks like run y to process p
q process p is hidden in run x
q process p covers register r in run x

Chapter 2 Synchronization Algorithms and Concurrent Programming 74


Gadi Taubenfeld © 2014
Example
run x looks like run y to process p

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

The values of the shared registers must also be the same

Chapter 2 Synchronization Algorithms and Concurrent Programming 75


Gadi Taubenfeld © 2014
Example
process p is hidden in run x

• 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

writes must be overwritten before any other process has


read the value written

Chapter 2 Synchronization Algorithms and Concurrent Programming 76


Gadi Taubenfeld © 2014
Example
process p covers register r in run x

• 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

writes must be overwritten before any other process has


read the value written

Chapter 2 Synchronization Algorithms and Concurrent Programming 77


Gadi Taubenfeld © 2014
Lemma 2.17
(very simple)

run x looks like run y to


processes in P

P events x y then, this is


only also a run

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

Chapter 2 Synchronization Algorithms and Concurrent Programming 78


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.

p is in its then, p is
critical not hidden
section

Proof: Assume to the contrary … by Lemma 2.17 …

Chapter 2 Synchronization Algorithms and Concurrent Programming 79


Gadi Taubenfeld © 2014
Lemma 2.19

Then, for any p there


exists y such that
x looks like y to p

all the y all processes,


processes except maybe p,
are hidden are in their
remainders
x

Proof: By induction on the number of steps of


processes other than p in x ...

Chapter 2 Synchronization Algorithms and Concurrent Programming 80


Gadi Taubenfeld © 2014
Lemma 2.19: Example

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

Chapter 2 Synchronization Algorithms and Concurrent Programming 81


Gadi Taubenfeld © 2014
Lemma 2.20
(simple)

all the
processes
are Then, for every
hidden p there exists z
x
only p takes
steps

z
p covers unique
register

Chapter 2 Synchronization Algorithms and Concurrent Programming 82


Gadi Taubenfeld © 2014
Lemma 2.20: Proof
By Lemma 2.19: there exists y …
By the deadlock-freedom assumption …
By Lemma 2.17 …
… p is hidden … imp. by Lemma 2.18

x looks like y to p

all processes, except


all the maybe p, are in their
processes remainders
are hidden

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

Proof: By induction on k the size of P.

Chapter 2 Synchronization Algorithms and Concurrent Programming 84


Gadi Taubenfeld © 2014
Lemma 2.21
(main lemma)

Proof: By induction on k the size of P.


q Base: k = 0 is trivial.
q Induction hypothesis: Assume that it holds for k ³ 0.
q Step: We prove that it holds for k+1.
m let P be a set of processes where |P|= k;
m let p be a process not in P.
m We show that:
only P+p
take steps

z P+p are hidden and


cover |P+p|
distinct registers

Chapter 2 Synchronization Algorithms and Concurrent Programming 85


Gadi Taubenfeld © 2014
Lemma 2.21
each process in PBytakes
the induction hypothesis:
only P take one step first; all in
steps their remainders ; ...

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!

Chapter 2 Synchronization Algorithms and Concurrent Programming 86


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

• 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

Chapter 2 Synchronization Algorithms and Concurrent Programming 87


Gadi Taubenfeld © 2014
Tight Space Bounds
Section 2.5.2

An Upper Bound

Theorem: There is a deadlock-free mutual


exclusion algorithm for n processes that uses
n shared bits.

Chapter 2 Synchronization Algorithms and Concurrent Programming 88


Gadi Taubenfeld © 2014
The One-Bit Algorithm
code of process i , i Î {1 ,..., n}
p3 writes pn writes
false
true false
true

1 2 3 4 n
b false false false
true false
true false false
true bits

critical section

Chapter 2 Synchronization Algorithms and Concurrent Programming 89


Gadi Taubenfeld © 2014
The One-Bit Algorithm
code 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
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

Chapter 2 Synchronization Algorithms and Concurrent Programming 90


Gadi Taubenfeld © 2014
Question

Does the correctness of the One-bit algorithm


depend on the order in which the bits are read
within the loops ?

Chapter 2 Synchronization Algorithms and Concurrent Programming 91


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?

Chapter 2 Synchronization Algorithms and Concurrent Programming 92


Gadi Taubenfeld © 2014
Section 2.5.3

Question
Computing with Infinitely Many Process

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 ?

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.

Chapter 2 Synchronization Algorithms and Concurrent Programming 93


Gadi Taubenfeld © 2014
Section 2.5.3

Computing with Infinitely Many Process: The algorithm

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 …

process 4 runs alone … process 4 is the winner


process 3 runs ... process 3 lost
process 6 runs ... process 3 lost
This is just one game, we need infinitely many …

Chapter 2 Synchronization Algorithms and Concurrent Programming 94


Gadi Taubenfeld © 2014
Automatic Discovery of Algorithms
Section 2.6

Chapter 2 Synchronization Algorithms and Concurrent Programming 95


Gadi Taubenfeld © 2014
Section 2.6

Automatic Discovery of Algorithms

How do we invent algorithms?

eureka

This is one way è

See next slide for another way ...


(maybe they are the same ...)

Chapter 2 Synchronization Algorithms and Concurrent Programming 96


Gadi Taubenfeld © 2014
Automatic Discovery

correct
Algorithm algorithms Algorithm algorithms
Generator Verifier

Chapter 2 Synchronization Algorithms and Concurrent Programming 97


Gadi Taubenfeld © 2014
System Overview
algorithms
Algorithm Algorithm
Generator Verifier
verification
results
correct
algorithms
User Interface

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

Chapter 2 Synchronization Algorithms and Concurrent Programming 98


Gadi Taubenfeld © 2014
Results
User-defined parameters Results
Shared Entry Exit Complex Starvation Tested Correct appx.
bits conditions freedom algorithms alg. running
hours
2 6 1 Yes 7,196,536,269 0 216
2 7 1 846,712,059 66 39
3 4 1 Yes Yes 25,221,389 105 0.4
3 6 1 Yes 1,838,128,995 10 47
4 4 1 Yes Yes 129,542,873 480 1
4 5 1 129,190,403 56 1
4 6 1 Yes *900,000,000 80 12
5 5 1 *22,000,000 106 0.4
6 5 1 *70,000,000 96 1

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

Chapter 2 Synchronization Algorithms and Concurrent Programming 99


Gadi Taubenfeld © 2014
Next Chapter
In Chapter 3 we will consider more
advanced solutions to the mutual
exclusion problem using atomic
registers.

-- Gadi

Chapter 2 Synchronization Algorithms and Concurrent Programming 10


Gadi Taubenfeld © 2014 0

You might also like