0% found this document useful (0 votes)
39 views41 pages

Deadlocks

A deadlock consists of a set of blocked processes, each holding a resource and waiting to acquire a resource held by another process in the set. There are four necessary conditions for deadlock: mutual exclusion, hold and wait, no preemption, and circular wait. Deadlocks can be handled through prevention, avoidance, or detection. Prevention ensures the system will never enter a deadlocked state by restricting one of the necessary conditions. Avoidance allows the system to reach states where deadlock is possible but avoids them. Detection finds deadlocks after they occur and recovers from them.

Uploaded by

riyasinghal1324
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)
39 views41 pages

Deadlocks

A deadlock consists of a set of blocked processes, each holding a resource and waiting to acquire a resource held by another process in the set. There are four necessary conditions for deadlock: mutual exclusion, hold and wait, no preemption, and circular wait. Deadlocks can be handled through prevention, avoidance, or detection. Prevention ensures the system will never enter a deadlocked state by restricting one of the necessary conditions. Avoidance allows the system to reach states where deadlock is possible but avoids them. Detection finds deadlocks after they occur and recovers from them.

Uploaded by

riyasinghal1324
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/ 41

Deadlocks

The Deadlock Problem

 A deadlock consists of a set of


blocked processes, each holding
a resource and waiting to acquire
a resource held by another
process in the set
 Example #1
A system has 2 disk drives
 P1 and P2 each hold one disk drive
and each needs the other one
System Model

 Resource types R1, R2, . . ., Rm


CPU cycles, memory space, I/O
devices
 Each resource type Ri has 1 or more
instances
 Each process utilizes a resource as
follows:
 request
 use
 release
Necessary Conditions
Deadlock can arise if four conditions hold simultaneous
 Mutual exclusion: only one process at a time can
use a resource
 Hold and wait: a process holding at least one
resource is waiting to acquire additional
resources held by other processes
 No preemption: a resource can be released only
voluntarily by the process holding it after that
process has completed its task
 Circular wait: there exists a set {P , P , …, P } of
0 1 0
waiting processes such that P0 is waiting for a
resource that is held by P1, P1 is waiting for a
resource that is held by P2, …, Pn–1 is waiting for a
resource that is held by
Pn, and Pn is waiting for a resource that is held by
P0
Resource-Allocation Graph

A set of vertices V and a set of edges E.


 V is partitioned into two types:
 P = {P1, P2, …, Pn}, the set consisting of all
the processes in the system

 R = {R1, R2, …, Rm}, the set consisting of all


resource types in the system
 request edge – directed edge P1  Rj
 assignment edge – directed edge
Rj  P i
Resource-Allocation Graph (Cont.)

 Process

 Resource Type with 4 instances

 Pi requests instance of Rj Pi
Rj

Rj
 Pi is holding an instance of Rj Pi
Resource Allocation Graph With A Deadlock
Before P3 requested After P3 requested an
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 and if several instances exists per
resource type  possibility of deadlock
Methods for Handling Deadlocks
Methods for Handling Deadlocks

 Prevention
 Ensurethat 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
Deadlock Prevention
Deadlock Prevention

prevent deadlock, at least one of the necessary conditions can no


 Mutual Exclusion – The mutual-exclusion
condition must hold for non-sharable resources

 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
Deadlock Prevention (Cont.)
 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 – impose a total ordering of all resource


types, and require that each process requests resources
in an increasing order of enumeration. For example:

F(tape drive) = 1
F(disk drive) = 5
F(printer) = 12
Deadlock Avoidance
Deadlock Avoidance

uires that the system has some additional a priori infor


ilable.
 Simplest and most useful model requires that
each process declare the maximum number
of resources of each type that it may need

 safe State A state is said to be safe, if it is


not deadlocked.
 A safe state is one in which the total
resource situation is such that all users
would be able to finish their execution.
Safe, Unsafe , Deadlock State
Avoidance algorithms

 For a single instance of a


