OS_Unit 5
OS_Unit 5
DEADLOCK
What is Deadlock?
Deadlock is a situation where no process got Starvation is a situation where the low
1
blocked and no process proceeds priority process got blocked and the high
priority processes proceed.
Deadlock is an infinite waiting. Starvation is a long waiting but not infinite.
2
3. No preemption
○ Previously granted resources cannot be forcibly taken away from process.
4. Circular wait
○ There must be a circular chain of 2 or more processes. Each process is waiting for resource that is
held by next member of the chain.
1. Deadlock Prevention
○ aim to condition a system to remove any possibility of deadlock occurring
2. Deadlock Avoidance
○ by identifying safe states and unsafe states
3. Deadlock Detection
○ methods use in which deadlock can occurs
4. Deadlock Recovery
○ used to resolve the deadlock from the system
Deadlock prevention
Mutual Exclusion
This condition must hold for non-sharable resources. For example, a printer cannot be
simultaneously shared by several processes. In contrast, Sharable resources do not require
mutually exclusive access and thus cannot be involved in a deadlock. A good example of a
sharable resource is Read-only files because if several processes attempt to open a read-only
file at the same time, then they can be granted simultaneous access to the file.
Circular Wait
it can be bypassed if the operating system prevents the formation of the circle
Deadlock ignorance (Ostrich Algorithm)
� When storm approaches, an ostrich puts his head in the
sand (ground) and pretend (imagine) that there is no
problem at all.
� Ignore the deadlock and pretend that deadlock never
occur.
� Reasonable if
○ deadlocks occur very rarely
○ difficult to detect
○ cost of prevention is high
� UNIX and Windows takes this approach
RAG (Resource Allocation Graph)
Example
Let's consider 3 processes P1, P2 and P3, and
two types of resources R1 and R2. The resources
are having 1 instance each.
According to the graph, R1 is being used by P1,
P2 is holding R2 and waiting for R1, P3 is waiting
for R1 as well as R2.
The graph is deadlock free since no cycle is being
formed in the graph.
RAG (Resource Allocation Graph)
Deadlock detection for single resource (RAG - Resource Allocation
Graph)
▪ We are starting from node D.
R A B ▪ Empty list L = ()
▪ Add current node so Empty list = (D).
C S D T E ▪ From this node there is one outgoing arc to T so
add T to list.
▪ So list become L = (D, T).
F U DEADLOCK V
▪ Continue this step….so we get list as below
L = (D, T, E)………… L = (D, T, E, V, G, U, D)
W G ▪ In the above step in list the node D appears twice,
so deadlock.
Deadlock detection for single resource (RAG - Resource Allocation Graph)
� Algorithm for detecting deadlock for single resource
○ For each node, N in the graph, perform the following five steps with N as the starting node.
1) Initialize L to the empty list, designate all arcs as unmarked.
2) Add current node to end of L, check to see if node now appears in L two times. If it does, graph
contains a cycle (listed in L), algorithm terminates.
3) From given node, see if any unmarked outgoing arcs. If so, go to step 4; if not, go to step 5.
4) Pick an unmarked outgoing arc at random and mark it. Then follow it to the new current node and
go to step 2.
5) If this is initial node, graph does not contain any cycles, algorithm terminates. Otherwise, dead
end. Remove it, go back to previous node, make that one current node, go to step 2.
Deadlock detection for multiple resources
Tape CD Tape CD
Plott Scan Plott Scan
Driv Rom Driv Rom
T= es
ers ners
s
A= es
ers ners
s
4 2 3 1 2 1 0 0
total no of each resource no of resources that are
available (free)
Tape CD Tape CD
Proc Plott Scan Proc Plott Scan
C= Driv Rom R= Driv Rom
ess ers ners ess ers ners
es s es s
P1 0 0 1 0 P1 2 0 0 1
P2 2 0 0 1 P2 1 0 1 1
P3 0 1 2 0 P3 2 1 0 0
no of resources held by each no of resources still needed by
process each process to proceed
Deadlock detection for multiple resources
Tape CD Tape CD
Plott Scan Plott Scan
Driv Rom Driv Rom
T= es
ers ners
s
A= es
ers ners
s
4 2 3 1 0 0 0 0
total no of each resource no of resources that are
available (free)
Tape CD Tape CD
Proc Plott Scan Proc Plott Scan
C= Driv Rom R= Driv Rom
ess ers ners ess ers ners
es s es s
P1 0 0 1 0 P1 2 0 0 1
P2 2 0 0 1 P2 1 0 1 1
P3 2 2 2 0 P3 2 1 0 0
no of resources held by each no of resources still needed by
process each process to proceed
Deadlock detection for multiple resources
Tape CD Tape CD
Plott Scan Plott Scan
Driv Rom Driv Rom
T= es
ers ners
s
A= es
ers ners
s
4 2 3 1 2 2 2 0
total no of each resource no of resources that are
available (free)
Tape CD Tape CD
Proc Plott Scan Proc Plott Scan
C= Driv Rom R= Driv Rom
ess ers ners ess ers ners
es s es s
P1 0 0 1 0 P1 2 0 0 1
DEADLOCK
P2 2 0 0 1 P2 1 0 1 1
P3 0 0 0 0 P3 2 1 0 0
no of resources held by each no of resources still needed by
process each process to proceed
Deadlock recovery
1. Recovery through pre-emption Hold P1 Request
○ In this method resources are temporarily taken away from its
current owner and give it to another process.
○ The ability to take a resource away from a process, have another R1 R2
process use it, and then give it back without the process noticing it is
highly dependent on the nature of the resource.
P2 Hold
○ Recovering this way is frequently difficult or impossible.
Process Has / Hold Max required Process Has / Hold Max required Process Has / Hold Max required
5
A 3 9 A 3 9 A 9 9
6
B 0 - B 0 - B 0 -
C 7 7 7 C 0 - C 0 -
Free : 0 Free : 7 Free : 1
Unsafe state
Process Has / Hold Max required Process Has / Hold Max required Process Has / Hold Max required
A 3 9 A 4 9 A 4 9
B 2 4 1 B 2 4 B 4 4
2
C 2 7 C 2 7 C 2 7
Free : 3 Free : 2 Free : 0
Process Has / Hold Max required Process Has / Hold Max required Process Has / Hold Max required
A 1 6 A 1 6 A 1 6
B 1 5 B 1 5 B 5 5
C 0 - C 0 - C 0 -
4
D 7 7 D 0 - D 0 -
Free : 1 Free : 8 Free : 4
Banker’s algorithm for single resource (safe state)
Process Has / Hold Max required Process Has / Hold Max required Process Has / Hold Max required
A 1 6 A 6 6 A 0 -
B 0 - B 0 - B 0 -
C 0 5 - C 0 - C 0 -
D 0 - D 0 - D 0 -
Free : 9 Free : 4 Free : 10
6 3 4 2 5 3 2 2 1 0 2 0
total no of each resource resources hold available (free) resources
Tape CD Tape CD
P2 0 1 0 0 P2 0 1 1 2
process to proceed
P3 1 1 1 0 P3 3 1 0 0
P4 1 1 0 1 P4 0 0 1 0
process
P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tape CD Tape CD Tape CD
Plott Scan Plott Scan Plott Scan
Driv Rom Driv Rom Driv Rom
ers ners ers ners ers ners
es s es s es s
6 3 4 2 5 3 2 2 1 0 1 0
total no of each resource resources hold available (free) resources
Tape CD Tape CD
P2 0 1 0 0 P2 0 1 1 2
process to proceed
P3 1 1 1 0 P3 3 1 0 0
P4 1 1 1 1 P4 0 0 0 0
process
P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tape CD Tape CD Tape CD
Plott Scan Plott Scan Plott Scan
Driv Rom Driv Rom Driv Rom
ers ners ers ners ers ners
es s es s es s
6 3 4 2 5 3 2 2 2 1 2 1
total no of each resource resources hold available (free) resources
Tape CD Tape CD
P2 0 1 0 0 P2 0 1 1 2
process to proceed
P3 1 1 1 0 P3 3 1 0 0
P4 - - - - P4 0 0 0 0
process
P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tape CD Tape CD Tape CD
Plott Scan Plott Scan Plott Scan
Driv Rom Driv Rom Driv Rom
ers ners ers ners ers ners
es s es s es s
6 3 4 2 5 3 2 2 1 0 2 1
total no of each resource resources hold available (free) resources
Tape CD Tape CD
P2 0 1 0 0 P2 0 1 1 2
process to proceed
P3 1 1 1 0 P3 3 1 0 0
P4 - - - - P4 0 0 0 0
process
P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tape CD Tape CD Tape CD
Plott Scan Plott Scan Plott Scan
Driv Rom Driv Rom Driv Rom
ers ners ers ners ers ners
es s es s es s
6 3 4 2 5 3 2 2 5 1 3 2
total no of each resource resources hold available (free) resources
Tape CD Tape CD
P2 0 1 0 0 P2 0 1 1 2
process to proceed
P3 1 1 1 0 P3 3 1 0 0
P4 - - - - P4 0 0 0 0
process
P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tape CD Tape CD Tape CD
Plott Scan Plott Scan Plott Scan
Driv Rom Driv Rom Driv Rom
ers ners ers ners ers ners
es s es s es s
6 3 4 2 5 3 2 2 5 0 2 0
total no of each resource resources hold available (free) resources
Tape CD Tape CD
P2 0 2 1 2 P2 0 0 0 0
process to proceed
P3 1 1 1 0 P3 3 1 0 0
P4 - - - - P4 0 0 0 0
process
P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tape CD Tape CD Tape CD
Plott Scan Plott Scan Plott Scan
Driv Rom Driv Rom Driv Rom
ers ners ers ners ers ners
es s es s es s
6 3 4 2 5 3 2 2 5 2 3 2
total no of each resource resources hold available (free) resources
Tape CD Tape CD
P2 - - - - P2 0 0 0 0
process to proceed
P3 1 1 1 0 P3 3 1 0 0
P4 - - - - P4 0 0 0 0
process
P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tape CD Tape CD Tape CD
Plott Scan Plott Scan Plott Scan
Driv Rom Driv Rom Driv Rom
ers ners ers ners ers ners
es s es s es s
6 3 4 2 5 3 2 2 2 1 3 2
total no of each resource resources hold available (free) resources
Tape CD Tape CD
P2 - - - - P2 0 0 0 0
process to proceed
P3 4 2 1 0 P3 0 0 0 0
P4 - - - - P4 0 0 0 0
process
P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tape CD Tape CD Tape CD
Plott Scan Plott Scan Plott Scan
Driv Rom Driv Rom Driv Rom
ers ners ers ners ers ners
es s es s es s
6 3 4 2 5 3 2 2 6 3 4 2
total no of each resource resources hold available (free) resources
Tape CD Tape CD
P2 - - - - P2 0 0 0 0
process to proceed
P3 - - - - P3 0 0 0 0
P4 - - - - P4 0 0 0 0
process
P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tape CD Tape CD Tape CD
Plott Scan Plott Scan Plott Scan
Driv Rom Driv Rom Driv Rom
ers ners ers ners ers ners
es s es s es s
6 3 4 2 5 3 2 2 4 2 3 2
total no of each resource resources hold available (free) resources
Tape CD Tape CD
P2 - - - - P2 0 0 0 0
process to proceed
P3 - - - - P3 0 0 0 0
P4 - - - - P4 0 0 0 0
process
P5 2 1 1 0 P5 0 0 0 0
Banker’s algorithm for multiple resource
Tape CD Tape CD Tape CD
Plott Scan Plott Scan Plott Scan
Driv Rom Driv Rom Driv Rom
ers ners ers ners ers ners
es s es s es s
6 3 4 2 5 3 2 2 6 3 4 2
total no of each resource resources hold available (free) resources
Tape CD Tape CD
P2 - - - - P2 0 0 0 0
process to proceed
P3 - - - - P3 0 0 0 0
P4 - - - - P4 0 0 0 0
process
P5 - - - - P5 0 0 0 0
Banker’s algorithm for multiple resource
Tape CD Tape CD Tape CD
Plott Scan Plott Scan Plott Scan
Driv Rom Driv Rom Driv Rom
ers ners ers ners ers ners
es s es s es s
6 3 4 2 5 3 2 2 1 0 2 0
total no of each resource resources hold available (free) resources
Tape CD Tape CD
P2 0 1 0 0 P2 0 1 1 2
process to proceed
P3 1 1 1 0 P3 3 1 0 0
P4 1 1 0 1 P4 0 0 1 1
process
P5 0 0 0 0 P5 2 1 1 0
Deadlock prevention
Section - 6
Deadlock prevention
� Deadlock can be prevented by attacking the one of the four conditions that leads
to deadlock.
1. Attacking the Mutual Exclusion Condition
■ No deadlock if each resource can be assigned to more than one process.
■ We can not assign some resources to more than one process at a time such as CD-
Recorder, Printer etc…
■ So this solution is not feasible.
2. Attacking the Hold and Wait Condition
■ Require processes to request all their resources before starting execution.
■ A process is allowed to run if all resources it needed is available. Otherwise nothing will be
allocated and it will just wait.
■ Problem with this strategy is that a process may not know required resources at start of run.
■ Resource will not be used optimally.
3. Attacking the No Preemption Condition
■ When a process P0 request some resource R which is held by another process P1 then resource
R is forcibly taken away from the process P1 and allocated to P0.
■ Consider a process holds the printer, halfway through its job; taking the printer away from this
process without having any ill effect is not possible.
■ This is not a possible option.
Deadlock prevention
� Deadlock can be prevented by attacking the one of the four conditions that leads
to deadlock.
4. Attacking the Circular Wait Condition
■ Provide a global numbering of all the resources.
1) Printer
2) Scanner
3) Plotter
4) Tape Drive
5) CD Rom
■ Now the rule is that: processes can request resources whenever they want to, but all
requests must be made in numerical order.
■ A process need not acquire them all at once.
■ Circular wait is prevented if a process holding resource n cannot wait for resource m, if m > n.
■ A process may request 1st a CD ROM, then tape drive. But it may not request 1st a tape drive,
then CD ROM.
■ Resource graph can never have cycle.