Definition
Banker's Algorithm
The Banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests for safety by simulating the allocation of predetermined maximum possible amounts of all resources. It ensures that the system always remains in a safe state.
The name comes from the analogy of a bank that never lends more money than it can satisfy if all customers simultaneously demanded their remaining credit lines (using the repayments of finished customers to satisfy the others).
Notation
For a system with processes and resource types:
- Resource Vector (): Total resources in the system, .
- Available Vector (): Resources currently not allocated, .
- Claim Matrix (): Maximum requirement of each process, where is the maximum need of process for resource .
- Allocation Matrix (): Current allocation, where is the amount of resource held by process .
- Required Matrix (): Remaining need, where .
Safe State Algorithm
To determine if a state is safe:
-
Initialise a working vector and mark all processes as “unfinished.”
-
Find an unfinished process such that its remaining requirements are less than or equal to :
-
If no such process exists, go to Step 5.
-
If such a process is found, assume it completes, add its currently held resources to (), mark it as “finished,” and return to Step 2.
-
If all processes are marked “finished,” the state is safe; otherwise, it is unsafe.
Application
When a process requests resources :
- Verify (request within declared limit).
- Verify (resources available).
- Provisional Allocation: Temporarily update .
- Safety Check: Run the safe state algorithm.
- If safe, grant the request; if unsafe, rollback and block the process.
Example
Consider a system with processes and resource categories. The total resource vector is
The system state is defined by the following matrices and the available vector :
The required matrix is calculated as :
Safety Analysis
To determine if the current state is safe, we simulate the completion of processes using a temporary work vector , starting with the available resources :
Step 1: Find a Process to Satisfy
We look for an unfinished process whose remaining requirements () can be met by our current available resources ().
- Comparing with Insufficient resources.
- Comparing with Satisfied!
Simulation: runs to completion and releases its held resources ().
Step 2: Find the Next Process
With , we check the remaining unfinished processes ().
- Comparing with Satisfied!
Simulation: runs to completion and releases its held resources ().
Step 3: Continue Search
With , we check and .
- Comparing with Satisfied!
Simulation: runs to completion and releases its held resources ().
Step 4: Final Process
With , only remains.
- Comparing with Satisfied!
Simulation: runs to completion and releases its held resources ().
Conclusion
Since a valid sequence exists () where all processes can eventually be satisfied and finish, the system is in a safe state.