Mutual Exclusion - PowerPoint PPT Presentation

1 / 76
About This Presentation
Title:

Mutual Exclusion

Description:

Problem: the algorithm doesn't guaranty. no lockout, a process may starve. Example: ... may never enter the critical section : faster than. 16. Mutual exclusion ... – PowerPoint PPT presentation

Number of Views:145
Avg rating:3.0/5.0
Slides: 77
Provided by: costas
Category:

less

Transcript and Presenter's Notes

Title: Mutual Exclusion


1
Mutual Exclusion
  • testset variables

2
Entry
While (testset(v) 1)
Critical section
Reset (v)
Exit
3
entry
testset
v
0
4
critical section
0
v
1
5
exit
reset
v
0
6
Two processors
entry
entry
testset
testset
v
0
7
critical section
entry
1
v
0
1
8
critical section
entry
testset
v
0
1
9
critical section
entry
1
v
0
1
10
entry
exit
testset
reset
v
0
11
critical section
0
v
1
12
exit
reset
v
0
13
Problem the algorithm doesnt guaranty
no lockout, a process may starve
Example may never enter the critical
section
critical section
entry
testset
v
1
14
Example 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
16
Mutual exclusion
  • read-modify-write variables

17
Shared variable v
Ticket of current process in critical section
v.first
Ticket of last process waiting in entry section
v.last
18
Entry
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)
19
v.first 1
v.last 0
20
p.last
0
v.first 1
v.last 0
21
entry
p.last
1
v.first 1
v.last 1
22
critical section
p.last
1
v.first 1
v.last 1
23
exit
p.last
1
v.first 2
v.last 1
24
Four processes
p.last
0
0
0
0
v.first 1
v.last 0
25
entry
p.last
1
0
0
0
v.first 1
v.last 1
26
entry
entry
p.last
1
2
0
0
v.first 1
v.last 2
27
entry
entry
entry
p.last
1
2
3
0
v.first 1
v.last 3
28
entry
entry
entry
entry
p.last
1
2
3
4
v.first 1
v.last 4
29
critical section
entry
entry
entry
p.last
1
2
3
4
v.first 1
v.last 4
30
entry
entry
exit
entry
p.last
1
2
3
4
v.first 2
v.last 4
31
critical section
entry
entry
2
3
4
v.first 2
v.last 4
32
entry
exit
entry
2
3
4
v.first 3
v.last 4
33
entry
entry
3
4
v.first 3
v.last 4
34
The behavior is similar with a queue
critical section
entry
entry

i
i1
ik
v.first i
v.last ik
(head)
(tail)
35
Good features of algorithm
  • Guarantees no lockout
  • (any process will eventually enter
  • the critical section)
  • Uses only one shared variable (v)

36
A problem values can grow unbounded


i
i1
ik
37
Solution a circular queue

1
n
2
v.first
v.last
Only n different values are needed
(for n processes)
38
Mutual exclusion
  • read/write variables

39
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
40
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)
41
Problem this part of the code doesnt
behave correctly
//read tail
t tail tail tail 1
Entry
//write tail
42
A good scenario
0
tail
43
A good scenario
(t0)
0
Read 0 Write 1
1
tail
44
A good scenario
(t1)
1
0
Read 1 Write 2
2
tail
45
A good scenario
(t2)
1
2
0
Read 2 Write 3
3
tail
46
A bad scenario
0
Read 0
0
tail
47
A bad scenario
0
0
Read 0 Write 1
Write 1 (delayed)
1
tail
48
A bad scenario
0
1
0
Write 1 (delayed)
Read 1 Write 2
2
tail
49
A bad scenario
0
1
0
Read 2 Write 3
Write 1 (delayed)
2
3
tail
50
A bad scenario
0
1
0
Write 1
Read 2 Write 3
2
1
Wrong value!!!
tail
51
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
52
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
53
V10
V21
V30
V40
0
1
0
0
entry
Max 0 Max 1 1
54
V10
V21
V30
V42
0
1
0
2
entry
entry
Max 1 Max 1 2
55
V10
V21
V33
V42
0
1
3
2
entry
entry
entry
Max 2 Max 1 3
56
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)
57
V14
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
58
V14
V21
V33
V42
4
1
3
2
critical region
entry
entry
entry
reads all values
Realizes it has the highest value
59
V10
V21
V33
V42
0
1
3
2
exit
entry
entry
entry
sets value to 0
60
V10
V21
V33
V42
0
1
3
2
entry
entry
critical region
61
V10
V21
V30
V42
0
1
0
2
entry
entry
exit
62
V10
V21
V33
V42
0
1
0
2
entry
critical region
And so on
63
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
64
The maximum values they read are the same
V10
V21
V30
V41
0
1
0
1
entry
entry
65
Solution use IDs to break symmetries
(highest ID wins)
V10
V21
V30
V41
1, 4
0
0
1, 2
entry
critical section
66
V10
V21
V30
V40
0
0
0
1, 2
entry
exit
67
V10
V21
V30
V40
0
0
0
1, 2
critical section
68
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
69
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)
70
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
71
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
72
The Tournament Algorithm
We can implement a tournament mutual exclusion
algorithm, using the equal priority pairwise
algorithm
73
Mutual exclusion for pairs of processes
winner
74
(No Transcript)
75
Critical section
winner
76
Advantages of tournament algorithm
  • O(n) variables
  • Bounded values of variables
  • Preserves no lockout
  • (since each pair mutual exclusion
  • is no lockout)
Write a Comment
User Comments (0)
About PowerShow.com