Presentation is loading. Please wait.

Presentation is loading. Please wait.

Resource Allocation, Deadlock and Banker’s Algorithm 60-330 Supplementary Notes Dr. R. D. Kent Last modified: Dec. 11, 2006.

Similar presentations


Presentation on theme: "Resource Allocation, Deadlock and Banker’s Algorithm 60-330 Supplementary Notes Dr. R. D. Kent Last modified: Dec. 11, 2006."— Presentation transcript:

1 Resource Allocation, Deadlock and Banker’s Algorithm 60-330 Supplementary Notes Dr. R. D. Kent Last modified: Dec. 11, 2006

2 Introduction These slides are intended to illustrate and clarify some details of deadlock detection and avoidance applied to resource allocation.  In particular, they clarify examples in the textbook by providing significant detail Assume that all resource types and the number of instances of each type are known by the O/S  Initially configured at system startup/bootstrap  Tracked along with processes as they Request resources for the first time Use resources dynamically

3 Assumed Knowledge (1) Configured at startup and updated regularly  All resource types and the number of instances of each type Stored in a vector called Available with M entries (one for each resource type). Available[ j ] = k ; /* implies there are k instances of resource type j */ At a given, specific time  The maximum resource demand of each of N processes Stored in a NxM matrix Max Max[ i ][ j ] = k ; /* process P i may request at most k instances of resource type R j

4 Assumed Knowledge (2) At a given, specific time:  The number of resources of each type currently allocated to each of N processes Stored in a NxM matrix Allocation Allocation[ i ][ j ] = k ; /* process P i is currently allocated k instances of resource type R j  The remaining resource needs of each process Stored in a NxM matrix Need Need[ i ][ j ] = k ; /* process P i may need k instances of resource type R j to complete its task NOTE: Need = Max – Allocation ;

5 Assumed Knowledge (3) Comments:  The contents of these data structures varies over time as the processes progress to completion through the states  Sometimes it is convenient to refer to an entire row of an NxM matrix X as X i, where X i is a vector of length M that is associated with process P i.  Notation: Assume X, Y are vectors of length N X ≤ Y if and only if X[j] ≤ Y[j] for all j = 1.. N Ex. X = (1,7,3,2) and Y = (0,3,2,1) implies Y ≤ X

6 Assumed Knowledge (4) Comments: (continued)  The required vector and matrix data structures may be declared (statically) at compile time Need to consider the maximum number of processes (large) and resources (small) that the hardware and operating system can manage.  Ex. int Available [ MAX_Process ] ; int Max [MAX_Process ] [ MAX_Resource ] ;  If structures are allocated dynamically, code must be developed to manage the heap

7 Banker’s Algorithm Used to determine safe resource allocation to processes  Safety implies no deadlocks will occur By studying different scenarios of possible resource allocation, we try to find at least one scenario that is deadlock free, or safe Time consuming (algorithm complexity)  Resource allocation is performed using safe scenarios Can a request be safely granted?

8 Banker’s Algorithm At a given time assume that current values are stored in structures  Define: NP :: Number of current processes NR :: Number of currently available resources  Data Structures: Available – vector with NR entries Max, Allocation, Need – matrices with NP rows and NR entries in each row

9 Safety Algorithm Periodically, we need to determine if the system in a safe state.  Define vectors int Work [ NR ] ; int Finish [ NP ] ;  Initialize Work = Available ; For all k, Finish[ k ] = false ; /* 0 */ Safe State: For a set of processes there exists at least one schedule of allocating resources so that each process can progress to completion.

10 Safety Algorithm Goal: If all values Finish [ k ] == true (or 1), then the system is in a safe state. Starting at k = 0 (ie. first process).  First step: If both Finish[ k ] == false and Need k ≤ Work Then goto Third step, otherwise continue.  Second step: Work = Work + Allocation k ; Finish[ k ] = true ; /* or 1 */ k = k + 1 ; if k < NR then goto First step, otherwise report “No safe state exists”.

11 Safety Algorithm Third step: If, for all k from 0 to NR-1, Finish[ k ] == true Then system is in a safe state. Note that it is possible that no safe state exists (see Second step).  Therefore there is possibility of deadlock.

12 Resource-Request Algorithm We need to determine if a resource request can be safely granted. Define an NxM matrix Request that contains all current resource requests (from M resource types) for all N processes.  Request k is the resource request vector (a row in the matrix) for the process P k.  Resource request information (integer values) is obtained when jobs are submitted (through job headers).

