Lecture 7
Lecture 7
Chapter 7 Deadlock
1. Deadlock Problem
A set of blocked processes each holding a resource and waiting to acquire a resource held
by another process in the set.
• Example #1: System has 2 tape drives. P1 and P2 each hold one tape drive and
each needs another one.
• Example #2: semaphores A and B, initialized to 1
P0 P1
wait (A); wait(B)
wait (B); wait(A)
2. Assumptions
• Resource types R1, R2, . . ., Rm (e.g., CPU cycles, memory space, I/O devices)
• Each resource type Ri has Wi instances.
• Each process utilizes a resource as follows:
o request
o use
o release
3. Deadlock Situation
Deadlock can arise if the following four conditions hold simultaneously:
• 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.
• Circular wait: there exists a set {P1, P2, …, Pn} of waiting processes such that
P1 is waiting for a resource that is held by P2, P2 is waiting for a resource that is
held by P3, …, Pn–1 is waiting for a resource that is held by Pn, and Pn is waiting
for a resource that is held by P1.
P vertex (a process)
Rj
Rj
(a) (b)
Figure 7.2 (a) RAG with deadlock and (b) RAG with a cycle but no deadlock
Choice #2: Allow the system to enter a deadlock state and then recover
c. Deadlock Detection & Recovery
6.2 Deadlock Avoidance: Requires that the system has some additional a priori
information available.
• Requires each process to declare the maximum number of resources of each type
that it may need.
• Safe State:
o System is in safe state if there exists a safe sequence of all processes (i.e.,
the system can allocate all the requested resources in some order).
o Safe Sequence for the current allocation state: <P1, P2, …, Pn>, for each
Pi, Pi resource request can be satisfied by currently available resources +
resources held by all the Pj, with j<i.
• Deadlock Avoidance ensures that the system will never enter the unsafe state
Figure 7.3 Safe/Unsafe/Deadlock State Spaces
• When every resource type has only one instance (RAG Algorithm)
o Before Pi starts executing, all its claim edges are generated and added to
the RAG.
o Claim edge Pi Rj indicated that process Pj may request resource Rj;
represented by a dashed line.
Claim edge converts to request edge when a process requests a
resource.
When a resource is released by a process, assignment edge
reconverts to a claim edge.
o Resource request is granted only if converting the request edge to an
assignment edge does not result in the formation of a circle in the RAG.
Unsafe State In A Resource-Allocation Graph Resource-Allocation Graph For Deadlock Avoidance
Safety Algorithm
Local Vectors:
Work: a vector of length m.
1. Let Work and Finish be vectors of length m and n, respectively.
Finish: a vector of length n
Initialize:
Work := Available
Step 1. Work = Available;
Finish
Finish[i] = false, for[i]all
= false for i - 1,3, …, n.
i=1,2,...,n;
2. Find and i such that both:
Step 2. Find(a)i such that
Finish [i] (Finish[i]=false
= false && Need[i] <= Work);
If no such i exist then the system is in an unsafe state; return;
(b) Needi ≤ Work
If no such i exists, go to step 4.
Step 3. Work = Work+Allocation[i];
3.Finish[i]
Work :==Work
true;+ Allocationi
Finish[i] := true
Step 4. Ifgo to step =2.true for all i then the system is in a safe state; return;
Finish[i]
Else goto [i]
4. If Finish Step 2; for all i, then the system is in a safe state.
= true
Resource-Request Algorithm for Process Pi
Local Vector:
Request: m-dimensional
Request vector. r[j] is the number of instances of jth resource type Pi is
i = request vector for process Pi. If Requesti [j] = k then
currently requesting.
process P wants k instances of resource type R
i j.
1. If Requesti ≤ Needi go to step 2. Otherwise, raise error
Step 1. If Requestcondition,
<= Need[i] thenprocess
since goto Step has2;exceeded its maximum claim.
Else, raise an error; return;
2. If Requesti ≤ Available, go to step 3. Otherwise Pi must
wait, since resources are not available.
Step 2. If Request<= Available[i] then goto Step 3;
3. Pretend to allocate
Else, block the current processrequested
Pi; return;resources to Pi by modifying
the state as follows:
Step 3. Available
Backup the current values := Available
of Available, = Request
Need[i], i;
and Allocation[i];
Allocation
Available = Available-Request;i := Allocation i + Request i;
Allocation[i] = Allocation[i]+Request;
Needi := Needi – Requesti;;
Need[i] = Need[i]-Request;
• If safe the resources are allocated to Pi.
• If unsafe Pi must wait, and the old resource-allocation
Step 4. Call the Safetystate
Algorithm;
is restored
If it is safe then physically allocate requested resources; return;
Else restore Available, Allocation[i], and Need[i] and block the process Pi; return;
Example of Banker’s Algorithm
P1 requests
(1,0,2)
• Check that Request ≤Available (that is, (1,0,2) ≤(3,3,2) true.
Allocation Need Available
AB C ABC ABC
P0 010 743 230
P1 302 020
P2 301 600
P3 211 011
P4 002 431
• Executing safety algorithmshows 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 P0be granted?
6.3 Deadlock Detection and Recovery: In this method, OS allows the system to enter
deadlock state. Then OS detects the deadlock with a detection algorithm, and runs
a recovery scheme.
Note that in the single resource instance case, circle in RAG is both a
necessary and a sufficient condition.
Note that the algorithm to detect a cycle in a graph requires an order of n2
operations, where n is the number of vertices of the graph.
4. If Finish[i] = false, for some i, 1 <= i <= n, then the system is in deadlock
state.
Moreover, if Finish[i] = false, then Pi is deadlocked.
• Resource Preemption
o Selecting a victim – minimize cost.
o Rollback – return to some safe state, restart process fro that state.
o Starvation – same process is always picked as a victim, include the
number of rollback in cost factor.