Mutual Exclusion Algorithms
Mutual Exclusion Algorithms
Mutual Exclusion Algorithms
Non-token based:
A site/process can enter a critical section when an
assertion (condition) becomes true.
Algorithm should ensure that the assertion will be true
in only one site/process.
Token based:
A unique token (a known, unique message) is shared
among cooperating sites/processes.
Possessor of the token has access to critical section.
Need to take care of conditions such as loss of token,
crash of token holder, possibility of multiple tokens, etc.
Performance
Performance metrics
Next site
enters CS
Last site
exits CS
Time
Synchronization
delay
CS Request
arrives
Messages
sent
Enter CS
Exit CS
Time
Response Time
Performance ...
Simple Solution
Notations:
Si: Site I
Ri: Request set, containing the ids of all Sis from which
permission must be received before accessing CS.
Non-token based approaches use time stamps to order requests
for CS.
Smaller time stamps get priority over larger ones.
Lamports Algorithm
Lamports Algorithm
Requesting CS:
Executing CS:
Releasing CS:
Exiting CS: send a time stamped RELEASE message to all sites in its
request set.
Receiving RELEASE message: Sj removes Sis request from its queue.
Lamports Algorithm
Performance.
Optimization
10
(2,1)
S2
(1,2)
S3
Step 2:
S1
(1,2) (2,1)
S2 enters CS
S2
(1,2) (2,1)
S3
(1,2) (2,1)
11
Lamports: Example
Step 3:
S1
(1,2) (2,1)
S2 leaves CS
S2
(1,2) (2,1)
S3
Step 4:
S1
(1,2) (2,1)
(2,1)
(1,2) (2,1)
S1 enters CS
S2
(2,1)
(1,2) (2,1)
S3
(1,2) (2,1)
(2,1)
12
Ricart-Agrawala Algorithm
13
Ricart-Agrawala: Performance
Performance:
Optimization:
14
Ricart-Agrawala: Example
Step 1:
S1
(2,1)
S2
(1,2)
S3
Step 2:
S1
S2 enters CS
S2
(2,1)
S3
15
Ricart-Agrawala: Example
Step 3:
S1
S1 enters CS
S2
(2,1)
S3
S2 leaves CS
16
Maekawas Algorithm
17
Requesting CS
18
Request Subsets
Example k = 2; (N = 3).
Example k = 3; N = 7.
19
Performance
Synchronization delay: 2T
Messages: 3 times square root of N (one each for REQUEST,
REPLY, RELEASE messages)
Deadlocks
20
Handling Deadlocks
21
Handling Deadlocks
REQUEST(tsi,i) to Sj:
INQUIRE(j) to Sk:
YIELD(k) to Sj:
Sj is locked by Sk -> Sj sends FAILED to Si, if Sis request has higher time
stamp.
Otherwise, Sj sends INQUIRE(j) to Sk.
Sj assumes it has been released by Sk, places Sks request in its queue
appropriately, sends a REPLY(j) to the top request in its queue.
22
Token-based Algorithms
23
Suzuki-Kasami Algorithm
24
After finishing CS
(assuming Si has token), LN[i] := RNi[i]
Token consists of Q and LN. Q is a queue of requesting sites.
Token holder checks if RNi[j] = LN[j] + 1. If so, place j in Q.
Send token to the site at head of Q.
Performance
25
Suzuki-Kasami: Example
Step 1: S1 has token, S3 is in queue
Site Seq. Vector RN
S1 10, 15, 9
S2 10, 16, 9
S3 10, 15, 9
Step 2: S3 gets token, S2 in queue
Site Seq. Vector RN
S1 10, 16, 9
S2 10, 16, 9
S3 10, 16, 9
Step 3: S2 gets token, queue empty
Site Seq. Vector RN
S1 10, 16, 9
S2 10, 16, 9
S3 10, 16, 9
Token Vect. LN
10, 15, 8
Token Queue
3
Token Vect. LN
Token Queue
10, 15, 9
Token Vect. LN
Token Queue
10, 16, 9
<empty>
26
Instead of broadcast: each site maintains information on other sites, guess the
sites likely to have the token.
Data Structures:
Si maintains SVi[1..M] and SNi[1..M] for storing information on other sites: state
and highest sequence number.
Token contains 2 arrays: TSV[1..M] and TSN[1..M].
States of a site
R : requesting CS
E : executing CS
H : Holding token, idle
N : None of the above
Initialization:
27
Requesting CS
28
Releasing CS
29
Singhal: Example
Sn
S4
R
R
R
N
N
N
S3
S2
S1
H
1
N
2
Sn
S4
S2
S1
S3
R
N
N
N
30
Singhal: Example
Assume there are 3 sites in the system. Initially:
Site 1: SV1[1] = H, SV1[2] = N, SV1[3] = N. SN1[1], SN1[2], SN1[3] are 0.
Site 2: SV2[1] = R, SV2[2] = N, SV2[3] = N. SNs are 0.
Site 3: SV3[1] = R, SV3[2] = R, SV3[3] = N. SNs are 0.
Token: TSVs are N. TSNs are 0.
Assume site 2 is requesting token.
S2 sets SV2[2] = R, SN2[2] = 1.
S2 sends REQUEST(2,1) to S1 (since only S1 is set to R in SV[2])
S1 receives the REQUEST. Accepts the REQUEST since SN1[2] is smaller than
the message sequence number.
Since SV1[1] is H: SV1[2] = R, TSV[2] = R, TSN[2] = 1, SV1[1] = N.
Send token to S2
S2 receives the token. SV2[2] = E. After exiting the CS, SV2[2] = TSV[2] = N.
Updates SN, SV, TSN, TSV. Since nobody is REQUESTing, SV2[2] = H.
Assume S3 makes a REQUEST now. It will be sent to both S1 and S2. Only S2
responds since only SV2[2] is H (SV1[1] is N now).
31
Raymonds Algorithm
Sites are arranged in a logical directed tree. Root: token holder. Edges:
directed towards root.
Every site has a variable holder that points to an immediate neighbor
node, on the directed path towards root. (Roots holder point to itself).
Requesting CS
If Si does not hold token and request CS, sends REQUEST upwards
provided its request_q is empty. It then adds its request to request_q.
Non-empty request_q -> REQUEST message for top entry in q (if not
done before).
Site on path to root receiving REQUEST -> propagate it up, if its
request_q is empty. Add request to request_q.
Root on receiving REQUEST -> send token to the site that forwarded the
message. Set holder to that forwarding site.
Any Si receiving token -> delete top entry from request_q, send token to
that site, set holder to point to it. If request_q is non-empty now, send
REQUEST message to the holder site.
32
Raymonds Algorithm
Performance
33
Token
holder
S1
S2
S4
Token
request
S3
S6
S5
S7
Step 2:
S1
S3
S2 Token
S4
S5
S6
S7
34
S2
S4
S5
S6
S7
Token
holder
35
Comparison
Non-Token
Lamport
2T+E
Ricart-Agrawala 2T+E
Maekawa
2T+E
T
T
2T
Messages(ll)
Messages(hl)
3(N-1)
2(N-1)
3*sq.rt(N)
3(N-1)
2(N-1)
5*sq.rt(N)
Token
Messages(ll)
Messages(hl)
Suzuki-Kasami
Singhal
Raymond
2T+E
2T+E
T(log N)+E
N
N/2
log(N)
N
N
4
T
T
Tlog(N)/2
36