You are on page 1of 19

Operating Systems

Ilyas Butt
Lecture 11

Deadlock Detection
Allow system to enter deadlock state
Detection algorithm
Recovery scheme
Single Instance of Each Resource Type
Maintain a wait-for graph
Nodes are processes
Pi Pj if Pi is waiting for Pj
2

Single Instance of Each


Resource Type

Periodically invoke an algorithm that searches for a


cycle in the wait-for graph.
The algorithm is expensiveit requires an order of n2
operations, where n is the number of vertices in the
graph.

RAG

wait-for Graph

Multiple Instance of Each


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
4
requesting k more instances of resource type Pj.

Detection Algorithm
1. Let Work and Finish be vectors of length m and n,
respectively Initialize:
Work = Available;
for i = 1, 2, , n
if Allocationi 0 then
Finish[i] = false;
else
Finish[i] = true;
5

Contd. Detection Algorithm


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 + Allocation i
Finish[i] = true
go to step 2.
4. If Finish[i] == false, for some i, 1i n, then the system is in
deadlock state. Moreover, if Finish[i] == false, then P i is deadlocked.
6

Example of Detection Algorithm


Consider the following system:
P = { P 0, P 1, P 2, P 3, P 4 }

Process

R = { A, B, C }

Allocatio Request
n
A B C
A B C

P0

010

000

A: 7 instances

P1

200

202

B: 2 instances

P2

302

000

P3

211

100

P4

002

002

C: 5 instances

Finish Sequence: <>

Work
A

Example System State


Process

Allocatio Request
n
A B C
A B C

Work
A

P0

010

000

P1

200

202

P2

302

000

P3

211

100

P4

002

002

Finish Sequence: <P0>

Contd. Example System State


Process

Allocatio Request
n
A B C
A B C

Work
A

P0

010

000

P1

200

202

P2

302

000

P3

211

100

P4

002

002

Finish Sequence: <P0, P2 >

Contd. Example System State


Process

Allocatio Request
n
A B C
A B C

Work
A

P0

010

000

P1

200

202

P2

302

000

P3

211

100

P4

002

002

Finish Sequence: < P0, P2, P3 >

10

Contd. Example System State


Process

Allocatio Request
n
A B C
A B C

Work
A

P0

010

000

P1

200

202

P2

302

000

P3

211

100

P4

002

002

Finish Sequence:< P0, P2, P3, P4 >

11

Contd. Example System State


Final finish sequence:
< P0, P2, P3, P4 , P1 >
Other possible finish sequences:
< P0, P2, P3, P1 , P4 >
< P0, P2, P4, P1 , P3 >
< P0, P2, P4, P3 , P1 >
12

Example System State


P2 requests an additional instance of C.
Do we have a finish sequence?

Proces Request
s
A B C
P0

000

P1

202

P2

001

P3

100

P4

002
13

Contd. Example System State


Proce
ss
P0
P1
P2
P3
P4

Allocati Reque
on
st
A B C A B C
010
000
200
202
302
001
211
100
002
002

Finish Sequence: <>

Work
A B

14

Contd. Example System State


Allocatio Reques
n
t
Proces
s
A B C A B C
P0
010
000
200
202
P1
302
001
P2
211
100
P3
002
002
P4
Finish Sequence: <P0>

Work
A

0
0

0
1

0
0

15

Contd. Example System State


P0s request can be satisfied with currently available
resources, but request for no other process can be
satisfied after that.
Deadlock exists, consisting of processes P1, P2, P3, and
P4.

16

Detection Algorithm
How often should the detection algorithm be invoked?
Every time a request for allocation cannot be granted
immediatelyexpensive but process causing the
deadlock is identified, along with processes involved
in deadlock
Periodically, or based on CPU utilization
Arbitrarilythere may be many cycles in the resource
graph and we would not be able to tell which of the
many deadlocked processes caused the deadlock. 17

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 processes?
Priority of a process.
How long the process has computed, and how much longer to
completion.
Resources the process has used.
Resources the process needs to complete.
How many processes will need to be terminated.
Is the process interactive or batch?
18

Recovery from Deadlock:


Resource Pre-emption
Selecting a victim minimize cost.
Rollback return to some safe state, restart process
from that state.
Starvation same process may always be picked as
victim; include number of rollbacks in cost factor.

19

You might also like