Banker's Algorithm
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.
Advantages
Following are the essential characteristics of the Banker's algorithm:
- It contains various resources that meet the requirements of each process.
- Each process should provide information to the operating system for upcoming resource requests, the number of resources, and how long the resources will be held.
- It helps the operating system manage and control process requests for each type of resource in the computer system.
- The algorithm has a Max resource attribute that represents indicates each process can hold the maximum number of resources in a system.
Disadvantages
- It requires a fixed number of processes, and no additional processes can be started in the system while executing the process.
- The algorithm does no longer allows the processes to exchange its maximum needs while processing its tasks.
- Each process has to know and state their maximum resource requirement in advance for the system.
- The number of resource requests can be granted in a finite time, but the time limit for allocating the resources is one year.
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:
Safety algorithm
Resource request algorithm
Safety Algorithm
It is a safety algorithm used to check whether or not a system is in a safe state or follows the safe sequence in a banker's algorithm:
1. There are two vectors Wok and Finish of length m and n in a safety algorithm.
Initialize: Work = Available
Finish[i] = false; for I = 0, 1, 2, 3, 4… n - 1.
2. Check the availability status for each type of resources [i], such as:
Need[i] <= Work
Finish[i] == false
If the i does not exist, go to step 4.
3. Work = Work +Allocation(i) // to get new resource allocation
Finish[i] = true
Go to step 2 to check the status of resource availability for the next process.
4. If Finish[i] == true; it means that the system is safe for all processes.
Resource Request Algorithm
A resource request algorithm checks how a system will behave when a process makes each type of resource request in a system as a request matrix.
Let create a resource request array R[i] for each process P[i]. If the Resource Requesti [j] equal to 'K', which means the process P[i] requires 'k' instances of Resources type R[j] in the system.
1. When the number of requested resources of each type is less than the Need resources, go to step 2 and if the condition fails, which means that the process P[i] exceeds its maximum claim for the resource. As the expression suggests:
If Request(i) <= Need
Go to step 2;
2. And when the number of requested resources of each type is less than the available resource for each process, go to step (3). As the expression suggests:
If Request(i) <= Available
Else Process P[i] must wait for the resource since it is not available for use.
3. When the requested resource is allocated to the process by changing state:
Available = Available - Request
Allocation(i) = Allocation(i) + Request (i)
Needi = Needi - Requesti
When the resource allocation state is safe, its resources are allocated to the process P(i). And if the new state is unsafe, the Process P (i) has to wait for each type of Request R(i) and restore the old resource-allocation state.
Example: Consider a system that contains five processes P1, P2, P3, P4, P5 and the three resource types A, B and C. Following are the resources types: A has 10, B has 5 and the resource type C has 7 instances.
Process | Allocation A B C | Max A B C | Available A B C |
---|---|---|---|
P1 | 0 1 0 | 7 5 3 | 3 3 2 |
P2 | 2 0 0 | 3 2 2 | |
P3 | 3 0 2 | 9 0 2 | |
P4 | 2 1 1 | 2 2 2 | |
P5 | 0 0 2 | 4 3 3 |
Answer the following questions using the banker's algorithm:
- What is the reference of the need matrix?
- Determine if the system is safe or not.
- What will happen if the resource request (1, 0, 0) for process P1 can the system accept this request immediately?
Ans. 2: Context of the need matrix is as follows:
Need [i] = Max [i] - Allocation [i]
Need for P1: (7, 5, 3) - (0, 1, 0) = 7, 4, 3
Need for P2: (3, 2, 2) - (2, 0, 0) = 1, 2, 2
Need for P3: (9, 0, 2) - (3, 0, 2) = 6, 0, 0
Need for P4: (2, 2, 2) - (2, 1, 1) = 0, 1, 1
Need for P5: (4, 3, 3) - (0, 0, 2) = 4, 3, 1
Process | Need A B C |
---|---|
P1 | 7 4 3 |
P2 | 1 2 2 |
P3 | 6 0 0 |
P4 | 0 1 1 |
P5 | 4 3 1 |
Hence, we created the context of need matrix.
Ans. 2: Apply the Banker's Algorithm:
Available Resources of A, B and C are 3, 3, and 2.
Now we check if each type of resource request is available for each process.
Step 1: For Process P1:
Need <= Available
7, 4, 3 <= 3, 3, 2 condition is false.
So, we examine another process, P2.
Step 2: For Process P2:
Need <= Available
1, 2, 2 <= 3, 3, 2 condition true
New available = available + Allocation
(3, 3, 2) + (2, 0, 0) => 5, 3, 2
Similarly, we examine another process P3.
Step 3: For Process P3:
P3 Need <= Available
6, 0, 0 < = 5, 3, 2 condition is false.
Similarly, we examine another process, P4.
Step 4: For Process P4:
P4 Need <= Available
0, 1, 1 <= 5, 3, 2 condition is true
New Available resource = Available + Allocation
5, 3, 2 + 2, 1, 1 => 7, 4, 3
Similarly, we examine another process P5.
Step 5: For Process P5
P5 Need <= Available
4, 3, 1 <= 7, 4, 3 condition is true
New available resource = Available + Allocation
7, 4, 3 + 0, 0, 2 => 7, 4, 5
Now, we again examine each type of resource request for processes P1 and P3.
Step 6: For Process P1:
P1 Need <= Available
7, 4, 3 <= 7, 4, 5 condition is true
New Available Resource = Available + Allocation
7, 4, 5 + 0, 1, 0 => 7, 5, 5
So, we examine another process P2.
Step 7: For Process P3:
P3 Need <= Available
6, 0, 0 <= 7, 5, 5 condition is true
New Available Resource = Available + Allocation
7, 5, 5 + 3, 0, 2 => 10, 5, 7
Hence, we execute the banker's algorithm to find the safe state and the safe sequence like P2, P4, P5, P1 and P3.
Ans. 3: For granting the Request (1, 0, 2), first we have to check that Request <= Available, that is (1, 0, 2) <= (3, 3, 2), since the condition is true. So the process P1 gets the request immediately.