Mutual exclusion - PowerPoint PPT Presentation

1 / 71
About This Presentation
Title:

Mutual exclusion

Description:

The process in the head is. in critical section. A new ... Tj. critical. section. 54. T. Lock=1. next. After if statement operation. Qi. Lock=0. next. nil ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 72
Provided by: costas
Category:

less

Transcript and Presenter's Notes

Title: 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
Algorithm 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)
4
Problem this part of the code doesnt
behave correctly
//read tail
t tail tail tail 1
Entry
//write tail
5
A good scenario
0
tail
6
A good scenario
(t0)
0
Read 0 Write 1
1
tail
7
A good scenario
(t1)
1
0
Read 1 Write 2
2
tail
8
A good scenario
(t2)
1
2
0
Read 2 Write 3
3
tail
9
A bad scenario
0
Read 0
0
tail
10
A bad scenario
0
0
Read 0 Write 1
Write 1 (delayed)
1
tail
11
A bad scenario
0
1
0
Write 1 (delayed)
Read 1 Write 2
2
tail
12
A bad scenario
0
1
0
Read 2 Write 3
Write 1 (delayed)
2
3
tail
13
A bad scenario
0
1
0
Write 1
Read 2 Write 3
2
1
Wrong value!!!
tail
14
A 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
15
V10
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
16
V10
V21
V30
V40
0
1
0
0
entry
Max 0 Max 1 1
17
V10
V21
V30
V42
0
1
0
2
entry
entry
Max 1 Max 1 2
18
V10
V21
V33
V42
0
1
3
2
entry
entry
entry
Max 2 Max 1 3
19
V14
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)
20
V14
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)
21
V14
V21
V33
V42
4
1
3
2
critical region
entry
entry
entry
reads all values
Realizes it has the highest value
22
V14
V20
V33
V42
0
4
3
2
entry
entry
entry
exit
sets value to 0
23
V14
V20
V33
V42
4
0
3
2
critical region
entry
entry
24
V14
V20
V33
V40
4
0
3
0
entry
entry
exit
25
V14
V20
V33
V40
4
0
3
0
entry
critical region
And so on
26
A 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
27
The maximum values they read are the same
V10
V21
V30
V41
0
1
0
1
entry
entry
28
Solution use IDs to break symmetries
(lowest ID wins)
V10
V21
V30
V41
1, 4
0
0
1, 2
critical section
entry
29
V10
V20
V30
V41
0
1, 4
0
0
entry
exit
30
V10
V21
V30
V40
0
0
0
1, 2
critical section
31
The 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
32
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 algorithm with bounded
values)
33
Mutual 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
34
An 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
35
The Tournament Algorithm
We can implement a tournament mutual exclusion
algorithm, using the equal priority pairwise
algorithm
36
Mutual exclusion for pairs of processes
winner
37
(No Transcript)
38
Critical section
winner
39
Advantages of tournament algorithm
  • O(n) variables
  • Bounded values of variables
  • Preserves no lockout
  • (since each pair mutual exclusion
  • is no lockout)

40
MCS 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
41
Local 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
42
head
tail
Lock0
Lock0
Lock0
Lock1
next
next
nil
next
next
Critical region
T
43
head
tail
Lock1
Lock0
Lock0
Lock1
next
next
nil
next
next
Critical region
T
44
head
tail
Lock1
Lock0
Lock1
next
next
nil
next
Critical region
T
45
head
tail
Lock1
Lock1
next
nil
next
T
Critical region
46
nil
T
47
Entry 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
48
Atomic operation
Swap(T,Qi) x T //read T T Qi
// swap T with Qi return x //
return old value of T
49
Qi
Empty queue
nil
Lock0
next
nil
T
Qi is created
50
Qi
nil
Lock0
next
nil
Ti
T
After swap operation
51
Qi
in critical section
Lock1
next
nil
T
After if statement
52
Qi
Qj
critical section
Lock1
Lock0
next
nil
next
nil
T
Process j arrives
53
Qi
Qj
critical section
Lock1
Lock0
next
nil
next
nil
T
Tj
After swap operation
54
Qi
Qj
critical section
Lock1
Lock0
next
next
nil
T
After if statement operation
55
Processes i and j Arrive simultaneously
Qi
Empty queue
Lock0
nil
nil
next
Qj
T
Lock0
nil
next
56
Execution of swap assigns an order
first
second
Qi
Qj
nil
Lock0
Lock0
Ti
nil
nil
next
next
Tj
T
57
critical section
Qi
Qj
Lock1
Lock0
nil
next
next
T
58
Exit 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
59
Atomic 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
60
Qi
Lock0
Lock0
Lock0
Lock1
next
next
nil
next
next
Critical section
T
before compareswap
61
Qi
Ti
Lock1
Lock0
Lock0
Lock1
next
next
nil
next
next
Critical section
T
after compareswap
62
Lock1
Lock0
Lock1
next
next
nil
next
Critical section
T
63
Critical section
Lock1
Lock1
next
nil
next
T
64
An extreme case
Critical section
Lock1
next
nil
T
65
An extreme case
A new node Will be insterted
Critical section
Lock0
Lock1
next
nil
next
nil
T
will execute swap
will execute Compareswap
66
Case 1 swap executes first
Lock0
Lock1
next
nil
next
nil
Waits until next points to something
T
67
Case 1 swap executes first
Lock0
Lock1
next
nil
next
T
68
Case 1 swap executes first
Critical section
Lock1
Lock1
next
nil
next
T
69
Case 2 compareswap executes first
Lock0
Lock1
nil
next
nil
next
nil
T
70
Case 2 compareswap executes first
Lock0
Lock1
nil
next
nil
next
nil
T
71
Case 2 compareswap executes first
Critical section
Lock1
next
nil
T
Write a Comment
User Comments (0)
About PowerShow.com