0% found this document useful (0 votes)
8 views52 pages

Unit 4 Deadlock

The document discusses the deadlock problem in operating systems, where processes are blocked while holding resources and waiting for others. It outlines the four necessary conditions for deadlock, methods for handling it (prevention, avoidance, detection, and doing nothing), and provides examples of algorithms like the Banker’s algorithm for resource allocation and deadlock avoidance. Additionally, it describes resource allocation graphs and detection algorithms to identify and recover from deadlocks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views52 pages

Unit 4 Deadlock

The document discusses the deadlock problem in operating systems, where processes are blocked while holding resources and waiting for others. It outlines the four necessary conditions for deadlock, methods for handling it (prevention, avoidance, detection, and doing nothing), and provides examples of algorithms like the Banker’s algorithm for resource allocation and deadlock avoidance. Additionally, it describes resource allocation graphs and detection algorithms to identify and recover from deadlocks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 52

The deadlock problem

Deadlock is a situation where a set of


processes are blocked because each process
is holding a resource and waiting for another
resource acquired by some other process.
Consider an example when two trains are
coming toward each other on same track and
there is only one track, none of the trains can
move once they are in front of each other.
Similar situation occurs in operating systems
when there are two or more processes hold
some resources and wait for resources held
by other.
For example, in the below diagram, Process 1 is
holding Resource 1 and waiting for resource 2
which is acquired by process 2, and process 2 is
waiting for resource 1.
Deadlock characterization
Deadlock can arise if following four
conditions hold simultaneously
Mutual Exclusion: One or more than one
resource are non-sharable (Only one process can
use at a time)
Hold and Wait: A process is holding at least one
resource and waiting for resources.
No Preemption: A resource cannot be taken from
a process unless the process releases the resource.
Circular Wait: A set of processes are waiting for
each other in circular form.
Methods for handling deadlocks
 Prevention
 Ensure that the system will never enter a deadlock
state
 Avoidance
 Ensure that the system will never enter an unsafe
state
 Detection
 Allow the system to enter a deadlock state and then
recover
 Do Nothing
 Ignore the problem and let the user or system
administrator respond to the problem; used by most
operating systems, including Windows and UNIX
Deadlock Prevention
If we can be able to violate one of the four
necessary conditions and don't let them occur
together then we can prevent the deadlock.
Let's see how we can prevent each of the
conditions:
 Mutual Exclusion
Mutual section from the resource point of view is
the fact that a resource can never be used by
more than one process simultaneously which is
fair enough but that is the main reason behind
the deadlock. If a resource could have been used
by more than one process at the same time then
the process would have never been waiting for
any resource.
However, if we will be able to violate resources
behaving in the mutually exclusive manner then
the deadlock can be prevented.
Spooling
 For a device like printer, spooling can work. There
is a memory associated with the printer which
stores jobs from each of the process into it.
 Later, Printer collects all the jobs and print each
one of them according to FCFS.
 By using this mechanism, the process doesn't have
to wait for the printer and it can continue whatever
it was doing.
 Later, it collects the output when it is produced.
Hold and wait
 we must guarantee that whenever a process
requests a resource, it does not hold any other
resources
 Require a process to request and be allocated all its
resources before it begins execution, or allow a
process to request resources only when the process
has none
 Result: Low resource utilization; starvation possible
No preemption
 If a process that is holding some resources
requests another resource that cannot be
immediately allocated to it, then all resources
currently being held are released
 Preempted resources are added to the list of
resources for which the process is waiting
 A process will be restarted only when it can
regain its old resources, as well as the new
ones that it is requesting
Circular Wait
 To violate circular wait, we can assign a priority
number to each of the resource. A process can't
request for a lesser priority resource. This ensures
that not a single process can request a resource
which is being utilized by some other process and
no cycle will be formed.
 Each resource will be assigned with a numerical
number. A process can request for the resources
only in increasing order of numbering.
For Example, if P1 process is allocated R5
resources, now next time if P1 ask for R4, R3 lesser
than R5 such request will not be granted, only
request for resources more than R5 will be granted.
Deadlock Avoidance
 In deadlock avoidance, the request for any resource
will be granted if the resulting state of the system
doesn't cause deadlock in the system.
 The state of the system will continuously be checked
