Operating Systems For Internetworking Additional Resources Banker's Algorithm - Example Explained
Operating Systems For Internetworking Additional Resources Banker's Algorithm - Example Explained
Bankers algorithm is based on several assumptions. To fully understand the way it works one have to be aware of them. 1. Processes before submission to the system claim maximal requests of the system resources. 2. This also means that a process not always needs maximal amount of resources throughout its whole lifetime but at some stage reaches maximum resource count. 3. Process can be allocated resource according to its claims in particular moment if and only if this allocation leads to the safe state. Safe state is a state which allows fulfilling all requests from the remaining processes. In other words there exist a sequence of process executions from safe state which leads to successful execution and finish of all processes. 4. If there is no execution which leads to an and thus allocation of requested resources leads to a possible situation which can cause a deadlock process request is rejected and process waits gives way to another process request. To see how Banker algorithm works lets consider such allocation of resources and requests: Allocation matrix of current resource allocation (Ri for i from 1 to 4) of resources to the processes Pi (i from 0 to 4) Maximal Claims maximum claims per process Need matrix of process needs in terms of resources. The remaining resources to be allocated to a process to fulfill its maximal requests. Available a vector of currently available resources of each type. Allocationinitial R1 R2 P0 P1 P2 P3 P4 0 1 1 0 0 0 0 3 6 0 R3 1 0 5 3 1 R4 2 0 4 2 4
Maximal Claims R1 R2 R3 P0 P1 P2 P3 P4 0 1 2 0 0 0 7 3 6 6 1 5 5 5 5
R4 2 0 6 2 6
Needinitial R1 P0 P1 P2 P3 P4 0 0 1 0 0
R2 0 7 0 0 6
R3 0 5 0 2 4
R4 0 0 2 0 2
Resources vector R1 R2 R3 R4 3 14 12 12 Available vectorinitial R1 R2 R3 R4 1 5 2 0 The first step in running the algorithm is to determine whether current state is safe or not. To check this we try to assign resources to all processes in some arbitrary order. This means for instance we try run first P0, then P3, P2 and P1 and check whether it is possible to fulfill requirements of all processes. If this sequence does not work we try another one until we find one or we give up. If we cannot find such a sequence this means that the system is not in a safe state. If the system is in safe state, for each incoming request we have to verify whether the next request can be satisfied without leading to a state which is not safe. Having initial state as above first we have to check whether the system is in safe state. Indeed, the system is in safe place because all process requests can be satisfied when we run processes in following order: To prove that this is true lets try to assign resources in this order and show this allocation 2
on defined above matrixes: Available vector has 1 unit or resource 1, 5 of resource 2 and so on (refer to Available vector). Lets run first P0 (highlighted in red) because it has all resources it needs at the moment.
AllocationP0 R1 R2 P0 P1 P2 P3 P4 0 1 1 0 0 0 0 3 6 0
R3 0 0 5 3 1
R4 0 0 4 2 4
Available vectorP0 finished R1 R2 R3 R4 1 5 2 0 After execution P0 (now marked in yellow to show that is has successfully finished) releases all resources it needed which is 1 unit of R3 and 2 units of R4. We have to update now available vector and allocation matrix: AllocationP0 finished R1 R2 R3 P0 P1 P2 P3 P4 0 1 1 0 0 0 0 3 6 0 0 0 5 3 1 R4 0 0 4 2 4
Available vectorP0 finished R1 R2 R3 R4 1 5 3 2 In this situation there are enough resources (see Available vector and compare with Need matrix entry for process 2) and we can run process P2 till the end. Therefore we allocate additional 1 unit of resource 1 and 2 units of resource 4. Allocation matrix then looks as below: 3
AllocationP2 R1 R2 P0 P1 P2 P3 P4 0 1 2 0 0 0 0 3 6 0
R3 0 0 5 3 1
R4 0 0 6 2 4
Available vectorP2 R1 R2 R3 R4 0 5 3 0 When process P2 finished its execution the state of the system resources utilization is as below. AllocationP2 finished R1 R2 R3 P0 P1 P2 P3 P4 0 1 0 0 0 0 0 0 6 0 0 0 0 3 1 R4 0 0 0 2 4
Available vectorP2 finished R1 R2 R3 R4 2 8 8 6 So process P0 and P2 finished successfully thus the corresponding lines in Allocation matrix are set to 0. The resources used by P2 are returned to the system and set as available. As a result there are 2 units of resource 1, 8 of resource 2 and 3 and 6 of resource 4 available in the system. The next process chosen for execution is P4. Below is presented resources allocation for this process: AllocationP4 R1 R2 P0 P1 P2 0 1 0 0 0 0 R3 0 0 0 R4 0 0 0 4
P3 P4
0 0
6 6
3 5
2 6
Available vectorP4 R1 R2 R3 R4 2 2 4 4 After process finishes its execution the state of resource allocation is as follows:
AllocationP4 finished R1 R2 R3 P0 P1 P2 P3 P4 0 1 0 0 0 0 0 0 6 0 0 0 0 3 0
R4 0 0 0 2 0
Available vectorP4 finished R1 R2 R3 R4 2 8 9 10 Next to execute is process P1. After allocation of the resources according to its needs (see Needs matrix) updates of Allocation matrix and resources vector are as follows: AllocationP1 R1 R2 P0 P1 P2 P3 P4 0 1 0 0 0 0 7 0 6 0 R3 0 5 0 3 0 R4 0 0 0 2 0
Available vectorP1 R1 R2 R3 R4 2 1 4 10 And after the end of execution of process P1: AllocationP1 finished 5
R1 P0 P1 P2 P3 P4 0 0 0 0 0
R2 0 0 0 6 0
R3 0 0 0 3 0
R4 0 0 0 2 0
Available vectorP1 finished R1 R2 R3 R4 3 8 9 10 And the final step to check that maximum process requests for this sequence of execution can be successfully finished is execution of process P3. After allocation of the maximal amount of resources to this process we have: AllocationP3 R1 R2 P0 P1 P2 P3 P4 0 0 0 0 0 0 0 0 6 0 R3 0 0 0 5 0 R4 0 0 0 2 0
Available vectorP3 R1 R2 R3 R4 3 8 7 10 And finally all processes in our sequence P0, P2, P1, P4, P3 have been successfully allocated all resources. AllocationP3 finished R1 R2 R3 P0 P1 P2 P3 P4 0 0 0 0 0 0 0 0 6 0 0 0 0 5 0 R4 0 0 0 2 0
Available vectorP3 finished R1 R2 R3 R4 3 14 12 12 To verify this we check whether all available resources equal to the maximum number of resources. Resources vector (all resources in the system) R1 R2 R3 R4 3 14 12 12 As one can see they are. So now we proved that the system in the state (called Initial) from the beginning of this example is in safe state because there exist at least one sequence of processes execution which leads to successful allocation of resources according to claimed by processes maximum number of requests (see Maximal Claims). Now lets assume that in this state (our initial state from page 1) P1 requests additional resource according to the request vector as below: Request vector for P1 R1 R2 R3 R4 0 4 2 0 To grant these resources to P1 one needs to verify that there is enough resource available in the system. So we check Available vector (we called it initial on page 1): Available vectorinitial R1 R2 R3 R4 1 5 2 0 There is enough resources to grant this request. This means that our allocation of resources after this request is granted looks like AllocationP1 request R1 R2 R3 P0 P1 P2 P3 P4 0 1 1 0 0 0 4 3 6 0 1 2 5 3 1 R4 2 0 4 2 4
And Available vector looks like: Available vectorP1 request R1 R2 R3 R4 1 1 0 0 After this allocation process waits for additional resources according to updated Needs matrix: Needinitial R1 P0 P1 P2 P3 P4 0 0 1 0 0 R2 0 3 0 0 6 R3 0 3 0 2 4 R4 0 0 2 0 2
Unfortunately the remaining resources lead to the situation that the other processes apart from P0 are unable to claim their maximum needs and therefore finish their execution unless process P1 releases resources. This is because the remaining resources are R1 1 unit, R2 1 unit. As one can see P2 needs apart from 1 unit of P1 also 2 units of R4 in result it cannot be executed currently if it claims maximum number of resources. P3 and P4 are in the same situation. Bankers algorithm does not allow this situation even though this might not lead to the deadlock. In our situation after P1 was granted its resources none of the processes apart from P0 can finish and return enough resources so P1 can finish and return its resources allowing the remaining request to be fulfilled. In short this leads directly to a deadlock. So in our situation P1 request cannot be granted because in the worst case scenario it leads to a deadlock e.g. P1 (0,4,2,0) and just afterwards P2 (1,0,0,2) -> deadlock. Additional resources: William Stallings (4th edition, International Edition) - Chapter 6, Section 6.3 Deadlock avoidance (there is good overview and explanation of another example).