13 Resource-Request Algorithm When P k requests resources:  Step 1: If Request k ≤ Need k, goto Step 2, otherwise report “Processes exceed max. claim”  Step 2: If Request k ≤ Available, goto Step 3, otherwise make P k wait since resources are not available

14 Resource-Request Algorithm  Step 3: (Prove the scenario is safe) Available = Available - Request k ; Allocation k = Allocation k + Request k ; Need k = Need k – Request k ;  If the resulting resource-allocation scenario is safe, the allocate the resources to P k to complete the transaction. Requires applying the Safety Algorithm  If the state is not safe, then P k must wait for Request k, and the old state is restored.

15 Example: Safe State Allocation[0][1][2]Max[0][1][2] P0010753 P1200322 P2302902 P3211222 P4002433 Available[0][1][2] 332 Various processes have requested the maximum amount of resources they intend to use (Max). They are allocated resources so that, at a given moment, one knows the Allocation matrix and also what is currently Available. Is the system in a safe state? What is a safe sequence? Following the textbook, we assume that the maximum resources available are: (10, 5, 7)

16 Example Allocation[0][1][2]Max[0][1][2] P0010753 P1200322 P2302902 P3211222 P4002433 Available[0][1][2] 332 Need[0][1][2] P0743 P1122 P2600 P3011 P4431 Determine current Need = Max – Allocation

17 Example Allocation[0][1][2]Max[0][1][2] P0010753 P1200322 P2302902 P3211222 P4002433 Available[0][1][2] 332 Need[0][1][2] P0743 P1122 P2600 P3011 P4431 Determine current Need = Max – Allocation Max – Allocation → Need :: 3 – 2 = 1 2 – 0 = 2 2 – 0 = 2

18 Example Allocation[0][1][2]Max[0][1][2] P0010753 P1200322 P2302902 P3211222 P4002433 Available[0][1][2] 332 Need[0][1][2] P0743 P1122 P2600 P3011 P4431 Determine all processes that can be satisfied by the Available resources. NOTE: Both P1 and P3 are candidates, but we choose P1 because it occurs next.

19 Example Allocation[0][1][2]Max[0][1][2] P0010753 P1322322 P2302902 P3211222 P4002433 Available[0][1][2] 210 Need[0][1][2] P0743 P1--- P2600 P3011 P4431 Now perform the Allocation of Needed resources to P1 Add Need Subtract Need

20 Example Allocation[0][1][2]Max[0][1][2] P0010753 P1322322 P2302902 P3211222 P4002433 Available[0][1][2] 210 Need[0][1][2] P0743 P1--- P2600 P3011 P4431 Once a process’ needs are fulfilled, free those resources to re-allocate to the next process in sequence. Available[0][1][2] 532 Since the Maximum requirement is fulfilled, the process can run to completion in order to free its resources to other processes.

21 Example Allocation[0][1][2]Max[0][1][2] P0010753 P1---322 P2302902 P3211222 P4002433 Available[0][1][2] 532 Need[0][1][2] P0743 P1--- P2600 P3011 P4431 Reapply the updated Available to Need and arrive at the choice of P3 (P4 would work, but occurs later). Then, free its resources.

22 Example Allocation[0][1][2]Max[0][1][2] P0010753 P1---322 P2302902 P3222222 P4002433 Available[0][1][2] 521 Need[0][1][2] P0743 P1--- P2600 P3--- P4431 Reapply the updated Available to Need and arrive at the choice of P3 (P4 would work, but occurs later). Then, free its resources.

23 Example Allocation[0][1][2]Max[0][1][2] P0010753 P1---322 P2302902 P3---222 P4002433 Available[0][1][2] 743 Need[0][1][2] P0743 P1--- P2600 P3--- P4431 Reapply the updated Available to Need and arrive at the choice of P4 (the next one that is satisfied).

24 Example Allocation[0][1][2]Max[0][1][2] P0010753 P1---322 P2302902 P3---222 P4433433 Available[0][1][2] 312 Need[0][1][2] P0743 P1--- P2600 P3--- P4--- Allocate the Need resources to P4.

25 Example Allocation[0][1][2]Max[0][1][2] P0010753 P1---322 P2302902 P3---222 P4---433 Available[0][1][2] 745 Need[0][1][2] P0743 P1--- P2600 P3--- P4--- Allocate the Need resources to P4. Then, since P4 can run to completion, free its resources.

26 Example Allocation[0][1][2]Max[0][1][2] P0010753 P1---322 P2302902 P3---222 P4---433 Available[0][1][2] 745 Need[0][1][2] P0743 P1--- P2600 P3--- P4--- Both P0 and P2 can be satisfied from Available resources. Choose P0 to Allocate the Need. (NOTE: the textbook chooses P2 for this example.)

