0% found this document useful (0 votes)
4 views9 pages

Banker Algorithm With Example

The Banker's Algorithm is a deadlock avoidance algorithm used in operating systems, similar to how banks determine loan approvals based on available resources. It requires processes to declare their maximum resource needs and uses data structures like Available, Max, Allocation, and Need to manage resource allocation safely. The algorithm includes a safety check to ensure the system remains in a safe state while processing requests for resources.

Uploaded by

ankit362541
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Banker Algorithm With Example

The Banker's Algorithm is a deadlock avoidance algorithm used in operating systems, similar to how banks determine loan approvals based on available resources. It requires processes to declare their maximum resource needs and uses data structures like Available, Max, Allocation, and Need to manage resource allocation safely. The algorithm includes a safety check to ensure the system remains in a safe state while processing requests for resources.

Uploaded by

ankit362541
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Banker's Algorithm in Operating System

Banker's algorithm is a deadlock avoidance algorithm. It is named so because this algorithm is used
in banking systems to determine whether a loan can be granted or not.
Consider there are n account holders in a bank and the sum of the money in all of their accounts is S.
Every time a loan has to be granted by the bank, it subtracts the loan amount from the total
money the bank has. Then it checks if that difference is greater than S. It is done because, only then,
the bank would have enough money even if all the n account holders draw all their money at once.
Banker's algorithm works in a similar way in computers.
Whenever a new process is created, it must specify the maximum instances of each resource type that
it needs, exactly.
Characteristics of Banker's Algorithm
The characteristics of Banker's algorithm are as follows:
 If any process requests for a resource, then it has to wait.
 This algorithm consists of advanced features for maximum resource allocation.
 There are limited resources in the system we have.
 In this algorithm, if any process gets all the needed resources, then it is that it should return
the resources in a restricted period.
 Various resources are maintained in this algorithm that can fulfill the needs of at least one
client.
Let us assume that there are n processes and m resource types.
Data Structures used to implement the Banker’s Algorithm
Some data structures that are used to implement the banker's algorithm are:
1. Available
It is an array of length m. It represents the number of available resources of each type. If Available[j]
= k, then there are k instances available, of resource type Rj.
2. Max
It is an n x m matrix which represents the maximum number of instances of each resource that a
process can request. If Max[i][j] = k, then the process Pi can request atmost k instances of resource
type Rj.
3. Allocation
It is an n x m matrix which represents the number of resources of each type currently allocated to each
process. If Allocation[i][j] = k, then process Pi is currently allocated k instances of resource type Rj.
4. Need
It is a two-dimensional array. It is an n x m matrix which indicates the remaining resource needs of
each process. If Need[i][j] = k, then process Pi may need k more instances of resource type Rj to
complete its task.
Need[i][j] = Max[i][j] - Allocation [i][j]
Banker’s algorithm comprises of two algorithms:
1. Safety algorithm
2. Resource request algorithm
Safety Algorithm
A safety algorithm is an algorithm used to find whether or not a system is in its safe state. The
algorithm is as follows:
1. Let Work and Finish be vectors of length m and n, respectively. Initially,
2. Work = Available
3. Finish[i] =false for i = 0, 1, ... , n - 1.
This means, initially, no process has finished and the number of available resources is represented by
the Available array.
4. Find an index i such that both
5. Finish[i] ==false
6. Need(i) <= Work
If there is no such i present, then proceed to step 4.
It means, we need to find an unfinished process whose needs can be satisfied by the available
resources. If no such process exists, just go to step 4.
7. Perform the following:
8. Work = Work + Allocation(i)
9. Finish[i] = true
Go to step 2.
When an unfinished process is found, then the resources are allocated and the process is marked
finished. And then, the loop is repeated to check the same for all other processes.
10. If Finish[i] == true for all i, then the system is in a safe state.
That means if all processes are finished, then the system is in safe state.
This algorithm may require an order of mxn² operations in order to determine whether a state is safe
or not.
Banker's Algorithm in Operating System
Banker's algorithm is a deadlock avoidance algorithm. It is named so because this algorithm is used
in banking systems to determine whether a loan can be granted or not.
Consider there are n account holders in a bank and the sum of the money in all of their accounts is S.
Every time a loan has to be granted by the bank, it subtracts the loan amount from the total
money the bank has. Then it checks if that difference is greater than S. It is done because, only then,
the bank would have enough money even if all the n account holders draw all their money at once.
Banker's algorithm works in a similar way in computers.
Whenever a new process is created, it must specify the maximum instances of each resource type that
it needs, exactly.
Characteristics of Banker's Algorithm
The characteristics of Banker's algorithm are as follows:
 If any process requests for a resource, then it has to wait.
 This algorithm consists of advanced features for maximum resource allocation.
 There are limited resources in the system we have.
 In this algorithm, if any process gets all the needed resources, then it is that it should return
