deadlock example
play

Deadlock Example Process 1 Process 2 Resource 1 Resource 2 - PowerPoint PPT Presentation

Deadlock Example Process 1 Process 2 Resource 1 Resource 2 Example Process 1 Process 2 Process 3 Resource 1 Resource 2 Resource 3 Addressing Deadlock Prevention: Design the system so that deadlock is impossible Avoidance:


  1. Deadlock

  2. Example Process 1 Process 2 Resource 1 Resource 2

  3. Example Process 1 Process 2 Process 3 Resource 1 Resource 2 Resource 3

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

  5. A Model ¥ P = {p 1 , p 2 , É, p n } be a set of processes ¥ R = {R 1 , R 2 , É, R m } be a set of resources ¥ c j = number of units of R j in the system ¥ S = {S 0 , S 1 , É} be a set of states representing the assignment of R j to p i Ð State changes when processes take action Ð This allows us to identify a deadlock situation in the operating system

  6. State Transitions ¥ The system changes state because of the action of some process, p i ¥ There are three pertinent actions: Ð Request (Òr i Ó): request one or more units of a resource Ð Allocation (Òa i Ó): All outstanding requests from a process for a given resource are satisfied Ð Deallocation (Òd i Ó): The process releases units of a resource x i S j S k

  7. Properties of States ¥ Want to define deadlock in terms of patterns of transitions ¥ Define: p i is blocked in S j if p i cannot cause a transition out of S j

  8. Properties of States ¥ Want to define deadlock in terms of patterns of transitions ¥ Define: p i is blocked in S j if p i cannot cause a transition out of S j a 1 r 3 S j r 1 p 2 is blocked in S j

  9. Properties of States (cont) ¥ If p i is blocked in S j , and will also be blocked in every S k reachable from S j , then p i is deadlocked ¥ S j is called a deadlock state

  10. Example ¥ One process, two units of one resource ¥ Can request one unit at a time d d r a r a S 0 S 1 S 2 S 3 S 4

  11. Extension of Example d 0 d 0 r 0 a 0 r 0 a 0 S 00 S 10 S 20 S 30 S 40 r 1 r 1 r 1 r 1 r 1 d 0 d 0 r 0 a 0 r 0 a 0 S 01 S 11 S 21 S 31 S 41 d 1 d 1 d 1 d 1 a 1 a 1 a 1 a 1 d 0 r 0 a 0 r 0 S 02 S 12 S 22 S 32 r 1 r 1 d 0 r 1 r 1 r 0 a 0 r 0 S 03 S 13 S 23 S 33 d 1 d 1 a 1 a 1 r 0 S 04 S 14

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

  13. 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 ¥ Approach 2: If a process needs to acquire a new resource, it must first release all resources it holds, then reacquire all it needs ¥ What does this say about state transition diagrams?

  14. Circular Wait ¥ Have a situation in which there are K processes holding units of K resources R P R i P holds R P i P R P requests R

  15. 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 R j if R i < R j for all R i the process is currently holding

  16. 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 R j if R i < R j for all R i the process is currently holding ¥ Here is how we saw the easy solution for the dining philosophers

  17. Allowing Preemption ¥ Allow a process to time-out on a blocked request -- withdrawing the request if it fails r u S i S j w u d v r u S k

  18. Avoidance ¥ Construct a model of system states, then choose a strategy that will guarantees that the system will not go to a deadlock state ¥ Requires extra information -- 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

  19. Safe vs Unsafe States ¥ Safe state : one in which there is guaranteed to be a sequence of transitions that 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 there is such a sequence Ð Unsafe state can lead to a deadlock state if too many processes exercise their maximum claim at once

  20. More on Safe & Unsafe States Normal Execution No Request Max Claim Yes Execute, then release

  21. More on Safe & Unsafe States Likely to be in a safe state Normal Execution No Request Max Claim Yes Execute, then release Probability of being in unsafe state increases

  22. More on Safe & Unsafe States Normal Execution No Request Max Claim Yes Execute, then ¥Suppose all processes take ÒyesÓ branch release ¥Avoidance strategy is to allow this to happen, yet still be safe

  23. More on Safe & Unsafe States Safe States Unsafe States Deadlock States

  24. BankerÕs Algorithm ¥ Let maxc[i, j] be the maximum claim for R j by p i ¥ Let alloc[i, j] be the number of units of R j held by p i ¥ Can always compute Ð avail[j] = c j - Σ 0 ≤ i < n alloc[i,j] Ð Then number of available units of R j ¥ Should be able to determine if the state is safe or not using this info

  25. BankerÕs Algorithm ¥ Copy the alloc[i,j] table to allocÕ[i,j] ¥ Given C, maxc and allocÕ, compute avail vector ¥ Find p i : maxc[i,j] - allocÕ[i,j] ≤ avail[j] for 0 ≤ j < m and 0 ≤ i < n. Ð If no such p i 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 p i ; go to Step 2

  26. Example Maximum Claim C = <8, 5, 9, 7> Process R 0 R 1 R 2 R 3 p 0 3 2 1 4 p 1 0 2 5 2 p 2 5 1 0 5 p 3 1 5 3 0 p 4 3 0 3 3 Allocated Resources Process R 0 R 1 R 2 R 3 p 0 2 0 1 1 p 1 0 1 2 1 p 2 4 0 0 3 p 3 0 2 1 0 p 4 1 0 3 0

  27. Example Maximum Claim C = <8, 5, 9, 7> Process R 0 R 1 R 2 R 3 ¥Compute total allocated p 0 3 2 1 4 p 1 0 2 5 2 p 2 5 1 0 5 p 3 1 5 3 0 p 4 3 0 3 3 Allocated Resources Process R 0 R 1 R 2 R 3 p 0 2 0 1 1 p 1 0 1 2 1 p 2 4 0 0 3 p 3 0 2 1 0 p 4 1 0 3 0 Sum 7 3 7 5

  28. Example Maximum Claim C = <8, 5, 9, 7> Process R 0 R 1 R 2 R 3 ¥Compute total allocated p 0 3 2 1 4 ¥Determine available units p 1 0 2 5 2 p 2 5 1 0 5 avail = <8-7, 5-3, 9-7, 7-5> p 3 1 5 3 0 = <1, 2, 2, 2> p 4 3 0 3 3 Allocated Resources Process R 0 R 1 R 2 R 3 p 0 2 0 1 1 p 1 0 1 2 1 p 2 4 0 0 3 p 3 0 2 1 0 p 4 1 0 3 0 Sum 7 3 7 5

  29. Example Maximum Claim C = <8, 5, 9, 7> Process R 0 R 1 R 2 R 3 ¥Compute total allocated p 0 3 2 1 4 ¥Determine available units p 1 0 2 5 2 p 2 5 1 0 5 avail = <8-7, 5-3, 9-7, 7-5> p 3 1 5 3 0 = <1, 2, 2, 2> p 4 3 0 3 3 ¥Can anyoneÕs maxc be met? maxc[2,0]-allocÕ[2,0] = 5-4 = 1 ≤ 1 = avail[0] Allocated Resources maxc[2,1]-allocÕ[2,1] = 1-0 = 1 ≤ 2 = avail[1] Process R 0 R 1 R 2 R 3 maxc[2,2]-allocÕ[2,2] = 0-0 = 0 ≤ 2 = avail[2] p 0 2 0 1 1 maxc[2,3]-allocÕ[2,3] = 5-3 = 2 ≤ 2 = avail[3] p 1 0 1 2 1 p 2 4 0 0 3 p 3 0 2 1 0 p 4 1 0 3 0 Sum 7 3 7 5

  30. Example Maximum Claim C = <8, 5, 9, 7> Process R 0 R 1 R 2 R 3 ¥Compute total allocated p 0 3 2 1 4 ¥Determine available units p 1 0 2 5 2 p 2 5 1 0 5 avail = <8-7, 5-3, 9-7, 7-5> p 3 1 5 3 0 = <1, 2, 2, 2> p 4 3 0 3 3 ¥Can anyoneÕs maxc be met? maxc[2,0]-allocÕ[2,0] = 5-4 = 1 ≤ 1 = avail[0] Allocated Resources maxc[2,1]-allocÕ[2,1] = 1-0 = 1 ≤ 2 = avail[1] Process R 0 R 1 R 2 R 3 maxc[2,2]-allocÕ[2,2] = 0-0 = 0 ≤ 2 = avail[2] p 0 2 0 1 1 maxc[2,3]-allocÕ[2,3] = 5-3 = 2 ≤ 2 = avail[3] p 1 0 1 2 1 ¥P 2 can exercise max claim p 2 4 0 0 3 p 3 0 2 1 0 avail[0] = avail[0]+allocÕ[2,0] = 1+4 = 5 p 4 1 0 3 0 avail[1] = avail[1]+allocÕ[2,1] = 2+0 = 2 Sum 7 3 7 5 avail[2] = avail[2]+allocÕ[2,2] = 2+0 = 2 avail[3] = avail[3]+allocÕ[2,3] = 2+3 = 5

  31. Example Maximum Claim C = <8, 5, 9, 7> Process R 0 R 1 R 2 R 3 ¥Compute total allocated p 0 3 2 1 4 ¥Determine available units p 1 0 2 5 2 p 2 5 1 0 5 avail = <8-3, 5-3, 9-7, 7-2> p 3 1 5 3 0 = <5, 2, 2, 5> p 4 3 0 3 3 ¥Can anyoneÕs maxc be met? maxc[4,0]-allocÕ[4,0] = 5-1 = 4 ≤ 5 = avail[0] Allocated Resources maxc[4,1]-allocÕ[4,1] = 0-0 = 0 ≤ 2 = avail[1] Process R 0 R 1 R 2 R 3 maxc[4,2]-allocÕ[4,2] = 3-3 = 0 ≤ 2 = avail[2] p 0 2 0 1 1 maxc[4,3]-allocÕ[4,3] = 3-0 = 3 ≤ 5 = avail[3] p 1 0 1 2 1 p 2 0 0 0 0 p 3 0 2 1 0 p 4 1 0 3 0 Sum 3 3 7 2

Recommend


More recommend