MCS 720 Fall 1992 Operating Systems I page 1 DEADLOCK EXAMPLE Consider the following state of a computing system. resources resources process assigned still needed ------- ---------- ------- p1 3 0 1 1 1 1 0 0 p2 0 1 0 0 0 1 1 2 p3 1 1 1 0 3 1 0 0 p4 1 1 0 1 0 0 0 0 p5 0 0 0 0 2 1 1 0 available possessed existing -------- --------- -------- 1 0 2 0 5 3 2 2 6 3 4 2 The current state is safe because the processes could finish in the ord- er p4 (which needs no more resources to finish), then p1, p2, p3, and p5 (for example). available -------- 1 0 2 0 + 1 1 0 1 p4 = 2 1 2 1 + 3 0 1 1 p1 = 5 1 3 2 + 0 1 0 0 p2 = 5 2 3 2 + 1 1 1 0 p3 = 6 3 4 2 + 0 0 0 0 p5 = 6 3 4 2 = existing If a request from p5 arrives for (1,0,1,0), can the whole request be safely granted immediately? Yes, the resulting state is still safe because the processes could still finish in the same order. p5 1 0 1 0 1 1 0 0 available -------- 1 0 2 0 - 1 0 1 0 p5 = 0 0 1 0 + 1 1 0 1 p4 = 1 1 1 1 + 3 0 1 1 p1 = 4 1 2 2 + 0 1 0 0 p2 = 4 2 2 2 + 1 1 1 0 p3 = 5 3 3 2 + 1 0 1 0 p5 = 6 3 4 2 = existing Deadlock Example Handout 3 November 10, 1992 MCS 720 Fall 1992 Operating Systems I page 2 Consider the following snapshot of a system. current maximum still process allocation demand needs ------- ---------- ------- ----- p1 0 0 1 2 0 0 1 2 0 0 0 0 p2 1 0 0 0 1 7 5 0 0 7 5 0 p3 1 3 5 4 2 3 5 6 1 0 0 2 p4 0 3 3 2 0 6 5 2 0 3 2 0 p5 0 0 1 4 3 6 5 6 3 6 4 2 available possessed existing -------- --------- -------- 1 1 2 0 2 6 10 12 3 7 12 12 Is this system currently deadlocked? Why? In a safe state? Why? In an unsafe state? Why? If a request from p5 arrives for (0,1,2,0), can that whole request be safely granted immediately? In what state (deadlocked, safe, unsafe) would immediately granting that whole request leave the system? Which processes, if any, are or may become deadlocked if this whole request is granted immediately? The current state is safe because the processes can finish in the order: p1, p3, p4, p2, p5. The successive "available" vector values, after each process finishes in this order, are: (1,1,3,2), (2,4,8,6), (2,7,11,8), (3,7,11,8), (3,7,12,12). So no processes are deadlocked and the state is not unsafe. If we were to give (0,1,2,0) to p5, then the "available" vector would become (1,0,0,0), and we could finish off p1, p3, p4, at which point the "available" vector would be (2,6,9,8). But p2 still needs (0,7,5,0), while holding (1,0,0,0); and p5 still needs (3,5,2,2), while holding (0,1,3,4). So deadlock is possible, if the processes make their maximum demands, with p2 holding a resource of the first type that p5 needs to complete, and p5 holding a resource of the second type that p2 needs to complete. Therefore the resulting state is unsafe, and we must force p5 to wait. Deadlock Example Handout 3 November 10, 1992