You are on page 1of 49

21CS56 – Operating systems

Chapter 8- (Module3) Deadlocks

Department of Information Science and Engg


1
Transform Here
Chapter 8: Deadlocks
● System Model
● Deadlock Characterization
● Methods for Handling Deadlocks
● Deadlock Prevention
● Deadlock Avoidance
● Deadlock Detection
● Recovery from Deadlock

Department of Information Science and Engg


2
Transform Here
Chapter Objectives

● To develop a description of deadlocks, which prevent sets


of concurrent processes from completing their tasks
● To present a number of different methods for preventing or
avoiding deadlocks in a computer system

Department of Information Science and Engg


3
Transform Here
Background

A process requests resources, if the resources are not


available at that time, the process enters a waiting state.
Sometimes, a waiting process is never again able to change
state, because the resources it has requested are held by other
waiting processes.
This situation is called a Deadlock.

“Every process in a set of processes is waiting for an event


that can be caused only by another process in the set”.

Department of Information Science and Engg


4
Transform Here
System Model
A system consists of a finite number of resources to be
distributed among a number of competing processes.
The resources are partitioned into several types, each consisting
of some number of identical instances. Memory space, CPU
cycles, files, and I/0 devices are examples of resource types.
A process must request a resource before using it and must
release the resource after using it.
A process may request as many resources as it requires carrying
out its designated task.
The number of resources requested may not exceed the total
number of resources available in the system.

Department of Information Science and Engg


5
Transform Here

System Model
Each process utilizes a resource as follows:
● Request
● Use
● Release
A set of processes is in a deadlocked state when every process in the set is
waiting for an event that can be caused only by another process in the set.
The events with which we are mainly concerned here are resource
acquisition and release.
The request and release of resources may be system calls,
Example: request() and release() of a device, open() and close() of a file, and
allocate() and free() memory system calls.
A system table records whether each resource is free or allocated; for each
resource that is allocated, the table also records the process to which its is
allocated.

Department of Information Science and Engg


6
Transform Here
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously
● Mutual exclusion: At least one resource must be held in a
non-sharable mode; only one process at a time can use the resource.
If another process requests that resource, the requesting process
must be delayed until the resource has been released.
● Hold and wait: a process holding at least one resource and is
waiting to acquire additional resources held by other processes
● No preemption: Resources cannot be preempted; 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 {P0, P1, …, Pn} of 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.

Department of Information Science and Engg


7
Transform Here
Deadlock with Semaphores

Department of Information Science and Engg


8
Transform Here
Resource-Allocation Graph
“ A deadlock can be described more precisely in terms of a
directed graph called system 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 active
processes in the system

● R = {R1, R2, …, Rm}, the set consisting of all resource types


in the system

● request edge – directed edge Pi → Rj

● assignment edge – directed edge Rj → Pi

Department of Information Science and Engg


9
Transform Here
Resource-Allocation Graph (Cont.)
● Process

● Resource Type with 4 instances

● Pi requests instance of Rj
P
i

● Pi is holding an instance of Rj
P
i

Department of Information Science and Engg


10
Transform Here
Example of a Resource Allocation Graph
The sets P, R and E:
🡺 P = {P1, P2, P3}
🡺 R= {R1, R2, R3, R4}
🡺 E = {Pl →Rl, P2 → R3, Rl → P2, R2 → P2, R2 → P1, R3 → P3 }

Resource instances:
One instance of resource type R1
Two instances of resource type R2
One instance of resource type R3
Three instances of resource type R4
Process states:
Process P1 is holding an instance of resource type R2
and is waiting for an instance of resource type R1.
Process P2 is holding an instance of R1 and an
instance of R2 and is waiting for an instance of R3.
Process P3 is holding instance of R3

Department of Information Science and Engg


11
Transform Here
Resource Allocation Graph With A Deadlock
If the graph does contain a cycle, then a deadlock may
exist.
If each resource type has exactly one instance, then a cycle
implies that a deadlock has occurred.
If the cycle involves only a set of resource types, each of
which has only a single instance, then a deadlock has
occurred. Each process involved in the cycle is deadlocked.
If each resource type has several instances, then a cycle
does not necessarily imply that a deadlock has occurred.
In this case, a cycle in the graph is a necessary but not a
sufficient condition for the existence of deadlock.

Department of Information Science and Engg


12
Transform Here
Illustration: Resource Allocation Graph with a Deadlock
To illustrate this concept, the
resource-allocation graph depicted in
below figure:
Suppose that process P3 requests an
instance of resource type R2.
Since no resource instance is currently
available, a request edge P3 → R2 is
added to the graph.
At this point, two minimal cycles exist in
the system:
1. P1 →R1 → P2 → R3 → P3 → R2→P1
2. P2 →R3 → P3 → R2 → P2

