You are on page 1of 63

Chapter 7: Deadlocks

 The Deadlock Problem


 System Model
 Deadlock Characterization
 Methods for Handling Deadlocks
 Don’t care
 Deadlock Prevention

 Deadlock Avoidance
 Deadlock Detection and Recovery (from Deadlock)
Chapter Objectives

 To develop an understanding of deadlocks,


which prevents a set of concurrent processes
from completing their tasks, while waiting for
some resources occupied by each other
indefinitely
 To present a number of different methods for
preventing, avoiding and handling deadlocks in
a computer system.
Example: Narrow Bridge Crossing
R1 R2

 Traffic can flow only in one direction on a bridge


 Two sections of a bridge can be viewed as resources
(R1 & R2). One resource is occupied by one traffic, other
by the other
 If a deadlock occurs, it can be resolved if one claimant
backs up (preempts resource and rollback).
 Several cars may have to be backed up, if a deadlock
occurs.
 Starvation is also possible.
Traffic Crossing (Yellow claimants, Red resources)
T
r
a
f
f
i
c
2
Traffic 1

R1 R2
Trafic1 needs resources R1 & R2 Trafic3 needs resources R3 & R4 to cross
R4 R3

Traffic 3

There are 4 resources R1,R2,R3 and


There are 4 users. traffic T1,T2,T3 and T4
R4
Deadlock: Traffic Crossing
T
r
Partial allocation of resources and a
waiting for remaining to be free f
f
i
c
2
Traffic 1 ( T1 )

R1 R2

R4 R3

Traffic 3 ( T3 )
Deadlock (Hold and wait)
T1 holds resource R1, waiting for R2 Circular wait of users (processes)
User T1 waiting for T2 to complete
T2 holds resource R2, waiting for R3
User T2 waiting for T3 to complete
T3 holds resource R3, waiting for R4 User T3 waiting for T4 to complete
T4 holds resource R4, waiting for R1 User T4 waiting for T1 to complete
Traffic Crossing (deadlock Prevention)
(Yellow users, Red resources) T
r
a
f
f
i
c
2
Traffic 1

R1 R2

R4 R3

The 4 resources R1,R2,R3 and R4, marked Traffic 3


with red lines are usable only for a very
brief time, out of crossing, the user shall
wait, till all required resources are There are 4 users. traffic T1,T2,T3 and T4
available (Prevention) and can use and
release resources immediately. No user can
hold these resources for long. Any offence
will be penalized. This protocol can avoid
deadlock.
The Deadlock Problem
Deadlock: A state in which a set of blocked processes,
each holding one or few resources and waiting to acquire
one or few resources held by other processes of the set,
in a cycle.
 Example 1
 A System has only 2 disk drives.
 P1 and P2 each hold one disk drive and each needs another one.
 Keep the resource Occupied and wait for the resource occupied
by others
 Example 2
 Concurrent processes P0 and P1 share semaphores A and B,
which are initialized to 1. Both have waited (locked) a semaphore
P0 P1
wait (A); wait(B)
wait (B); wait(A)
……… ………
Process P0 waiting for process P1 to signal (release) semaphore B and
P1 is waiting for semaphore A to be signaled by P0
Resources System Model
 Resource types R1, R2, . . ., Rm
 Physical:-CPU cycles, memory space, I/O devices
 Virtual:-That exists only logically (through software)
 Critical:-That is sharable, but can be allocated to only
one requester
 Multiple Resources: Each resource type Ri may
have Wi instances (occurrences).
 Utilization Protocol: Each process utilizes a
resource as follows:
 requests
 Uses or holds
 releases
Deadlock Characterization
Deadlock can occur if following conditions hold simultaneously.
 Mutual exclusion: Exclusive usage of a resource, only
by one process, at a time.
 Hold and wait: A process holding at least one resource,
and waiting to acquire additional resources held by other
processes.
 Non preemption: A resource cannot be released, until
that process has completed its task.
 Circular wait: there exists a set {P0, …, Pn} of waiting
