# 1 Thursday, June 29, 2006 "If you really think there's a bug you should report a bug. Maybe you're not using it properly. Have you ever considered that?"

## Presentation on theme: "1 Thursday, June 29, 2006 "If you really think there's a bug you should report a bug. Maybe you're not using it properly. Have you ever considered that?""— Presentation transcript:

1 Thursday, June 29, 2006 "If you really think there's a bug you should report a bug. Maybe you're not using it properly. Have you ever considered that?" - Bill Gates, Microsoft

2 Safety Algorithm 1.Let Work and Finish be vectors of length m and n, respectively. Initialize: Work = Available Finish [i] = false for i - 1,3, …, n. 2.Find and i such that both: (a) Finish [i] = false (b) Need i Work If no such i exists, go to step 4. 3.Work = Work + Allocation i Finish[i] = true go to step 2. 4.If Finish [i] == true for all i, then the system is in a safe state.

3 Resource-Request Algorithm for Process P i Request = request vector for process P i. If Request i [j] = k then process P i wants k instances of resource type R j. 1.If Request i Need i go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim. 2.If Request i Available, go to step 3. Otherwise P i must wait, since resources are not available.

4 Resource-Request Algorithm for Process P i 3.Pretend to allocate requested resources to P i by modifying the state as follows: Available = Available - Request i ; Allocation i = Allocation i + Request i ; Need i = Need i – Request i If safe the resources are allocated to P i. If unsafe P i must wait, and the old resource-allocation state is restored

5 Example of Bankers Algorithm §5 processes P 0 through P 4 ; §4 resource types: A (3 instances), B (14 instances), C (12 instances), D(12 instances). §Snapshot at time T 0 : AllocationMaxAvailable A B C D P00 0 1 2 1 5 2 0 P11 0 0 01 7 5 0 P21 3 5 42 3 5 6 P30 6 3 20 6 5 2 P40 0 1 40 6 5 6

6 Example (Cont.) §The content of the matrix. Need is defined to be Max - Allocation. §Is the system in a safe state?

7 Example of Bankers Algorithm §5 processes P 0 through P 4 ; §4 resource types: A (3 instances), B (14 instances), C (12 instances), D(12 instances). §Snapshot at time T 0 : AllocationMaxNeedAvailable A B C D P00 0 1 2 0 0 1 5 2 0 P11 0 0 01 7 5 0 0 7 5 0 P21 3 5 42 3 5 61 0 0 2 P30 6 3 20 6 5 20 0 2 0 P40 0 1 40 6 5 60 6 4 2 Sanity check?

8 §Should a request from Process P1 for (0,4,2,0) be granted immediately? Available = Available – Request 1 ; Allocation 1 = Allocation 1 + Request 1 ; Need 1 = Need 1 – Request 1

9 Request from Process P1 for (0,4,2,0) §5 processes P 0 through P 4 ; §4 resource types: A (3 instances), B (14 instances), C (12 instances), D(12 instances). §Snapshot at time T 0 : AllocationMaxNeedAvailable A B C D P00 0 1 2 0 0 1 1 0 0 P11 4 2 01 7 5 0 0 3 3 0 P21 3 5 42 3 5 61 0 0 2 P30 6 3 20 6 5 20 0 2 0 P40 0 1 40 6 5 60 6 4 2 Sanity check?

10 Deadlock Avoidance Disadvantages: §Prior knowledge of number of resources by processes §Resources may break... §No of processes may change, algorithm must be able to dynamically change its data structures. §Overhead executing detection algorithm

11 Deadlock Detection §Allow system to enter deadlock state §Detection algorithm §Recovery scheme

12 Single Instance of Each Resource Type §Maintain wait-for graph l Nodes are processes. l P i P j if P i is waiting for P j. §Periodically invoke an algorithm that searches for a cycle in the graph.

13 Resource-Allocation Graph and Wait-for Graph

14 Several Instances of a Resource Type §Available: A vector of length m indicates the number of available resources of each type. §Allocation: An n x m matrix defines the number of resources of each type currently allocated to each process. §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.

15 Detection Algorithm 1.Let Work and Finish be vectors of length m and n, respectively Initialize: (a) Work = Available (b)For i = 1,2, …, n, if Allocation i 0, then Finish[i] = false;otherwise, Finish[i] = true. 2.Find an index i such that both: (a)Finish[i] == false (b)Request i Work If no such i exists, go to step 4.

16 Detection Algorithm (Cont.) 3.Work = Work + Allocation i Finish[i] = true go to step 2. 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.

17 Example of Detection Algorithm §Three processes P 1 through P 3 ; Four resource types A (4 instances), B (2 instances), and C (3 instances), D (1 instance). §Snapshot at time T 0 : AllocationRequestAvailable A B C D 2 1 0 0 P10 0 1 02 0 0 1 P22 0 0 11 0 P30 1 2 02 1 0 0

18 Example (Cont.) §P 2 requests an additional instance of type D. State of system? AllocationRequestAvailable A B C D 2 1 0 0 P10 0 1 02 0 0 1 P22 0 0 11 0 1 1 P30 1 2 02 1 0 0

19 Example (Cont.) l Can reclaim resources held by process P 3, but insufficient resources to fulfill other processes; requests. l Deadlock exists, consisting of processes P 1 and P 2.

20 Detection-Algorithm Usage §When, and how often, to invoke depends on: l How often a deadlock is likely to occur? l How many processes will be affected by deadlock when it happens. §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.

21 Recovery from Deadlock: Process Termination §Abort all deadlocked processes. §Abort one process at a time until the deadlock cycle is eliminated. §In which order should we choose to abort? l Priority of the process. l How long process has computed, and how much longer to completion. l Resources the process has used. l Resources process needs to complete. l How many processes will need to be terminated. l Is process interactive or batch?

22 Recovery from Deadlock: Resource Preemption §Selecting a victim – minimize cost. §Rollback – return to some safe state, restart process for that state. §Starvation.