Unit 3 - Deadlocks
Unit 3 - Deadlocks
Process
Pi requests instance of Rj
Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
Example of a Resource Allocation Graph
Resource Allocation Graph With A Deadlock
Graph With A Cycle But No Deadlock
Basic Facts
then deadlock.
if several instances per resource type,
possibility of deadlock.
Methods for Handling Deadlocks
Prevention/Avoidance
Ensure that the system will never enter a
deadlock state.
Detection/Correction
Allow system to enter a deadlock state and then
recover.
Ignorance
Ignore problem and pretend that deadlocks
never occur
Used by most operating systems, including UNIX.
Deadlock Prevention
Restrain the ways request can be made.
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.
Deadlock Avoidance
Ensures system will never enter an unsafe
state
Thereby avoids the possibility of deadlock
Safe States: An Example
3
Suppose process P2 requests and is allocated 1 more tape
drive.
Question: Is the resulting state still safe?
Answer: No! Because there does not exist a safe sequence
anymore.
Only P1 can be allocated its maximum needs.
IF P0 and P2 request 5 more drives and 6 more drives,
respectively, then the resulting state will be deadlocked.
Deadlock Avoidance - Concepts
Key Ideas:
Initially the system is in a safe state
Whenever a process requests an
available resource, system will allocate
resource immediately only if the
resulting state is still safe!
Otherwise, requesting process must
wait.
Why does this work?
By induction, all reachable states are
safe states
By definition, all safe states are not
deadlocked
Avoidance Algorithms
3 resource types:
A (10 instances), B (5instances), and C (7
instances).
Snapshot at time T0:
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 301 600
P3 211 011
P4 002 431
Executing safety algorithm shows that sequence < P1, P3,
P4, P0, P2> satisfies safety requirement.
Can request for (3,3,0) by P4 be granted?
Can request for (0,2,0) by P0 be granted?
Deadlock Detection
Detection algorithm
Recovery scheme
Single Instance of Each Resource
Type
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i.
Example (Cont.)
Request
ABC
P0 0 0 0
P1 2 0 1
P2 001
P3 100
P4 002
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.
Detection-Algorithm Usage