resource type, use a resource-
allocation graph

 For multiple instances of a


resource type, use the banker’s
algorithm
Resource-Allocation Graph Scheme

 Introduce a new kind of edge called a claim edge

 Claim edge Pi 


Rj indicates that process Pj
may request resource Rj; which is represented by a
dashed line

 A claim edge converts to a request edge when a


process requests a resource

 A request edge converts to an assignment edge


when the resource is allocated to the process

 When a resource is released by a process, an


assignment edge reconverts to a claim edge

 Resources must be claimed a priori in the system


Resource-Allocation Graph with
Claim Edges

Assignment Request
edge edge

Claim
Claim edge
edge
Unsafe State In Resource-Allocation
Graph

Assignment Request
edge edge

Assignment
Claim edge
edge
Resource-Allocation Graph
Algorithm

 Suppose that process Pi requests a


resource Rj

 The request can be granted only if


converting the request edge to an
assignment edge does not result in
the formation of a cycle in the
resource allocation graph
Banker’s Algorithm

 Used when there exists multiple instances of


a resource type

 Each process must a priori claim maximum


use .
Data Structures for the Banker’s
Algorithm

= number of processes, and m = number of resources t


 Available: Vector of length m. If available [j] = k,
there are k instances of resource type Rj available.
 Max: n x m matrix. If Max [i,j] = k, then process Pi
may request at most k instances of resource type Rj.
 Allocation: n x m matrix. If Allocation[i,j] = k then Pi
is currently allocated k instances of Rj.
 Need: n x m matrix. If Need[i,j] = k, then Pi may need
k more instances of Rj to complete its task.

Need [i,j] = Max[i,j] – Allocation [i,j]


Safety Algorithm

1. Let Work and Finish be vectors of length m and n,


respectively. Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1

2. Find an i such that both:


(a) Finish [i] = false
(b) Needi  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] == true for all i, then the system is in a


safe state
Example of Banker’s Algorithm
 5 processes P through P ;
0 4

3 resource types:
A (10 instances), B (5instances), and C (7
instances)
Snapshot at time T0:
Allocation MaxAvailable
ABC ABCABC
P0 0 1 0 753332
P1 2 0 0 322
P2 3 0 2 902
P3 2 1 1 222
P4 0 0 2 433
Example (Cont.)

 The content of the matrix Need is defined to be


Max – Allocation
Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
 The system is in a safe state since the
sequence < P1, P3, P4, P2, P0> satisfies safety
criteria
Example: P1 Request (1,0,2)
 Check that Request  Available (that is, (1,0,2) 
(3,3,2)  true
Allocation Need Available
ABC ABC ABC
P0 0 1 0 743 230
P1 302 020
P2 3 0 2 600
P3 2 1 1 011
P4 0 0 2 431
 Executing safety algorithm shows that sequence <
P1, P3, P4, P0, P2> satisfies safety requirement
Deadlock Detection
Deadlock Detection

 For deadlock detection, the system


must provide

 An algorithm that examines the state of the


system to detect whether a deadlock has
occurred

 And an algorithm to recover from the


deadlock
Single Instance of Each Resource
Type
 Requires the creation and maintenance
of a wait-for graph
 Consists of a variant of the resource-
allocation graph
 The graph is obtained by removing the
resource nodes from a resource-allocation
graph and collapsing the appropriate edges
 Consequently; all nodes are processes

 Periodically invoke an algorithm that


searches for a cycle in the graph
 If there is a cycle, there exists a deadlock
Resource-Allocation Graph and Wait-for
Graph

Resource-Allocation Graph
Corresponding wait-for graph
Multiple Instances of a Resource
Type

Required data structures:


 Available: A vector of length m indicates the
number of available resources of each type.

 Allocation: An n x m matrix defines the


number of resources of each type currently
allocated to each process.

 Request: An n x m matrix indicates the


current request of each process. If Request
[ij] = k, then process Pi is requesting k more
instances of resource type. Rj.
Steps of Algorithm

 Let Work and Finish be vectors of length m


and n respectively. Initialize Work= Available.
For i=0, 1, …., n-1, if Allocationi = 0,
then Finish[i] = true; otherwise, Finish[i]= false.
 Find an index i such that both
a) Finish[i] == false
b) Requesti <= Work
If no such i exists go to step 4.
 Work= Work+ Allocation