27 Example Allocation[0][1][2]Max[0][1][2] P0753753 P1---322 P2302902 P3---222 P4---433 Available[0][1][2] 002 Need[0][1][2] P0--- P1--- P2600 P3--- P4--- Both P0 and P2 can be satisfied from Available resources. Choose P0 to Allocate the Need. (NOTE: the textbook chooses P2 for this example.)

28 Example Allocation[0][1][2]Max[0][1][2] P0---753 P1---322 P2302902 P3---222 P4---433 Available[0][1][2] 755 Need[0][1][2] P0--- P1--- P2600 P3--- P4--- Both P0 and P2 can be satisfied from Available resources. Choose P0 to Allocate the Need. (NOTE: the textbook chooses P2 for this example.) Then, free the resources

29 Example Allocation[0][1][2]Max[0][1][2] P0---753 P1---322 P2302902 P3---222 P4---433 Available[0][1][2] 755 Need[0][1][2] P0--- P1--- P2600 P3--- P4--- Finally, we handle P2 which can clearly be satisfied because, as required, Need ≤ Available

30 Example Allocation[0][1][2]Max[0][1][2] P0---753 P1---322 P2902902 P3---222 P4---433 Available[0][1][2] 155 Need[0][1][2] P0--- P1--- P2--- P3--- P4--- Finally, we handle P2 which can clearly be satisfied because, as required, Need ≤ Available

31 Example Allocation[0][1][2]Max[0][1][2] P0---753 P1---322 P2---902 P3---222 P4---433 Available[0][1][2] 1057 Need[0][1][2] P0--- P1--- P2--- P3--- P4--- Freeing resources may have to be limited by the actual maximum resource availability (not specified for this example). Also, recall that processes request maximum resources throughout their lifetime, not those needed immediately.

32 Example Need[0][1][2] P0743 P1122 P2600 P3011 P4431 This leads to the possible resource allocation orderings: P1, P3, P4, P0, P2 Again, this is different than discussed in the textbook (P2 comes first, then P0). For these sequences the vector Finish [ k ] == true for all NP=5 processes 1 2 3 4 5 Available[0][1][2] 1057 All of the stated Needs have been shown to be allocatable from the total allowed resource set Available. NOTE: All resources have now been reclaimed, in agreement with initial assumption.

33 Example: Resource Allocation Allocation[0][1][2]Need[0][1][2] P0010743 P1200122 P2302600 P3211011 P4002431 Available[0][1][2] 332 We established that the state is safe. If P1 makes a new request: Request 1 = { 1, 0, 2 } Can this request be granted safely and immediately? We started with the state tables populated with safe state values.

34 Example Allocation[0][1][2]. Need [0][1][2] P0010743 P12+1=30+0=00+2=21-1=02-0=22-2=0 P2302600 P3211011 P4002431 Available[0][1][2] 3-1=23-0=32-2=0 For: Request 1 = { 1, 0, 2 } Assume it can be allocated immediately.

35 Example Allocation[0][1][2]. Need[0][1][2] P0010743 P1302020 P2302600 P3211011 P4002431 Available[0][1][2] 230 For: Request 1 = { 1, 0, 2 } After modifying the Need and Available, then test for safety.