processes such that P0 is waiting for a resource that is
being held by P1, P1 is waiting for a resource that is held
by P2, so on … and Pn is waiting for a resource that is
held by P0.
Conditions for Deadlock
Resource-Allocation Graph (to determine
deadlock in a system)
 A Graph consists of a set of . vertices V and a set of
edges E
 V is partitioned into two types:
 Processes = {P1, P2, …, Pn}, the set of all the processes in the
system.
 Resources = {R1, R2, …, Rm}, the set of all resource types in the
system.

 E is partitioned into two types:


 Request edge: A process requesting a resource
– directed edge Pi  Rk
 Assignment (Allocation) edge: A resource allocated to a process
– directed edge Rj  Pk
Resource-Allocation Graph
(Processes and Multiple instances of resources.)
 Process

 Resource Type with 1 & 4 instances

Pi
 Pi requests an instance of Rj
Rj

 Pi is holding an instance of Rj Pi

Rj
Example of a Resource Allocation
Graph
Resource Allocation Graph With A
Deadlock

Set of deadlocked processes: {P2,P3} and {P1,P2,P3)


Graph With A Cycle But No Deadlock
Basic Facts
 graph contains no cycles  no deadlock.

 graph contains a cycle 


 if only one instance per resource type, then deadlock.
 if several instances per resource type, possibility of
deadlock. Needs further checking:
 Possibility of the availability of resources, being waited for
allocation:
 possible: no deadlock;
 not possible (allocated to a waiting process) possibility of deadlock
Wait for graph
 Processes may be waiting for each other in “wait for”
graph if we eliminate the resources in between
 In first loop P1 is waiting for P2, P2 is waiting for P3 and P3 is
waiting for P1
 In second loop P2 is waiting for P3 and P3 is waiting for P2

P1 P2 P3

Resource Allocation Graph Wait for Graph


Resource-Allocation Graph and Wait-for Graph
(by retaining only processes and eliminating resources)

Resource-Allocation Graph Corresponding wait-for graph


Methods for Handling Deadlocks
1. Ignore the problem and pretend that deadlocks
never occur in the system; used by most
operating systems, including UNIX.

2. Ensure that the system will never enter in a


deadlock state.
 Prevention
 Avoidance
 Both of these need, pre-stating of the maximum resource requirement
 Prevention uses bulk allocation while Avoidance uses piece wise allocation

3. The system may enter into deadlock state,


detect it and then recover.
 Detection and Recovery
Dealing with Deadlock

 Three general approaches exist for dealing with deadlock:


Deadlock Prevention
Break any necessary condition to prevent deadlock
 Mutual Exclusion – not required for sharable
resources; this condition can not be broken for
non-sharable resources.
 Hold and Wait – must guarantee that whenever
a process requests a resource, it does not hold
any other resources.
 Requires a process to release the allocated resources
before it requests for more (break hold condition), i.e.
request only when the process has no resource.
 Low resource utilization; starvation possible.
Deadlock Prevention (Cont.)
 Non Preemption –
 If a process that is holding some resource, requests
another resource that cannot be immediately allocated
to it, then all resources currently being held are
released, to break non preemption condition.
 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
(Break “wait” condition via bulk allocation).
 Circular Wait – impose a total ordering of all
resource types, and each process requests
resources in an increasing order of enumeration.
(Break circularity: no process could request already allocated resource)
 Design a system in such a way that the possibility
of deadlock is excluded
 Two main methods:
 Indirect

 prevent the occurrence of any one of the

necessary conditions
 Direct

 prevent the occurrence of a circular wait (bulk

allocation)
Deadlock Prevention (Cont.)
To break any necessary condition to prevent deadlock
is not so easy;
1. Mutual exclusion : Non-shareable resource can not be used jointly to
break mutual-exclusion
2. Hold and wait : If processes release resources or abort, to break hold-and-
wait condition, then wastage or under utilization of earlier execution
3. Non-preemption: Processes normally do not release resources midway,
voluntarily, if they have to wait for currently non available resource
4. Circular wait: If each resource is given distinct no to avoid circular wait and
each process requests for a specific resource by its Id, then the process
will lose generality, and cause resource under utilization.

