The Banker's algorithm provides the safe execution of processes in arbitrary order without a
Note: Click here to learn more about deadlock avoidance.
The algorithm is named after the banking system. It works the same as the bank does before approving a loan for any person. First, the bank subtracts the required loan from the total amount the bank has. If the remaining amount is greater than the total money of the account holders, the needed loan is sanctioned. In this case, the bank would always be in a safe state if all account holders withdrew their money.
Let's understand the step-by-step working of the banker's algorithm:
The process makes a request for the required resources.
The banker's algorithm checks if the request is valid or invalid. The request is valid if it is less than the available resources. If it's not, the process is asked to wait and we move to the next process.
After allocating resources to the process, it executes successfully and returns all the already allocated resources. We add these resources to the total resources. Now, we check if the new amount of resources can fulfill the need of processes that are on the waiting list.
We add the executed process to the list. This is known as a safe sequence. We then repeat Step 1 until all processes get executed.
If all the processes exist in the safe sequence list, the system is in a safe state, and a deadlock will never occur.
Note: If we are unable to fulfil the request of all processes, then the deadlock will definitely occur.
Let's understand the banker's algorithm as an example. Consider a system with five processes, P1 to P5, with three different resources A, B, and C. A has 10, B has 5 and C has 7 resources in total. We want to check whether or not the system is in a safe state. If it is, what would be the sequence?
Let's suppose we get the details of the system in which the allocated resources and their maximum need is mentioned. The remaining column represents the remaining need for a particular process. We can calculate the values of the last two columns by using the following formulas:
Here are a few advantages of the banker's algorithm:
The max resource property of this algorithm allows the process to use the maximum available resources.
It helps the operating system in managing all types of resources.
It always has some resources that can meet the requirements of at least one process.
After execution, the process returns all the allocated resources.
Here are a few disadvantages of the banker's algorithm:
Each process should know about its maximum requirement of resources.
A process can't change its need while processing.
We can't add a new process while processing.
Free Resources