1 / 71

Deadlock

Deadlock. 10. Deadlock. System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection & Recovery. Example. Process 1. Process 2. Resource 1. Resource 2. Process holds the resource. Process requests the resource.

egan
Télécharger la présentation

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. Operating Systems: A Modern Perspective, Chapter 10

  2. Deadlock 10 Operating Systems: A Modern Perspective, Chapter 10

  3. Deadlock • System Model • Deadlock Characterization • Methods for Handling Deadlocks • Deadlock Prevention • Deadlock Avoidance • Deadlock Detection & Recovery Operating Systems: A Modern Perspective, Chapter 10

  4. Example Process 1 Process 2 Resource 1 Resource 2 Process holds the resource Process requests the resource Operating Systems: A Modern Perspective, Chapter 10

  5. Three Deadlocked Processes Process 1 Process 2 Process 3 Resource 1 Resource 2 Resource 3 Process holds the resource Process requests the resource Operating Systems: A Modern Perspective, Chapter 10

  6. A Model • P = {p1, p2, …, pn} be a set of processes • R = {R1, R2, …, Rm} be a set of resources • cj = number of units of Rj in the system • S = {S0, S1, …} be a set of states representing the assignment of Rj to pi • State changes when processes take action • This allows us to identify a deadlock situation in the operating system Operating Systems: A Modern Perspective, Chapter 10

  7. State Transitions • The system changes state because of the action of some process, pi • There are three pertinent actions: • Request (“ri”): request one or more units of a resource • Allocation (“ai”): All outstanding requests from a process for a given resource are satisfied • Deallocation (“di”): The process releases units of a resource xi Sj Sk Operating Systems: A Modern Perspective, Chapter 10

  8. a1 r3 Sj r1 p2 is blocked in Sj Properties of States • Want to define deadlock in terms of patterns of transitions • Define: pi is blocked in Sj if pi cannot cause a transition out of Sj Operating Systems: A Modern Perspective, Chapter 10

  9. Properties of States (cont) • If pi is blocked in Sj, and will also be blocked in every Sk reachable from Sj, then pi is deadlocked • Sj is called a deadlock state Operating Systems: A Modern Perspective, Chapter 10

  10. Example • One process, two units of one resource • Can request one unit at a time d d r a r a S0 S1 S2 S3 S4 Operating Systems: A Modern Perspective, Chapter 10

  11. Extension of Example d0 d0 r0 a0 r0 a0 S00 S10 S20 S30 S40 r1 r1 r1 r1 r1 d0 d0 r0 a0 r0 a0 S01 S11 S21 S31 S41 d1 d1 d1 d1 a1 a1 a1 a1 d0 r0 a0 r0 S02 S12 S22 S32 r1 r1 d0 r1 r1 r0 a0 r0 S03 S13 S23 S33 d1 d1 a1 a1 r0 S04 S14 Operating Systems: A Modern Perspective, Chapter 10

  12. Resource Allocation Graph Operating Systems: A Modern Perspective, Chapter 10

  13. Resource Allocation Graph (cont.) Operating Systems: A Modern Perspective, Chapter 10

  14. A Simple Process-Resource Model Instance P1 P2 P3 R1 R2 R3 Process holds resource Process requests resource Operating Systems: A Modern Perspective, Chapter 10

  15. Example of a graph with no cycles Operating Systems: A Modern Perspective, Chapter 10

  16. Example of a graph with a cycle Operating Systems: A Modern Perspective, Chapter 10

  17. Basic Facts Operating Systems: A Modern Perspective, Chapter 10

  18. Figure 10‑10: A Model of a State with a Circular Wait R P Ri P holds R Pi P R P requests R Operating Systems: A Modern Perspective, Chapter 10

  19. Dining Philosophers Revisited philosopher(int i){ while (TRUE) { // Think // Eat P(fork[i]); /* Pick up left fork */ P(fork[(i+1) mod 5]); /* Pick up right fork */ eat(); V(fork[(i+1) mod 5]); V(fork[i]); } } philosopher4(){ while (TRUE) { // Think // Eat P(fork[0]); /* Pick up right fork */ P(fork[4]); /* Pick up left fork */ eat(); V(fork[4]); V(fork[0]); } } semaphore fork[5]; fork[0] = fork[1] = fork[2] = fork[3] = fork[4] = 1; fork(philosopher, 1, 0); fork(philosopher, 1, 1); fork(philosopher, 1, 2); fork(philosopher, 1, 3); fork(philosopher4, 0); Operating Systems: A Modern Perspective, Chapter 10

  20. Addressing Deadlock • Prevention: Design the system so that deadlock is impossible • Avoidance: Construct a model of system states, then choose a strategy that will not allow the system to go to a deadlock state • Detection & Recovery: Check for deadlock (periodically or sporadically), then recover • Manual intervention: Have the operator reboot the machine if it seems too slow Operating Systems: A Modern Perspective, Chapter 10

  21. Prevention • Necessary conditions for deadlock • Mutual exclusion • Hold and wait • Circular waiting • No preemption • Ensure that at least one of the necessary conditions is false at all times • Mutual exclusion must hold at all times Operating Systems: A Modern Perspective, Chapter 10

  22. Hold and Wait • Need to be sure a process does not hold one resource while requesting another • Approach 1: Force a process to request all resources it needs at one time • Used in batch processing systems Operating Systems: A Modern Perspective, Chapter 10

  23. Hold and Wait • Approach 2: If a process needs to acquire a new resource, it must first release all resources it holds, then reacquire all it needs • Used in interactive systems Operating Systems: A Modern Perspective, Chapter 10

  24. Circular Wait • Have a situation in which there are K processes holding units of K resources R P Ri P holds R Pi P R P requests R Operating Systems: A Modern Perspective, Chapter 10

  25. Circular Wait (cont) • There is a cycle in the graph of processes and resources • Choose a resource request strategy by which no cycle will be introduced • Total order on all resources, then can only ask for Rj if Ri < Rj for all Ri the process is currently holding • This is how we noticed the easy solution for the dining philosophers Operating Systems: A Modern Perspective, Chapter 10

  26. Allowing Preemption • Allow a process to time-out on a blocked request -- withdrawing the request if it fails ru Si Sj wu dv ru Sk Operating Systems: A Modern Perspective, Chapter 10

  27. Avoidance • Define a model of system states, then choose a strategy that will guarantee that the system will not go to a deadlock state • Requires extra information, e.g., the maximum claim for each process • Allows resource manager to see the worst case that could happen, then to allow transitions based on that knowledge Operating Systems: A Modern Perspective, Chapter 10

  28. Safe vs Unsafe States • Safe state: one in which the system can assure that any sequence of subsequent transitions leads back to the initial state • Even if all exercise their maximum claim, there is an allocation strategy by which all claims can be met • Unsafe state: one in which the system cannot guarantee that the system will transition back to the initial state • Unsafe state can lead to a deadlock state if too many processes exercise their maximum claim at once Operating Systems: A Modern Perspective, Chapter 10

  29. Likely to be in a safe state Probability of being in unsafe state increases More on Safe & Unsafe States Normal Execution No Request Max Claim Yes Execute, then release Operating Systems: A Modern Perspective, Chapter 10

  30. More on Safe & Unsafe States I Disallow Safe States Unsafe States Deadlock States Operating Systems: A Modern Perspective, Chapter 10

  31. Basic Facts Operating Systems: A Modern Perspective, Chapter 10

  32. Banker's Algorithm Operating Systems: A Modern Perspective, Chapter 10

  33. Banker’s Algorithm • Let maxc[i, j] be the maximum claim for Rj by pi • Let alloc[i, j] be the number of units of Rj held by pi • Can always compute • avail[j] = cj - S0i< nalloc[i,j] • Then number of available units of Rj • Should be able to determine if the state is safe or not using this info Operating Systems: A Modern Perspective, Chapter 10

  34. Banker’s Algorithm • Copy the alloc[i,j] table to alloc’[i,j] • Given C, maxc and alloc’, compute avail vector • Find pi: maxc[i,j] - alloc’[i,j]  avail[j] for 0  j < m and 0  i < n. • If no such pi exists, the state is unsafe • If alloc’[i,j] is 0 for all i and j, the state is safe • Set alloc’[i,j] to 0; deallocate all resources held by pi; go to Step 2 Operating Systems: A Modern Perspective, Chapter 10

  35. Compute total allocated • Determine available units avail = <8-7, 5-3, 9-7, 7-5> = <1, 2, 2, 2> • Can anyone’s maxc be met? maxc[2,0]-alloc’[2,0] = 5-4 = 11 = avail[0] maxc[2,1]-alloc’[2,1] = 1-0 = 12 = avail[1] maxc[2,2]-alloc’[2,2] = 0-0 = 02 = avail[2] maxc[2,3]-alloc’[2,3] = 5-3 = 22 = avail[3] • P2 can exercise max claim avail[0] = avail[0]+alloc’[2,0] = 1+4 = 5 avail[1] = avail[1]+alloc’[2,1] = 2+0 = 2 avail[2] = avail[2]+alloc’[2,2] = 2+0 = 2 avail[3] = avail[3]+alloc’[2,3] = 2+3 = 5 Example Maximum Claim C = <8, 5, 9, 7> Process R0 R1 R2 R3 p0 3 2 1 4 p1 0 2 5 2 p2 5 1 0 5 p3 1 5 3 0 p4 3 0 3 3 Allocated Resources Process R0 R1 R2 R3 p0 2 0 1 1 p1 0 1 2 1 p2 4 0 0 3 p3 0 2 1 0 p4 1 0 3 0 Sum 7 3 7 5 Operating Systems: A Modern Perspective, Chapter 10

  36. Compute total allocated • Determine available units avail = <8-3, 5-3, 9-7, 7-2> = <5, 2, 2, 5> • Can anyone’s maxc be met? maxc[4,0]-alloc’[4,0] = 5-1 = 45 = avail[0] maxc[4,1]-alloc’[4,1] = 0-0 = 02 = avail[1] maxc[4,2]-alloc’[4,2] = 3-3 = 02 = avail[2] maxc[4,3]-alloc’[4,3] = 3-0 = 35 = avail[3] • P4 can exercise max claim avail[0] = avail[0]+alloc’[4,0] = 5+1 = 6 avail[1] = avail[1]+alloc’[4,1] = 2+0 = 2 avail[2] = avail[2]+alloc’[4,2] = 2+3 = 5 avail[3] = avail[3]+alloc’[4,3] = 5+0 = 5 Example Maximum Claim C = <8, 5, 9, 7> Process R0 R1 R2 R3 p0 3 2 1 4 p1 0 2 5 2 p2 5 1 0 5 p3 1 5 3 0 p4 3 0 3 3 Allocated Resources Process R0 R1 R2 R3 p0 2 0 1 1 p1 0 1 2 1 p2 0 0 0 0 p3 0 2 1 0 p4 1 0 3 0 Sum 3 3 7 2 Operating Systems: A Modern Perspective, Chapter 10

  37. Compute total allocated • Determine available units avail = <8-2, 5-1, 9-4, 7-2> = <6, 4, 5, 5> Example Maximum Claim C = <8, 5, 9, 7> Process R0 R1 R2 R3 p0 3 2 1 4 p1 0 2 5 2 p2 5 1 0 5 p3 1 5 3 0 p4 3 0 3 3 • Can anyone’s maxc be met? (Yes, any of them can) Allocated Resources Process R0 R1 R2 R3 p0 2 0 1 1 p1 0 1 2 1 p2 0 0 0 0 p3 0 2 1 0 p4 0 0 0 0 Sum 2 1 4 2 Operating Systems: A Modern Perspective, Chapter 10

  38. Detection & Recovery • Check for deadlock (periodically or sporadically), then recover • Can be far more aggressive with allocation • No maximum claim, no safe/unsafe states • Differentiate between • Serially reusable resources: A unit must be allocated before being released • Consumable resources: Never release acquired resources; resource count is number currently available Operating Systems: A Modern Perspective, Chapter 10

  39. Reusable Resource Graphs (RRGs) • Micro model to describe a single state • Nodes = {p0, p1, …, pn}  {R1, R2, …, Rm} • Edges connect pi to Rj, or Rj to pi • (pi, Rj) is a request edge for one unit of Rj • (Rj, pi) is an assignment edge of one unit of Rj • For each Rj there is a count, cj of units Rj • Number of units of Rj allocated to pi plus the number requested by pi cannot exceed cj Operating Systems: A Modern Perspective, Chapter 10

  40. State Transitions due to Request • In Sj, pi is allowed to request qch units of Rh, provided pi has no outstanding requests. • Sj Sk, where the RRG for Sk is derived from Sj by adding q request edges from pi to Rh q edges pi Rh pi Rh pi request q units State Sk State Sj of Rh Operating Systems: A Modern Perspective, Chapter 10

  41. State Transition for Acquire • In Sj, pi is allowed to acquire units of Rh, iff there is (pi, Rh) in the graph, and all can be satisfied. • Sj Sk, where the RRG for Sk is derived from Sj by changing each request edge to an assignment edge. pi Rh pi Rh pi acquires units State Sk State Sj of Rh Operating Systems: A Modern Perspective, Chapter 10

  42. State Transition for Release • In Sj, pi is allowed to release units of Rh, iff there is (Rh, pi) in the graph, and there is no request edge from pi. • Sj Sk, where the RRG for Sk is derived from Sj by deleting all assignment edges. pi Rh pi Rh pi releases units State Sk State Sj of Rh Operating Systems: A Modern Perspective, Chapter 10

  43. Single Instance of Each Resource Type • Maintain a resource allocation graph • Periodically invoke algorithm that searches for cycles in the graph • If there is a cycle ==> set of processes in cycle are in deadlock Operating Systems: A Modern Perspective, Chapter 10

  44. Example R p P holds one unit of R P requests one unit of R p R A Deadlock State Operating Systems: A Modern Perspective, Chapter 10

  45. Example Not a Deadlock State No Cycle in the Graph Operating Systems: A Modern Perspective, Chapter 10

  46. Example p0 p1 S00 Operating Systems: A Modern Perspective, Chapter 10

  47. Example p0 p0 p1 p1 S00 S01 Operating Systems: A Modern Perspective, Chapter 10

  48. Example p0 p0 p0 p1 p1 p1 S00 S01 S11 Operating Systems: A Modern Perspective, Chapter 10

  49. Example p0 p0 p0 p0 p1 p1 p1 p1 S00 S01 S11 S21 Operating Systems: A Modern Perspective, Chapter 10

  50. Example p0 p0 p0 p0 p0 p1 p1 p1 p1 p1 S00 S01 S11 S21 S22 Operating Systems: A Modern Perspective, Chapter 10

More Related