36 Example Allocation[0][1][2]. Need[0][1][2] P0010743 P1322000 P2302600 P3211011 P4002431 Available[0][1][2] 210 For: Request 1 = { 1, 0, 2 } After modifying the Need and Available, then test for safety. We find a sequence: { P1, Allocate resources to P1

37 Example Allocation[0][1][2]. Need[0][1][2] P0010743 P1------ P2302600 P3211011 P4002431 Available[0][1][2] 532 For: Request 1 = { 1, 0, 2 } After modifying the Need and Available, then test for safety. We find a sequence: { P1, P3, Free P1 resources, Find P3: Next <= Available

38 Example Allocation[0][1][2]. Need[0][1][2] P0010743 P1------ P2302600 P3222000 P4002431 Available[0][1][2] 521 For: Request 1 = { 1, 0, 2 } After modifying the Need and Available, then test for safety. We find a sequence: { P1, P3, Allocate resources to P3

39 Example Allocation[0][1][2]. Need[0][1][2] P0010743 P1------ P2302600 P3------ P4002431 Available[0][1][2] 743 For: Request 1 = { 1, 0, 2 } After modifying the Need and Available, then test for safety. We find a sequence: { P1, P3, P4, Free P3 resources, Find P4 (next): Next <= Available

40 Example Allocation[0][1][2]. Need[0][1][2] P0010743 P1------ P2302600 P3------ P4433000 Available[0][1][2] 312 For: Request 1 = { 1, 0, 2 } After modifying the Need and Available, then test for safety. We find a sequence: { P1, P3, P4, Allocate resources to P4

41 Example Allocation[0][1][2]. Need[0][1][2] P0010743 P1------ P2302600 P3------ P4------ Available[0][1][2] 745 For: Request 1 = { 1, 0, 2 } After modifying the Need and Available, then test for safety. We find a sequence: { P1, P3, P4, P0, Free P4 resources, Find P0 (next): Next <= Available

42 Example Allocation[0][1][2]. Need[0][1][2] P0------ P1------ P2302600 P3------ P4------ Available[0][1][2] 755 For: Request 1 = { 1, 0, 2 } After modifying the Need and Available, then test for safety. We find a sequence: { P1, P3, P4, P0, P2 } Free P0 resources, Find P2 (next): Next <= Available

43 Example Allocation[0][1][2]. Need[0][1][2] P0------ P1------ P2302600 P3------ P4------ Available[0][1][2] 755 For: Request 1 = { 1, 0, 2 } After modifying the Need and Available, then test for safety. We find a sequence: { P1, P3, P4, P0, P2 } SAFE STATE ! Since the state is safe, then the request R 1 can be granted immediately.

44 Example Allocation[0][1][2]. Need[0][1][2] P0010743 P1302020 P2302600 P3211011 P4002431 Available[0][1][2] 230 Having already assumed: Request 1 = { 1, 0, 2 } for P1 Now consider, in addition: Request 4 = { 3, 3, 0 } for P4. This request cannot be granted because there are not sufficient resources available.

45 Example Allocation[0][1][2]. Need[0][1][2] P0010743 P1302020 P2302600 P3211011 P4002431 Available[0][1][2] 230 Having already assumed: Request 1 = { 1, 0, 2 } for P1 Next consider: Request 0 = { 0, 2, 0 } for P0. This request cannot be granted. Although sufficient resources are available, P0 retains a Need and this leads to an unsafe state. It cannot be guaranteed to complete, hence it cannot free sufficient resources.

46 Deadlock Detection Previous Banker’s algorithms provide answers to the questions:  Is a particular system (of allocated resources) safe?  Can a request for resources be granted safely? However, a safe state is not necessarily deadlock free !

47 Deadlock Detection Now ask the question  For a given resource allocation state, does a deadlock exist?  These slides elaborate on this point and an example This is important to support  Deadlock Prevention (or Avoidance)  Deadlock Recovery  These slides do not discuss these points. Read the discussion in the textbook (Silberschatz, Chapter 7)

48 Deadlock Detection Data structures  Scalars NP :: Number of Processes NR :: Number of Resources  Vectors Available, Work :: NR resources Finish :: NP processes  NP x NR Matrices Allocation :: Each of NP processes have been allocated NR resources Request :: Each of NP processes requesting NR resources

49 Deadlock Detection Algorithm  Step 1: Initialize Work = Available ; For all k < NP  if Allocation k ≠ 0 Then Finish[ k ] = false Else Finish[ k ] = true ;  Note that Finish[k]=true; means that it cannot produce a deadlock since it does not control any resources.

50 Deadlock Detection Algorithm  Middle logic Step 2:  Find a process index k such that Finish[ k ] == false AND Request k ≤ Work  If no k exists, goto Step 4 (Final step) Step 3:  Work = Work + Allocation ;  Finish[ k ] = true ;  Goto Step 2  This investigates every possible allocation sequence

51 Deadlock Detection Algorithm  Final logic Step 4:  If Finish[ k ] == false for any k in 0 ≤ k ≤ (NR-1) System is in a deadlock state Else, System is deadlock free. NOTE:  Finish[ k ] == false implies that P k is deadlocked

52 Example: Deadlock Detection Allocation[0][1][2]Request[0][1][2] P0010000 P1200202 P2303000 P3211100 P4002002 Work[0][1][2] (Available) 000 We consider the following state with declared Allocations and Available resources. A Request is made by every process at the same time. Can a sequence be found for allocating resources that does not lead to deadlock?

53 Example Allocation[0][1][2]Request[0][1][2] P0010000 P1200202 P2303000 P3211100 P4002002 Work[0][1][2] (Available) 000 There are two processes, P0 and P2, that are not requesting resources. Start at P0 because it is first (and later, if necessary, we can try P2 first).

54 Example Allocation[0][1][2]Request[0][1][2] P0010--- P1200202 P2303000 P3211100 P4002002 Work[0][1][2] (Available) 010 Assume that P0 runs to completion, then frees its resources so they become available again.

55 Example Allocation[0][1][2]Request[0][1][2] P0------ P1200202 P2303000 P3211100 P4002002 Work[0][1][2] (Available) 010 Next, P2 is assumed to complete.

56 Example Allocation[0][1][2]Request[0][1][2] P0------ P1200202 P2------ P3211100 P4002002 Work[0][1][2] (Available) 313 And, P2 frees its resources.

57 Example Allocation[0][1][2]Request[0][1][2] P0------ P1200202 P2------ P3211100 P4002002 Work[0][1][2] (Available) 313 The next state (in order) that can complete is P3. NOTE: Also could allocate and complete P1 and P4.

58 Example Allocation[0][1][2]Request[0][1][2] P0------ P1200202 P2------ P3311--- P4002002 Work[0][1][2] (Available) 213 Allocate resources to P3.

59 Example Allocation[0][1][2]Request[0][1][2] P0------ P1200202 P2------ P3------ P4002002 Work[0][1][2] (Available) 524 Then, reclaim resources from P3.

60 Example Allocation[0][1][2]Request[0][1][2] P0------ P1200202 P2------ P3------ P4002002 Work[0][1][2] (Available) 524 Noting that we can allocate resources and complete both P1 and P4, we choose P4 because it is next in order.

61 Example Allocation[0][1][2]Request[0][1][2] P0------ P1200202 P2------ P3------ P4004--- Work[0][1][2] (Available) 522 Allocate resources to P4.

62 Example Allocation[0][1][2]Request[0][1][2] P0------ P1200202 P2------ P3------ P4------ Work[0][1][2] (Available) 526 Then reclaim resources from P4.

63 Example Allocation[0][1][2]Request[0][1][2] P0------ P1402--- P2------ P3------ P4------ Work[0][1][2] (Available) 324 Finally, allocate resources to P1.

64 Example Allocation[0][1][2]Request[0][1][2] P0------ P1------ P2------ P3------ P4------ Work[0][1][2] (Available) 726 And reclaim the resources from P1.

65 Example Allocation[0][1][2]Request[0][1][2] P0------ P1------ P2------ P3------ P4------ Work[0][1][2] (Available) 726 And reclaim the resources from P1. Thus the sequence { P0, P2, P3, P4, P1 } is deadlock free, since Finish[k] = true for all k.

66 Example And reclaim the resources from P1. Thus the sequence { P0, P2, P3, P4, P1 } is deadlock free, since Finish[k] = true for all k. Other sequences that are deadlock free are: { P0, P2, P3, P1, P4 } { P2, P0, P3, P1, P4 } { P2, P0, P3, P4, P1 } Allocation[0][1][2]Request[0][1][2] P0010000 P1200202 P2303000 P3211100 P4002002 Work[0][1][2] (Available) 000

67 Example: Modified request Now, modify the resource request for P2 – it requests one more resource of type [2]. Allocation[0][1][2]Request[0][1][2] P0010000 P1200202 P2303001 P3211100 P4002002 Work[0][1][2] (Available) 000 This request will lead to deadlock. Follow the trace as the scenario develops...

68 Example: Modified request Only P0 has Request 0 ≤ Work Allocation[0][1][2]Request[0][1][2] P0010000 P1200202 P2303001 P3211100 P4002002 Work[0][1][2] (Available) 000

69 Example: Modified request Allocate resources to P0, then reclaim. Allocation[0][1][2]Request[0][1][2] P0------ P1200202 P2303001 P3211100 P4002002 Work[0][1][2] (Available) 010

70 Example: Modified request Now we see that there are not sufficient resources to allocate to any other process. Allocation[0][1][2]Request[0][1][2] P0------ P1200202 P2303001 P3211100 P4002002 Work[0][1][2] (Available) 010 Hence, this state is deadlocked and the resource request cannot be made.

71 Deadlock Recovery Read Section 7.7 in the textbook. Most systems take the simplest approach, aborting all deadlocked processes More sophisticated approaches can be developed  They take much more time to execute hence are applied less often  They are strongly dependent on choice of policies.

72 Summary We covered essential aspects of deadlock detection and avoidance in respect of resource allocation to handle process requests. We paid special attention to  Issue of safe states  Resource allocation Read Chapter 7 of Silberschatz et al.


Download ppt "Resource Allocation, Deadlock and Banker’s Algorithm 60-330 Supplementary Notes Dr. R. D. Kent Last modified: Dec. 11, 2006."

Similar presentations


Ads by Google