Ch8 OS


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Ch8 OS

  1. 1. Chapter 8: Deadlocks <ul><li>System Model </li></ul><ul><li>Deadlock Characterization </li></ul><ul><li>Methods for Handling Deadlocks </li></ul><ul><li>Deadlock Prevention </li></ul><ul><li>Deadlock Avoidance </li></ul><ul><li>Deadlock Detection </li></ul><ul><li>Recovery from Deadlock </li></ul><ul><li>Combined Approach to Deadlock Handling </li></ul>Operating System Concepts
  2. 2. The Deadlock Problem <ul><li>A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set. </li></ul><ul><li>Example </li></ul><ul><ul><li>System has 2 tape drives. </li></ul></ul><ul><ul><li>P 1 and P 2 each hold one tape drive and each needs another one. </li></ul></ul><ul><li>Example </li></ul><ul><ul><li>semaphores A and B , initialized to 1 </li></ul></ul><ul><ul><ul><ul><ul><li>P 0 P 1 </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>wait (A); wait(B) </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>wait (B); wait(A) </li></ul></ul></ul></ul></ul>Operating System Concepts
  3. 3. System Model <ul><li>Resource types R 1 , R 2 , . . ., R m </li></ul><ul><ul><ul><li>CPU cycles, memory space, I/O devices </li></ul></ul></ul><ul><li>Each resource type R i has W i instances. </li></ul><ul><li>Each process utilizes a resource as follows: </li></ul><ul><ul><li>request </li></ul></ul><ul><ul><li>use </li></ul></ul><ul><ul><li>release </li></ul></ul>Operating System Concepts
  4. 4. Deadlock Characterization <ul><li>Mutual exclusion: only one process at a time can use a resource. </li></ul><ul><li>Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes. </li></ul><ul><li>No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task. </li></ul><ul><li>Circular wait: there exists a set { P 0 , P 1 , …, P 0 } of waiting processes such that P 0 is waiting for a resource that is held by P 1 , P 1 is waiting for a resource that is held by </li></ul><ul><li>P 2 , …, P n –1 is waiting for a resource that is held by P n , and P 0 is waiting for a resource that is held by P 0 . </li></ul>Operating System Concepts Deadlock can arise if four conditions hold simultaneously.
  5. 5. Resource-Allocation Graph <ul><li>V is partitioned into two types: </li></ul><ul><ul><li>P = { P 1 , P 2 , …, P n }, the set consisting of all the processes in the system. </li></ul></ul><ul><ul><li>R = { R 1 , R 2 , …, R m }, the set consisting of all resource types in the system. </li></ul></ul><ul><li>request edge – directed edge P i  R j </li></ul><ul><li>assignment edge – directed edge R j  P i </li></ul>Operating System Concepts A set of vertices V and a set of edges E .
  6. 6. Resource-Allocation Graph (Cont.) <ul><li>Process </li></ul><ul><li>Resource Type with 4 instances </li></ul><ul><li>P i requests instance of R j </li></ul><ul><li>P i is holding an instance of R j </li></ul>Operating System Concepts P i P i R j R j
  7. 7. Example of a Resource Allocation Graph Operating System Concepts
  8. 8. Resource Allocation Graph With A Deadlock Operating System Concepts
  9. 9. Resource Allocation Graph With A Cycle But No Deadlock Operating System Concepts
  10. 10. Basic Facts <ul><li>If graph contains no cycles  no deadlock. </li></ul><ul><li>If graph contains a cycle  </li></ul><ul><ul><li>if only one instance per resource type, then deadlock. </li></ul></ul><ul><ul><li>if several instances per resource type, possibility of deadlock. </li></ul></ul>Operating System Concepts
  11. 11. Methods for Handling Deadlocks <ul><li>Ensure that the system will never enter a deadlock state(by using a protocol to prevent or avoid a deadlock). </li></ul><ul><li>Allow the system to enter a deadlock state and then recover. </li></ul><ul><li>Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX. </li></ul>Operating System Concepts
  12. 12. Deadlock Prevention <ul><li>Mutual Exclusion – not required for sharable resources; must hold for nonsharable resources. </li></ul><ul><li>Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources. </li></ul><ul><ul><li>Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none. </li></ul></ul><ul><ul><li>Low resource utilization; starvation possible. </li></ul></ul>Operating System Concepts Restrain the ways request can be made.
  13. 13. Deadlock Prevention (Cont.) <ul><li>No Preemption – </li></ul><ul><ul><li>If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. </li></ul></ul><ul><ul><li>Preempted resources are added to the list of resources for which the process is waiting. </li></ul></ul><ul><ul><li>Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting. </li></ul></ul><ul><li>Circular Wait – impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration. </li></ul>Operating System Concepts
  14. 14. Deadlock Avoidance <ul><li>Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need. </li></ul><ul><li>The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition. </li></ul><ul><li>Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes. </li></ul>Operating System Concepts Requires that the system has some additional a priori information available.
  15. 15. Safe State <ul><li>When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state . </li></ul><ul><li>System is in safe state if there exists a safe sequence of all processes. </li></ul><ul><li>Sequence < P 1 , P 2 , …, P n > is safe if for each P i , the resources that Pi can still request can be satisfied by currently available resources + resources held by all the P j , with j<i . </li></ul><ul><ul><li>If P i resource needs are not immediately available, then P i can wait until all P j have finished. </li></ul></ul><ul><ul><li>When P j is finished, P i can obtain needed resources, execute, return allocated resources, and terminate. </li></ul></ul><ul><ul><li>When P i terminates, P i +1 can obtain its needed resources, and so on. </li></ul></ul>Operating System Concepts
  16. 16. Basic Facts <ul><li>If a system is in safe state  no deadlocks. </li></ul><ul><li>If a system is in unsafe state  possibility of deadlock. </li></ul><ul><li>Avoidance  ensure that a system will never enter an unsafe state. </li></ul>Operating System Concepts
  17. 17. Safe, Unsafe , Deadlock State Operating System Concepts
  18. 18. Resource-Allocation Graph Algorithm <ul><li>Claim edge P i  R j indicated that process P j may request resource R j ; represented by a dashed line. </li></ul><ul><li>Claim edge converts to request edge when a process requests a resource. </li></ul><ul><li>When a resource is released by a process, assignment edge reconverts to a claim edge. </li></ul><ul><li>Resources must be claimed a priori in the system. </li></ul>Operating System Concepts
  19. 19. Resource-Allocation Graph For Deadlock Avoidance Operating System Concepts
  20. 20. Unsafe State In Resource-Allocation Graph Operating System Concepts
  21. 21. Banker’s Algorithm <ul><li>Multiple instances. </li></ul><ul><li>Each process must a priori claim maximum use. </li></ul><ul><li>When a process requests a resource it may have to wait. </li></ul><ul><li>When a process gets all its resources it must return them in a finite amount of time. </li></ul>Operating System Concepts
  22. 22. Data Structures for the Banker’s Algorithm <ul><li>Available: Vector of length m . If available [ j ] = k , there are k instances of resource type R j available. </li></ul><ul><li>Max: n x m matrix. If Max [ i,j ] = k , then process P i may request at most k instances of resource type R j . </li></ul><ul><li>Allocation: n x m matrix. If Allocation[ i,j ] = k then P i is currently allocated k instances of R j. </li></ul><ul><li>Need: n x m matrix. If Need [ i,j ] = k , then P i may need k more instances of R j to complete its task. </li></ul><ul><ul><ul><li>Need [ i,j] = Max [ i,j ] – Allocation [ i,j ]. </li></ul></ul></ul>Operating System Concepts Let n = number of processes, and m = number of resources types.
  23. 23. Safety Algorithm <ul><li>1. Let Work and Finish be vectors of length m and n , respectively. Initialize: </li></ul><ul><ul><ul><ul><li>Work = Available </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Finish [ i ] = false for i=1,2 ,3, …, n. </li></ul></ul></ul></ul><ul><li>2. Find an i such that both: </li></ul><ul><ul><li>(a) Finish [ i ] = false </li></ul></ul><ul><ul><li>(b) Need i  Work </li></ul></ul><ul><ul><li>If no such i exists, go to step 4. </li></ul></ul><ul><li>3. Work = Work + Allocation i Finish [i] = true go to step 2. </li></ul><ul><li>4. If Finish [ i ] == true for all i , then the system is in a safe state. </li></ul>Operating System Concepts
  24. 24. Resource-Request Algorithm for Process P i <ul><li>Request i = request vector for process P i . If Request i [ j ] = k then process P i wants k instances of resource type R j . </li></ul><ul><ul><li>1. If Request i  Need i go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim. </li></ul></ul><ul><ul><li>2. If Request i  Available , go to step 3. Otherwise P i must wait, since resources are not available. </li></ul></ul><ul><ul><li>3. Pretend to allocate requested resources to P i by modifying the state as follows: </li></ul></ul><ul><ul><ul><ul><li>Available = Available - Request i ; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Allocation i = Allocation i + Request i ; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Need i = Need i – Request i;; </li></ul></ul></ul></ul><ul><ul><ul><li>If safe  the resources are allocated to P i . </li></ul></ul></ul><ul><ul><ul><li>If unsafe  P i must wait, and the old resource-allocation state is restored </li></ul></ul></ul>Operating System Concepts
  25. 25. Example of Banker’s Algorithm <ul><li>5 processes P 0 through P 4 ; 3 resource types A (10 instances), B (5instances, and C (7 instances). </li></ul><ul><li>Snapshot at time T 0 : </li></ul><ul><li>Allocation Max Available </li></ul><ul><li>A B C A B C A B C </li></ul><ul><li>P 0 0 1 0 7 5 3 3 3 2 </li></ul><ul><li> P 1 2 0 0 3 2 2 </li></ul><ul><li> P 2 3 0 2 9 0 2 </li></ul><ul><li> P 3 2 1 1 2 2 2 </li></ul><ul><li> P 4 0 0 2 4 3 3 </li></ul>Operating System Concepts
  26. 26. Example (Cont.) <ul><li>The content of the matrix. Need is defined to be Max – Allocation. </li></ul><ul><li>Need </li></ul><ul><li>A B C </li></ul><ul><li> P 0 7 4 3 </li></ul><ul><li> P 1 1 2 2 </li></ul><ul><li> P 2 6 0 0 </li></ul><ul><li> P 3 0 1 1 </li></ul><ul><li> P 4 4 3 1 </li></ul><ul><li>The system is in a safe state since the sequence < P 1 , P 3 , P 4 , P 2 , P 0 > satisfies safety criteria. </li></ul>Operating System Concepts
  27. 27. Example P 1 Request (1,0,2) (Cont.) <ul><li>Check that Request  Available (that is, (1,0,2)  (3,3,2)  true. </li></ul><ul><li>Allocation Need Available </li></ul><ul><li>A B C A B C A B C </li></ul><ul><li>P 0 0 1 0 7 4 3 2 3 0 </li></ul><ul><li>P 1 3 0 2 0 2 0 </li></ul><ul><li>P 2 3 0 1 6 0 0 </li></ul><ul><li>P 3 2 1 1 0 1 1 </li></ul><ul><li>P 4 0 0 2 4 3 1 </li></ul><ul><li>Executing safety algorithm shows that sequence < P 1 , P 3 , P 4 , P 0 , P 2 > satisfies safety requirement. </li></ul><ul><li>Can request for (3,3,0) by P 4 be granted? </li></ul><ul><li>Can request for (0,2,0) by P 0 be granted? </li></ul>Operating System Concepts
  28. 28. Deadlock Detection <ul><li>Allow system to enter deadlock state </li></ul><ul><li>Detection algorithm </li></ul><ul><li>Recovery scheme </li></ul>Operating System Concepts
  29. 29. Single Instance of Each Resource Type <ul><li>Maintain wait-for graph </li></ul><ul><ul><li>Nodes are processes. </li></ul></ul><ul><ul><li>P i  P j if P i is waiting for P j . </li></ul></ul><ul><li>Periodically invoke an algorithm that searches for a cycle in the graph. </li></ul><ul><li>An algorithm to detect a cycle in a graph requires an order of n 2 operations, where n is the number of vertices in the graph. </li></ul>Operating System Concepts
  30. 30. Resource-Allocation Graph and Wait-for Graph Operating System Concepts Resource-Allocation Graph Corresponding wait-for graph
  31. 31. Several Instances of a Resource Type <ul><li>Available: A vector of length m indicates the number of available resources of each type. </li></ul><ul><li>Allocation: An n x m matrix defines the number of resources of each type currently allocated to each process. </li></ul><ul><li>Request: An n x m matrix indicates the current request of each process. If Request [ i j ] = k , then process P i is requesting k more instances of resource type. R j . </li></ul>Operating System Concepts
  32. 32. Detection Algorithm <ul><li>1. Let Work and Finish be vectors of length m and n , respectively Initialize: </li></ul><ul><ul><li>(a) Work = Available </li></ul></ul><ul><ul><li>(b) For i = 1,2, …, n , if Allocation i  0, then Finish [i] = false;otherwise, Finish [i] = true . </li></ul></ul><ul><li>2. Find an index i such that both: </li></ul><ul><ul><li>(a) Finish [ i ] == false </li></ul></ul><ul><ul><li>(b) Request i  Work </li></ul></ul><ul><ul><li>If no such i exists, go to step 4. </li></ul></ul>Operating System Concepts
  33. 33. Detection Algorithm (Cont.) <ul><li>3. Work = Work + Allocation i Finish [ i ] = true go to step 2. </li></ul><ul><li>4. If Finish [ i ] == false, for some i , 1  i  n , then the system is in deadlock state. Moreover, if Finish [ i ] == false , then P i is deadlocked. </li></ul>Operating System Concepts
  34. 34. Example of Detection Algorithm <ul><li>Five processes P 0 through P 4 ; three resource types A (7 instances), B (2 instances), and C (6 instances). </li></ul><ul><li>Snapshot at time T 0 : </li></ul><ul><li>Allocation Request Available </li></ul><ul><li>A B C A B C A B C </li></ul><ul><li>P 0 0 1 0 0 0 0 0 0 0 </li></ul><ul><li>P 1 2 0 0 2 0 2 </li></ul><ul><li>P 2 3 0 3 0 0 0 </li></ul><ul><li>P 3 2 1 1 1 0 0 </li></ul><ul><li>P 4 0 0 2 0 0 2 </li></ul><ul><li>Sequence < P 0 , P 2 , P 3 , P 1 , P 4 > will result in Finish [ i ] = true for all i . </li></ul>Operating System Concepts
  35. 35. Example (Cont.) <ul><li>P 2 requests an additional instance of type C . </li></ul><ul><li>Request </li></ul><ul><li>A B C </li></ul><ul><li> P 0 0 0 0 </li></ul><ul><li> P 1 2 0 1 </li></ul><ul><li>P 2 0 0 1 </li></ul><ul><li>P 3 1 0 0 </li></ul><ul><li>P 4 0 0 2 </li></ul><ul><li>State of system? </li></ul><ul><ul><li>Can reclaim resources held by process P 0 , but insufficient resources to fulfill other processes; requests. </li></ul></ul><ul><ul><li>Deadlock exists, consisting of processes P 1 , P 2 , P 3 , and P 4 . </li></ul></ul>Operating System Concepts
  36. 36. Detection-Algorithm Usage <ul><li>When, and how often, to invoke depends on: </li></ul><ul><ul><li>How often a deadlock is likely to occur? </li></ul></ul><ul><ul><li>How many processes will need to be rolled back? </li></ul></ul><ul><ul><ul><li>one for each disjoint cycle </li></ul></ul></ul><ul><li>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. </li></ul>Operating System Concepts
  37. 37. Recovery from Deadlock: Process Termination <ul><li>Abort all deadlocked processes. </li></ul><ul><li>Abort one process at a time until the deadlock cycle is eliminated. </li></ul><ul><li>In which order should we choose to abort? </li></ul><ul><ul><li>Priority of the process. </li></ul></ul><ul><ul><li>How long process has computed, and how much longer to completion. </li></ul></ul><ul><ul><li>Resources the process has used. </li></ul></ul><ul><ul><li>Resources process needs to complete. </li></ul></ul><ul><ul><li>How many processes will need to be terminated. </li></ul></ul><ul><ul><li>Is process interactive or batch? </li></ul></ul>Operating System Concepts
  38. 38. Recovery from Deadlock: Resource Preemption <ul><li>Selecting a victim – minimize cost. </li></ul><ul><li>Rollback – return to some safe state, restart process for that state. </li></ul><ul><li>Starvation – same process may always be picked as victim, include number of rollback in cost factor. </li></ul>Operating System Concepts
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.