Powerful Google developer tools for immediate impact! (2023-24 C)
Mch7 deadlock
1. 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)
2. 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.
3. Example: Narrow Bridge Crossing
Traffic can flow only in one direction on a bridge
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 also possible.
5. Deadlock: Traffic Crossing
T
r
a
f
f
i
c
2
Traffic 1
R1
R4
Deadlock
T1 holds resource R1, waiting for R2
T2 holds resource R2, waiting for R3
T3 holds resource R3, waiting for R4
T4 holds resource R4, waiting for R1
R2
R3
Traffic 3
Circular wait
User T1 waiting for T2 to complete
User T2 waiting for T3 to complete
User T3 waiting for T4 to complete
User T4 waiting for T1 to complete
6. 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 in the set,
in a cycle.
Example
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
Concurrent processes P0 and P1 share semaphores A and B,
which are initialized to 1
P0
wait (A);
wait (B);
………
P1
wait(B)
wait(A)
………
Process P0 waiting for process P1 to release (signal) semaphore B and
P1 is waiting for semaphore A to be signaled by P0
7. 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.
Utilization Protocol: Each process utilizes a
resource as follows:
requests
uses
releases
8. Deadlock Characterization
Deadlock can occur if following conditions hold simultaneously.
Mutual exclusion: Exclusive use of a resource, only by
one process at a time.
Hold and wait: A process holding at least one resource,
is 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, …, P0} 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.
9. 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
10. Resource-Allocation Graph
(Processes and Multiple instances of resources.)
Process
Resource Type with 1 & 4 instances
Pi requests an instance of Rj
Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
14. Basic Facts
If graph contains no cycles ⇒ no deadlock.
If graph contains a cycle ⇒
if only one instance per resource type, then deadlock.
if several instances per resource type, possibility of
deadlock.
15. 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
Allow the system to enter a deadlock state
detect it and then recover.
3.
Detection and Recovery
16. Deadlock Prevention
Bulk allocation of all required resources (by process itself or optionally
by OS), in the beginning, if possible, otherwise reject request.
Alternatively break any necessary condition to prevent deadlock
Mutual Exclusion – not required for sharable
resources; must hold 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.
17. 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.
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.
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)
18. Deadlock Avoidance
Requires that the system has some additional a priori information available.
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.
19. Safe, Unsafe , Deadlock Threat
No deadlock threat
Maximum Need
Allocated
claim
P0
10
6
4
P1
4
2
2
P2
9
2
7
Current state:
Total resources = 12
Available = 2
Safe: if next allocation is done to process P1 (leaves 1 tape, needs 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)
20. 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 usually state
maximum need, but may not use all resources).
Avoidance ⇒ ensure that a system will never
enter in an unsafe state, before allocation of
resources.
21. Safe Allocation (Banker’s Algorithm)
When a process requests a resource, system decides, if
current allocation leaves the system in a safe state then
it allocates the resource, otherwise denies allocation.
System is in safe state if there exists a possibility that
current allocation leaves some of the processes that can
complete their execution and the resources thereby
released can satisfy needs of some other 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.
22. Avoidance algorithms
In case of Single instance of a resource type.
Use a resource-allocation graph
Multiple instances of a resource type. Use the
banker’s algorithm
Know total needs of 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 to fulfill need of others
23. 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.
24. Resource-Allocation Graph
R1 resource has been allocated to P1, P1 may request R2 resource
P2 have request for R1 resource and may request R2 resource
Request Edge
Assignment Edge
May request (needed)
25. Unsafe State In Resource-Allocation Graph
R1 resource has been allocated to P1, P1 may request R2 resource
P2 have request for R1 resource and R2 resource has been allocated to
P2
No threat of deadlock until P1 requests R2 resource
26. Resource-Allocation Graph Algorithm
Suppose that process Pi requests a resource Rj
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)
27. Banker’s Algorithm (Single Resource Type)
Multiple instances of single type of resources.
Each process must a priori claim maximum need
(State total need).
Allocation in parts, not in total
When a process requests a resource it may have
to wait.
When a process gets all its resources it must
return them in a finite amount of time.
Returned resources can fulfill other’s need
28. Banker’s Algorithm (Safe Allocation)
Algorithm for one type, that can be extended for more types of resources.
Safe-allocation is a procedure to avoid deadlocks, invoked in an allocation procedure
Needed[ ] and Allocated[ ] Arrays contain no. of still needed and allocated resources for
each process. Following program supposedly allocates resource to nth process among all P
processes, calls safe_allocation procedure, if safe, make actual allocation, otherwise not
Allocate (n) {
Allocated [n] = Allocated [n] + 1
Needed[ n] = Needed [ n ] -1
// make allocation for nth process
// Assume allocation for nth process
// decrease need of one resource
If (safe_ allocation ( P ) ) {
// go ahead for actual allocation
}
else {
Allocated [n] = Allocated [n] - 1
Needed[ n] = Needed [ n ] + 1
}
}
// revert supposed allocation
// reset need
29. Cont. Banker’s Algorithm (Safe Allocation)
Safe-allocation algorithm for single type of resource (Say Mag. Tapes), but of multiple
instances. It can be extended for other types of resources
logical safe-allocation( int P)
// P is total number of currently running processes
{
logical finish [ ] ;
// To contain indication that process can finish or complete
logical done = FALSE;
// Checked all
int resources = TOTAL ;
// Total available resources of this type
for ( n=0 ; n<P ; n++)
// do for all processes
{
finish [ n ] = FALSE ;
// assume nth process can not finish
resources = resources – Allocated [ n ];
}
// Presently available resources
30. Cont: safe-allocation procedure
.
while ( ! done )
{
done = TRUE ;
// assume all processes are done / checked
for (n=0; n < P ; n++)
// check processes that can pay-back, if finished
{
if ( ! finish [n] ) && Needed [n] <= resources )
{
finish [n] = TRUE;
// Oh, It can complete
resources = resources + Allocated [ n ];
// reclaim resources
done = FALSE ;
// needs further checking
}
}
}
if ( resources == TOTAL )
return ( TRUE );
else
return ( FALSE );
}
// all processes finished, all resources returned
// allocation is safe
// end of safe-allocation procedure
31. Example: Single Type
Resources
Process 1
Process 2
Process 3
A
F
A
N
A
N
A
N
0
8
0
6
0
3
0
4
.
.
-
-
-
-
-
-
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
7
1
5
1
1
2
1
3
A: Allocated;
F: Free;
N : still Needed
Safe
Request
32. Data Structures: Banker’s Algorithm (multiple types)
Extension to multiple types of resources:-
(use multidimensional array)
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 needed 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.
33. Safety Algorithm
1.Let Work and Finish be vectors of length m and
n, respectively. Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1.
2.Find an i such that both:
(a) Finish [i] = false
(b) Needi ≤ Work
If no such i exists, go to step 4.
3.Work = Work + Allocationi
Finish[i] = true
go to step 2.
4.If Finish [i] == true for all i, then the system is in
a safe state.
34. Resource-Request Algorithm for Process Pi
Request = request vector for process Pi. If Requesti [j] = k
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 – Request;
Allocationi = Allocationi + Requesti;
(Vector operation)
Needi = Needi – Requesti;
If safe ⇒ the resources are allocated to Pi.
If unsafe ⇒ Pi must wait, and the old resource-allocation state is
restored
35. Example of Banker’s Algorithm
5 processes P0 through P4;
3 resource types (A, B and C):
A (10 instances), B (5instances), and C (7 instances).
Resource Vector is (A,B,C) or (10,5,7)
Snapshot at some time T0:
Allocation Max Available
ABC ABC ABC
P0 0 1 0 7 5 3
332
P1 2 0 0
322
P2 3 0 2
902
P3 2 1 1
222
36. 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 <
37. 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 3 0 2
020
P2 3 0 1
600
P3 2 1 1
011
P4 0 0 2
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?
39. Deadlock Detection and Recovery
(adopted on systems with remote chances of deadlock)
Allows system to enter into deadlock state
No need to pre state resources need
Saves time otherwise consumed on running algorithms while
allocating each resource
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, adopt conserving approach
40. 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.
42. Several Instances of a Resource Type
Available: A vector of length m indicates the
number of available resources of each type.
Allocation: 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 [ij] = k,
then process Pi is requesting k more instances
of resource type. Rj.
43. Detection Algorithm
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.Find an index i such that both:
(a) Finish[i] == false
(b) Requesti ≤ Work
If no such i exists, go to step 4.
44. Detection Algorithm (Cont.)
3.Work = Work + Allocationi
Finish[i] = true
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 Pi is deadlocked.
Algorithm requires an order of O( m x n2) operations to detect
whether the system is in deadlocked state .
45. Example of Detection Algorithm
Five processes P0 through P4; three resource types
A (7 instances), B (2 instances), and C (6 instances).
Snapshot at time T0:
AllocationRequestAvailable
ABC ABC
ABC
P0 0 1 0
000
000
P1 2 0 0
202
P2 3 0 3 0 0 0
P3 2 1 1
100
P4 0 0 2
002
Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true
46. Example (Cont.)
P2 requests an additional instance of type C.
Request
ABC
P0 0 0 0
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.
47. 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.
48. Recovery from Deadlock:
Process Termination
Abort all deadlocked processes.
Abort one process at a time until the deadlock cycle is
eliminated.
Considerations in order to choose a process to abort:
Priority of the process.
How long process has computed, and how much longer to
completion.
Resources the process has used.
Resources process needs to complete.
How many processes will need to be terminated.
Is process interactive or batch?
49. Recovery from Deadlock: Resource
Preemption
Selecting a victim – minimize cost.
Rollback – return to some safe state, restart
process for that check pointed state.
Starvation Threat– same process may always
be picked as victim for denial of resource
allocation, include number of rollback in cost
factor.