for safe and unsafe states.
 In order to avoid deadlocks, the process must tell OS,
the maximum number of resources a process can
request to complete its execution.
 The simplest and most useful approach states that the
process should declare the maximum number of
resources of each type it may ever need.
 The Deadlock avoidance algorithm examines the
resource allocations so that there can never be a
circular wait condition.
Safe state
 When a process requests an available resource, the system must
decide if immediate allocation leaves the system in a safe state
 A system is in a safe state only if there exists a safe sequence
 A sequence of processes <P , P , …, P > is a safe sequence for
1 2 n
the current allocation state if, for each Pi, the resource requests
that Pi can still make, can be satisfied by currently available
resources plus resources held by all Pj, with j < i.
 That is:
 If the Pi resource needs are not immediately available, then Pi
can wait until all Pj have finished
 When Pj is finished, Pi can obtain needed resources, execute,
return allocated resources, and terminate
 When Pi terminates, Pi +1 can obtain its needed resources, and
so on
 If a system is in safe state  no deadlocks

 If a system is in unsafe state  possibility of


deadlock

 Avoidance  ensure that a system will never


enter an unsafe state
Safe, unsafe and deadlock state
Avoidance algorithms
Resource allocation graph
Banker’s algorithm
Resource allocation graph
As its name suggests, the resource allocation
graph 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.
Vertices are mainly of two types, Resource and
process. Each of them will be represented by a
different shape. Circle represents process
while rectangle represents resource.
A resource can have more than one instance.
Each instance will be represented by a dot
inside the rectangle.
Edges in RAG are also of two types, one
represents assignment and other represents
the wait of a process for a resource.
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.
Deadlock Detection using RAG
If a cycle is being formed in a Resource allocation
graph where all the resources have the single
instance then the system is deadlocked.
The following example contains three processes
P1, P2, P3 and three resources R1, R2, R3. All the
resources are having single instances each.
If we analyze the graph then we can find out that
there is a cycle formed in the graph since the
system is satisfying all the four conditions of
deadlock.
Allocation Matrix
Allocation matrix can be formed by using the
Resource allocation graph of a system. In
Allocation matrix, an entry will be made for
each of the resource assigned.
Request Matrix
In request matrix, an entry will be made for
each of the resource requested.

Avial = (0,0,0)
Neither we are having any resource available in the system
nor a process going to release. Each of the process needs at
least single resource to complete therefore they will
continuously be holding each one of them.
We cannot fulfill the demand of at least one process using the
available resources therefore the system is deadlocked as
Resource-Allocation Graph (Cont.)
 Process

 Resource Type with 4 instances

 Pi requests instance of Rj

Pi
Rj
 Pi is holding an instance of Rj

Pi
Rj
Resource Allocation Graph With A Deadlock
Before P3 requested an After P3 requested an
instance of R2 instance of R2
Graph With A Cycle But No Deadlock

Process P4 may release its instance of resource type R2. That


resource can then be allocated to P3, thereby breaking the cycle.
Relationship of cycles to deadlocks
 If a resource allocation graph contains no cycles
 no deadlock

 If a resource allocation graph contains a cycle


and if only one instance exists per resource type 
deadlock

 If a resource allocation graph contains a cycle


and if several instances exists per resource type 
possibility of deadlock
 In order to recover the system from deadlocks, either
OS considers resources or processes.
 For Resource
 Preempt the resource
 We can snatch one of the resources from the owner of the
resource (process) and give it to the other process with the
expectation that it will complete the execution and will release
this resource sooner. Well, choosing a resource which will be
snatched is going to be a bit difficult.
 Rollback to a safe state
 System passes through various states to get into the deadlock
state. The operating system can rollback the system to the
previous safe state. For this purpose, OS needs to implement
check pointing at every state.
 The moment, we get into deadlock, we will rollback all the
allocations to get into the previous safe state.
For Process
Kill a process
 Killing a process can solve our problem but the
bigger concern is to decide which process to kill.
Generally, Operating system kills a process which
has done least amount of work until now.
Kill all process
 This is not a suggestible approach but can be