Department of Information Science and Engg


13
Transform Here
Graph With A Cycle But No Deadlock

Department of Information Science and Engg


14
Transform Here
Methods for Handling Deadlocks
● Ensure that the system will never enter a deadlock state:
● Deadlock prevention
● Deadlock avoidance
● Allow the system to enter a deadlock state and then
recover
● Ignore the problem and pretend that deadlocks never
occur in the system; used by most operating systems,
including UNIX

Department of Information Science and Engg


15
Transform Here
Deadlock Prevention
• Restrain the ways request can be made
• Invalidate one of the four necessary conditions for deadlock:
● Mutual Exclusion – not required for sharable resources (e.g.,
read-only files); must hold for non-sharable resources
● Hold and Wait – must guarantee that whenever a process requests a
resource, it does not hold any other resources
● Require process to request and be allocated all its resources
before it begins execution, or
● Allow process to request resources only when the process has
none allocated to it.🡺 A process may request some resources and
use them. Before it can request any additional resources, it must
release all the resources that it is currently allocated.
Low resource utilization; starvation possible, Reduced system
throughput
Department of Information Science and Engg
16
Transform Here
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.
● Process will be restarted only when it can regain its old resources, as
well as the new ones that it is requesting.
● This protocol is often applied to resources whose state can be easily
saved and restored later, such as CPU registers and database
transactions.
● Circular Wait – impose a total ordering of all resource types, and require
that each process requests resources in an increasing order of
enumeration.
Violate any of the four necessary conditions & deadlock can never occur in the system

Department of Information Science and Engg


17
Transform Here
Deadlock Avoidance
Requires that the system has some additional a priori information
available
● Simplest and most useful model requires that each process
declare the maximum number of resources of each type that it
may need
● The deadlock-avoidance algorithm dynamically examines the
resource-allocation state to ensure that there can never be a
circular-wait condition.
● Resource-allocation state is defined by the number of available
and allocated resources, and the maximum demands of the
processes

Department of Information Science and Engg


18
Transform Here
Safe State
● When a process requests an available resource, system must
decide if immediate allocation leaves the system in a safe state
● System is in safe state if there exists a sequence <P1, P2, …, Pn>
of ALL the processes in the systems such that for each Pi, the
resources that Pi can still request can be satisfied by currently
available resources + resources held by all the Pj, with j < I
● That is:
● If 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

Department of Information Science and Engg


19
Transform Here
Basic Facts
● 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.
● A state is safe if the system can allocate resources to each
process (up to its maximum) in some order and still avoid a
deadlock. 🡺 safe sequence.
● A safe state is not a deadlocked state. Conversely, a deadlocked
state is an unsafe state. Not all unsafe states are deadlocks🡺
An unsafe state may lead to a deadlock.
Department of Information Science and Engg
20
Transform Here
Safe, Unsafe, Deadlock State

Department of Information Science and Engg


21
Transform Here
Avoidance Algorithms
● Single instance of a resource type
● Use a resource-allocation graph

● Multiple instances of a resource type


● Use the banker’s algorithm

Department of Information Science and Engg


22
Transform Here
Resource-Allocation Graph Scheme
● Claim edge Pi Rj indicated that process Pi may
request resource Rj; represented by a dashed line
● Claim edge converts to request edge when a
process requests a resource
● Request edge converted to an assignment edge
when the resource is allocated to the process
● When a resource is released by a process,
assignment edge reconverts to a claim edge
● Resources must be claimed a priori in the system

Department of Information Science and Engg


23
Transform Here
Resource-Allocation Graph

Department of Information Science and Engg


24
Transform Here
Unsafe State In Resource-Allocation Graph

Department of Information Science and Engg


25
Transform Here
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

Department of Information Science and Engg


26
Transform Here
Banker’s Algorithm
● The Banker’s algorithm is applicable to a resource allocation
system with multiple instances of each resource type.
● When a new process enters the system, it must declare the
maximum number of instances of each resource type that it
may need. This number may not exceed the total number of
resources in the system.
● When a user requests a set of resources, the system must
determine whether the allocation of these resources will
leave the system in a safe state.
● If it will, the resources are allocated; otherwise, the process
must wait until some other process releases enough
resources.

Department of Information Science and Engg


27
Transform Here
Data Structures for the Banker’s Algorithm
Let n = number of processes, and m = number of resources types.
● 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]

Department of Information Science and Engg


28
Transform Here
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

Department of Information Science and Engg