Alternatively, it is better, to set processes for execution


only, if all the required resources are available (bulk
allocation). (It needs pre-stating of the required resources. Bulk
allocation results in under utilization of resources)
 A decision is made dynamically whether the
current resource allocation request, if granted,
will potentially lead to a deadlock or not

 Requires knowledge of future resources


request by the processes

 Processes shall pre-state their requirements


Deadlock Avoidance (better resource utilization)
Requires that the system has some a priori information available.
(i.e. Avoidance also needs pre stating of the required resources, like Prevention).
 Simplest and most useful model requires that
each process declare the maximum number of
resources of each type that it may need.
 The deadlock-avoidance algorithm dynamically
examines the resource-allocation state to ensure
that there can never be a circular-wait condition,
before servicing a request for resource allocation.
 Resource-allocation state is defined by the
number of available and allocated resources, and
the maximum demands of the processes.
State: Safe, Unsafe , Deadlock Threat

No deadlock threat

Still needed
Maximum Need Allocated claim
P0 10 6 4
P1 4 2 2
P2 9 2 7
Current state: Total resources = 12 Available = 2
In case of Prevention D/L handling technique only one process can execute at a time.
Safe: if next allocation is done to process P1 (leaves 1 resource, need of P1 can be fulfilled that will return 4)
Unsafe: if next allocation is done to process P0 or P2 (needs of none can be fulfilled)
Basic Facts
 If a system is in safe state  no threat of
deadlock.

 If a system is in unsafe state  possibility of


deadlock (since processes state maximum need,
but may not use all resources).

 Avoidance  ensure that a system will never


enter in an unsafe state (deadlock), before actual
allocation of resources.
Resource-Allocation Graph
R1 resource has been allocated to P1, P1 may request R2 resource
P2 has requested for R1 resource and may request R2 resource

Assignment Edge Request Edge

May request (needed)


Unsafe State In Resource-Allocation Graph
Total resources are 2
R1 resource has been allocated to P1, P1 may request R2 resource;
R2 resource has been allocated to P2 and P2 has requested for R1
resource allocation shown in second figure

May request Allocated Need Allocated


P1 1 1 P1 1 1
P2 1 0 P2 1 1

Threat of Deadlock; the circular loop completes in second figure, However, if P1


exits without requesting then there will be no Deadlock
Resource-Allocation Graph Scheme
 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.
 Request edge converted to an assignment edge
when the resource is allocated to the process.
 When a resource is released by a process,
assignment edge reconverts to a claim edge.
 Resources must be claimed a priori in the
system.
Example: (Single resource type, multiple instances)
Total no. of resources is 8

Resources Process 1 Process 2 Process 3 Result


A F A N A N A N
0 8 0 6 0 3 0 4
Request for allocation
. . - - - - - -
5 3 4 2 0 3 1 3 Safe
6 2 4 2 1 2 1 3 Safe
7 1 4 2 1 2 2 2 Unsafe
6 2 4 2 1 2 1 3 Revert
7 1 5 1 1 2 1 3 Safe

Notations: A: Allocated; F: Free; N : still Needed


Reques
t
Safe Allocation (Banker’s
Algorithm)
 When a process requests a resource, Algorithm decides,

if current allocation leaves the system in a safe state


then allocate the resource, otherwise deny allocation.
 Safe state: if there exists a possibility, that current
allocation leaves some of the processes, that can
complete their execution and release the resources ,
which enables to satisfy need of remaining processes.
 That is:
 If Pi needs a resource, already allocated to Pj , and is not
immediately available, then Pi can wait until all Pj have finished.
 When Pj is finished, Pi can obtain needed resources, execute,
return allocated resources, and terminate.
 When Pi terminates, Pk can obtain its needed resources, and so
on.
Avoidance or Bankers Algorithm
 In case of Single instance of a resource type.
Use a resource-allocation graph
 Multiple instances of a resource type, makes the
graph clumsy. Use the Banker’s Algorithm:
 Know total needs of all borrowers
 Do not allocate all money (resources) at a time
 Keep track of allocations and claims (still needed)
 Allocate only, if current allocation leaves enough
