CS8603 – DISTRIBUTED
SYSTEMS
UNIT III DISTRIBUTED MUTEX & DEADLOCK
Distributed mutual exclusion algorithms: Introduction-
Preliminaries-Lamport‘s algorithm Ricart-Agrawala algorithm-
Maekawa‘s algorithm-Suzuki–Kasami‘s broadcast algorithm.
Deadlock detection in distributed systems: Introduction-System
model-Preliminaries Models of deadlocks-Knapp‘s classification-
Algorithms for the single resource model, the AND model and the
OR model.
Distributed mutual exclusion – Introduction
Critical Section
When a process is accessing a shared variable, it is said to be in
critical section.
Shared
Process P1 variable Critical Section(CS)
is accessing the Eg: a=100
shared variable
P1 P2 Pn
Mutual Exclusion Important Note:
No two processes can be in the Processes P2,P3..Pn cannot
same CS @ the same time. This enter into CS, until P1 comes
is called Mutual Exclusion. out
Distributed mutual exclusion – Introduction
To implement Distributed Mutual Exclusion, we can use Message
Passing technique.
DME algorithms must deal with:
•Unpredictable message delays
•Incomplete knowledge of the system state
3 basic approaches for Distributed Mutual Exclusion are:
1.Token based approach
2.Non-token based approach
3.Quorum based approach
(1)Token-based approach
A unique token is shared among the sites.
A site is allowed to enter CS only if it has the token
Mutual exclusion is ensured because the token is unique.
(2)Non-token-based approach
A site enters its CS when a condition become true.
(Condition is defined on its local variables)
Mutual exclusion is ensured because the condition becomes true
only at one site at a given time.
(3)Quorum – based approach
Suppose DS has 10 sites {P1,P2,P3….P10}
Quorum1: {P3,P4,P5}
Quorum2: {P5,P6,P7}
Suppose if P1 wants to enter CS, it requests permission from
Quorum1
So, P3,P4 and P5 decides whether P1 should be allowed to enter CS.
Here, P5 which is a common site to both Quorums, will ensure that
Quorum2 sites do not permit any other site to enter CS other than P1.
Releasing the Critical Section
Site Si, upon exiting the CS, removes its request from
the top of its request queue & broadcasts a timestamped
RELEASE message to all other sites.
When a site Sj receives a RELEASE message from Si, it
removes Si’s request from its request queue.
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.
Ricart Agarwala - Illustration
Problem of deadlocks
Maekawa’s algorithm can get into deadlock bcoz:
1)A site is locked by the requesting site.
2)Requests are not timestamped
Let Ri be Request set of Si
Let Rj be Request set of Sj
Let Rk be Request set of Sk
Suppose,
Ri n Rj = {Sij} Sij is locked by Si, causes Sj to wait for Sij’s permission
Rj n Rk ={Sjk} Sjk is locked by Sj, causes Sk to wait
Rk n Ri ={Ski} Ski is locked by Sk, causes Si to wait
As all 3 sites Si, Sj and Sk are waiting this leads to deadlock.
To solve this problem, a site should give up(yield) a lock if its timestamp is
larger than the timestamp of the waiting site.
3 types of messages are required for deadlock handling:
a.FAILED
This message is sent by Si to Sj if Si cannot grant permission to sj
now
b. INQUIRE
This message is sent from Si to Sj to know whether Sj has got
permission from its quorum.
c.YIELD
This message is sent from Si to Sj if Si is giving its permission to Sj.
Thus , Maekawa algorithm requires extra messages to handle deadlocks.
Max.no.of messages required per CS execution= 5*Squareroot(N)
Deadlock Detection in Distributed Systems
Deadlocks are a fundamental problem in DS
Definition:
Deadlock is defined as the condition where a set of processes
request resources that are held by other processes.
Three strategies to deal with deadlocks
1.Deadlock Prevention
2.Deadlock avoidance
3.Deadlock Detection
Deadlock Prevention
It is achieved by:
1)A process acquires all the needed resources before it begins
execution.
2) Pre-empting the process that holds the needed resource
[Pre-empting means finishing in advance]
Deadlock Avoidance
A resource is granted to a process if the resulting system state is safe.
Deadlock Detection
1.Status of the process-resource interaction is checked to see if there is
a deadlock situation
2.To solve the deadlock problem, abort the deadlocked process.
System Model
DS consists of a set of processes connected by a N/w.
Communication delay is finite but unpredictable.
Distributed program is composed of n asynchronous processes P1,P2…
Pn.
They communicate by message passing
Each process is running on a different processor.
No global memory, No global clock
Messages may be:
•Delivered out of order
•May be lost
•Duplicated due to retransmission
•Processes may fail
•Communication links may go down
•System modelled as directed graph in which
Vertex- Process
Edge-- Channels
Assumptions
1.Systems have reusable resources
2.When one process access a resource , others should wait.
3.Only one copy of each resource is available.
In DS, handling of deadlocks is highly complicated because:
1)No site has accurate knowledge of the current state of the system
2)There is a finite unpredictable delay.
a)Deadlock prevention is achieved by:
1)A process acquire all needed resources simultaneously before it
begins execution.
2)Pre-empting a process that holds the needed resource.
Drawbacks:
1.This approach is highly inefficient
2.Impractical
b) Deadlock avoidance
A resource is granted to a process if the resulting system state is safe.
It is also impractical.
c)Deadlock Detection
It requires checking the status of process-resource interaction and
finding if there is any cyclic wait.
It is the best approach to handle deadlocks in DS.
Wait-For-Graph(WFG)
State of the system can be modelled by directed graph called WFG.
Here,
Nodes are processes
If P1 is blocked and waiting for P2 to release some resource, then,
there is a directed edge from P1 to P2.
Figure: Example of WFG
P1 waits for P2
P2 waits for P3
P3 waits for P4 which in turn waits for P1.
This is a directed cycle which indicates deadlock
Preliminaries
1.Deadlock handling strategies
2.Issues in deadlock detection
(1)Deadlock handling strategies
a)Deadlock prevention
b) Deadlock avoidance
c)Deadlock Detection
Issues in Deadlock detection
There are 2 issues in deadlock detection:
1.Detection of Existing deadlocks
2.Resolution of detected deadlocks
Detection of Deadlocks
It involves 2 issues:
1.Maintenance of WFG
2.Searching of WFG for the presence of cycles.
Algorithms for deadlock detection are categorised as:
1.Centralized algorithms
2.Distributed algorithms
3.Hierarchical algorithms
Correctness Criteria
A deadlock detection algorithm must satisfy the following 2 conditions:
1.Progress(No undetected deadlocks)
The algorithm should detect all existing deadlocks in a finite time.
The deadlock detection activity should continuously progress until the
deadlock is detected.
2. Safety(No False Deadlocks)
It should not report deadlocks that does not exist(called False
deadlocks or Phantom deadlocks)
In DS, where there is no global memory & no global clock, it is difficult to
design a correct deadlock detection algorithm.
Resolution of a detected deadlock
It involves:
1. Breaking the existing wait-for dependencies b/w the processes in
order to resolve the deadlock.
2. Rollback the deadlocked processes and assign their resources so
that they can resume execution.
3. Information regarding wait-for-dependencies are sent along the
edges of the wait-for-graph.
4. So, when a wait-for-dependency is broken , the corresponding
information should be immediately cleaned from the system.
5. If this information is not cleaned in a timely manner, it may result in
the detection of phantom deadlocks.
Models of Deadlocks
Deadlock detection algorithms may be classified based on the complexity
of the resource requests they permit:
1.Single resource model
2. AND model
3. OR model
4. AND-OR model
5. (P Q) model
6. Unrestricted model
1.Single resource model
Simplest resource model in DS
Process can have only one outstanding request for only one unit of
resource.
Max. Outdegree of node in a WFG =1
Presence of a cycle in WFG=> Deadlock
(2) AND model
Process can request more than one resource simultaneously
Request is satisfied only after all resources are granted to it.
Requested resources may exist at different locations
Outdegree > 1
Presence of cycle=>deadlock
Cycle=>deadlock
But Deadlock need not be indicated by a cycle
Eg:
Process P44 not in cycle but deadlocked as it is dependent on P24.
AND model is more general than Single resource model
(2)OR model
Process can request more than one resource simultaneously
Request is satisfied if any one of the resources is granted to it.
Requested resources may exist at different locations
Nodes are called OR nodes.
Presence of cycle does not imply deadlock.
Presence of a knot only indicates deadlock
A deadlock in OR model is defined as below:
A process Pi is blocked if it has a pending OR request to be
satisfied.
With every blocked process, there is a dependent set of processes.
A process moves from idle to active state on receiving a grant msg
from any of the processes in its dependent set,
A process if permanently blocked if it never receives a grant from
any of the processes.
(3) AND – OR model
It is the generalization of AND & OR model
Request for multiple resources is of the form:
X and ( y or z)
Requested resources may exist at different locations.
Deadlock cannot be detected from WFG
Deadlock is a stable property. Once it exists it does not go away by
itself. By using this property deadlock can be detected.
But it is a very inefficient strategy.
(4) P out of Q model
It is another form of AND-OR model
Possible to request and get k out of n resources
Both are same in expressive power
but P out of Q model is more compact than AND-OR model
Every request in P out of Q can be expressed in AND-OR and vice-
versa.
(5) Unrestricted model
No assumptions are made about resource requests
Only assumption is:
Deadlock is stable
More general model
More theoretical and involves more overhead.
Chandy Misra Haas Algorithm for AND model
It is based on edge chasing
It uses probe
Probe is a triplet(i,j,k)
Deadlock detection is initiated for Pi.
Probe is sent by home site of Pj to home site of Pk.
Travels along the edges of Global WFG and if probe reaches to
the initiator itself , then a deadlock is detected
Terminologies used in this algorithm
1. Dependent process
Pi is said to be dependent on some process Pj:
a) if there exists a sequence of processes Pi, Pi1, Pi2,…Pim,
Pj such that except Pj all are blocked.
b) Except Pi each process is holding a resource for which the
previous process is waiting.
Terminologies used in this algorithm
2. Locally Dependent process
Pi is said to be locally dependent on some process Pj:
a) Pi is dependent on Pj &
b) Both Pi and Pj are at the same site.
Data Structures
Each process maintains a boolean array called
dependent i [ ]
If Pj is dependent on Pi, then
dependent i [j] = true
If Pj is not dependent on Pi, then
dependent i [j] = false
Algorithm
Process of sending probe
1. If Process Pi is locally dependent on itself then declare
deadlock.
2. Else for all Pj and Pk check the following:
2a) Pi is locally dependent on Pj
2b) Pj is waiting on Pk
2c) Pj and Pk are on different sites.
If 2a) 2b) and 2c) are true, send probe(i,j,k) to the home site of
Pk.
On receiving of probe(i,j,k) Pk does the following:
1. Pk checks the following conditions:
(1a) Pk is blocked
(1b) dependent k [i]=false
(1c) Pk has not replied to all requests of Pj.
If all above conditions are found to be true then:
1. Set dependent k[i]=true
2. If k==i, declare Pi is deadlocked
3. Else for all Pm and Pn check the following conditions:
a) Pk is locally dependent on Pm
b) Pm is waiting on Pn
c) Pm and Pn are on different sites.
If a), b) & c) are true, send probe(k,m,n) to the home site of Pn.
Performance
m(n-1)/2 messages are needed to detect deadlock.
m -> no. of processes
n -> number of sites
Delay in detecting deadlock is O(n)
Advantages
Size of messages is fixed
Very short probes(only 3 integer words)
Little computation at each site
Low overhead
Does not report Phantom deadlocks.