Banker's Algorithm (Deadlock Avoidance)
Definition:
The Banker's Algorithm is a deadlock avoidance algorithm used in operating systems.
It helps the system decide whether or not to grant resource requests by checking if
doing so keeps the system in a safe state.
The algorithm is named after a banker who gives loans only if he is sure he can satisfy
all customers' maximum future demands without running out of money.
Key Terms:
- Maximum Need: Maximum number of resources a process may need.
- Allocation: Current number of resources allocated to the process.
- Need: Remaining resources the process may still request (Need = Max - Allocation).
- Available: Total free resources currently available in the system.
How It Works (Step-by-step):
1. Each process declares its maximum need of each resource type.
2. When a process requests resources:
- The system pretends to allocate them.
- It checks if the system remains in a safe state.
- A safe state means there's at least one sequence in which all processes can finish.
3. If the system is still safe -> grant the resources.
4. If the system is not safe -> deny the request and make the process wait.
Safety Check Algorithm:
1. Create a Work vector equal to Available, and a Finish array (initially false for all processes).
2. Find a process P such that:
- Finish[P] == false, and
- Need[P] <= Work
3. If found:
- Work = Work + Allocation[P]
- Finish[P] = true
- Repeat Step 2.
4. If all Finish[i] = true, the system is in a safe state.
Advantages of Banker's Algorithm:
- Prevents Deadlock: It avoids unsafe allocations that could lead to deadlock.
- Safe System State: Always keeps the system in a safe condition.
- Predictable Behavior: Allocations are done only after confirming safety.
- Works Well with Fixed Resource Requests: Good when process needs are known in advance.
Limitations (optional):
- Requires all processes to declare max resources in advance.
- Can be slow for large systems (many calculations).
- Not practical for systems where resources change frequently.