reserves to fulfill the need of some one else, who will
return all borrowed money.
 Pool up returned amount, to fulfill the need of others
Banker’s Algorithm (Single Type, Multiple Instances)
 Multiple instances of single type of resources.
 Each process must a priori claim maximum need
(State need).
 Piece wise allocation (allocation in parts, not in bulk) Keep
record of allocated resources
 When a process requests a resource, first it will be
allocated hypothetically (supposedly)
 If hypothetical allocation leaves the system safe
(invokes safety algorithm), then actually allocate the
resource, otherwise, revert the supposed allocation.
 When a process gets all its resources, it must return
them, in a finite amount of time.
 Returned (pooled up) resources can fulfill other’s need
Banker’s Algorithm (Safe Allocation)
Algorithm for one type, that can be extended for more types of resources.
An allocate() procedure, which invokes Safe-allocation(), is a procedure to avoid deadlocks
Needed[ ] and Allocated[ ] Arrays contain no. of still needed and allocated resources
for each process. Following code supposedly, allocates a resource to nth process, calls
safe_allocation procedure for all P processes and if it returns safe, makes actual
allocation, otherwise reverts, returning back recent supposed allocation.
allocate (n) { // make allocation for nth process
Allocated [n] = Allocated [n] + 1 // Assumed allocation for nth process
Needed[ n] = Needed [ n ] -1 // decrease need of one resource
P = max-no-of-processes() // all running processes
If (safe_ allocation ( P ) ) {
// Safe, go ahead for actual allocation
}
else { // revert supposed allocation
Allocated [n] = Allocated [n] - 1 // revert supposed allocation
Needed[ n] = Needed [ n ] + 1 // reset need / claim
}
}
Cont. Banker’s Algorithm (Safe Allocation)
Safe-allocation (or safety) algorithm for multiple instances of single type of resource (Say
Mag. Tapes). It can be extended for multiple types of resources.
At the end of this first part of safe allocation code, all processes will have finish flag as False and
the resources will contain no. of currently available or un-allocated resources

logical safe-allocation( int P) // P is total number of currently running processes


{
logical finish [ ] ; // To indicate that process can finish or complete
logical done = FALSE; // Check all reclaimed resources
int resources = TOTAL ; // Total available resources of this type
// Allocation part follows
for ( n=0 ; n<P ; n++) // Set processes cannot finish. Determine free resources

{
finish [ n ] = FALSE ; // assume nth process can not finish
resources = resources – Allocated [ n ]; // Presently free resources
}
Max. Need
Cont: safe-allocation procedure (second Allocated
part)
P0 10 6
while ( ! done )
P1 4 2
{
P2 9 2
done = TRUE ; // assume all processes are done / checked
for (n=0; n < P ; n++) // check processes that can return, if finished
{ // resource reclaiming part follows
if ( ! finish [n] ) && Needed [n] <= resources )
{
finish [n] = TRUE; // nth process can complete
resources = resources + Allocated [ n ]; // return resources
done = FALSE ; // needs further checking after reclaim
}
}
}

if ( resources == TOTAL ) // all processes finished, all resources returned


return ( TRUE ); // allocation is safe
else
return ( FALSE );

} // end of safe-allocation procedure