implemented if the problem becomes very serious.
Killing all process will lead to inefficiency in the
system because all the processes will execute again
from starting.
Banker’s Algorithm
The banker’s algorithm is a resource
allocation and deadlock avoidance algorithm.
Let ‘n’ be the number of processes in the
system and ‘m’ be the number of resources
types.
Allocationi specifies the resources currently
allocated to process Pi and Needi specifies the
additional resources that process Pi may still
request to complete its task.
Banker’s algorithm consist of Safety
algorithm and Resource request algorithm
Safety Algorithm
The algorithm for finding out whether or not a system
is in a safe state can be described as follows:
 1) Let Work and Finish be vectors of length ‘m’ and ‘n’
respectively.
Initialize: Work = Available
Finish[i] = false; for i=1, 2, 3, 4….n
 2) Find an i such that both
a) Finish[i] = false
b) Needi <= Work
if no such i exists goto step (4)
 3) Work = Work + Allocation[i]
Finish[i] = true
goto step (2)
 4) if Finish [i] = true for all i
then the system is in a safe state
 Resource-Request Algorithm
 Let Request be the request array for process P . Request [j] = k
i i i
means process Pi wants k instances of resource type R j. When a
request for resources is made by process P i, the following
actions are taken:
 1) If Request <= Need
i i
Goto step (2) ; otherwise, raise an error condition, since the
process has exceeded its maximum claim.
 2) If Request <= Available
i
Goto step (3); otherwise, Pi must wait, since the resources are
not available.
 3) Have the system pretend to have allocated the requested
resources to process Pi by modifying the state as
follows:
Available = Available – Requesti
Allocationi = Allocationi + Requesti
Needi = Needi– Requesti
Example: Considering a system with five processes
P0 through P4 and three resources types A, B, C.
Resource type A has 10 instances, B has 5 instances
and type C has 7 instances. Suppose at time
t0 following snapshot of the system has been taken:
Question1. What will be the content of
the Need matrix?
Need [i, j] = Max [i, j] – Allocation [i, j]
So, the content of Need Matrix is:
Question2. Is the system in safe state? If
Yes, then what is the safe sequence?
Question3. What will happen if
process P1 requests one additional instance of
resource type A and two instances of resource
type C?
What if P4 requests (3,3,0)?
P0 requests (0,2,0)?
Deadlock Detection
Allow system to enter deadlock state
Detection algorithm
Recovery scheme
Resource-Allocation Graph Corresponding wait-for graph
Detection Algorithm
1. Let Work and Finish be vectors of length m and n,
respectively Initialize:
(a) Work :- Available
(b) For i = 1,2, …, n, if Allocationi  0, then
Finish[i] := false;otherwise, Finish[i] := true.
2. Find an index i such that both:
(a) Finish[i] = false
(b) Requesti  Work
If no such i exists, go to step 4.
3. Work := Work + Allocationi
Finish[i] := true
go to step 2.
4. If Finish[i] = false, for some i, 1  i  n, then the system is in
deadlock state. Moreover, if Finish[i] = false, then Pi is
deadlocked.
Example of Detection
Algorithm
Five processes P through P ; three resource types
0 4
A (7 instances), B (2 instances), and C (6 instances).
Snapshot at time T :
0
AllocationRequestAvailable
ABC ABC ABC
P0 0 1 0 000 000
P1 2 0 0 202
P2 3 0 3 0 0 0
P3 2 1 1 100
P4 0 0 2 002
Sequence <P , P , P , P , P > will result in Finish[i]
0 2 3 4 1
= true for all i.
Example (Cont.)
P requests an additional instance of type C.
2
Request
ABC
P0 0 0 0
P1 2 0 2
P2 0 0 1
P3 1 0 0
P4 0 0 2
State of system?
 Can reclaim resources held by process P , but
0
insufficient resources to fulfill other processes; requests.
 Deadlock exists, consisting of processes P , P , P , and
1 2 3
P4.
Recovery from Deadlock: Process Termination

Abort all deadlocked processes.


Abort one process at a time until the deadlock
cycle is eliminated.
In which order should we choose to abort?
 Priority of the process.
 How long process has computed, and how much
longer to completion.
 Resources the process has used.
 Resources process needs to complete.
 How many processes will need to be terminated.
 Is process interactive or batch?
Recovery from Deadlock: Resource Preemption

Selecting a victim – minimize cost.


Rollback – return to some safe state, restart
process fro that state.
Starvation – same process may always be
picked as victim, include number of rollback
in cost factor.

You might also like