The request for any resource will be granted if the resulting state of the system doesn't cause a deadlock in the system
Problem with Deadlock Avoidance
Handling deadlock by using a technique like deadlock prevention may cause problems like low device utilization and starvation. To overcome these problems, deadlock avoidance can be used.
Algorithms for Deadlock avoidance
- Safe state
- Resource-Allocation-Graph Algorithm
- Banker’s Algorithm
A state is safe if the system can allocate resources to each process up to its maximum need in some sequence (safe sequence) and still avoid deadlock
Safe sequence: A sequence of processes that don’t cause process deadlock after completion of execution.
Bankers Algorithm: Constructing a safe sequence for single type resource
Consider a system having 12 instances of any resource and P0, P1 and P2 are given process with no. of resource instances required.
Using the table, we calculate the number of allocated instances and the currently available number of instances.
Bankers Algorithm: Constructing a safe sequence for multiple types of resource
To construct a safe sequence it needs the following input:
- Available number of instances
- Maximum number of instances
- Number of allocated instances
- The current need for a process
Consider a system with 3 resource types [ A, B, C] and each resource having a different number of instances and Maximum Available Instance: [A=10, B=5, C=7]. Also, Suppose that at time T0 the processes [P0,…..P4] enter into the system with some required number of resource instances for execution.
We update the available number of resources instance after the execution of each process since each process releases allocated resources.
Here is the full implementation with a classy design:
Source code: https://github.com/SudeepMi/bankers_algorithm
Buy me a coffee: Esewa ( 9808383500 )