If resources have pooled up, needs further checking (setting Done flag as False), some processes may finish
Banker’s Algorithm II (Safe Allocation Algorithm for one type of
resources)
Allocate (n) { // Allocate one resource to nth process
logical finish [ ] ; // Indication that process can finish or complete
logical done = FALSE; // Checked all
int resources = TOTAL ; // Total available resources of this type
Allocated [n] = Allocated [n] + 1 // Assume allocation to nth process
Needed[ n] = Needed [ n ] -1 // decrease need of one resource
P = max-no-of-processes() // all running processes
for ( n=0 ; n<P ; n++) // do for all processes
{
finish [ n ] = FALSE ; // assume nth process can not finish
resources = resources – Allocated [ n ]; // Presently free resources
}
while ( ! done )
{
done = TRUE ; // assume all processes are done / checked
for (i=0; i < P ; i++) // check processes that can pay-back, if finished
{
if ( ! finish [i] ) && Needed [i] <= resources )
{
finish [i] = TRUE; // nth process can complete
resources = resources + Allocated [ i ]; // reclaim resources
done = FALSE ; // resources have pooled up; let us check further for unfinished processes
}
}
}
if ( resources == TOTAL ) { // all processes have finished, all resources returned
// Safe State; go ahead for actual allocation
}
else { // revert supposed allocation
Allocated [n] = Allocated [n] - 1 // Unsafe State; revert supposed allocation
Needed[ n] = Needed [ n ] + 1 // reset need
}
} // end of allocate procedure
Data Structures: Banker’s Algorithm (multiple resource
Extension to multiple types of resources:- (use multidimensional arrays)
types)
Let n = Maximum number of processes,
and m = number of resource types. {one vector (dimension) for each resource type}

 Available: Vector of length m. If available [j] = k, there


are k instances of resource type Rj available.
 Max: n x m matrix. If Max [i,j] = k, then process Pi may
request at most k instances of resource type Rj. (It can
be avoided by using only Need array)
 Allocation: n x m matrix. If Allocation [i,j] = k then Pi is
currently allocated k instances of Rj.
 Need: n x m matrix. If Need [i,j] = k, then Pi may need k
more instances of Rj to complete its task.

Need [i,j] = Max[i,j] – Allocation [i,j].


Safety Algorithm
1.Let Available and Finish be vectors of length m
and n, respectively. Initialize:
Available = Maximum Available resource vector
Finish [i] = false; for i = 0, 1, …, n- 1.
2. (checking) Find an ith process such that both:
(a) Finish [i] = false
(b) Needi  Available
If no such i exists, go to step 4.
3. (reclaiming) Available = Available + Allocatedi
(Pool up available
Finish[i] = true resources)
go to step 2.
4.If Finish [i] == true for all i, then the system is in
a safe state.
Resource-Request Algorithm for Process
Pi
Request = request vector for process P . If Request [j] = k
i i
then process Pi wants k instances of resource type Rj.
1. If Requesti  Needi go to step 2. Otherwise, raise error
condition, since process has exceeded its maximum claim.
2. If Requesti  Available, go to step 3. Otherwise Pi must wait,
since resources are not available.
3. Pretend to allocate requested resources to Pi by modifying the
state as follows:
Available = Available – Requesti; (Vector operation)
Allocatedi = Allocatedi + Requesti;
Needi = Needi – Requesti;
4. Test safety of allocation,
 If safe  the resources are actually allocated to Pi.
 If unsafe  Pi must wait, and the old resource-allocation
state is restored
Example of Banker’s Algorithm
 5 processes P0 through P4;
3 resource types (A, B and C):
A (10 instances), B (5 instances), and C (7 instances).
Resource Vector is (A,B,C) or (10,5,7)
 Snapshot at some time T0:
Allocation Max Available
ABC ABCABC
P0 0 1 07 5 3 3 3 2
P1 200322
P2 302902
P3 211222
P4 0 0 24 3 3
Example (Cont.)
 The content of the matrix Need is defined to be Max
– Allocation.
Need
ABC
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1

 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, vector
comparison (1,0,2)  (3,3,2)  true.
Allocation Need Available
ABC ABC ABC
P0 0 1 0 743 230
P1 3 0 2 020
P2 3 0 1 600
P3 2 1 1 011
P4 0 0 2 431
 Executing safety algorithm shows that the 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?
Banker’s Algorithm Example P1 Requests
(1,0,2)
Processes Max. Need Allocated Need Available
Total: ABC ABC ABC 10 5 7 Reclaiming Algorithm
P0 753 010 743 10 4 7 done = FALSE
while ( ! done )
{
P1 322 200 122 847 done = TRUE ;
for (n=0; n < P ; n++)
P2 902 302 600 545 {
if ( ! finish [n] ) && Needed [n] <=

P3 222 211 011 334 resources )


{
finish [n] = TRUE;
P4 433 002 431 332 resources = resources + Allocated [ n ];
done = FALSE ;
102<332 Req. by P1 Allocate it }
}

}
(1 0 2) P1 322 302 020 230 Allocation to P1
P (Need)SAFE: If all processes can complete with reclaimed resources 2 3 0
Reclaiming (Need < = resources)
P1 (0 2 0) 322 322 --- 532 230 + 302
P3 (0 1 1) 222 222 --- 743 532 + 211
P4 (4 3 1) 433 433 --- 745
P0 (7 4 3) 753 753 --- 755
P2 (6 0 0) 902 902 --- 10 5 7 755 + 302
Order processes finish