the resources in a restricted period.
 Various resources are maintained in this algorithm that can fulfill the needs of at least one
client.
Let us assume that there are n processes and m resource types.
Data Structures used to implement the Banker’s Algorithm
Some data structures that are used to implement the banker's algorithm are:
1. Available
It is an array of length m. It represents the number of available resources of each type. If Available[j]
= k, then there are k instances available, of resource type Rj.
2. Max
It is an n x m matrix which represents the maximum number of instances of each resource that a
process can request. If Max[i][j] = k, then the process Pi can request atmost k instances of resource
type Rj.
3. Allocation
It is an n x m matrix which represents the number of resources of each type currently allocated to each
process. If Allocation[i][j] = k, then process Pi is currently allocated k instances of resource type Rj.
4. Need
It is a two-dimensional array. It is an n x m matrix which indicates the remaining resource needs of
each process. If Need[i][j] = k, then process Pi may need k more instances of resource type Rj to
complete its task.
Need[i][j] = Max[i][j] - Allocation [i][j]
Banker’s algorithm comprises of two algorithms:
1. Safety algorithm
2. Resource request algorithm
Safety Algorithm
A safety algorithm is an algorithm used to find whether or not a system is in its safe state. The
algorithm is as follows:
1. Let Work and Finish be vectors of length m and n, respectively. Initially,
2. Work = Available
3. Finish[i] =false for i = 0, 1, ... , n - 1.
This means, initially, no process has finished and the number of available resources is represented by
the Available array.
4. Find an index i such that both
5. Finish[i] ==false
6. Need(i) <= Work
If there is no such i present, then proceed to step 4.
It means, we need to find an unfinished process whose needs can be satisfied by the available
resources. If no such process exists, just go to step 4.
7. Perform the following:
8. Work = Work + Allocation(i)
9. Finish[i] = true
Go to step 2.
When an unfinished process is found, then the resources are allocated and the process is marked
finished. And then, the loop is repeated to check the same for all other processes.
10. If Finish[i] == true for all i, then the system is in a safe state.
That means if all processes are finished, then the system is in safe state.
This algorithm may require an order of mxn² operations in order to determine whether a state is safe
or not.
Resource Request Algorithm
Now the next algorithm is a resource-request algorithm and it is mainly used to determine whether
requests can be safely granted or not.
Let Request(i) be the request vector for the process Pi. If Requesti[j]==k, then process Pi wants k
instance of Resource type Rj. When a request for resources is made by the process Pi, the following
are the actions that will be taken:
1. If Request(i) <= Need(i), then go to step 2;else raise an error condition, since the process has
exceeded its maximum claim.
2. If Request(i) <= Available(i) then go to step 3; else Pi must have to wait as resources are not
available.
3. Now we will assume that resources are assigned to process Pi and thus perform the following steps:
Available = Available - Request(i);
Allocation(i) = Allocation(i) + Request(i);
Need(i) = Need(i) - Request(i);
If the resulting resource allocation state comes out to be safe, then the transaction is completed and,
process Pi is allocated its resources. But in this case, if the new state is unsafe, then Pi waits for
Requesti, and the old resource-allocation state is restored.
Disadvantages of Banker's Algorithm
Some disadvantages of this algorithm are as follows:
1. During the time of Processing, this algorithm does not permit a process to change its
maximum need.
2. Another disadvantage of this algorithm is that all the processes must know in advance about
the maximum resource needs.
3. This algorithm permits the requests to be provided in constrained time, but for one year which
is a fixed period.
Now its time to take a look at the Example of Banker's Algorithm:
Banker's Algorithm Example::
Let us consider the following snapshot for understanding the banker's algorithm:

Allocation Max Available


Processes
ABC ABC ABC

P0 112 433 210

P1 212 322

P2 401 902

P3 020 753

P4 112 112

1. calculate the content of the need matrix?


2. Check if the system is in a safe state?
3. Determine the total sum of each type of resource?
Solution:
1. The Content of the need matrix can be calculated by using the formula given below:
Need = Max – Allocation

2. Let us now check for the safe state.


