Banker's Algorithm in Operating System (OS)There is an algorithm called Banker's Algorithm used in removing deadlocks while dealing with the safe allocation of resources to processes in a computer system. It gives all the resources to each process and avoids resource allocation conflicts. Overview of Banker's Algorithm
Real Life ExampleImagine having a bank with T amount of money and n account holders. At some point, whenever one of the account holders requests a loan:
Banker's Algorithm in Operating SystemsLikewise, in an operating system:
AdvantagesFollowing are the essential characteristics of the Banker's algorithm:
Disadvantages
When working with a banker's algorithm, it requests to know about three things:
Important Data Structures in the Banker's AlgorithmSuppose n is the number of processes, and m is the number of each type of resource used in a computer system.
The Banker's Algorithm is essentially a combination of two components:
Safety AlgorithmA safe state is said to be a state from which the system can execute some sequence of processes without deadlock. A system is said to be safe if all its processes can be executed safely in some sequence. It is a safety algorithm used to check whether or not a system is in a safe state or follows the safe sequence in a banker's algorithm: 1. There are two vectors, Wok and Finish, of length m and n in a safety algorithm. Initialization:
2. Check the availability status for each type of resource [i], such as: If the i does not exist, go to step 4. 3. If a process i satisfies the conditions in step 2, simulate the allocation by updating the Work vector: This adds the resources currently allocated to process I back to the Work vector, stimulating the release of resources after the process has finished. Go to step 2 to check the status of resource availability for the next process. 4. If Finish[i] == true, it means that the system is safe for all processes. Resource Request AlgorithmA Resource Request Algorithm A description of how the system behaves when a process requests a resource needs to be made. The algorithm will check whether the requested resources can be allocated without causing deadlock. The request can then be portrayed as a request matrix. A resource request algorithm checks how a system will behave when a process makes each type of resource request in a system as a request matrix. Let create a resource request array R[i] for each process P[i]. If the Resource Requesti [j] equal to 'K', which means the process P[i] requires 'k' instances of Resources type R[j] in the system. Steps of the Resource Request Algorithm:1. Check if the Request is within the Process's Maximum Claim: At every request of resources done by a process P[i], the algorithm checks if the number of resources requested is less than or equal to the Need of that process: If the condition holds, then it proceeds to the next one. Otherwise, process P[i] has exceeded its maximum claim; the request is denied. If the process exceeds its maximum claim, this is then a violation of the rules of the system; the process cannot be allowed to proceed. 2. Check if the Requested Resources are Available: The system now checks the availability of the requested resources in sufficient quantity. This is done by comparing the request [i] with the Available resources: If this condition is satisfied, proceed to the next step. If not, then P[i] must wait until the required resources become available. This makes sure the system does not allocate resources it does not currently have, which otherwise could lead to deadlock or unsafe states. 3. Simulate Resource Allocation: If available, the system allocates temporary requested resources to process P[i] by modifying the Available, Allocation, and Need matrices. 4. Check System Safety:
Example: Consider a system that contains five processes P1, P2, P3, P4, P5 and the three resource types A, B and C. Following are the resources types: A has 10, B has 5 and the resource type C has 7 instances.
Answer the following questions using the banker's algorithm:
Ans. 1: The context of the need matrix is as follows: Need [i] = Max [i] - Allocation [i]
Hence, we created the context of need matrix. Ans. 2: Apply the Banker's Algorithm: Available Resources of A, B and C are 3, 3, and 2. Now we check if each type of resource request is available for each process. Step 1: For Process P1: Need <= Available 7, 4, 3 <= 3, 3, 2 condition is false. So, we examine another process, P2. Step 2: For Process P2: Need <= Available 1, 2, 2 <= 3, 3, 2 condition true New available = available + Allocation (3, 3, 2) + (2, 0, 0) => 5, 3, 2 Similarly, we examine another process P3. Step 3: For Process P3: P3 Need <= Available 6, 0, 0 < = 5, 3, 2 condition is false. Similarly, we examine another process, P4. Step 4: For Process P4: P4 Need <= Available 0, 1, 1 <= 5, 3, 2 condition is true New Available resource = Available + Allocation 5, 3, 2 + 2, 1, 1 => 7, 4, 3 Similarly, we examine another process P5. Step 5: For Process P5: P5 Need <= Available 4, 3, 1 <= 7, 4, 3 condition is true New available resource = Available + Allocation 7, 4, 3 + 0, 0, 2 => 7, 4, 5 Now, we again examine each type of resource request for processes P1 and P3. Step 6: For Process P1: P1 Need <= Available 7, 4, 3 <= 7, 4, 5 condition is true New Available Resource = Available + Allocation 7, 4, 5 + 0, 1, 0 => 7, 5, 5 So, we examine another process P2. Step 7: For Process P3: P3 Need <= Available 6, 0, 0 <= 7, 5, 5 condition is true New Available Resource = Available + Allocation 7, 5, 5 + 3, 0, 2 => 10, 5, 7 Hence, we execute the banker's algorithm to find the safe state and the safe sequence like P2, P4, P5, P1 and P3. Ans. 3: Whether the system can accommodate the resource request (1,0,0) for the process, P1 can be checked by using Banker's Algorithm as follows: 1. Checking if the request is within the maximum claim of P1 If the resources requested: (1,0,0) are less than or equal to P1's Need, then go ahead; otherwise, reject. 2. Check whether the system has enough available resources to handle the request (1,0,0). If the resources are available, go; otherwise, P1 should wait. 3. Simulate resource allocation: Allocate the resources to P1 temporarily and check if the system is safe or not using the Safety Algorithm. If the system is safe, the request is granted. Otherwise, cancel the allocation and let P1 wait. |