1 / 21

Deadlock

Deadlock. Permanent blocking of a set of process that compete for system resources or communicate with each other. Conditions that characterizes deadlock Mutual exclusion Hold and wait No pre-emption Circular wait. P 5. R 1. R 3. R 4. P 1. P 2. P 3. P 4. R 2. R 5.

anka
Download Presentation

Deadlock

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Deadlock • Permanent blocking of a set of process that compete for system resources or communicate with each other. • Conditions that characterizes deadlock • Mutual exclusion • Hold and wait • No pre-emption • Circular wait

  2. P5 R1 R3 R4 P1 P2 P3 P4 R2 R5 Resource allocation graph Mathematically G=(V,E) Where V={P,R} , P=set of Processes and R=set of Recources And E={set of Claim Edge, Set of Allocation Edge} Claim or request edge, from process to resource Allocation edge, from resource to process

  3. Different methods of deadlock handling • Prevention • ensure that one of the condition for characterizing deadlock does not arise. • Avoidance • avoid the situations that may lead us to deadlock. • Detection and recovery • if the system faces deadlock, detect it and then take the measure to recover from it.

  4. Deadlock Prevention • Ensuring that Hold and Wait never occur • Ensure that when a process makes a request for a new resource, at that time it does not hold any other resource. • Grab all the required resource at the very beginning. • Drawbacks • Process may face starvation • Resource utilization is very poor • Alternate approach, release the currently holding resources when a new request is made.

  5. A P0 P1 B C Deadlock PreventionAllowing preemption If a process makes a request for a new resource, and the request can’t be granted then OS will preempt all the resource from this process and in the list of resource for which the process is waiting, add these resources. Later on the process has to grab these preempted resource as well as that resource for which it gave the request. Request can’t be granted P0 waits for resource B, and resource A and C are Preempted. Finally P0 waits for resource A,B and C

  6. P1(ready) P3(waiting) P0 P2 P2 executes by Preempting resource B from P3. A A B B C C Deadlock PreventionAllowing preemption Alternate approach : If the request can’t be granted, check whether the resource is currently held by any active process or not. If yes then the process which have made the request will go to wait state. If no , held by a waiting process, then preempt the resource from the waiting process. In this way all the resource from a waiting process may be taken away. When such a waiting process will start again it has to grab all the resource that it has loosed while waiting. This protocol is applicable for those resources whose state can be easily saved and restored, like CPU registers Memory Spaces etc. It can’t be applied to to resources like Printer tape drive etc. P0 goes to wait state

  7. Deadlock PreventionNot allowing to occur the Circular wait Circular wait can be prevented by defining a linear ordering of resources. If a process is holding a resource R, then it can subsequently request only those resources of types following R in the ordering. i.e. Resource are allocated in increasing order. Resource type are assigned a unique number, F(Ri). If a process request for a new resource Ri then it must first release all resources Rj, if it is holding, such that F(Rj)F(Ri). It will guarantee that there will be no circular wait. Proof: Let there is a circular wait P0 P1 … Pn-1 Pn P0 If it happens then F(R0)< F(R1)< …..< F(Rn-1)< F(Rn)< F(R0) must be satisfied. But it contradicts that F(R0)< F(R0)

  8. R1 R1 P1 P2 P1 P2 R2 R2 R1 P1 P2 R2 Deadlock Avoidance In the situation of single instance of every resource type— When a process makes a request for a resource, request can be granted if no cycle is formed in the resource allocation graph after converting that claim edge to an allocation edge.

  9. Deadlock AvoidanceBanker’s algorithm • If there are multiple instances of different resource types, a cycle in the resource allocation graph is not sufficient to declare deadlock. • Different data structures used in Banker’s algorithm • Available : 1-array, Available[i]=k means k instances of i-th resource type is currently available. • Max : 2- dimensional array, Max[i][j]=k means process Pi can request for at most k instances of resource Rj. • Allocation : 2- dimensional array, Allocation [i][j]=k means process Pi is currently holding k instances of resource Rj. • Need : 2- dimensional array, Need[i][j]=k means process Pi needs k more instances of resource Rj. Need[i][j]=max[i][j] – Allocation [i][j]

  10. Deadlock AvoidanceBanker’s algorithm (Contd.) • Works in two phase • Resource request algorithm • Changes in the various data structure is done here • Safety algorithm • Checks whether the state is safe or not. A state is called unsafe if it leads to a deadlock state. Otherwise the state is safe.

  11. Deadlock Avoidance Resource request algorithm (Contd.) • Requesti is the request vector of process Pi; Requesti[j]=k means at this moment Pi nwants k instances of resource Rj. • If Requesti Needi go to step 2. Else declare error. • If Requesti Available go to step 3. Else Pi has to wait. • Do the following changes • Available= Available - Requesti • Allocationi = Allocationi + Requesti • Needi = Needi - Requesti

  12. Deadlock Avoidance (Contd.) safety algorithm • Temporary data structure used Work and Finish, arrays of length #resources and #processes respectively. • Initialize, work=Available and i Finish[i]=false • Find an i such that (( Finish[i]=false) && (Needi  Work)) • If no such i exists then go to step 4 • Work=work+allocationi • Finish[i]=true • Go to step 2 • If i Finish[i]=true then the system is in a safe state. Else the system is in a unsafe state and undo the changes in the data structures done by the first phase.

  13. T T T T T 5 3 2 7 5 5 7 4 3 13 5 5 7 4 5 + 2 1 1 + 0 0 2 + 6 0 0 + 0 1 0 P1 P3 P4 P0 P2 Example Work = 3 3 2 + 2 0 0

  14. 0 1 2 3 3 0 Change Allocation0 Change Need0 If the current state of the system is as given below, can the request by P0 for (0,0,2) be granted ? Example Resource Request Algorithm Change Available 7 4 1 The allocation of the resource to process P0 leads us to this new state where the available resource can not satisfy request for maximum claim by any process. That means it is an unsafe state. So the request will not be granted.

  15. Drawback of banker’s algorithm It has to maintain the data structures. Which wastes a lot of memory space. To collect the information for Max matrix is not a simple job, it will waste a lot of CPU time. Drawback of all deadlock Avoidance Avoidance algorithm has to be executed every time a process makes a request for a resource. But every resource allocation will not lead us to deadlock. i.e. we have to execute the algorithm if there is no chance of deadlock. In other words it will waste a lot of CPU time.

  16. P5 P5 R1 R3 R4 P2 P3 P1 P1 P2 P3 P4 P4 R2 R5 Detection and RecoveryResources have single instances Cycle in the wait-for graph will declare that there is a deadlock Wait-for graph can be obtained from resource allocation graph. If there are two edges Pi Rb and Rb Pj in the resource allocation graph, then there will be an edge Pi Pj in the wait-for graph.

  17. P0 P0 R0 R0 R0 R0         R1 R1 R1 R1     P1 P2 P1 P2 P0 P0         R2 R2 R2 R2 P1 P2 P1 P2 Detection and RecoveryResources have multiple instances Reduction of Resource allocation graph Graph is reduced by Pi if a) Pi has no request edge b) Pi is not blocked c) There is assignment edge directed to Pi. There is no deadlock if the graph can be reduced to zero edges

  18. P0 R0   R1  P1 P2   R2 Detection and RecoveryResources have multiple instances This graph can’t be reduced, so there is a deadlock

  19. Detection and RecoveryVariation of banker’s algorithm (multiple instances ) • Uses three data structures Available and Allocation • And Request 2-dimensional array • Initialize, work=Available and i Finish[i]=false • Find an i such that (( Finish[i]=false) && (Requesti  Work)) • If no such i exists then go to step 4 • Work=work+allocationi • Finish[i]=true • Go to step 2 • If i Finish[i]=true then the system is in a safe state. Else the system is in a unsafe state and undo the changes in the data structures done by the first phase.

  20. Recovery • Abort all deadlocked processes. • Back up each deadlocked process to some previously defined checkpoint and restart, deadlock may re-occur here. • Successively abort deadlocked processes until deadlock no longer exits. Select the victim depending on some minimum cost. • Successively preempt resources until deadlock no longer exits. Select the victim depending on some minimum cost. • Least amount of CPU time comsumed • Least no of output produced • Most estimated time remaining • Least total resource allocated so far • Lowest priority

More Related