You are on page 1of 25

1

Operating Systems (OS)


Deadlock
Lecture 11

Dag Nystrm

2
Outline
What is deadlock?
Strategies for handling deadlock

3
What is deadlock?
Consider the following example:
A
B
S1
S2
Process
Resource
Legend:
A B
A owns B
A B
A wants B
The arrow points at the
controlling node
4
Formal definition of 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
5
Resources
A hardware device or a piece of data
Two types of resources
Preemptable
Non-preemptable
Deadlock can only occur with non-preemptable
resources.
The following show how a resource is used:
Claim the resource
Use the resource
Release the resource
6
Conditions for deadlock
1. Mutual exclusion
A resource is either assigned to 1 process or available
2. Hold and wait condition
Possible to own one resource while claiming another
3. No preemption condition
granted resources cannot taken away by force
4. Circular wait condition
Each waiting process is waiting for the next in the chain
All four conditions must be met in order for a deadlock to
occur
7
Dining philosophers problem
5 philosophers
1 table
5 forks
A philosopher can either
think or eat
Eating procedure:
1. Pick up left fork
2. Pick up right fork
3. Eat
4. Return both forks

How can we deal
with the problem?
8
Deadlock management strategies
1. The Ostrich algorithm
2. Deadlock detection
3. Deadlock avoidance
4. Deadlock prevention
9
The ostrich algorithm
The easiest way to deal with the problem:
Stick your head in the sand and
pretend that there is no problem
How is it possible to use such an algorithm?

Other errors are
much more frequent
To not add limitations to
the operating system
The ostrich algorithm is both Windows and UNIX approved!!
10
Deadlock Detection
No limitations to resource allocations
Use algorithm to detect possible deadlock
Two cases:
1. One resource of each type
2. Multiple resources of each type
Resolve any deadlock found
When should algorithm be run?
When resource is claimed
Periodically, every n time unit
During idle periods

11
Deadlock Detection
- One resource of each type
Make a resource graph
Locate any cycles
Break the cycles found
Exercise:
P
A
owns R and wants S
P
B
wants T
P
C
owns S
P
D
owns U and wants S and T
P
E
owns T and wants V
P
F
owns W and wants S
P
G
owns V and wants U

Is there deadlock in the system?
Process
Resource
A B
A owns B
A B
A wants B
The arrow points at the
controlling node
12
Deadlock Detection
- One resource of each type

R
P
A
S
W
U
T
V
P
B
P
C P
D
P
E
P
F
P
G

R
P
A
S
W
U
T
V
P
B
P
C P
D
P
E
P
F
P
G
YES!!!
13
Deadlock Detection
- One resource of each type
Algorithm for finding cycle:
1. For every node N in the graph, perform the following steps using N as
starting node.
2. Initiate L as an empty set and unmark all arrows
3. If current node is NOT in the set, add current node to set, else deadlock is
detected and the algorithm is terminated
4. If there are unmarked arrows from the current node go to 5 else go to 6
5. Randomly select one arrow and follow it.Set the new node as the current
node. Go to 3
6. We have reached a dead end. Backtrack to previous node. If previous node
is starting node go to 1 using a new starting node. If not, go to 4
14
Deadlock Detection
- One resource of each type

R PA
S
W
U
T
V
PB
PC PD
PE
PF
PG
N=P
B
L={} =>L={P
B
} =>L={P
B
, T}
=>L={P
B
, T, P
E
}
=>L={P
B
, T, P
E
,

V}
=>L={P
B
, T, P
E
,

V, P
G
}
=>L={P
B
, T, P
E
,

V, P
G
, U}
=>L={P
B
, T, P
E
,

V, P
G
, U, P
D
,T}
Deadlock
=>L={P
B
, T, P
E
,

V, P
G
, U, P
D
}
15
Solved using Matrices
Deadlock Detection
- Many resources of each type
| |
n
e e e E ...
2 1
= A vector of existing resources of type e
x

| |
n
a a a A ...
2 1
=
A vector of available resources of type a
x

(
(
(
(

=
mn m m
n
n
c c c
c c c
c c c
C

1 1
2 22 21
1 12 11
A matrix containing the # of resources of
Type n claimed by process m.
(
(
(
(

=
mn m m
n
n
r r r
r r r
r r r
R

1 1
2 22 21
1 12 11
A matrix containing the # of resources of
Type n requested by process m.
We see that:
j j
m
i
ij
e a c = +

=1
16
For two vectors, X and Y
Deadlock Detection
- Many resources of each type
Y X s iff
i i
y x s
for m i s s 0
For example:
| | | |
| | | | 4 2 3 2 4 3 2 1
4 4 3 2 4 3 2 1
s
s
17
The detection algorithm is applied periodically

1. Find an unmarked process P
i
for which R
i
A.
2. If such a process is found, add C
i
to A. Mark the process
and go to step 1.
3. If no such process is found, terminate.
All unmarked processes are deadlocked
Deadlock Detection
- Many resources of each type
s
How on earth does this algorithm work?!?!?
18
Exercise:
Deadlock Detection
- Many resources of each type
| | 1 3 2 4 = E | | 0 0 1 2 = A
(
(
(

=
0 2 1 0
1 0 0 2
0 1 0 0
C
Is there deadlock in the system??
(
(
(

=
0 0 1 2
0 1 0 1
1 0 0 2
R
1
| | 0 2 2 2 ' = A
2
| | 1 2 2 4 ' ' = A
3
| | 1 3 2 4 ' ' ' = A
NO!!!
19
Deadlock Detection
How to recover from deadlock
Preemption
Process rollback
Kill one of the deadlocked processs
20
Deadlock Avoidance
Avoids deadlock by cautious allocations
Safe and unsafe states
An allocation can only be granted if it puts
the system in a safe state.
21
Deadlock Avoidance
- Safe and unsafe states
A
B
Safe Safe
Safe
Safe
Safe
Unsafe
p q
r s

Both
Finished

I8
I7
I6
I5
I4 I3 I2 I1
Pri
nte
r
Plo
tter
Plotter
Printer
Unreach-
able
22
Deadlock Avoidance
- Safe and unsafe states
Algorithms to determine the state
Based on the E,A,C and R matrices.
Example:
A total of 10 resources, with the following allocations:



Proc Has Max
A 3 9
B 2 4
C 2 7
Is the system in a safe or unsafe state??
Free 3
1
5
2
7
3
10
SAFE!!!
23
Deadlock Avoidance
-Bankers Algorithm
Made by Dijkstra
Originating from loan allocations in a bank
Examines if an allocation leads to a safe or
unsafe state
Is run when resource is requested
Two cases
One type of resource
Multiple types of resources
24
Deadlock Avoidance
-Bankers Algorithm
Example using one type of resource:

7 2 C
4 2 B
9 3 A
Max Has Proc
Free 3
Initial state
SAFE!
A requests 1
7 2 C
4 2 B
9 4 A
Max Has Proc
Free 2
UNSAFE!
B requests 1
7 2 C
4 3 B
9 3 A
Max Has Proc
Free 2 SAFE!
C requests 1
7 3 C
4 3 B
9 3 A
Max Has Proc
Free 1 SAFE!
25
Deadlock Prevention
Very difficult to achieve
Attack one of the conditions for deadlock
Attack Mutual Exclusion
Attack Hold and Wait
Attack No preemption
Attack Circular wait

You might also like