Presentation is loading. Please wait.

Presentation is loading. Please wait.

Deadlock Permanent blocking of a set of process that compete for system resources or communicate with each other. Conditions that characterizes deadlock.

Similar presentations


Presentation on theme: "Deadlock Permanent blocking of a set of process that compete for system resources or communicate with each other. Conditions that characterizes deadlock."— 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 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 P1 P2 P3 P5 P4 R2 R5 R1 R4 R3

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 Deadlock Prevention Allowing 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. P0 A C P1 B 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

7 Deadlock Prevention Allowing 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. P1(ready) B C P0 A P3(waiting) B C P2 executes by Preempting resource B from P3. P2 A P0 goes to wait state

8 Deadlock Prevention Not 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)

9 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. P1 P2 R1 R2 P1 P2 R1 R2 P1 P2 R1 R2

10 Deadlock Avoidance Banker’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]

11 Deadlock Avoidance Banker’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.

12 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

13 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.

14 Example Process Allocation Max Available A B C P0 P1 P2 P3 P4 Need A B C Finish F P0 T P1 T P2 T P3 T P4 T Work =

15 Example If the current state of the system is as given below, can the request by P0 for (0,0,2) be granted ? Process Allocation Max Available A B C P0 P1 P2 P3 P4 Need A B C Resource Request Algorithm Change Available 0 1 2 Change Need0 Change Allocation0 3 3 0 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.

16 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.

17 Detection and Recovery Resources 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. P1 P2 P3 P5 P4 R2 R5 R1 R4 R3 P1 P2 P3 P5 P4

18 Detection and Recovery Resources 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. P0 P1 P2 R0   R2 R1 P0 P1 P2 R0   R2 R1 P0 P1 P2 R0   R2 R1 P0 P1 P2 R0   R2 R1 There is no deadlock if the graph can be reduced to zero edges

19 Detection and Recovery Resources have multiple instances
  R2 R1 This graph can’t be reduced, so there is a deadlock

20 Detection and Recovery Variation 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.

21 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


Download ppt "Deadlock Permanent blocking of a set of process that compete for system resources or communicate with each other. Conditions that characterizes deadlock."

Similar presentations


Ads by Google