Tracing algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies safety criteria for the request (1,0,2) by P1 to be
granted.
Exercise:
A total of 3 types of resources (A, B and C) are available on a system,
such that there are A (5 instances), B (7 instances), and C (10
instances). That is, the Available Resource Vector (A,B,C) is (5,7,10). The
status of different resources for each process is shown as under:
Processes Max. Need Allocated Need Available
Total: ABC ABC ABC 5 7 10
P0 537 100 437 4 7 10
P1 223 002 221 478
P2 029 023 006 455
P3 222 112 110 343
P4 334 020 314 323

Do the following requests, to allocate resources will result into Safe state, if
so, in which sequence the processes will complete, otherwise, which
processes will enter in deadlock state? (consider any two of the following,
individually not one after the other)
1.P1 requests for resources (0 2 1) Safe.
2.P4 requests for resources (2 1 0) Unsafe. (P0 and P4 can not finish)
3.P0 requests for resources (2 2 0) Unsafe. (All processes will deadlock)
Solution Processes Max. Allocated Need Available
Total: Need ABC ABC 5 7 10
P0 537 100 437 4 7 10
P1 223 002 221 478
P2 029 023 006 455
P3 222 112 110 343
P4 334 020 314 323

The request by process P1 for resources (0 2 1) < Available resources (3 2 3), so after supposed
allocation to P1, the allocated will be (0 2 3), the Need will be (2,0,0) and Available resources
will be (3 0 2) Y/N
Process Can finish ? Allocated Need Available after reclaiming
  Need <= Available   ABC ABC Allocated + Available
P0 437 < 302 N 100 437  
P1 200 < 302 Y 023   023 +302 = 325
P2 006 < 325 N 002 006  
P3 110 < 325 Y 112   112 + 325 =437
314 < 437 Y 020   020 + 437 = 457
437 < 457 Y 100   100 + 457 = 557
P4 Y
006 < 557 023 0 2 3 + 5 5 7 = 5 7 10
P0
P2
The processes will finish in the sequence: < P1, P3, P4, P0, P2 >, This allocation results in Safe state.
Whereas, the request by process P4 for resources (3 0 3) < Available resources (3 2 3). After supposed
allocation to P4, the allocated will be (3 2 3), the need will be ( 0 1 1) and available resources will be (0 2
0). With this much available resources, need of none of the processes can be met. Therefore, this is an
Un-Safe state, so this allocation shall be reverted
Deadlock Detection and Recovery
(adopted on systems with remote chances of deadlock)
 Allows processes to enter into deadlock state
 No need to pre state needed resources
 Saves time otherwise consumed on running safety algorithms on
allocating each resource (record resource-process allocation)
 Keep record of Available (Work), Requests and Allocations
 Detection algorithm
 Execute automatically after regular intervals or interactively:
 When system or a group of processes look “BUSY, DOING
NOTHING”
 When ready queue is nearly empty and blocked queues are heavily
populated
 When idle process is taking most of the execution time
 Recovery scheme
 detecting deadlock, for recovery adopt conservative approach
Single Instance of Each Resource Type
 Maintain wait-for graph
 Nodes are processes.
 Pi  Pj if Pi is waiting for Pj.

 Periodically invoke an algorithm that searches for


a cycle in the graph. If there is a cycle, there
exists a deadlock.

 An algorithm to detect a cycle in a graph requires


an order of n2 operations, where n is the number
of vertices in the graph.
Resource-Allocation Graph and Wait-for Graph
(by retaining only processes and eliminating resources)

