Prerequisite: Banker’s Algorithm
The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests for safety by simulating the allocation for predetermined maximum possible amounts of all resources, then makes an “s-state” check to test for possible activities, before deciding whether allocation should be allowed to continue.
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 resources types.
- It is a 1-d array of size ‘m’ indicating the number of available resources of each type.
- Available[ j ] = k means there are ‘k’ instances of resource type Rj
- It is a 2-d array of size ‘n*m’ that defines the maximum demand of each process in a system.
- Max[ i, j ] = k means process Pi may request at most ‘k’ instances of resource type Rj.
- It is a 2-d array of size ‘n*m’ that defines the number of resources of each type currently allocated to each process.
- Allocation[ i, j ] = k means process Pi is currently allocated ‘k’ instances of resource type Rj
- It is a 2-d array of size ‘n*m’ that indicates the remaining resource need of each process.
- Need [ i, j ] = k means process Pi currently allocated ‘k’ instances of resource type Rj
- Need [ i, j ] = Max [ i, j ] – Allocation [ i, j ]
Allocationi specifies the resources currently allocated to process Pi and Needi specifies the additional resources that process Pi may still request to complete its task.
Banker’s algorithm consist of Safety algorithm and Resource request algorithm
The algorithm for finding out whether or not a system is in a safe state can be described as follows:
- Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively.
Initialize: Work= Available
Finish [i]=false; for i=1,2,……,n
- Find an i such that both
a) Finish [i]=false
if no such i exists goto step (4)
- Work=Work + Allocation_i
- If Finish[i]=true for all i,
then the system is in safe state.
Safe sequence is the sequence in which the processes can be safely executed.
In this post, implementation of Safety algorithm of Banker’s Algorithm is done.
System is in safe state. Safe sequence is: 1 3 4 0 2
Considering a system with five processes P0 through P4 and three resources types 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:
We must determine whether the new system state is safe. To do so, we need to execute Safety algorithm on the above given allocation chart.
Time complexity = O(n*n*m) where n = number of processes and m = number of resources.
This article is contributed by Sahil Chhabra (akku). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Program for Least Recently Used (LRU) Page Replacement algorithm
- Program for SSTF disk scheduling algorithm
- Program for Next Fit algorithm in Memory Management
- Program for Best Fit algorithm in Memory Management using Linked List
- Peterson's Algorithm for Mutual Exclusion | Set 1 (Basic C implementation)
- Peterson's Algorithm for Mutual Exclusion | Set 2 (CPU Cycles and Memory Fence)
- Program for FCFS CPU Scheduling | Set 1
- Program for Shortest Job First (or SJF) CPU Scheduling | Set 1 (Non- preemptive)
- Program for Round Robin scheduling | Set 1
- Program for FCFS CPU Scheduling | Set 2 (Processes with different arrival times)
- Program for Priority CPU Scheduling | Set 1
- Program for Shortest Job First (SJF) scheduling | Set 2 (Preemptive)
- Buddy Memory Allocation Program | Set 1 (Allocation)
- Buddy Memory Allocation Program | Set 2 (Deallocation)
- Banker's Algorithm in Operating System
- Peterson's Algorithm in Process Synchronization
- C-SCAN Disk Scheduling Algorithm
- Optimal Page Replacement Algorithm
- Banker's Algorithm in Operating System
- Dekker's algorithm in Process Synchronization