29
Transform Here
Resource-Request Algorithm for Process Pi
Requesti = request vector for process Pi. If Requesti [j] = k then
process Pi wants k instances of resource type Rj
1. If Requesti ≤ Needi go to step 2. Otherwise, raise error
condition, since process has exceeded its maximum claim
2. If Requesti ≤ Available, go to step 3. Otherwise Pi must wait,
since resources are not available
3. Pretend to allocate requested resources to Pi by modifying the
state as follows:
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
● If safe 🡺 the resources are allocated to Pi
● If unsafe 🡺 Pi must wait, and the old resource-allocation
state is restored

Department of Information Science and Engg


30
Transform Here
Example of Banker’s Algorithm
● 5 processes P0 through P4;
3 resource types:
A (10 instances), B (5instances), and C (7 instances)
● Snapshot at time T0:
Allocation Max Available
ABC ABC ABC
P0 0 1 0 753 332
P1 2 0 0 322
P2 3 0 2 902
P3 2 1 1 222
P4 0 0 2 433

Department of Information Science and Engg


31
Transform Here
Example
Example:
Consider a system that contains five processes P1, P2, P3, P4, P5 and the three
resource types A, B and C. Following are the resources types: A has 10, B has 5
and the resource type C has 7 instances.

Process Allocation Max Available


A B C A B C A B C

P1 0 1 0 7 5 3 3 3 2

P2 2 0 0 3 2 2

P3 3 0 2 9 0 2

P4 2 1 1 2 2 2

P5 0 0 2 4 3 3
Answer the following questions using the banker's algorithm:
1. What is the content of the need matrix?
2. Determine if the system is safe or not.
3. What will happen if the resource request (1, 0, 0) comes for process P1.
can the system accept this request immediately?
Department of Information Science and Engg
32
Transform Here
Example (Contd…)
Ans 1. Content of the need matrix is as follows:

Need [i] = Max [i] - Allocation [i]


Need for P1: (7, 5, 3) - (0, 1, 0) = 7, 4, 3
Need for P2: (3, 2, 2) - (2, 0, 0) = 1, 2, 2
Need for P3: (9, 0, 2) - (3, 0, 2) = 6, 0, 0
Need for P4: (2, 2, 2) - (2, 1, 1) = 0, 1, 1
Need for P5: (4, 3, 3) - (0, 0, 2) = 4, 3, 1
● The content of the matrix Need is defined to be Max – Allocation
Need
ABC
P1 7 4 3
P2 1 2 2
P3 6 0 0
P4 0 1 1
P5 4 3 1

Department of Information Science and Engg


33
Transform Here
Example (Contd…)
Ans. 2: Apply the Banker's Algorithm: (Safety algorithm)
Available Resources of A, B and C are 3, 3, and 2.
Now we check if each type of resource request is available for each process.
Step 1: For Process P1:
Need <= Available
(7, 4, 3) <= (3, 3, 2) condition is false.
So, we examine another process, P2.
Step 2: For Process P2:
Need <= Available
(1, 2, 2) <= (3, 3, 2) condition true
New available = available + Allocation
(3, 3, 2) + (2, 0, 0) = (5, 3, 2) and Finish(P2) = True
Similarly, we examine another process P3.
Step 3: For Process P3:
P3 Need <= Available
(6, 0, 0) < = (5, 3, 2) condition is false.
Similarly, we examine another process, P4.
Step 4: For Process P4:
P4 Need <= Available
(0, 1, 1) <= (5, 3, 2) condition is true
New Available resource = Available + Allocation
(5, 3, 2) + (2, 1, 1) = (7, 4, 3) and Finish(P4) = True
Department of Information Science and Engg
34
Transform Here
Example (Contd…)
Similarly, we examine another process P5.
Step 5: For Process P5:
P5 Need <= Available
(4, 3, 1) <= (7, 4, 3) condition is true
New available resource = Available + Allocation
(7, 4, 3) + (0, 0, 2) = (7, 4, 5) and Finish(P5) = True
Now, we again examine each type of resource request for processes P1 and P3.
Step 6: For Process P1:
P1 Need <= Available
(7, 4, 3) <= (7, 4, 5) condition is true
New Available Resource = Available + Allocation
(7, 4, 5) + (0, 1, 0) = (7, 5, 5) and Finish(P1) = True
So, we examine another process P3.
Step 7: For Process P3:
P3 Need <= Available
(6, 0, 0) <= (7, 5, 5) condition is true
New Available Resource = Available + Allocation
(7, 5, 5) + (3, 0, 2) => (10, 5, 7) and Finish(P3) = True
Hence, we execute the banker's algorithm to find the safe state and the safe
sequence is <P2, P4, P5, P1, P3>.

Department of Information Science and Engg


35
Transform Here
Deadlock Detection
If a system does not employ either a
deadlock-prevention or a deadlock avoidance
algorithm, then a deadlock situation may occur. In
this environment, the system may provide:
✔An algorithm that examines the state of the
system to determine whether a deadlock has
occurred
✔An algorithm to recover from the deadlock

