Transcript slide
Lecture 7: Deadlocks
Operating System
Fall 2006
1
Contents
What is deadlock
Conditions for deadlock
Methods for Handling Deadlocks
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection and Recovery
2
What is deadlock?
A set of blocked processes each holding a resource
and waiting to acquire a resource held by another
process in the set.
Example:
Process P
…
request A
…
request B
…
release A
…
release B
Process Q
…
request B
…
request A
…
release B
…
release A
3
Bridge Crossing Example
Traffic only in one direction.
Each section of a bridge can be viewed as a resource.
If a deadlock occurs, it can be resolved if one car
backs up (preempt resources and rollback).
Several cars may have to be backed up if a deadlock
occurs.
Starvation is possible.
4
Reusable Resources
One that can be safely used by only one process at a
time and is not depleted by that use.
Serially Reusable
Example:
Memory (main & secondary)
Processors
I/O channels
Semaphores
Files
Databases
Printer etc.
5
Example 1
P
Request disk drive
Lock disk drive
Request tape drive
Lock tape drive
… perform function
Unlock disk drive
Unlock tape drive
Q
Request tape drive
Lock tape drive
Request disk drive
Lock disk drive
… perform function
Unlock tape drive
Unlock disk drive
6
Example 2
Suppose we have a pool of 200 kbytes of
memory for allocation:
P
…
Request 80 Kbytes
…
Request 60 Kbytes
Q
…
Request 70 Kbytes
…
Request 80 Kbytes
7
Consumable Resources
One that can be created and destroyed
Example:
Message passed among processes
8
Conditions for deadlock
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. No resources can be forcibly removed from a process
holding it.
Circular wait: there exists a set {P0, P1, …, P0} 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 P0 is waiting for a resource that is held by P0.
9
Methods for Handling Deadlocks
Deadlock prevention: Design policies so that deadlock
can’t occur.
Deadlock avoidance: The OS will monitor the progress of
processes so that deadlock can always be avoided.
Deadlock detection and recovery: Do nothing and allow
deadlock occur, if deadlock are detected, then recover at
a minimal cost.
Ignore the problem and pretend that deadlocks never
occur in the system; used by most operating systems,
including UNIX.
10
Deadlock Prevention
Design policies so that deadlock can’t occur:
Mutual Exclusion – usually cannot be disallowed
Hold and wait
No preemption – preemption is allowed.
require each process to release all resources held before making a new
request.
a process may request resources ahead of time.
Drawback: inefficient utilization of resources.
A good example is the example of the memory pool. If we get into a
deadlock, move the allocated memory to secondary memory
(preemption) and restore later.
Circular Wait – Assign a linear ordering of resource types.
R1<R2<R3…
If a process has been allocated resources of type i (Ri), then it may
request resources of type higher than i. If it wants resources of type
lower than i, then it must release resources of type i.
11
Deadlock Avoidance
When a process requests resources, a decision is
made whether the current request will, if granted,
potentially lead to a deadlock. Deadlock avoidance
thus requires knowledge of future processes resource
request.
Do not start a process if its demands might lead to deadlock
Do not grant an incremental resource request to a process if
this allocation might lead to deadlock.
12
Deadlock Avoidance(cont.)
Resource-allocation state is defined by the number of available
and allocated resources, and the maximum demands of the
processes.
resource ( R1 , R2 ,..., Rm ) Total amount of each resource in the system
Available (V1 ,V2 ,...,Vm ) Total amount of each resource not allocated to
a process
C11 C12
C
C22
claim 21
...
...
Cn1 Cn 2
A11
A21
Allocation
...
An1
... C1m
... C1m Requirement of each process of each resource
(declared by the processes ahead of time)
... ...
... Cnm
A12
A22
...
An 2
... A1m
... A1m
Current allocation
... ...
... Anm
13
Example
4 processes, 3 resources in the system
n=4, m=3
R (9,3, 6)
Total amount of each resource in the system
V (0,1,1)
Total amount of each resource not allocated to
a process
3
6
claim
3
4
2 2
1 3
1 4
2 2
1
6
Allocation
2
0
Requirement of each process of each resource
(declared by the processes ahead of time)
0 0
1 2
1 1
0 2
Current allocation
14
Safe State
The state of the system is simply the current
allocation of resources to processes.
A safe state is one in which there is at least
one sequence that does not result in a
deadlock
An unsafe state is a state that is not safe.
15
Is this example a safe state?
n=4, m=3
4 processes, 3 resources
R (9,3, 6)
Total amount of each
resource in the system
V (0,1,1)
Total amount of each
resource not allocated to
a process
3
6
claim
3
4
2 2
1 3 Requirement of
each process of
1 4 each resource
2 2
1
6
Allocation
2
0
0 0
1 2 Current
allocation
1 1
0 2
P1 cannot proceed. In the worst case, P1 will request 2
units of R1, 2 units of R2 and 2 units of R3.
Impossible to satisfy
P2 can proceed. In the worst case, P2 will request 1 more
units of R3. Possible to satisfy.
When it completes, it will release all resources.
V (0,1,1) (6,1, 2) (6, 2,3)
Now P1 can proceed.
V (6, 2,3) (1, 0, 0) (7, 2,3)
Now P3 can proceed.
V (7, 2,3) (2,1,1) (9,3, 4)
Now P4 can proceed.
V (9,3, 4) (0, 0, 2) (9,3, 6)
It is a safe sate.
16
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.
In deadlock avoidance, each time a process requests
resource, the OS will determine if it will, after granting the
resources to the process, lead it to an unsafe state. At the
beginning, the system is in a safe sate, at each step, the
system is always in a safe state.
Avoidance algorithm: Banker’s Algorithm
17
Banker’s Algorithm
Checking to see if a state is safe or not
Algorithm:
1.
2.
3.
4.
Find a process Pk such that Claim(k,*)Allocation(k,*)Available(*) for *. If not found,
go to 4.
Available(*) <- Available(*)+Allocation(k,*) for
*.
Goto 1
If there is no process left, the state is safe.
Otherwise, it is not safe.
18
Example 1
n=4, m=3
4 processes, 3 resources
R (9,3, 6)
Total amount of each
resource in the system
V (0,1,1)
Total amount of each
resource not allocated to
a process
3
6
claim
3
4
Suppose P1 makes a request of 1 unit of R2. Should the OS
Grants the request? Well, the OS should if it lead it to a safe
State.
V (0, 0,1)
3
6
claim
3
4
2 2
1 3 Requirement of
each process of
1 4 each resource
2 2
1
6
Allocation
2
0
0 0
1 2 Current
allocation
1 1
0 2
This is a safe state.
2 2
1 3
1 4
2 2
1
6
Allocation
2
0
1 0
1 2
1 1
0 2
Is this a safe state?
Yes.
So the OS will grant this request.
19
Example 2
n=4, m=3
4 processes, 3 resources
R (9,3, 6)
Total amount of each
resource in the system
V (0,1,1)
Total amount of each
resource not allocated to
a process
3
6
claim
3
4
Suppose P3 asks for one unit of R3. Should the OS grants
the request?
V (0,1, 0)
3
6
claim
3
4
2 2
1 3 Requirement of
each process of
1 4 each resource
2 2
1
6
Allocation
2
0
0 0
1 2 Current
allocation
1 1
0 2
This is a safe state.
2 2
1 3
1 4
2 2
1
6
Allocation
2
0
0 0
1 2
1 2
0 2
Is this a safe state?
No.
So the OS will block this request.
20
Deadlock Detection and Recovery
Allow deadlocks to occur. If and when it occurs,
recover from it.
The system checks periodically whether it is in
deadlock. If a deadlock occurs, recovery procedure
will be called.
21
Deadlock Detection Algorithm
Detection Algorithm:
Available (V1 ,V2 ,...,Vm )
A11
A
Allocation 21
...
An1
A12
A22
...
An 2
Q11 Q12
Q
Q22
Request 21
...
...
Qn1 Qn 2
... A1m
... A1m
... ...
... Anm
... Q1m
... Q1m
... ...
... Qnm
1. Initialize a temporary vector w to equal to Available
vector. w(*)=V(*)
2. Mark each process that has a row in the Allocation
matrix of all zeros.
3. Find an index k such that process k is currently
unmarked and the kth row of Q w. That is,
Q(k,*) w(*) for *. If no such row is found,
goto step 5
4. Mark process k and add the corresponding row of
the allocation matrix to w.
That is, w(*)=w(*)+A(k,*). Go to step 3
5. If every process is marked, then there is no deadlock.
Otherwise, the unmarked process are deadlocked.
Are we in a deadlock state?
22
Example 1
V (0,1,1)
1
6
Allocation
2
0
0
0
Request
1
3
0 0
1 2
1 1
0 2
1 1
1 0
0 1
2 3
w=(0,1,1)
P1’s request w
w=(0,1,1)+(1,0,0)=(1,1,1)
P2’s request w
w=(1,1,1)+(6,1,2)=(7,2,3)
P3’s request w
w=(7,2,3)+(2,1,1)=(9,3,4)
P4’s request w
w=(9,3,4)+(0,0,2)=(9,3,6)
Every process is marked. No deadlock.
Is there a deadlock?
23
Example 2
V (0,1,1)
1
6
Allocation
2
0
4
0
Request
2
0
0 0
1 2
1 1
0 2
w=(0,1,1)
P4’s request w
w=(0,1,1)+(0,0,2)=(0,1,3)
P1,P2,P3 are involved in a deadlock.
1 0
2 1
0 0
1 1
Is there a deadlock?
24
Recovery
Two methods:
1.
2.
Abort all deadlocked processes.
Find a subset of deadlocked processes
with the minimum cost that can break the
deadlock.
25
Example for method 2
V (0,1,3)
1 0 0
Allocation 6 1 2
2 1 1
Aborting P2 will resolve the deadlock,
the cost = 9
Aborting P1 will not resolve the deadlock
Aborting P3 will resolve the deadlock,
the cost = 4
4 1 0
Request 0 2 1
2 0 0
P3 is the optimal choice.
Cost of aborting P1 = c1 = 3
Cost of aborting P2 = c2 = 9
Cost of aborting P3 = c3 = 4
The problem is NP-Hard.
26
End of lecture 7
Thank you!
27