Mutual exclusion
description
Transcript of Mutual exclusion
1
Mutual exclusion
read/write variables
2
The 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
3
Entry:
Algorithm outline
Exit:
t = tail;tail = tail + 1;Wait until t == head;
Critical section
head = head + 1;
(tail and head are shared variables, t is a local variable)
4
Entry: t = tail;tail = tail + 1;
Problem: this part of the code doesn’t behave correctly
//read tail//write tail
5
0
tail
A good scenario
6
1
1p0
tail
Read 0Write 1
A good scenario
(t=0)
7
2
1p0
2p1
tail
Read 1Write 2
A good scenario
(t=1)
8
3
1p0
2p1 3p
2
tail
Read 2Write 3
A good scenario(t=2)
9
0
1p0
tail
Read 0
A bad scenario
10
1
1p0
2p0
tail
Read 0Write 1
A bad scenario
Write 1(delayed)
11
2
1p0
2p0 3p
1
tail
Read 1Write 2
A bad scenario
Write 1(delayed)
12
3
1p0
2p0 3p
1
tail
Read 2Write 3
A bad scenario
Write 1(delayed)
4p2
13
1
1p0
2p0 3p
1
tail
A bad scenario
Write 1 Read 2Write 3
4p2
Wrong value!!!
14
1p0
A Solution: distributed counting
V[1]=0
We need an array of shared variables v[1], v[2], …, v[n]
Process has value v[i]ip
2p0
V[2]=0
3p0
V[3]=0
4p0
V[4]=0
15
1p0
V[1]=0
2p0
V[2]=0
3p0
V[3]=0
4p0
V[4]=0
In the entry code, a processreads the values of all other processes.
The new value is the maximum + 1
16
1p0
V[1]=0
2p1
V[2]=1
3p0
V[3]=0
4p0
V[4]=0
entry
Max = 0;Max + 1 = 1;
17
1p0
V[1]=0
2p1
V[2]=1
3p0
V[3]=0
4p2
V[4]=2
entry entry
Max = 1;Max + 1 = 2;
18
1p0
V[1]=0
2p1
V[2]=1
3p3
V[3]=3
4p2
V[4]=2
entry entry
Max = 2;Max + 1 = 3;
entry
19
1p4
V[1]=4
2p1
V[2]=1
3p3
V[3]=3
4p2
V[4]=2
entry entryentryentry
Max = 3;Max + 1 = 4;
Everybody gets a unique value(a unique position in the distributed queue)
20
1p4
V[1]=4
2p1
V[2]=1
3p3
V[3]=3
4p2
V[4]=2
entry entryentryentry
Then the processes compare their valueswith all the other values.
The lowest value enters the critical region(different than 0)
21
1p4
V[1]=4
2p1
V[2]=1
3p3
V[3]=3
4p2
V[4]=2
entry entryentrycriticalregion
2p reads all values
2p Realizes it has the lowest value
22
1p4
V[1]=4
2p0
V[2]=0
3p3
V[3]=3
4p2
V[4]=2
entry entryentryexit
2p sets value to 0
23
1p4
V[1]=4
2p0
V[2]=0
3p3
V[3]=3
4p2
V[4]=2
entry entry criticalregion
24
1p4
V[1]=4
2p0
V[2]=0
3p3
V[3]=3
4p0
V[4]=0
entry entry exit
25
And so on……
1p4
V[1]=4
2p0
V[2]=0
3p3
V[3]=3
4p0
V[4]=0
entry criticalregion
26
1p0
V[1]=0
2p0
V[2]=0
3p0
V[3]=0
4p0
V[4]=0
A problem:When two processes enterat the same time, they may choose the same value.
entry entry
27
1p0
V[1]=0
2p1
V[2]=1
3p0
V[3]=0
4p1
V[4]=1
entry entry
The maximum values they read are the same
28
1p0
V[1]=0
2p1, 2
V[2]=1
3p0
V[3]=0
4p1, 4
V[4]=1
entrycriticalsection
Solution: use ID’s to break symmetries
(lowest ID wins)
29
1p0
V[1]=0
2p0
V[2]=0
3p0
V[3]=0
4pV[4]=1
entryexit
1, 4
30
The Complete Bakery Algorithm
V[i] = 0; choosing[i] = false;
choosing[i] = true;V[i] = max(V[1], V[2], …, V[n])+1;
Process i:
choosing[i] = false;
for (k = 1; k <= n; k++)
Wait until choosing[k] == false;Wait until V[k] == 0 or (V[k],k) > (V[i],i)
Critical sectionV[i] = 0;
Entry:
Exit:
31
Advantages 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 algorithmwith bounded values)
32
Mutual Exclusion for 2 processes
Want[0] = 1;Wait until want[1]== 0;
Critical section
Want[0] = 0;
1: Want[1] = 0; Wait until want[0] ==0; Want[1] = 1; if (want[0] == 1) goto 1;
Critical section
Want[1] = 0;
high priority process low priority process
Entry:
Exit:
Good: Uses only bounded values on variablesProblem:low priority process may lockout
33
1: Want[0] = 0;Wait until (want[1]== 0 or Priority == 0);Want[0] = 1;if priority == 1 then if Want[1] == 1 then goto Line 1Else wait until Want[1]==0;Critical sectionPriority = 1;Want[0] = 0;
Process 0 Process 1
Entry:
Exit:
Good: Uses only bounded values on variables Supports no lockout
An equal priority algorithm
1: Want[1] = 0;Wait until (want[0]== 0 or Priority == 1);Want[1] = 1;if priority == 0 then if Want[0] == 1 then goto Line 1Else wait until Want[0]==0;Critical sectionPriority = 0;Want[1] = 0;
34
The Tournament Algorithm
We can implement a tournament mutualexclusion algorithm, using the equalpriority pairwise algorithm
1p 2p 3p 4p 5p 6p 7p 8p
35
1p 2p 3p 4p 5p 6p 7p 8p
Mutual exclusion for pairs of processes
1p 4p 5p 8pwinner
36
1p 2p 3p 4p 5p 6p 7p 8p
1p 4p 5p 8p
4p 8p
37
1p 2p 3p 4p 5p 6p 7p 8p
1p 4p 5p 8p
4p 8p
4pwinner
Critical section
38
Advantages of tournament algorithm
•Bounded values of variables
•O(n) variables
•Preserves no lockout (since each pair mutual exclusion is no lockout)
39
MCS Mutual Exclusion Algorithm
Lock=0next nil
Lock=0next
Lock=0next
Lock=1next
T
head tail
Process in criticalregion
Process waiting to get incritical region
40
Lock=0next nil
Lock=0next
Lock=0next
Lock=1next
T
head tail
Global Shared Memory
Local memories (for example cache)
ProcessesSpin on their own memories
41
Lock=0next nil
Lock=0next
Lock=0next
Lock=1next
T
head tail
Critical region
42
Lock=0next nil
Lock=0next
Lock=1next
Lock=1next
T
head tail
Critical region
43
Lock=0next nil
Lock=1next
Lock=1next
T
head tail
Critical region
44
Lock=1next nil
Lock=1next
T
headtail
Critical region
45
nil
T
46
Entry Code for processor i:
Qi = pointer to a new queuenode; // Qi, *Qi is in local memory
Qi->Lock = 0;Qi->Next = nil;Ti = Swap(T, Qi); //Ti is in local memory
If Ti nil then Ti -> next = Qi;else Qi->Lock =1; //it is at the head of the queue
Wait until Qi->lock = 1;
47
Swap(T,Qi){ x = T; //read T T = Qi; // swap T with Qi return x; // return old value of T}
Atomic operation
48
nil
T
Lock=0next nil
Qi is created
QiEmpty queue
49
nil
T
Lock=0next nil
After swap operation
Qi
Ti
50
T
Lock=1next nil
After if statement
Qi
in critical section
51
T
Lock=1next nil
Process j arrives
Qi
Lock=0next nil
Qj
critical section
52
T
Lock=1next nil
After swap operation
Qi
Lock=0next nil
Qj
Tj
critical section
53
T
Lock=1next
After if statement operation
Qi
Lock=0next nil
Qj
critical section
54
nil
T
Lock=0next nil
Processes i and jArrive simultaneously Qi
Lock=0next nil
Qj
Empty queue
55
nil
TiLock=0next nil
Execution of swap assigns an order
Qi
Lock=0next nil
Qj
Tj T
secondfirst
56
Lock=1next
Qi
Lock=0next nil
Qj
T
critical section
57
Exit Code for processor i:
Ti = Compare&Swap(T, Qi, nil); //Ti is in local memory
If Ti Qi then wait until (Qi->next nil) Ti -> next->lock = 1;Delete Qi and *Qi
58
Compare&Swap(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
}
Atomic operation
59
Lock=0next nil
Lock=0next
Lock=0next
Lock=1next
TCritical section
Qi
before compare&swap
60
Lock=0next nil
Lock=0next
Lock=1next
Lock=1next
T
Qi
after compare&swap
Critical section
Ti
61
Lock=0next nil
Lock=1next
Lock=1next
T
Critical section
62
Lock=1next nil
Lock=1next
T
Critical section
63
Lock=1next nil
T
Critical section
An extreme case
64
Lock=0next nil
Lock=1next nil
T
Critical section
An extreme case
A new node Will be insterted
will execute swapwill execute
Compare&swap
65
Lock=0next nil
Lock=1next nil
T
Case 1: swap executes first
Waits until next pointsto something
66
Lock=0next nil
Lock=1next
T
Case 1: swap executes first
67
Lock=1next nil
Lock=1next
T
Case 1: swap executes first
Critical section
68
Lock=0next nil
Lock=1next nil
T
Case 2: compare&swap executes first
nil
69
Lock=0next nil
Lock=1next nil
T
Case 2: compare&swap executes first
nil
70
Lock=1next nil
T
Case 2: compare&swap executes first
Critical section