0% found this document useful (0 votes)
2 views

OS_Unit 5

Deadlock

Uploaded by

ankita shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

OS_Unit 5

Deadlock

Uploaded by

ankita shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Unit 5

DEADLOCK
What is Deadlock?

Every process needs some resources to complete its


execution. However, the resource is granted in a
sequential order.
1. The process requests for some resource.
2. OS grant the resource if it is available otherwise let the
process waits.
3. The process uses it and release on the completion.

A Deadlock is a situation where each of the computer


process waits for a resource which is being assigned to
some another process. In this situation, none of the
process gets executed since the resource it needs, is held
by some other process which is also waiting for some
other resource to be released.
What is Deadlock?
� A set of processes is deadlocked if each process in the set is
waiting for an event that only another process in the set can
cause.
� Deadlocks are a set of blocked processes each holding a
resource and waiting to acquire a resource held by another
process.
Deadlock v/s Starvation
Deadlock Starvation
Deadlock v/s Starvation

Sr. Deadlock Starvation

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

Every Deadlock is always a starvation. Every starvation need not be deadlock.


3

The requested resource is blocked by the The requested resource is continuously be


4
other process. used by the higher priority processes.
Deadlock happens when Mutual exclusion, It occurs due to the uncontrolled priority
5
hold and wait, No preemption and circular and resource management.
wait occurs simultaneously.
Conditions that lead to deadlock (Deadlock characteristics)
1. Mutual exclusion
○ Each resource is either currently assigned to exactly one process or is available.
○ Only one process at a time can use a resource.

2. Hold and wait


○ Process currently holding resources granted earlier can request more resources.

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.

All four of these conditions must be present for a deadlock to occur.


Strategies for dealing with deadlock

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.

Hold and wait


condition occurs when a process holds a resource and is
also waiting for some other resource in order to complete
its execution. Thus if we did not want the occurrence of
this condition then we must guarantee that when a
process requests a resource, it does not hold any other
resource.
Deadlock prevention
No Preemption
When a process requests some resources, if they are available, then allocate them. If in case
the requested resource is not available then we will check whether it is being used or is allocated
to some other process waiting for other resources. If that resource is not being used, then the
operating system preempts it from the waiting process and allocate it to the requesting process.
And if that resource is being used, then the requesting process must wait".

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)

The resource allocation graph is the pictorial


representation of the state of a system.
It is the complete information about all the
processes which are holding some resources or
waiting for some resources.
It also contains the information about all the
instances of all the resources whether they are
available or being used by the processes.
In Resource allocation graph, the process is
represented by a Circle while the Resource is
represented by a rectangle. Let's see the types
of vertices and edges in detail.
RAG (Resource Allocation Graph)

Edges in RAG are also of two types, one


represents assignment and other represents the
wait of a process for a resource. The above image
shows each of them.
A resource is shown as assigned to a process if
the tail of the arrow is attached to an instance to
the resource and the head is attached to a
process.
A process is shown as waiting for a resource if the
tail of an arrow is attached to the process while
the head is pointing towards the resource.
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.

2. Recovery through rollback A A A


⮩ PCB (Process Control Block) and resource
state are periodically saved at “checkpoint”. F1 F2
⮩ When deadlock is detected, rollback the R
preempted process up to the previous safe R
state before it acquired that resource.
⮩ Discard the resource manipulation that First Second B
occurred after that checkpoint. Checkpoints Checkpoints
⮩ Start the process after it is determined it can
run again.
Deadlock recovery
3. Recovery through killing processes Hold P1 Request
Hold
○ The simplest way to break a deadlock is to kill one
or more processes.
R1 R2
○ Kill all the process involved in deadlock
○ Kill process one by one. Request Hold
○ After killing each process check for deadlock
■ If deadlock recovered then stop killing more P3 P2
process
■ Otherwise kill another process Hold R3 Request
Hold
Deadlock avoidance
(Banker’s algorithm)
Section - 5
Safe and unsafe states
� A state is said to be safe if it is not deadlocked and there is some scheduling
order in which every process can run to completion even if all of them suddenly
request their maximum number of resources immediately.
� Total resources are 10
� 7 resources already allocated Process Has / Hold Max required

� So there are 3 still free


