Title: Mutual Exclusion
1Mutual Exclusion
2Entry
While (testset(v) 1)
Critical section
Reset (v)
Exit
3entry
testset
v
0
4critical section
0
v
1
5exit
reset
v
0
6Two processors
entry
entry
testset
testset
v
0
7critical section
entry
1
v
0
1
8critical section
entry
testset
v
0
1
9critical section
entry
1
v
0
1
10entry
exit
testset
reset
v
0
11critical section
0
v
1
12exit
reset
v
0
13Problem the algorithm doesnt guaranty
no lockout, a process may starve
Example may never enter the critical
section
critical section
entry
testset
v
1
14Example may never enter the critical
section
entry
testset
v
critical section
1
15 faster than
Example may never enter the critical
section
critical section
entry
testset
v
1
16Mutual exclusion
- read-modify-write variables
17Shared variable v
Ticket of current process in critical section
v.first
Ticket of last process waiting in entry section
v.last
18Entry
p RMW(v, (v.first, v.last 1))
Repeat
q RMW(v,v)
Until q.first p.last
Critical section
RMV(v, (v.first1, v.last))
Exit
(p and q are local variables)
19v.first 1
v.last 0
20p.last
0
v.first 1
v.last 0
21entry
p.last
1
v.first 1
v.last 1
22critical section
p.last
1
v.first 1
v.last 1
23exit
p.last
1
v.first 2
v.last 1
24Four processes
p.last
0
0
0
0
v.first 1
v.last 0
25entry
p.last
1
0
0
0
v.first 1
v.last 1
26entry
entry
p.last
1
2
0
0
v.first 1
v.last 2
27entry
entry
entry
p.last
1
2
3
0
v.first 1
v.last 3
28entry
entry
entry
entry
p.last
1
2
3
4
v.first 1
v.last 4
29critical section
entry
entry
entry
p.last
1
2
3
4
v.first 1
v.last 4
30entry
entry
exit
entry
p.last
1
2
3
4
v.first 2
v.last 4
31critical section
entry
entry
2
3
4
v.first 2
v.last 4
32entry
exit
entry
2
3
4
v.first 3
v.last 4
33entry
entry
3
4
v.first 3
v.last 4
34The behavior is similar with a queue
critical section
entry
entry
i
i1
ik
v.first i
v.last ik
(head)
(tail)
35Good features of algorithm
- Guarantees no lockout
- (any process will eventually enter
- the critical section)
- Uses only one shared variable (v)
36A problem values can grow unbounded
i
i1
ik
37Solution a circular queue
1
n
2
v.first
v.last
Only n different values are needed
(for n processes)
38Mutual exclusion
39The Bakery Algorithm
The algorithm is similar with the
read-modify-write algorithm
There is a queue The process in the
head is in critical section
A new process is inserted in the tail
40Algorithm outline
t tail tail tail 1 Wait until t head
Entry
Critical section
Exit
head head 1
(tail and head are shared variables, t is a
local variable)
41Problem this part of the code doesnt
behave correctly
//read tail
t tail tail tail 1
Entry
//write tail
42A good scenario
0
tail
43A good scenario
(t0)
0
Read 0 Write 1
1
tail
44A good scenario
(t1)
1
0
Read 1 Write 2
2
tail
45A good scenario
(t2)
1
2
0
Read 2 Write 3
3
tail
46A bad scenario
0
Read 0
0
tail
47A bad scenario
0
0
Read 0 Write 1
Write 1 (delayed)
1
tail
48A bad scenario
0
1
0
Write 1 (delayed)
Read 1 Write 2
2
tail
49A bad scenario
0
1
0
Read 2 Write 3
Write 1 (delayed)
2
3
tail
50A bad scenario
0
1
0
Write 1
Read 2 Write 3
2
1
Wrong value!!!
tail
51A Solution distributed counting
V10
V20
V30
V40
0
0
0
0
We need an array of shared variables
v1, v2, , vn
Process has value vi
52V10
V20
V30
V40
0
0
0
0
In the entry code, a process reads the values of
all other processes. The new value is the
maximum 1
53V10
V21
V30
V40
0
1
0
0
entry
Max 0 Max 1 1
54V10
V21
V30
V42
0
1
0
2
entry
entry
Max 1 Max 1 2
55V10
V21
V33
V42
0
1
3
2
entry
entry
entry
Max 2 Max 1 3
56V14
V21
V33
V42
4
1
3
2
entry
entry
entry
entry
Max 3 Max 1 4
Everybody gets a unique value (a unique position
in the distributed queue)
57V14
V21
V33
V42
4
1
3
2
entry
entry
entry
entry
Then the processes compare their values with all
the other values.
The highest value enters the critical region
58V14
V21
V33
V42
4
1
3
2
critical region
entry
entry
entry
reads all values
Realizes it has the highest value
59V10
V21
V33
V42
0
1
3
2
exit
entry
entry
entry
sets value to 0
60V10
V21
V33
V42
0
1
3
2
entry
entry
critical region
61V10
V21
V30
V42
0
1
0
2
entry
entry
exit
62V10
V21
V33
V42
0
1
0
2
entry
critical region
And so on
63A problem
When two processes enter at the same time, they
may choose the same value.
V10
V20
V30
V40
0
0
0
0
entry
entry
64The maximum values they read are the same
V10
V21
V30
V41
0
1
0
1
entry
entry
65Solution use IDs to break symmetries
(highest ID wins)
V10
V21
V30
V41
1, 4
0
0
1, 2
entry
critical section
66V10
V21
V30
V40
0
0
0
1, 2
entry
exit
67V10
V21
V30
V40
0
0
0
1, 2
critical section
68The Complete Bakery Algorithm
Process i
Vi 0 choosingi false
Entry
choosingi true
Vi max(V1, V2, , Vn)1
choosingi false
for (k 1 k lt n k)
Wait until choosingk false
Wait until Vk 0 or (Vk,k)
gt (Vi,i)
Critical section
Exit
Vi 0
69Advantages of the bakery algorithm
- Uses Read/Write variables
- Satisfies no lockout property
Disadvantages
- Uses n shared variables for n processes
(actually, we cannot do better than that)
- The values can grow unbounded
(we would like to find an algorithm with bounded
values)
70Mutual Exclusion for 2 processes
high priority process
low priority process
Entry
Want0 1 Wait until want1 0 Critical
section Want0 0
1 Want1 0 Wait until want0 0
Want1 1 if (want0 1) goto 1
Critical section Want1 0
Exit
Good Uses only bounded values on variables
Problem
low priority process may lockout
71An equal priority algorithm
Process 1
Process 2
Entry
1 Want0 0 Wait until (want1 0
or Priority 0) Want0 1 if priority 1
then if Want1 1 then goto Line
1 Else wait until Want10 Critical
section Priority 1 Want0 0
1 Want1 0 Wait until (want0 0
or Priority 1) Want1 1 if priority 0
then if Want0 1 then goto Line
1 Else wait until Want00 Critical
section Priority 0 Want1 0
Exit
Good Uses only bounded values on variables
Supports no lockout
72The Tournament Algorithm
We can implement a tournament mutual exclusion
algorithm, using the equal priority pairwise
algorithm
73Mutual exclusion for pairs of processes
winner
74(No Transcript)
75Critical section
winner
76Advantages of tournament algorithm
- Bounded values of variables
- Preserves no lockout
- (since each pair mutual exclusion
- is no lockout)