Title: Mutual exclusion
1Mutual exclusion
2The 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
3Algorithm 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)
4Problem this part of the code doesnt
behave correctly
//read tail
t tail tail tail 1
Entry
//write tail
5A good scenario
0
tail
6A good scenario
(t0)
0
Read 0 Write 1
1
tail
7A good scenario
(t1)
1
0
Read 1 Write 2
2
tail
8A good scenario
(t2)
1
2
0
Read 2 Write 3
3
tail
9A bad scenario
0
Read 0
0
tail
10A bad scenario
0
0
Read 0 Write 1
Write 1 (delayed)
1
tail
11A bad scenario
0
1
0
Write 1 (delayed)
Read 1 Write 2
2
tail
12A bad scenario
0
1
0
Read 2 Write 3
Write 1 (delayed)
2
3
tail
13A bad scenario
0
1
0
Write 1
Read 2 Write 3
2
1
Wrong value!!!
tail
14A 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
15V10
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
16V10
V21
V30
V40
0
1
0
0
entry
Max 0 Max 1 1
17V10
V21
V30
V42
0
1
0
2
entry
entry
Max 1 Max 1 2
18V10
V21
V33
V42
0
1
3
2
entry
entry
entry
Max 2 Max 1 3
19V14
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)
20V14
V21
V33
V42
4
1
3
2
entry
entry
entry
entry
Then the processes compare their values with all
the other values.
The lowest value enters the critical region
(different than 0)
21V14
V21
V33
V42
4
1
3
2
critical region
entry
entry
entry
reads all values
Realizes it has the highest value
22V14
V20
V33
V42
0
4
3
2
entry
entry
entry
exit
sets value to 0
23V14
V20
V33
V42
4
0
3
2
critical region
entry
entry
24V14
V20
V33
V40
4
0
3
0
entry
entry
exit
25V14
V20
V33
V40
4
0
3
0
entry
critical region
And so on
26A 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
27The maximum values they read are the same
V10
V21
V30
V41
0
1
0
1
entry
entry
28Solution use IDs to break symmetries
(lowest ID wins)
V10
V21
V30
V41
1, 4
0
0
1, 2
critical section
entry
29V10
V20
V30
V41
0
1, 4
0
0
entry
exit
30V10
V21
V30
V40
0
0
0
1, 2
critical section
31The 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
32Advantages 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)
33Mutual 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
34An 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
35The Tournament Algorithm
We can implement a tournament mutual exclusion
algorithm, using the equal priority pairwise
algorithm
36Mutual exclusion for pairs of processes
winner
37(No Transcript)
38Critical section
winner
39Advantages of tournament algorithm
- Bounded values of variables
- Preserves no lockout
- (since each pair mutual exclusion
- is no lockout)
40MCS Mutual Exclusion Algorithm
head
tail
Lock0
Lock0
Lock0
Lock1
next
next
nil
next
next
T
Process in critical region
Process waiting to get in critical region
41Local memories (for example cache)
head
tail
Lock0
Lock0
Lock0
Lock1
next
next
nil
next
next
Processes Spin on their own memories
T
Global Shared Memory
42head
tail
Lock0
Lock0
Lock0
Lock1
next
next
nil
next
next
Critical region
T
43head
tail
Lock1
Lock0
Lock0
Lock1
next
next
nil
next
next
Critical region
T
44head
tail
Lock1
Lock0
Lock1
next
next
nil
next
Critical region
T
45head
tail
Lock1
Lock1
next
nil
next
T
Critical region
46nil
T
47Entry Code for processor i
Qi pointer to a new queuenode // Qi, Qi is in
local memory Qi-gtLock 0 Qi-gtNext nil Ti
Swap(T, Qi) //Ti is in local memory If Ti
nil then Ti -gt next Qi else
Qi-gtLock 1 //it is at the head of the
queue Wait until Qi-gtlock 1
48Atomic operation
Swap(T,Qi) x T //read T T Qi
// swap T with Qi return x //
return old value of T
49Qi
Empty queue
nil
Lock0
next
nil
T
Qi is created
50Qi
nil
Lock0
next
nil
Ti
T
After swap operation
51Qi
in critical section
Lock1
next
nil
T
After if statement
52Qi
Qj
critical section
Lock1
Lock0
next
nil
next
nil
T
Process j arrives
53Qi
Qj
critical section
Lock1
Lock0
next
nil
next
nil
T
Tj
After swap operation
54Qi
Qj
critical section
Lock1
Lock0
next
next
nil
T
After if statement operation
55Processes i and j Arrive simultaneously
Qi
Empty queue
Lock0
nil
nil
next
Qj
T
Lock0
nil
next
56Execution of swap assigns an order
first
second
Qi
Qj
nil
Lock0
Lock0
Ti
nil
nil
next
next
Tj
T
57critical section
Qi
Qj
Lock1
Lock0
nil
next
next
T
58Exit Code for processor i
Ti CompareSwap(T, Qi, nil) //Ti is in local
memory If Ti Qi then wait until
(Qi-gtnext nil) Ti -gt next-gtlock
1 Delete Qi and Qi
59Atomic operation
CompareSwap(T,Qi,nil) x T //read
value of T If T Qi then T nil
//swap T with nil if T and Qi are same return
x // return old value of T
60Qi
Lock0
Lock0
Lock0
Lock1
next
next
nil
next
next
Critical section
T
before compareswap
61Qi
Ti
Lock1
Lock0
Lock0
Lock1
next
next
nil
next
next
Critical section
T
after compareswap
62Lock1
Lock0
Lock1
next
next
nil
next
Critical section
T
63Critical section
Lock1
Lock1
next
nil
next
T
64An extreme case
Critical section
Lock1
next
nil
T
65An extreme case
A new node Will be insterted
Critical section
Lock0
Lock1
next
nil
next
nil
T
will execute swap
will execute Compareswap
66Case 1 swap executes first
Lock0
Lock1
next
nil
next
nil
Waits until next points to something
T
67Case 1 swap executes first
Lock0
Lock1
next
nil
next
T
68Case 1 swap executes first
Critical section
Lock1
Lock1
next
nil
next
T
69Case 2 compareswap executes first
Lock0
Lock1
nil
next
nil
next
nil
T
70Case 2 compareswap executes first
Lock0
Lock1
nil
next
nil
next
nil
T
71Case 2 compareswap executes first
Critical section
Lock1
next
nil
T