A 3 9
B 2 4
� A need 6 resources more to complete it. C 2 7
� B need 2 resources more to complete it. Free : 3
� C need 5 resources more to complete it.
Safe state
Process Has / Hold Max required Process Has / Hold Max required Process Has / Hold Max required
A 3 9 A 3 9 A 3 9
B 2 4 2 B 4 4 4 B 0 -
C 2 7 C 2 7 C 2 7
Free : 3 Free : 1 Free : 5

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


A 4 9 4
B 0 -
C 2 7
Free : 4
Deadlock avoidance
� Deadlock can be avoided by allocating resources carefully.
� Carefully analyze each resource request to see if it can be safely granted.
� Need an algorithm that can always avoid deadlock by making right choice all the time
(Banker’s algorithm).
� Banker’s algorithm for single resource
� Banker’s algorithm for multiple resource
Banker’s algorithm for single resource
� What the algorithm does is check to see if granting the request leads to an
unsafe state. If it does, the request is denied.
� If granting the request leads to a safe state, it is carried out.
� If we have situation as per figure
○ then it is safe state Process Has / Hold Max required
○ because with 10 free units A 0 6
○ one by one all customers can be served. B 0 5
C 0 4
D 0 7
Free : 10
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 1 6 A 1 6
B 1 5 B 1 5 B 1 5
C 2 4 C 4 4 C 0 -
2
D 4 7 D 4 7 D 4 3 7
Free : 2 Free : 0 Free : 4

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

• The order of execution is C, D, B, A. So if we can find proper order of


execution then there is no deadlock.
Banker’s algorithm for single resource (unsafe state)
Process Has / Hold Max required
A 1 6
B 2 5
C 2 4
D 4 7
Free : 1
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

no of resources still needed by each


Proc Plott Scan Proc Plott Scan
Driv Rom Driv Rom
ess ers ners ess ers ners
es s es s

P1 3 0 1 1 no of resources held by each P1 1 1 0 0

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

no of resources still needed by each


Proc Plott Scan Proc Plott Scan
Driv Rom Driv Rom
ess ers ners ess ers ners
es s es s

P1 3 0 1 1 no of resources held by each P1 1 1 0 0

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

no of resources still needed by each


Proc Plott Scan Proc Plott Scan
Driv Rom Driv Rom
ess ers ners ess ers ners
es s es s

P1 3 0 1 1 no of resources held by each P1 1 1 0 0

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

no of resources still needed by each


Proc Plott Scan Proc Plott Scan
Driv Rom Driv Rom
ess ers ners ess ers ners
es s es s

P1 4 1 1 1 no of resources held by each P1 0 0 0 0

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

no of resources still needed by each


Proc Plott Scan Proc Plott Scan
Driv Rom Driv Rom
ess ers ners ess ers ners
es s es s

P1 - - - - no of resources held by each P1 0 0 0 0

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

no of resources still needed by each


Proc Plott Scan Proc Plott Scan
Driv Rom Driv Rom
ess ers ners ess ers ners
es s es s

P1 - - - - no of resources held by each P1 0 0 0 0

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

no of resources still needed by each


Proc Plott Scan Proc Plott Scan
Driv Rom Driv Rom
ess ers ners ess ers ners
es s es s

P1 - - - - no of resources held by each P1 0 0 0 0

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

no of resources still needed by each


Proc Plott Scan Proc Plott Scan
Driv Rom Driv Rom
ess ers ners ess ers ners
es s es s

P1 - - - - no of resources held by each P1 0 0 0 0

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

no of resources still needed by each


Proc Plott Scan Proc Plott Scan
Driv Rom Driv Rom
ess ers ners ess ers ners
es s es s

P1 - - - - no of resources held by each P1 0 0 0 0

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

no of resources still needed by each


Proc Plott Scan Proc Plott Scan
Driv Rom Driv Rom
ess ers ners ess ers ners
es s es s

P1 - - - - no of resources held by each P1 0 0 0 0

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

no of resources still needed by each


Proc Plott Scan Proc Plott Scan
Driv Rom Driv Rom
ess ers ners ess ers ners
es s es s

P1 - - - - no of resources held by each P1 0 0 0 0

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

no of resources still needed by each


Proc Plott Scan Proc Plott Scan
Driv Rom Driv Rom
ess ers ners ess ers ners
es s es s

P1 3 0 1 1 no of resources held by each P1 1 1 0 0

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.

You might also like