![]() |
VOOZH | about |
Banker's Algorithm is a resource allocation and deadlock avoidance algorithm used in operating systems. It ensures that there exists at least one sequence of processes such that each process can obtain the needed resources, complete its execution,
The following Data structures are used to implement the Banker’s Algorithm:
Let 'n' be the number of processes in the system and 'm' be the number of resource types.
1. Available
2. Max
3. Allocation
4. Need
Allocation specifies the resources currently allocated to process Pi and Need specifies the additional resources that process Pi may still request to complete its task.
Consider a system with three processes (P1, P2, P3) and 6 instances of a resource. Let's say:
1. The available instances of the resource are: 1
2. The current allocation of resources to processes is:
P1: 2P2: 3P3: 13. The maximum demand (maximum resources each process may eventually request) is:
P1: 4P2: 5P3: 3In this situation:
Need = Maximum Demand - Allocation
Process | Maximum Demand | Allocation | Need |
|---|---|---|---|
P1 | 4 | 2 | 2 |
P2 | 5 | 3 | 2 |
P3 | 3 | 1 | 2 |
P1 may need 2 more resources to complete.P2 may need 2 more resource to complete.P3 may need 2 more resources to complete.However, there is only 1 resource available. Even though none of the processes are currently deadlocked, the system is in an unsafe state because there is no sequence of resource allocation that guarantees all processes can complete.
Banker's algorithm consists of a Safety algorithm and a Resource request algorithm.
The Banker’s Algorithm is made up of two parts:
The safety algorithm checks whether the system is in a safe state-meaning all processes can complete without causing deadlock.
Steps:
1. Initialize:
2. Look for a process Pi such that:
3. If such a process is found:
4. If all processes are marked finished (Finish[i] = true for all), the system is safe.
Essentially, the algorithm simulates process completion to verify that all processes can finish safely.
This algorithm decides if a process’s resource request can be granted safely.
Steps:
1. Check if the request exceeds the process’s maximum need: If Request[i] ≤ Need[i], continue; otherwise, error.
2. Check if resources are available: If Request[i] ≤ Available, continue; otherwise, the process waits.
3. Temporarily allocate the resources:
4. Run the Safety Algorithm:
In short, the resource-request algorithm ensures resources are only granted if the system remains safe, preventing deadlock.
Example: Considering a system with five processes P0 through P4 and three resources of type A, B, C. Resource type A has 10 instances, B has 5 instances and type C has 7 instances. Suppose at time t0 following snapshot of the system has been taken:
Need [i, j] = Max [i, j] – Allocation [i, j]
So, the content of Need Matrix is:
Applying the Safety algorithm on the given system,
We must determine whether this new system state is safe. To do so, we again execute Safety algorithm on the above data structures.
Hence the new system state is safe, so we can immediately grant the request for process P1 .
Hence, the SAFE Sequence is as follows: P1 -> P3 -> P4 -> P0 -> P2.