Вы находитесь на странице: 1из 2

Banker's Algorithm

The Banker's algorithm is such that it can calculate in advance before a resource is allocated to a process, whether it can lead to a deadlock (unsafe state) or it can certainly manage to avoid it (safe state). The Banker's algorithm is run by the operating system whenever a process requests resources. The algorithm avoids deadlock by denying or postponing the request if it determines that accepting the request could put the system in an unsafe state (one where deadlock could occur). When a new process enters a system, it must declare the maximum number of instances of each resource type that may not exceed the total number of resources in the system. Also, when a process gets all its requested resources it must return them in a finite amount of time. For the Banker's algorithm to work, it needs to know three things:

How much of each resource each process could possibly request How much of each resource each process is currently holding How much of each resource the system currently has available

Resources may be allocated to a process only if it satisfies the following conditions: 1. request max, else set error condition as process has crossed maximum claim made by it. 2. request available, else process waits until resources are available. The algorithm for the deadlock avoidance works as follows.

Each process declares the total required resources to the operating system at the beginning in advance. Cannot exceed the total resources of that type When a process requests the Operating System for a resource, the Operating System finds out whether the resource is free and whether it can be allocated. The Operating System allocates the resource only after ensuring that the system will still be in a safe state. If it finds that there can be a deadlock after the imaginary allocation at some point in time, it postpones the decision to allocate the resource.

Basic data structures to be maintained to implement the Banker's Algorithm: Let n be the number of processes in the system and m be the number of resource types. Then we need the following data structures:

Available: A vector of length m indicates the number of available resources of each type. If Available[j] = k, there are k instances of resource type Rj available. Max: An nm matrix defines the maximum demand of each process. If Max[i,j] = k, then Pi may request at most k instances of resource type Rj. Allocation: An nm matrix defines the number of resources of each type currently allocated to each process. If Allocation[i,j] = k, then process Pi is currently allocated k instance of resource type Rj. Need: An nm matrix indicates the remaining resource need of each process. If Need[i,j] = k, then Pi may need k more instances of resource type Rj to complete task.

Note: Need = Max - Allocation.

Safety Algorithm
Safety algorithm is used to find the state of the system. That is, system may be in safe state or unsafe state. 1. Let work and finish be vector of length m and n respectively. Initialize Work : =Available and Finish [i] := false for i = 1, 2, . . . n. 2. Find an i such that both a. Finish[i] = false b. Needi Work if no such i exists, go to step 4 3. Work := Work + Allocationi Finish[i] := true Go to step 2. 4. If Finish[i] = true for all i, then the system is in safe state.

Example System consists of five processes (P1, P2, P3, P4, P5) and three resources (R1, R2, R3). Resource type R1 has 10 instances, R2 has 5 instances and R3 has 7 instances.
Process R1 P1 P2 P3 P4 P5 0 2 3 2 0 Allocation R2 1 0 0 1 0 R3 0 0 2 1 2 R1 7 3 9 2 4 Max R2 5 2 0 2 3 R3 3 2 2 2 3 R1 7 1 6 0 4 Need R2 4 2 0 1 3 R3 3 2 0 1 1 3 3 2 Available / Work R1 R2 R3

Need = Max Allocation Safe sequence : Safe sequence is calculated as follows: 1. Need of each process is compared with available. If needi available, then the resources are allocated to that process and process will release the resource, if not next process need is taken for comparison. Need of process P1 is 7, 4, 3 and available is 3, 3, 2 False 7, 4, 3 > 3, 3, 2 Need (P1) available So system will move for next process 2. Need for process P2 is 1, 2, 2 and available is 3, 3, 2 True 1, 2, 2 < 3, 3, 2 Need (P2) available (Work) Then Finish[i] = True Request of P2 is granted and process P2 is release the resource to the system Work := Work + Allocation Work := 3, 3, 2 + 2, 0, 0 = 5, 3, 2 (New Available) This procedure is continued for all processes. . . 3. Next process P3 need 6, 0, 0 is compared with new available 5, 3, 2 False 6, 0, 0 > 5, 3, 2 Need (P3) available So system will move for next process 4. Process P4 need 0, 1, 1 is compared with new available 5, 3, 2 True 0, 1, 1 < 5, 3, 2 Need (P4) available Work := Work + Allocation Work := 5, 3, 2 + 2, 1, 1 = 7, 4, 3 (New Available) 5. Then Process P5 need 4, 3, 1 is compared with available 7, 4, 3 Need (P5) available True 4, 3, 1 < 7, 4, 3 Work := Work + Allocation Work := 7, 4, 3 + 0, 0, 2 = 7, 4, 5 (New Available) 6. Process P1 need 7, 4, 3 and available 7, 4, 5. If this request is granted then the system may be in the deadlock state. After granting the request, available resource is 0, 0, 2 and the system is in unsafe state. 7. Process P3 need 6, 0, 0 is compared with new available 7, 4, 5 Need (P3) available True 6, 0, 0 < 7, 4, 5 Work := Work + Allocation Work := 7, 4, 5 + 3, 0, 2 = 10, 4, 7 (New Available) 8. Remaining process P1 need 7, 4, 3 is compared with new available 10, 4, 7 Need (P1) available True 7, 4, 3 < 10, 4, 7 Work := Work + Allocation Work := 10, 4, 7 + 0, 1, 0 = 10, 5, 7 so safe sequence is <P2, P4, P5, P3, P1>

Вам также может понравиться