i
Finish[i]= true
Go to Step 2.
 If Finish[i]== false for some i, 0<=i<n, then the
system is in a deadlocked state. Moreover,
if Finish[i]==false the process Pi is deadlocked.
For Example
 In this, Work = [0, 0, 0] &
Finish = [false, false, false, false, false]
 i=0 is selected as both Finish[0] = false and [0, 0, 0]<=[0, 0, 0].
 Work =[0, 0, 0]+[0, 1, 0] =>[0, 1, 0] &
Finish = [true, false, false, false, false].
 i=2 is selected as both Finish[2] = false and [0, 0, 0]<=[0, 1, 0].
 Work =[0, 1, 0]+[3, 0, 3] =>[3, 1, 3] &
Finish = [true, false, true, false, false].
 i=1 is selected as both Finish[1] = false and [2, 0, 2]<=[3, 1, 3].
 Work =[3, 1, 3]+[2, 0, 0] =>[5, 1, 3] &
Finish = [true, true, true, false, false].
 i=3 is selected as both Finish[3] = false and [1, 0, 0]<=[5, 1, 3].
 Work =[5, 1, 3]+[2, 1, 1] =>[7, 2, 4] &
Finish = [true, true, true, true, false].
 i=4 is selected as both Finish[4] = false and [0, 0, 2]<=[7, 2, 4].
 Work =[7, 2, 4]+[0, 0, 2] =>[7, 2, 6] &
Finish = [true, true, true, true, true].
 Since Finish is a vector of all true it means there is no deadlock in this
example.
Recovery From Deadlock
Recovery from Deadlock

 Two Approaches
 Process termination
 Resource preemption
Recovery from Deadlock: Process
Termination

 Abort all deadlocked processes


 This approach will break the deadlock, but at great expense

 Abort one process at a time until the deadlock cycle is eliminated


 This approach incurs considerable overhead, since, after each
process is aborted, a deadlock-detection algorithm must be re-
invoked to determine whether any processes are still deadlocked

 Many factors may affect which process is chosen for termination


 What is the priority of the process?
 How long has the process run so far and how much longer will the
process need to run before completing its task?
 How many and what type of resources has the process used?
 How many more resources does the process need in order to finish its
task?
 How many processes will need to be terminated?
 Is the process interactive or batch?
Recovery from Deadlock: Resource
Preemption

 With this approach, we successively preempt some


resources from processes and give these resources to
other processes until the deadlock cycle is broken
 When preemption is required to deal with deadlocks,
then three issues need to be addressed:
 Selecting a victim – Which resources and which processes
are to be preempted?

 Rollback – If we preempt a resource from a process, what


should be done with that process?

 Starvation – How do we ensure that starvation will not


occur? That is, how can we guarantee that resources will
not always be preempted from the same process?
Summary
 Four necessary conditions must hold in the system for
a deadlock to occur
 Mutual exclusion
 Hold and wait
 No preemption
 Circular wait
 Four principal methods for dealing with deadlocks
 Use some protocol to (1) prevent or (2) avoid deadlocks,
ensuring that the system will never enter a deadlock state
 Allow the system to enter a deadlock state, (3) detect it,
and then recover
 Recover by process termination or resource preemption
 (4) Do nothing; ignore the problem altogether and pretend
that deadlocks never occur in the system (used by
Windows and Unix)
 To prevent deadlocks, we can ensure that at least one
of the four necessary conditions never holds

You might also like