Department of Information Science and Engg


36
Transform Here
Single Instance of Each Resource Type
▪ If all resources have only a single instance, then define a
deadlock detection algorithm that uses a variant of the
resource-allocation graph, called a wait-for graph.
▪ This graph is obtained from the resource-allocation graph by
removing the resource nodes and collapsing the appropriate
edges.

Department of Information Science and Engg


37
Transform Here
Resource-Allocation Graph and Wait-for Graph

▪ An edge from Pi to Pj in a
wait-for graph implies that
process Pi is waiting for
process Pj to release a
resource that Pi needs.
▪ An edge Pi → Pj exists in a
wait-for graph if and only if
the corresponding resource
allocation graph contains two
edges Pi →Rq and Rq→Pj for
some resource Rq

Resource-Allocation Graph Corresponding wait-for


graph

Department of Information Science and Engg


38
Transform Here
Resource-Allocation Graph and Wait-for Graph

A deadlock exists in the system if and only if the


wait-for graph contains a cycle.
To detect deadlocks, the system needs to maintain
the wait-for graph and periodically invoke an
algorithm that searches for a cycle in the graph.
An algorithm to detect a cycle in a graph requires an
order of n^2 operations, where n is the number of
vertices in the graph.

Department of Information Science and Engg


39
Transform Here
Several Instances of a Resource Type
● 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 [i][j] = k, then
process Pi is requesting k more instances of
resource type Rj.

Department of Information Science and Engg


40
Transform Here
Detection Algorithm
Let Work and Finish be vectors of length m and n, respectively
Initialize:
(a) Work = Available
Algorithm requires an order of
(b) For i = 1,2, …, n, if Allocationi ≠ 0,
O(mthen
x n2) operations to detect
Finish[i] = false; otherwise, Finish[i] = truethe system is in
whether
deadlocked state.

2. Find an index i such that both:


(a) Finish[i] == false
(b) Requesti ≤ Work

If no such i exists, go to step 4

Department of Information Science and Engg


41
Transform Here
Example of Detection Algorithm
● Five processes P0 through P4; three resource types
A (7 instances), B (2 instances), and C (6 instances)

● Snapshot at time T0:


Allocation Request Available
ABC ABC ABC
P0 010 000 000
P1 200 202
P2 303 000
P3 211 100
P4 002 002

● Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i

Department of Information Science and Engg


42
Transform Here
Example (Cont.)
● P2 requests an additional instance of type C
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 P0, but insufficient
resources to fulfill other processes; requests
● Deadlock exists, consisting of processes P1, P2, P3, and P4

Department of Information Science and Engg


43
Transform Here
Detection-Algorithm Usage
● When, and how often, to invoke depends on:
● How often a deadlock is likely to occur?🡺 Frequency
● How many processes will need to be rolled back?🡺 Processes
affected
▪If deadlocks occur frequently, then the detection algorithm should be invoked
frequently.
▪Resources allocated to deadlocked processes will be idle until the deadlock
can be broken.
▪If detection algorithm is invoked arbitrarily, there may be many cycles in the
resource graph and so we would not be able to tell which of the many
deadlocked processes “caused” the deadlock.

Department of Information Science and Engg


44
Transform Here
Detection-Algorithm Usage
● Problem: Deadlock occur only when some processes make a request
that cannot be granted immediately:
● Solution1:
The deadlock- algorithm must executed whenever a request for
allocation cannot be granted immediately,
In this case, we can identify
🡺 set of deadlocked processes
🡺 specific processes causing the deadlock
● Solution2:
The deadlock-algorithm must be executed in periodic intervals;
For example: 🡺 once in an hour
🡺whenever CPU utilization drops below certain threshold

Department of Information Science and Engg


45
Transform Here
Recovery from Deadlock

● 3 approaches to recovery from deadlock:


Inform the system –admin for manual intervention
Terminate one or more deadlocked-processes
Preempt (or block) some resources

Department of Information Science and Engg


46
Transform Here
1. Abort all deadlocked processes

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

● In which order should we choose to abort?


1. Priority of the process
2. How long process has computed, and how much longer to
completion
3. Resources the process has used
4. Resources process needs to complete
5. How many processes will need to be terminated
6. Is process interactive or batch?

Department of Information Science and Engg


47
Transform Here
Recovery from Deadlock: Resource Preemption

● Selecting a victim – minimize cost

● Rollback – return to some safe state, restart process for that state

● Starvation – same process may always be picked as victim,


include number of rollback in cost factor

Department of Information Science and Engg


48
Transform Here
End of Chapter 7

Department of Information Science and Engg


49
Transform Here

You might also like