Lecture - Mutual - Exclusion
Lecture - Mutual - Exclusion
ATM1: ATM2:
enter(S); enter(S);
// AccessResource() // AccessResource()
obtain bank amount; obtain bank amount;
add in deposit; add in deposit;
update bank amount; update bank amount;
// AccessResource() end // AccessResource() end
exit(S); // exit exit(S); // exit
7
Cloud Computing and Distributed Systems
Vu Pham Distributed Mutual Exclusion
Approaches to Solve Mutual Exclusion
• Single OS:
Each while loop execution and S++ are each atomic operations – supported
via hardware instructions such as compare-and-swap, test-and-set, etc.
exit() 2. signal(S) (or V(S) or up(s)):
S++; // atomic
N6
N32
Token: N80 N5
N6
N32
Token: N80 N5
N12 N3
Token: N80 N5
Executing the critical section: Site Si enters the CS when the following two
conditions hold:
L1: Si has received a message with timestamp larger than (tsi , i ) from all other
sites.
L2: Si ’s request is at the top of request _queuei .
When a site removes a request from its request queue, its own request
may come at the top of the queue, enabling it to enter the CS.
Proof:
Proof is by contradiction. Suppose two sites Si and Sj are executing the CS
concurrently. For this to happen conditions L1 and L2 must hold at both the sites
concurrently.
This implies that at some instant in time, say t, both Si and Sj have their own
requests at the top of their request_queues and condition L1 holds at them.
Without loss of generality, assume that Si ’s request has smaller timestamp than
the request of Sj .
From condition L1 and FIFO property of the communication channels, it is clear
that at instant t the request of Si must be present in request_queuej when Sj was
executing its CS. This implies that Sj ’s own request is at the top of its own
request_queue when a smaller timestamp request, Si ’s request, is present in the
request queuej – a contradiction!
Proof:
The proof is by contradiction. Suppose a site Si ’s request has a smaller
timestamp than the request of another site Sj and Sj is able to execute the
CS before Si .
For Sj to execute the CS, it has to satisfy the conditions L1 and L2. This
implies that at some instant in time say t, Sj has its own request at the top
of its queue and it has also received a message with timestamp larger than
the timestamp of its request from all other sites.
But request_queue at a site is ordered by timestamp, and according to
our assumption Si has lower timestamp. So Si ’s request must be placed
ahead of the Sj ’s request in the request _queuej . This is a contradiction!
(1,2)
S2
S3
(1,2) (1,2)
S2
(1,1), (1,2)
S3
(1,2)
S2
(1,1), (1,2)
Site S2 enters the CS
S3
No token
Uses the notion of causality and multicast
Has lower waiting time to enter CS than Ring-
Based approach
Notes:
When a site receives a message, it updates its clock using the
timestamp in the message.
When a site takes up a request for the CS for processing, it updates its
local clock and assigns a timestamp to the request.
Proof:
Proof is by contradiction. Suppose two sites Si and Sj ‘ are executing the
CS concurrently and Si ’s request has higher priority than the request of
Sj . Clearly, Si received Sj ’s request after it has made its own request.
Thus, Sj can concurrently execute the CS with Si only if Si returns a REPLY
to Sj (in response to Sj ’s request) before Si exits the CS.
However, this is impossible because Sj ’s request has lower priority.
Therefore, Ricart-Agrawala algorithm achieves mutual exclusion.
(1,1)
S1
(1,2)
S2
S3
(1,2)
S2
S3
S3
1. A site does not request permission from all other sites, but
only from a subset of the sites.
The request set of sites are chosen such that
∀ i ∀ j : 1 ≤ i , j ≤ N : : Ri ∩ Rj ≠ Φ.
Consequently, every pair of sites has a site which
mediates conflicts between that pair.
2. A site can send out only one REPLY message at any time.
A site can send a REPLY message only after it has received
a RELEASE message for the previous REPLY message.
The request sets for sites (i.e., quorums) in Maekawa’s algorithm are
constructed to satisfy the following conditions:
M1: (∀i ∀j : i ≠ j, 1 ≤ i , j ≤ N : : Ri ∩ Rj ≠ φ)
M2: (∀i : 1 ≤ i ≤ N : : Si ∈ Ri )
M3: (∀i : 1 ≤ i ≤ N : : |Ri | = K )
M4: Any site Sj is contained in K number of Ri s, 1 ≤ i , j ≤ N .
Maekawa used the theory of projective planes and showed that
N = K (K − 1) + 1. This relation gives |Ri | =
This means site Si received a REPLY message from all sites in Ri and
concurrently site Sj was able to receive a REPLY message from all sites in Rj .
Si
REQUEST(ts,i) FAILED (j)
Block at Sj IF (ts,i) < (ts,k)
Sj
Sk
Si
Sj
INQUIRE (j)
YIELD (k)
Sk
Si
Sj assumes as if it has been
released by Sk REPLY ( j) to top
request site i.e. Si
Sj
YIELD (k)
Sk
Reference: http://research.google.com/archive/chubby.html