deadlocks - wordpress.com · deadlocks: each process can request resources only in an increasing...
TRANSCRIPT
![Page 1: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/1.jpg)
Deadlocks
Prepared By: Kaushik Vaghani
Prepared By : Kaushik Vaghani
![Page 2: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/2.jpg)
Outline
• System Model
• Deadlock Characterization
• Methods for Handling Deadlocks
• Deadlock Prevention
• Deadlock Avoidance
• Deadlock Detection & Recovery
Prepared By: Kaushik Vaghani
![Page 3: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/3.jpg)
The Deadlock Problem
• A deadlock consists of a set of blocked processes,each holding a resource and waiting to acquire aresource held by another process in the set
• Example #1
– A system has 2 disk drives
– P1 and P2 each hold one disk drive and each needs theother one
• Example #2
– Semaphores A and B, initialized to 1
P0 P1
wait (A); wait(B)
wait (B); wait(A)
Prepared By: Kaushik Vaghani
![Page 4: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/4.jpg)
Bridge Crossing Example
• Traffic only in one direction
• The resource is a one-lane bridge
• 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 possiblePrepared By: Kaushik Vaghani
![Page 5: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/5.jpg)
System Model
• Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
• Each resource type Ri has 1 or more instances
• Each process utilizes a resource as follows:– request
– use
– release
Prepared By: Kaushik Vaghani
![Page 6: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/6.jpg)
Deadlock CharacterizationNecessary Conditions
• Mutual exclusion: At least one resource must be held in anonsharable mode; that is, only one process at a time can use theresource. If another process requests that resource, therequesting process must be delayed until the resource has beenreleased.
• Hold and wait: A process holding at least one resource iswaiting to acquire additional resources held by other processes.
• No preemption: Resource cannot be preempted; that is, aresource can be released only voluntarily by the process holding itafter that process has completed its task.
• Circular wait: there exists a set {P0, P1, …, Pn} of waitingprocesses 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 waitingfor a resource that is held byPn, and Pn is waiting for a resource that is held by P0.
Deadlock can arise if four conditions hold simultaneously.
Prepared By: Kaushik Vaghani
![Page 7: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/7.jpg)
Resource-Allocation Graph
• V is partitioned into two types:
– P = {P1, P2, …, Pn}, the set consisting ofall the processes in the system
– R = {R1, R2, …, Rm}, the set consistingof all resource types in the system
• request edge – directed edge P1 Rj
• assignment edge – directed edge Rj Pi
A set of vertices V and a set of edges E.
• Deadlocks can be described more precisely in terms of a directed graphcalled a system Resource-Allocation Graph
Prepared By: Kaushik Vaghani
![Page 8: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/8.jpg)
Resource-Allocation Graph (Cont.)
• Process
• Resource Type with 4 instances
• Pi requests instance of Rj
• Pi is holding an instance of Rj
Pi
Pi
Rj
Rj
Prepared By: Kaushik Vaghani
![Page 9: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/9.jpg)
Resource Allocation Graph With A Deadlock
Before P3 requested an
instance of R2
After P3 requested an
instance of R2
Given the definition of a resource-allocation graph, it can be shown that, if the graphcontains no cycles, then no process in the system is deadlocked. If the graph does contain acycle, then a deadlock may exist. Prepared By: Kaushik Vaghani
![Page 10: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/10.jpg)
Graph With A Cycle But No Deadlock
Process P4 may release its instance of resource type R2. That
resource can then be allocated to P3, thereby breaking the cycle.Prepared By: Kaushik Vaghani
![Page 11: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/11.jpg)
Relationship of cycles to deadlocks
• If a resource allocation graph contains no cycles no deadlock
• If a resource allocation graph contains a cycle and if only one instance exists per resource type deadlock
• If a resource allocation graph contains a cycle and andif several instances exists per resource type possibility of deadlock
Prepared By: Kaushik Vaghani
![Page 12: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/12.jpg)
Methods for Handling Deadlocks
Prepared By: Kaushik Vaghani
![Page 13: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/13.jpg)
Methods for Handling Deadlocks
• Prevention – Ensure that the system will never enter a deadlock state.
• Avoidance– Ensure that the system will never enter an unsafe state.
• Detection & Recovery– Allow the system to enter a deadlock state, detect it, and then
recover.
• Do Nothing – Ignore the problem and pretend that deadlocks never occur in
the system. Used by most OS, including Unix and Windows.
Prepared By: Kaushik Vaghani
![Page 14: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/14.jpg)
Methods for Handling Deadlocks (Cont.)
• To ensure that deadlocks never occur, the systemmay use either a deadlock-prevention or adeadlock-avoidance scheme.
• Deadlock Prevention provides a set of methodsfor ensuring that at least one of the necessaryconditions cannot hold.
• Deadlock Avoidance requires that the operatingsystem be given in advance additional informationconcerning which resources a process will requestand use during its lifetime. With this additionalknowledge, it can decide for each request whetheror not the process should wait.
Prepared By: Kaushik Vaghani
![Page 15: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/15.jpg)
Deadlock Prevention
Prepared By: Kaushik Vaghani
![Page 16: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/16.jpg)
Deadlock Prevention
• Mutual Exclusion – The mutual-exclusion condition musthold for non-sharable resources.
• Hold and Wait – we must guarantee that, whenever aprocess requests a resource, it does not hold any otherresources.
– Require a process to request and be allocated all its resources beforeit begins execution, or
– allow a process to request resources only when the process hasnone.
– Result: Low resource utilization; starvation possible.
As we noted , for a deadlock to occur, each of the four necessaryconditions must hold. By ensuring that at least one of these conditionscannot hold, we can prevent the occurrence of a deadlock.
Prepared By: Kaushik Vaghani
![Page 17: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/17.jpg)
Deadlock Prevention
• To illustrate the difference between these two protocols, weconsider a process that copies data from a DVD drive to a file ondisk, sorts the file, and then prints the results to a printer. If allresources must be requested at the beginning of the process, thenthe process must initially request the DVD drive, disk file, andprinter. It will hold the printer for its entire execution, even thoughit needs the printer only at the end.
• The second method allows the process to request initially only theDVD drive and disk file. It copies from the DVD drive to the diskand then releases both the DVD drive and the disk file. Theprocess must then again request the disk file and the printer. Aftercopying the disk file to the printer, it releases these two resourcesand terminates.
Prepared By: Kaushik Vaghani
![Page 18: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/18.jpg)
Deadlock Prevention (Cont.)• No Preemption –
– If a process that is holding some resources requests anotherresource that cannot be immediately allocated to it, then allresources currently being held are released
– Preempted resources are added to the list of resources for whichthe process is waiting
– A process will be restarted only when it can regain its oldresources, as well as the new ones that it is requesting
• Circular Wait – impose a total ordering of all resource types, and
require that each process requests resources in an increasing order of enumeration. For example:
F(tape drive) = 1F(disk drive) = 5F(printer) = 12
We can now consider the following protocol to preventdeadlocks: Each process can request resources only in anincreasing order of enumeration. That is, a process caninitially request any number of instances of a resource type-say, Ri. After that, the process can request instances ofresource type Rj if and only if F(Rj) > F(Ri).Prepared By: Kaushik Vaghani
![Page 19: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/19.jpg)
Deadlock Avoidance
Prepared By: Kaushik Vaghani
![Page 20: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/20.jpg)
Deadlock Avoidance
• Simplest and most useful model requires that each processdeclare the maximum number of resources of each type that itmay need. Given this a priori information, it is possible toconstruct an algorithm that ensures that the system will neverenter a deadlocked state.
• The deadlock-avoidance algorithm dynamically examines theresource-allocation state to ensure that there can never be acircular-wait condition.
• A resource-allocation state is defined by the number of availableand allocated resources, and the maximum demands of theprocesses
It Requires that the system has some additional a priori information
available about how resources are to be requested.
Prepared By: Kaushik Vaghani
![Page 21: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/21.jpg)
Safe State• When a process requests an available resource, the system
must decide if immediate allocation leaves the system in a safestate.
• A system is in a safe state only if there exists a safe sequence.
• A sequence of processes <P1, P2, …, Pn> is a safe sequence forthe current allocation state if, for each Pi, the resource requeststhat Pi can still make, can be satisfied by currently availableresources plus resources held by all Pj, with j < i.
• That is:
– If the Pi resource needs are 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, Pi +1 can obtain its needed resources, and so on
Prepared By: Kaushik Vaghani
![Page 22: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/22.jpg)
Safe State (continued)
• 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
Prepared By: Kaushik Vaghani
![Page 23: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/23.jpg)
Safe, Unsafe , Deadlock State
Prepared By: Kaushik Vaghani
![Page 24: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/24.jpg)
Avoidance algorithms
• For a single instance of a resource type, use a resource-allocation graph
• For multiple instances of a resource type, use the banker’s algorithm
Prepared By: Kaushik Vaghani
![Page 25: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/25.jpg)
Resource-Allocation Graph Scheme• For this, we use a variant of the resource allocation
graph and Introduce a new kind of edge called a claimedge in addition to the request and assignment edges.
• Claim edge Pi - - - - -> Rj indicates that process Pj mayrequest resource Rj; which is represented by a dashedline
• A claim edge converts to a request edge when a processrequests a resource
• A request edge converts to an assignment edge whenthe resource is allocated to the process
• When a resource is released by a process, an assignmentedge reconverts to a claim edge
Prepared By: Kaushik Vaghani
![Page 26: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/26.jpg)
Resource-Allocation Graph Scheme
• Resources must be claimed a priori in the system. Thatis, before process Pi starts executing, all its claim edgesmust already appear in the resource-allocation graph.
• Now Suppose that process Pi requests a resource Rj.
• The request can be granted only if converting therequest edge to an assignment edge does not result inthe formation of a cycle in the resource allocation graph.
• If no cycle exists, then the allocation of the resource willleave the system in a safe state. If cycle exists, then theallocation will put the system in an unsafe state.
Prepared By: Kaushik Vaghani
![Page 27: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/27.jpg)
Resource-Allocation Graph withClaim Edges
Requestedge
Assignmentedge
Claimedge
Claimedge
Prepared By: Kaushik Vaghani
![Page 28: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/28.jpg)
Unsafe State In Resource-Allocation Graph
Assignmentedge
Requestedge
AssignmentedgeClaim
edge
Prepared By: Kaushik Vaghani
![Page 29: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/29.jpg)
Banker’s Algorithm
• Used when there exists multiple instances of a resource type.
• Banker’s algorithm is less efficient than the resource-allocation graph scheme.
• The name was chosen because the algorithm could be usedin a banking system to ensure that the bank never allocatedits available cash in such a way that it could no longer satisfythe needs of all its customers.
• Each process must declare the maximum number ofinstances of each resource type that it may need when itenters the system.
Prepared By: Kaushik Vaghani
![Page 30: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/30.jpg)
Banker’s Algorithm
• When a user requests a set of resources, the systemmust determine whether the allocation of theseresources will leave the system in a safe state.
• If it will, the resources are allocated; otherwise, theprocess must wait until some other process releasesenough resources.
• When a process gets all its resources, it must return them in afinite amount of time
Prepared By: Kaushik Vaghani
![Page 31: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/31.jpg)
Data Structures for the Banker’s Algorithm
• Available: A vector of length m indicates the number ofavailable resources of each type. If available [j] = k, thereare k instances of resource type Rj available.
• Max: (n x m matrix). If Max [i][j] = k, then process Pi mayrequest at most k instances of resource type Rj.
• Allocation: (n x m matrix). If Allocation[i][j] = k then Pi iscurrently allocated k instances of Rj.
• Need: (n x m matrix). If Need[i][j] = k, then Pi may need kmore instances of Rj to complete its task.
Need [i,j] = Max[i][j] – Allocation [i][j]
Let n = number of processes in the system, m = number of resources types.
Prepared By: Kaushik Vaghani
![Page 32: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/32.jpg)
Data Structures for the Banker’s Algorithm
• We can treat each row in the matrices Allocationand Need as vectors and refer to them asAllocationi and Needi.
• The vector Allocationi specifies the resourcescurrently allocated to process Pi; the vectorNeedi specifies the additional resources thatprocess Pi may still request to complete its task.
Prepared By: Kaushik Vaghani
![Page 33: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/33.jpg)
Banker’s Algorithm (Cont.)
Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively.Initially,
Work = Available
Finish[i] = false for i = 0, 1, ... , n - 1.
This means, initially, no process has finished and the number ofavailable resources is represented by the Available array.
2. Find an index i such that bothFinish[i] == false
Needi <= Work
If there is no such i present, then proceed to step 4.
It means, we need to find an unfinished process whose need canbe satisfied by the available resources. If no such process exists,just go to step 4.
Prepared By: Kaushik Vaghani
![Page 34: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/34.jpg)
Banker’s Algorithm (Cont.)
Safety Algorithm
3. Perform the following:
Work = Work + Allocationi
Finish[i] = true;
Go to step 2.
When an unfinished process is found, then the resources areallocated and the process is marked finished. And then, the loop isrepeated to check the same for all other processes.
4. If Finish[i] == true for all i, then the system is in a safe state.
Prepared By: Kaushik Vaghani
![Page 35: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/35.jpg)
Banker’s Algorithm (Cont.)
Resource-Request Algorithm
• This is the algorithm for determining whether requests can be safely granted.
• Let Requesti be the request vector for process Pi.
If Requesti [j] == k, then process Pi wants k instances of resource type Rj.
• When a request for resources is made by process Pi , the following actions are taken:
1. If Requesti <= Needi , go to step 2. Otherwise, raise an errorcondition, since the process has exceeded its maximum claim.
2. If Requesti <= Available, go to step 3. Otherwise, Pi must wait,since the resources are not available.
3. Have the system pretend to have allocated the requestedresources to process Pi by modifying the state as follows:
Prepared By: Kaushik Vaghani
![Page 36: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/36.jpg)
Banker’s Algorithm (Cont.)
Resource-Request Algorithm
Available = Available - Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi - Requesti;
• If the resulting resource-allocation state is safe, the transaction is completed, and process Pi is allocated its resources.
• However, if the new state is unsafe, then Pi must wait for Requesti, and the old resource-allocation state is restored.
Prepared By: Kaushik Vaghani
![Page 37: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/37.jpg)
Deadlock Detection
Prepared By: Kaushik Vaghani
![Page 38: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/38.jpg)
Deadlock Detection
• For deadlock detection, the system mustprovide– An algorithm that examines the state of the system to detect
whether a deadlock has occurred
– And an algorithm to recover from the deadlock
• A detection-and-recovery scheme requiresvarious kinds of overhead– Run-time costs of maintaining necessary information and
executing the detection algorithm
– Potential losses inherent in recovering from a deadlockPrepared By: Kaushik Vaghani
![Page 39: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/39.jpg)
Single Instance of Each Resource Type• Requires the creation and maintenance of a
wait-for graph– Consists of a variant of the resource-allocation graph
– The graph is obtained by removing the resource nodes from a resource-allocation graph and collapsing the appropriate edges
– Consequently; all 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
Prepared By: Kaushik Vaghani
![Page 40: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/40.jpg)
Resource-Allocation Graph and Wait-for Graph
Resource-Allocation Graph Corresponding wait-for graphPrepared By: Kaushik Vaghani
![Page 41: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/41.jpg)
Multiple 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 [i][j] = k, then process Pi is requesting k more instances of resource type Rj.
Required data structures:
Prepared By: Kaushik Vaghani
![Page 42: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/42.jpg)
Multiple Instances of a Resource Type• we again treat the rows in the matrices Allocation and
Request as vectors; we refer to them as Allocationi andRequesti.
1. Let Work and Finish be vectors of length m and n, respectively. Initialize
Work= Available.
For i= 0, 1, ... , n-1, 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.
Prepared By: Kaushik Vaghani
![Page 43: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/43.jpg)
Multiple Instances of a Resource Type3. Work= Work+ Allocationi
Finish[i] = true
Go to step 2.
4. If Finish[i] == false for some i, 0 <= i < n, then the system is in a deadlocked state. Moreover, if Finish[i] == false, then process Pi
is deadlocked.
Prepared By: Kaushik Vaghani
![Page 44: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/44.jpg)
Detection-Algorithm Usage• When, and how often, to invoke the detection algorithm
depends on:– How often is a deadlock likely to occur?
– How many processes will be affected by deadlock when it happens?
• If the detection algorithm is invoked arbitrarily, there maybe many cycles in the resource graph and so we would notbe able to tell which one of the many deadlocked processes“caused” the deadlock
• If the detection algorithm is invoked for every resourcerequest, such an action will incur a considerable overheadin computation time
• A less expensive alternative is to invoke the algorithm atdefined interval- for example, once per hour or wheneverCPU utilization drops below 40%.
Prepared By: Kaushik Vaghani
![Page 45: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/45.jpg)
Recovery From Deadlock
Prepared By: Kaushik Vaghani
![Page 46: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/46.jpg)
Recovery from Deadlock
• Two Approaches
– Process Termination
– Resource Preemption
Prepared By: Kaushik Vaghani
![Page 47: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/47.jpg)
Recovery from Deadlock: Process Termination
• Abort all deadlocked processes– This approach will break the deadlock, but at great expense; deadlocked
process running from long time will have to be recomputed later.
• Abort one process at a time until the deadlock cycle is eliminated– This approach incurs considerable overhead, since, after each process is
aborted, a deadlock-detection algorithm must be re-invoked to determinewhether any processes are still deadlocked.
Many factors may affect which process is chosen for termination– What is the priority of the process?
– How long has the process run so far and how much longer will the processneed to run before completing its task?
– How many and what type of resources has the process used?
– How many more resources does the process need in order to finish its task?
– How many processes will need to be terminated?
– Is the process interactive or batch?
Prepared By: Kaushik Vaghani
![Page 48: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/48.jpg)
Recovery from Deadlock: Resource Preemption
• With this approach, we successively preempt some resources fromprocesses and give these resources to other processes until thedeadlock cycle is broken
• When preemption is required to deal with deadlocks, then three issuesneed to be addressed:
– Selecting a victim – Which resources and which processes are to bepreempted?
– Rollback – If we preempt a resource from a process, what should be donewith that process? Clearly it cannot continue with its normal execution; Itis missing some needed resources. We must roll back the process to somesafe state and restart it from that state.
– Starvation – How do we ensure that starvation will not occur? That is, howcan we guarantee that resources will not always be preempted from thesame process?
Prepared By: Kaushik Vaghani
![Page 49: Deadlocks - WordPress.com · deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances](https://reader034.vdocument.in/reader034/viewer/2022042307/5ed322276d4e1758d41019c9/html5/thumbnails/49.jpg)
Summary• Four necessary conditions must hold in the system for a deadlock to occur
– Mutual exclusion
– Hold and wait
– No preemption
– Circular wait
• Four principal methods for dealing with deadlocks
– Use some protocol to (1) prevent or (2) avoid deadlocks, ensuring that the system will never enter a deadlock state
– Allow the system to enter a deadlock state, (3) detect it, and then recover
• Recover by process termination or resource preemption
– (4) Do nothing; ignore the problem altogether and pretend that deadlocks never occur in the system (used by Windows and Unix)
• To prevent deadlocks, we can ensure that at least one of the four necessary conditions never holds
Prepared By: Kaushik Vaghani