Ds6 Mutual Exclusion
-
Upload
daun-jeong -
Category
Documents
-
view
235 -
download
0
Transcript of Ds6 Mutual Exclusion
-
8/2/2019 Ds6 Mutual Exclusion
1/41
Mutual Exclusion for DS
-
8/2/2019 Ds6 Mutual Exclusion
2/41
Distributed Mutual Exclusion Algorithms
Two Approaches:
(1) Token-based approach(2) Permission-based approach
Permission-Based Approach:
(1) Centralized Algorithm(2) Fully Distributed Algorithm
-
8/2/2019 Ds6 Mutual Exclusion
3/41
Centralized Algorithms
Centralized Algorithm:
1. One process is elected as the coordinator.2. Whenever a process wants to enter a Critical
Section, it sends a REQUEST message to thecoordinator asking for permission.3. If no other process is currently in CS, the
coordinator sends back a REPLY grantingpermission.
4. When the process finishes execution of CS, itsends a RELEASE message to the coordinator.
-
8/2/2019 Ds6 Mutual Exclusion
4/41
Centralized Algorithms
Advantages:
1. GUARANTEES mutual exclusion and it is also FAIR.2. NO STARVATION.3. Only 3 messages per entry are required to enter the
CS: REQUEST, GRANT, and RELEASE.
Drawbacks:
1. If the coordinator crashes, the entire system may go down.2. There is no distinction between a dead coordinator and
permission denied, since in both cases nomessagescome back to the requesting process.
3. The single coordinator can become a performancebottleneck.
-
8/2/2019 Ds6 Mutual Exclusion
5/41
Fully Distributed Algorithm (FDA)
>> When process Pi wants to enter the CS, it
generates a new TS (timestamp) and sendsthe message REQUEST (Pi, TS) to allprocesses in the system.
>> On receiving a REQUEST message, a process
Pj
may reply immediately by sending aREPLY message back to Pi, or it may defersending a REPLY back. (See below for thisdecision.)
>> A process that has received a REPLY
message from all other processes in thesystem can enter the CS, queuing incomingmessage and deferring them.
>> After exiting the CS, the process sends
REPLY messages to all its deferred requests.
-
8/2/2019 Ds6 Mutual Exclusion
6/41
Decision
DECISION on whether Pj replies immediately to a
REQUEST (Pi, TS) or defer its REPLY:
>> If Pj is in the CS, then it defers its REPLY to Pi.
>> If Pj does not want to enter the CS, then itsends a REPLY immediately.
>> If Pj wants to enter the CS but has not yetentered it, then it compares its own
REQUEST TS (timestamp) with the TS of theincoming message made by Pi. If Pjs TS >Pis TS (meaning that Pj is younger), then itsends a REPLY immediately to Pi. Otherwise,
it defers the REPLY.
-
8/2/2019 Ds6 Mutual Exclusion
7/41
Fully Distributed Algorithm (FDA)
(1) Mutual exclusion is obtained.
(2) Freedom from deadlock is ensured.
(3) Freedom from starvation is ensured,
since entry to the CS is scheduledbased on the TS ordering.
(4) The number of messages per entry to
CS is 2(n-1), where nis the number ofprocesses in the system.
-
8/2/2019 Ds6 Mutual Exclusion
8/41
Disadvantages with FDA
(1) The process need to know the identity of all otherprocesses in the system. When a new process joins
the group of processes participating in the mutualexclusion algorithm:
-- The new process must receive the names of all other processes in the group.
-- The name of the new process must be distributed to all the other processes in thegroup.
(2) If one process fails, then the entire scheme collapses.Resolution:
By continuously monitoring the state of all processesin the group. If one process fails, then all other
processes are notified, so that they will no longersend REQUEST messages to the failed process.When a process recovers, it must initiate theprocedure that allows it to rejoin the group.
-
8/2/2019 Ds6 Mutual Exclusion
9/41
Token-Based Algorithm for a Ring Structure
>> When a process receives the token, it may enter the CS,keeping the token.
>> After the process exits the CS, the token is passed aroundagain.
>> If the process receiving the token does not want to enterthe CS, it passes the token to its neighbor.
>> We assume that the processes in the systems arelogically organized in a ring structure. The physicalcommunication network need not be a ring. As long asthe processes are connected to one another, it ispossible to implement a logical ring.
>> If the ring is unidirectional, freedom from starvation isensured.
>> The number of messages per entry to CS varies from 1 toinfinite.
>> Two types of failure: (1) If the token is lost, an electionmust be called to generate a new token. (2) If a processfails, a new logical ring must be established.
-
8/2/2019 Ds6 Mutual Exclusion
10/41
Abstract Ring
p2
p6 p1
p5
p4
p3
p1
p2 p3
p6 p5
p4 p2p5
token
(a) System graph (b) Abstract ring
-
8/2/2019 Ds6 Mutual Exclusion
11/41
Token-based algorithm for a ring topology
1. A process Piwishing to enter the CS sends a
request message (request, Pi) along its out-edge and blocks itself.
2. When a process not possessing the tokenreceives a request message, it forwards the
message along its out-edge.3. When a process possessing the token receives
the message (request, Pi), it performs:
a. if it is not in CS, it forms the message(token, request queue, Pi) and sends it alongits out-edge.
b. If it is in CS, it enters Piin the request queueassociated with the token.
-
8/2/2019 Ds6 Mutual Exclusion
12/41
Token-based algorithm (conti.)4. When a process completes execution of the
CS, it checks if the request queue is empty. Ifnot, it removes the first process idfrom thequeue. Let this idbe Pi. It now forms a message(token, Pi, request queue) and sends it along
its out-edge.5. When a process Pjreceives the message
(token, Pi, request queue), it checks if Pj= Pi.If so, it creates a local data structure to storethe token and copies the request queue fromthe message. It now becomes active and entersthe CS. If Pjis not equal to Pi, it forwards the
message along its out-edge.
-
8/2/2019 Ds6 Mutual Exclusion
13/41
>> Phold designates the process in possession of
the privilege token.>> The Algorithm maintains three invariants:
(1) Process Phold is the root of the tree.
(2) Each process in the system belongs to the tree.
(3) Each process Pi other than Phold has exactlyone outgoing edge (Pi, Pj) where Pj is theparent of Pi in the tree.
>> A path from Pi to Phold exists in the system forevery Pi other than Phold.
>> The algorithm requires O(log n)messages perentry to CS, where nis the number of
messages in the system.
Raymonds Algorithm for an Abstract Inverted Tree
-
8/2/2019 Ds6 Mutual Exclusion
14/41
Abstract Inverted Tree
p1
p3
p2 p4
p6
p5
p1
p6 p2
p5
p3
p4
(a) System graph (b) Abstract inverted tree
-
8/2/2019 Ds6 Mutual Exclusion
15/41
Raymonds Algorithm for Mutual Exclusion(1) A process Pi wishing to enter a CS enters its own idin the local
queue. Also, it sends a REQUEST message containing its own idonits outgoing edge.
(2) A process Pr, which receives a REQUEST from another process,performs following actions:
> Put the idof the requester in its local queue.> Send a REQUEST containing its own idon its outgoing edge, if Pr is
not equal to Phold.
(3) On completing the execution of a CS, Phold performs following:
a. Remove the process at the head of the local queue. Let it be Pi.b. Send the token to Pi.c. Reverse the tree edge (Pi, Phold).
d. If the local queue is not empty, send a REQUEST containing its ownidto Pi.
(4) A process receiving the token performs following:> If its own request is at the top of the local queue, remove it from the
queue and enter CS.> Else, perform the four actions in steps (3).
-
8/2/2019 Ds6 Mutual Exclusion
16/41
An Example of Raymonds Algorithm>> The following Figure illustrates operation of Raymonds algorithm
when processes P4 and P1 make requests to enter CS. Figure (c)shows the situation after the requests made by P4 and P1 have
reached P5, the holder of the token (see Steps (1) and (2) ofRaymond Algorithm).
>> When process P5 releases the CS, it (see Step (3))(3)a. removes the P3 from its local queue,(3)b. passes the token to P3,
(3)c. reverses the edge (P3, P5).(3)d. P5now sends a request to P3 since its local queue is not empty.
The result is shown in Figure (d).
>> P3performs similar actions (see Step 4), which result in sending the
token to process P4, reversal of edge (P4, P3) and sending of arequest by P3to P4. The result is shown in Figure (e).
>> P4 now enter s CS. After P4 completes the CS, the token istransferred to process P1 via P3 and P5 in an analogous manner,which enables P1 to enter the CS. Note that this would not have
been possible if Step 3(d) had not been executed.
-
8/2/2019 Ds6 Mutual Exclusion
17/41
Illustrating Example
p5
p1p3
p6 p2 p4
p5
p1 p3
p6 p2p4
p3 p1
p1
p1
p1
p4p4
p4 p5p4
(c) (d)
-
8/2/2019 Ds6 Mutual Exclusion
18/41
Illustrating Example
p5
p1 p3
p6 p2 p4
p5
p1 p3
p6 p2p4
p1
p1
p1
p1
p4p3
p5p4 p5
(d) (e)
-
8/2/2019 Ds6 Mutual Exclusion
19/41
ELECTION Algorithm
>> The algorithms that determine where a
new copy of the coordinator should berestarted if the coordinator fails.
>> Election algorithms assume that a
unique priority number is associatedwith each active process in the
system. Further, assume the priority
number for Pito be i.>> The coordinator is always the process
with thehighest priority number.
Th B ll Al ith
-
8/2/2019 Ds6 Mutual Exclusion
20/41
The Bully Algorithm(a) Pi sends an ELECTION message to each process with a
higher priority number.
(b) If no response is received within time T, Pi assumesthat all processes with numbers greater than i havefailed and elect itself the NEW coordinator. (Pi informsall active processes with priority numbers less than ithat Pi is the new coordinator.)
(c) If an answer is received, Pi has done its election.NOTE:
If Pi is not the coordinator, then at any time duringexecution, it may receive one of the following TWO
messages:(1) Pj is the new coordinator (j > i).
(2) Pj has started an ELECTION (j < i). Pi sends aRESPONSE to Pj and begins its own ELECTION,provided that Pi has not already initiated such an
election.
A E l
-
8/2/2019 Ds6 Mutual Exclusion
21/41
An Examplea. Assumes the system consisting of processes P1 through
P4, and initially, all processes are active; P4 is thecoordinator.
b. P1 and P4 fail. P2 determines that P4 has failed bysending a REQUEST that is not answered within time T.P2 then begins its ELECTION by sending a REQUEST toP3(This is the only active process with priority number
greater than p2.)c. P3 receives the REQUEST, responds to P2, and begins its
own ELECTION by sending an ELECTION message to P4(P3 does not know P4 has failed.)
d. P2 receives P3s RESPONSE and begins waiting to
receive a message informing it that a process with ahigher priority number has been elected.
(What if no message is received within a time interval T?This means P3 failed after it sent a RESPONSE message
to P2. P2 should RESTART the ELECTION algorithm.)
-
8/2/2019 Ds6 Mutual Exclusion
22/41
(conti.)
e. P4 does not respond within an interval T, so
P3 elects itself the new coordinator andinforms P1 and P2. (P1 does not receive themessage, since it has failed.)
f. Later, when P1 recovers, it sends anELECTION message to P2, P3, and P4.
g. P2 and P3 respond to P1 and begin their ownELECTION. P3 will again be elected, thoughthe same events as before.
h. Finally, P4 recovers and notifies P1, P2, and
P3 that it is the current coordinator.(Note: P4 sends no ELECTION requests, since
it is the process with the highest numberin the system.)
-
8/2/2019 Ds6 Mutual Exclusion
23/41
Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
Another Example
(a) Process 4 holds an election. (b) Processes 5 and 6 respond,telling 4 to stop. (c) Now 5 and 6 each hold an election.
-
8/2/2019 Ds6 Mutual Exclusion
24/41
Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
Another Example (Conti)
(d) Process 6 tells 5 to stop. (e) Process 6 wins and tells
everyone.
-
8/2/2019 Ds6 Mutual Exclusion
25/41
The Ring Algorithm
The RING Algorithm for a system where processes are organized as
a Ring:>> A ring can be logically or physically, so each process knowswho its successor is.
>> When a process notices that the coordinator is not functioning, itbuilds an ELECTION message containing a new ACTIVE LISTwith its number being the only number, and sends the messageto its successor, or the one after that, until a running process islocated.
>> At each step along the way, the sender adds its own processnumber to the ACTIVE LIST in the message effectively makingitself a candidate to be elected as coordinator.
>> Eventually, the message gets back to the process that started itall. That process recognizes this event when it receives anincoming message containing its own process number in theACTIVE LIST.
>> At this point, the message type is changed to COORDINATORand circulated once again, this time to inform everyone else whothe coordinator is and who the members of the new ring are.When this message has circulated once, it is removed and
everyone goes back to work.
-
8/2/2019 Ds6 Mutual Exclusion
26/41
Election Algorithm Using a Ring
Figure
1
0
7
6
5
2
3
4
[2]
[2,3]
Election message
no response
previouscoordinatorhas crashed
[5]
[5,6]
[5,6,0]
E l ti f th b Fi
-
8/2/2019 Ds6 Mutual Exclusion
27/41
Explanation for the above Figure
What happens if two processes, say 2 and 5,discover simultaneously that the previous coordinator,process 7, has crashed?
Answer:
Each of these builds an ELECTIONmessage and
each of them starts circulating its message,independent of the other one. Eventually, bothmessages will go all the way around, and both 2 and5 will convert them into COORDINATORmessages,
with exactly the same members and in the sameorder. When both have gone around again, both willbe removed. It does no harm to have extra messagescirculating; at worst it consumes a little bandwidth, butthis not considered wasteful.
-
8/2/2019 Ds6 Mutual Exclusion
28/41
Elections in Wireless Environments
>> The Bully and Ring Election Algorithms are based onassumptions that message passing is reliable and the topology ofthe network does not change, which are not realistic in wirelessenvironments.
>> Vasudevan (2004) proposed a solution that elects a best leader
rather than just a random for a wireless ad hocnetwork.
>> Any node in the network, called the source, can initiate an electionby sending an ELECTION message to its immediate neighbors (i.e.,the nodes in its range).
>> When a node receives an ELECTION for the first time, it
designates the sender as its parent, and subsequently sends out anELECTION message to all its immediate neighbors, except for theparent.
( i )
-
8/2/2019 Ds6 Mutual Exclusion
29/41
(conti.)>> When a node receives an ELECTION messagefrom a node other than its parent, it merely
acknowledges the receipt.>> When node R has designated node Q as its parent,it forwards the ELECTION message to its immediateneighbors (excluding Q) and waits for
acknowledgements to come in before acknowledgingthe ELECTION message from Q.
>> This waiting has an important consequence:
1st: Note that neighbors that have already selected a
parent will immediately respond to R.2nd: If all neighbors already have a parent, R is a leafnode and will be able to report back to Q quickly. Indoing so, it will also report information such as itsbattery lifetime and other resource capacity.
( i )
-
8/2/2019 Ds6 Mutual Exclusion
30/41
(conti.)>> This information will later allow Q to compare
Rs capacities to that of other downstream nodes,and select the best eligible node for leadership.
>> Of course, Q had sent an ELECTION messageonly because its own parent P had done so as
well.
>> In turn, when Q eventually acknowledges theELECTION message previously sent by P, it will
pass the most eligible node to P as well.>> In this way, the source will eventually get toknow which node is best to be selected as leader,after which it will broadcast this information to all
other nodes.
-
8/2/2019 Ds6 Mutual Exclusion
31/41
Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
Elections in Wireless Environments (1)
Election algorithm in a wireless network, with node a as thesource. (a) Initial network. (b)(e) The build-tree phase
-
8/2/2019 Ds6 Mutual Exclusion
32/41
Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
Elections in Wireless Environments (2)
Election algorithm in a wireless network, with node a as the
source. (a) Initial network. (b)
(e) The build-tree phase
-
8/2/2019 Ds6 Mutual Exclusion
33/41
Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved. 0-13-239227-5
Elections in Wireless Environments (3)
Figure 6-22. (e) The build-tree phase.
(f) Reporting of best node to source.
-
8/2/2019 Ds6 Mutual Exclusion
34/41
Deadlock Handling for DS
Deadlock prevention algorithms presented
in a centralized system can be used in adistributed system.
For example, we can use the resource-
ordering deadlock-prevention technique bysimply defining a global orderingamongthe system resources, and allowing each
process to request a resource (at any site)with unique number ionly if it is not holdinga resource with a unique number greaterthan i.
-
8/2/2019 Ds6 Mutual Exclusion
35/41
A deadlock-prevention scheme based ontimestamp ordering with resource preemption
Each process in the system is assigned aunique timestamp when it is created.
The wait-die scheme:
Based on a nonpreemptivetechnique
When process Pi requests a resourcecurrently held by Pj, Pi is allowed to waitonly
if it has a smaller timestamp than does pj (i.e.,Pi is older than Pj).
Otherwise, Pi is rolled back (dies).
-
8/2/2019 Ds6 Mutual Exclusion
36/41
(conti.)
The wound-wait scheme:
Based on a preemptivetechnique
When process Pi requests a resourcecurrently held by Pj, Pi is allowed to wait
only if it has a larger timestamp than doespj (i.e., Pi is younger than Pj).
Otherwise, Pj is rolled back (Pj is wounded
by Pi.)
Example
-
8/2/2019 Ds6 Mutual Exclusion
37/41
Example Suppose that processes P1, p2, and P3
have timestamps 5, 10, and 15,
respectively. With the wait-die scheme, if P1 requests a
resource held by P2, P1 will wait. If P3
requests a resource held by P2, P3 will berolled back.
With the wound-wait scheme, if P1requests a resource held by P2, then theresource will be preemptedfrom P2, andP2 will be rolled back. If P3 requests aresource held by P2, then P3 will wait.
Discussion
-
8/2/2019 Ds6 Mutual Exclusion
38/41
Discussion Both schemes can avoid starvation provided that, when a
process is rolled back, it isnotassigned a new timestamp.
Differences: wait-die: an older process must wait for a younger one to
release its resource.
wound-wait : an older process never waits for a younger
process. wait-die: if Pi dies and is rolled back because it has requested
a resource held by Pj, then Pi may reissue the same sequenceof requests when it is restarted. If the resource is still held by Pj,then Pi will die again. Thus, Pi may rollback (die) several timesbefore acquiring the needed resource.
wound-wait : Pi is wounded and rolled back because Pj hasrequested a resource it holds. When Pi is restarted andrequests the resource now being held by Pj, Pi waits. Thus,
fewer rollbacks occurin the wound-wait scheme.
D dl k D t ti
-
8/2/2019 Ds6 Mutual Exclusion
39/41
Deadlock Detection The deadlock-prevention algorithm may
preempt resources even if no deadlock hasoccurred.
To prevent unnecessary preemptions, we
can use a deadlock-detection algorithm. If we assume only a single resource of each
type, a cycle in the wait-for graph represents
a deadlock. Problem in DS: How to maintain the wait-for
graph?
Wait For Graph
-
8/2/2019 Ds6 Mutual Exclusion
40/41
Wait-For Graph
Local wait-for graphs are constructed as usual.
When a process Pi in site S1 needs a resourceheld by process Pj in site S2, a requestmessage is sent by Pi to site S2. The edge Pi
Pj is then inserted in the local wait-for graphof site S2.
If any local wait-for graph has a cycle,
deadlock has occurred. The fact that we find no cycles in any of the
local wait-for graphs does not mean that thereare no deadlocks, however.
-
8/2/2019 Ds6 Mutual Exclusion
41/41
Example
p1
p5
p2
p3
p2
p3
p4
Site S1Site S2
p1
p5
p2
p4
Global wait-for graph