Resource-Allocation Graph Corresponding wait-for graph


In this graph there are two loops
{P1,P2,P4} and {P1,P2,P3,P4}
Resource-Allocation Graph
Algorithm
 Suppose that process P requests a resource R
i j

 The request can be granted only if converting the


request edge to an assignment edge does not
result in the formation of a cycle in the resource
allocation graph (Circular wait) in case of
Avoidance.
 In case assignment is granted, as in case of
Detection and Recovery then the processes
involved in cycles are in Deadlock condition
Several Instances of m Resource Types for n
processes
 Available: A vector of length m indicates the number of
total available resources of each type.

 Work: A vector of length m indicates currently available


resources of each type (working storage).
 Allocated: 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
requesting k more instances of resource type. Rj.

 finish: A vector of length n indicates current process can


not finish.
Detection Algorithm frequency
A check for deadlock can be made as frequently as
each resource is requested
 Advantages:
 it leads to early detection
 the algorithm is relatively simple
 Disadvantage
 frequent checks consumes considerable processor
time
less frequently, depending on how likely it is for a
deadlock to occur
Detection Algorithm
Keep record of allocations, requests, resource-process
allocation and available resources Available

1.Let Work and Finish be vectors of length m and


n, respectively Initialize:
(a) Work = Available
(b) For i = 1,2, …, n, if Allocationi  0, then
Finish[i] = false; otherwise, Finish[i] = true.
2.Check: Find an index i such that both:
(a) Finish[i] == false
(b) Requesti  Work

If no such i exists, go to step 4.


Detection Algorithm (Cont.)
3.Reclaim: Work = Work + Allocationi
Finish[i] = true
Reclaim allocated resources
go to step 2.

4.If Finish[i] == false, for some i, 1  i  n, then the


system is in deadlock state. Moreover, if Finish[i] ==
false, then process Pi is a member of deadlocked set.

Algorithm requires an order of O(m x n2) operations to detect


whether the system is in a deadlocked state.
Example of Detection Algorithm
 Five processes P0 through P4; three resource types
A (7 instances), B (2 instances), and C (6 instances).
(7 2 6)
 Snapshot at time T0:
Allocation Request Available
ABC ABC ABC
P0 0 1 0 000 716
P1 2 0 0 202
P2 3 0 3 000
P3 2 1 1 100
P4 0 0 2 002 000
 Sequence <P0, P2, P3, P4, P1> will result in Finish [i] = true for
all i, since the request vector for P0 and P2 is null, therefore
they will return allocated resources (3 1 3) in limited time.
Example (Cont.)
 P2 requests an additional instance of type C.
Request Available
ABC
P0 0 0 0 (0 1 0) after reclaiming
P1 2 0 1
P2 0 0 1
P3 1 0 0
P4 0 0 2
 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
 When, and how often, to invoke depends on:
 How often a deadlock is likely to occur?
 How many processes will need to be rolled back?
 one for each disjoint cycle

 If detection algorithm is invoked arbitrarily, there


may be many cycles in the resource graph and
so we would not be able to tell which of the
many deadlocked processes “caused” the
deadlock.
Recovery from Deadlock: Process
Termination
 Abort all deadlocked processes. (un economical)
 Abort one process (victim) at a time, by
successive pre-emption of resources, until
deadlock no longer exists. (conservative
approach)
 Back up each deadlocked process to some
previously defined checkpoint and restart
process later on, from the check pointed state
Selecting a victim
 Selecting a victim :– to minimize cost.
 Considerations in order to choose a victim
process to abort:
 Is the process interactive or batch?
 Priority of the process.
 How long process has computed, and how
much longer to complete (needs future info).
 Resources and their type, the process has
already used.
 Resources process still needs to complete
(needs future info).
 How many processes will need to be
terminated.
Recovery from Deadlock: Resource Preemption
or breaking “Non-preemption” condition
 Rollback – return to some safe state, restart
process from certain intermediate check pointed
state.

 Starvation Threat– same process may always be


picked as a victim for denial of resource
allocation, include number of rollback in cost
factor.
Summary

You might also like