Professional Documents
Culture Documents
DISTRIBUTED SYSTEMS
Radhika Pasumarthi
Outline:
❖Definition
❖Fundamental causes of deadlocks
❖Resource allocation graphs and wait for Graph
❖Approaches to handling deadlocks
▪ Deadlock prevention
▪ Deadlock avoidance
▪ Deadlock detection and recovery
❖Current Research
❖References
What is Deadlock?
• A set of processes is in a deadlock state if every process in the set is
waiting for an event (release) that can only be caused by some other
process in the same set.
• Two common places where deadlocks may occur are with processes in
an operating system (distributed or centralized) and with transactions in
a database.
• The resources may be either physical or logical.
– Physical resources: Printers, Tape Drivers, Memory space, CPU cycles.
– Logical resources: Files, Semaphores, Monitors.
• The simplest example is process A is waiting for a resource held by
process B and process B is waiting for a resource held by process A.
Several processes can be involved in a deadlock when there exists a
cycle of processes waiting for each other. process A waits for B which
waits for C which waits for A.
Conditions for a deadlock to occur
1. Mutual Exclusion: At least one of the resources is non-sharable (i.e only a
limited number of processes can use it at a time and it is requested by a
process while it is being used by another one, the requesting process has
to wait until the resource is released).
2. Hold and Wait: There must be at least one process that is holding at least
one resource and waiting for other resources that are being hold by other
processes.
3. No preemption: No resource can be preempted before the holding
process completes its task with that resource.
If a process that is holding some resources requests another resource
and that resource cannot be allocated it, then it must release all
resources that are currently allocated to it.
4. Circular Wait: There exists a set of processes (p1,p2….pn) such that
P1 is waiting for a resource held by p2
P2 is waiting for a resource held by p3 ……….
Pn-1 is waiting for a resource held by pn.
Pn is waiting for a resource held by p1.
One protocol to ensure that the circular wait condition never holds is
“impose a linear ordering of all resources types .“ then, each
process can only request resources in an increasing order of priority.
For eg. Set priorities r1=1,r2=2,r3=3 and r4=4 .with these priorities, if
process P wants to use r1 and r3,it should first request r1 ,then r3.
As an example, consider the traffic
deadlock in the following figure
Example (Contd..)
• Consider each section of the street as a resource.
• The simple rule to avoid traffic deadlock is that a vehicle should only
enter an intersection if it is assured that it will not have to stop inside the
intersection.
Resource Allocation Graphs
• If there is a cycle, and the cycle involves only resources which have
a single instance, then a deadlock has occurred.
Resource allocation Graphs (contd..)
process
Pi Rj
Assignment edge from resource Rj to process Pi
Pi Rj
R1
P1 P1 holds 2 copies of resource R1, and
P2 holds one copy of resource R1 and
request one copy of resource R2
R2
P2
There are three cycles, so a deadlock may exists.
Actually p1, p2 and p3 are deadlocked
• If all resources have only a single instance then we can define a deadlock
detection algorithm that uses a variant of resource-allocation graph, called
a wait for graph.
P5 P5
R1 R3 R4
P2 P3 P1 P2 P3
P1
P4 P4
R2 R5
➢ Deadlock avoidance
• Impractical in single processor systems
• Impractical in distributed systems
➢ Deadlock prevention
• Linear ordering
• For static resources
• Resources to be accessed are known in advance
• Ordering accesses by timestamps
• otherwise
Deadlock detection – Centralized Algorithm
Y
X
➢ Algorithm
• Similar to edge chasing algorithm
• Send query along all wait-for edges
• We assume that there is only one query going on, for general case, we just
need to use more variables to distinguish different queries
• In this algorithm, we also need to send reply
• Any node, if received replies from all edges during the computation, is
involved in a deadlock situation
Algorithm (contd…)
Old Young
Older Young process
process process process
Wants Holds
resource Holds
resource Wants resource
resource
Young Old
process process Young Old
process process
It Dies
It will wait
Wait-die
• As we have pointed out before, killing a transaction is relatively
harmless, since by definition it can be restarted safely later.
• Wait-die:
• If an old process wants a resource held by a young process,
the old one will wait.
• If a young process wants a resource held by an old process, the
young process will be killed.
• Observation: The young process, after being killed, will then start up
again, and be killed again. This cycle may go on many times before
the old one release the resource.
Wound-wait
• When a conflict arises, instead of killing the process making the
request, we can kill the resource owner. Without transactions, killing a
process might have severe consequences. With transactions, these
effects will vanish magically when the transaction dies.