Safe sequence:
1. For process P0, Need = (3, 2, 1) and
Available = (2, 1, 0)
Need <=Available = False
So, the system will move to the next process.
2. For Process P1, Need = (1, 1, 0)
Available = (2, 1, 0)
Need <= Available = True
Request of P1 is granted.
Available = Available +Allocation
= (2, 1, 0) + (2, 1, 2)
= (4, 2, 2) (New Available)
3. For Process P2, Need = (5, 0, 1)
Available = (4, 2, 2)
Need <=Available = False
So, the system will move to the next process.
4. For Process P3, Need = (7, 3, 3)
Available = (4, 2, 2)
Need <=Available = False
So, the system will move to the next process.
5. For Process P4, Need = (0, 0, 0)
Available = (4, 2, 2)
Need <= Available = True
Request of P4 is granted.
Available = Available + Allocation
= (4, 2, 2) + (1, 1, 2)
= (5, 3, 4) now, (New Available)
6. Now again check for Process P2, Need = (5, 0, 1)
Available = (5, 3, 4)
Need <= Available = True
Request of P2 is granted.
Available = Available + Allocation
= (5, 3, 4) + (4, 0, 1)
= (9, 3, 5) now, (New Available)
7. Now again check for Process P3, Need = (7, 3, 3)
Available = (9, 3, 5)
Need <=Available = True
The request for P3 is granted.
Available = Available +Allocation
= (9, 3, 5) + (0, 2, 0) = (9, 5, 5)
8. Now again check for Process P0, = Need (3, 2, 1)
= Available (9, 5, 5)
Need <= Available = True
So, the request will be granted to P0.
Safe sequence: < P1, P4, P2, P3, P0>
The system allocates all the needed resources to each process. So, we can say that the system is
in a safe state.
3. The total amount of resources will be calculated by the following formula:
The total amount of resources= sum of columns of allocation + Available
= [8 5 7] + [2 1 0] = [10 6 7]

Disadvantages of Banker's Algorithm


Some disadvantages of this algorithm are as follows:
1. During the time of Processing, this algorithm does not permit a process to change its
maximum need.
2. Another disadvantage of this algorithm is that all the processes must know in advance about
the maximum resource needs.
3. This algorithm permits the requests to be provided in constrained time, but for one year which
is a fixed period.
Now its time to take a look at the Example of Banker's Algorithm:
Banker's Algorithm Example::
Let us consider the following snapshot for understanding the banker's algorithm:

Allocation Max Available


Processes
ABC ABC ABC

P0 112 433 210

P1 212 322
P2 401 902

P3 020 753

P4 112 112

1. calculate the content of the need matrix?


2. Check if the system is in a safe state?
3. Determine the total sum of each type of resource?
Solution:
1. The Content of the need matrix can be calculated by using the formula given below:
Need = Max – Allocation

2. Let us now check for the safe state.


Safe sequence:
1. For process P0, Need = (3, 2, 1) and
Available = (2, 1, 0)
Need <=Available = False
So, the system will move to the next process.
2. For Process P1, Need = (1, 1, 0)
Available = (2, 1, 0)
Need <= Available = True
Request of P1 is granted.
Available = Available +Allocation
= (2, 1, 0) + (2, 1, 2)
= (4, 2, 2) (New Available)
3. For Process P2, Need = (5, 0, 1)
Available = (4, 2, 2)
Need <=Available = False
So, the system will move to the next process.
4. For Process P3, Need = (7, 3, 3)
Available = (4, 2, 2)
Need <=Available = False
So, the system will move to the next process.
5. For Process P4, Need = (0, 0, 0)
Available = (4, 2, 2)
Need <= Available = True
Request of P4 is granted.
Available = Available + Allocation
= (4, 2, 2) + (1, 1, 2)
= (5, 3, 4) now, (New Available)
6. Now again check for Process P2, Need = (5, 0, 1)
Available = (5, 3, 4)
Need <= Available = True
Request of P2 is granted.
Available = Available + Allocation
= (5, 3, 4) + (4, 0, 1)
= (9, 3, 5) now, (New Available)
7. Now again check for Process P3, Need = (7, 3, 3)
Available = (9, 3, 5)
Need <=Available = True
The request for P3 is granted.
Available = Available +Allocation
= (9, 3, 5) + (0, 2, 0) = (9, 5, 5)
8. Now again check for Process P0, = Need (3, 2, 1)
= Available (9, 5, 5)
Need <= Available = True
So, the request will be granted to P0.
Safe sequence: < P1, P4, P2, P3, P0>
The system allocates all the needed resources to each process. So, we can say that the system is
in a safe state.
3. The total amount of resources will be calculated by the following formula:
The total amount of resources= sum of columns of allocation + Available
= [8 5 7] + [2 1 0] = [10 6 7]

You might also like