EXPERIMENT NO :- 7
AIM :- Write a c program to implement Bankers Algorithm
THEORY :-
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.
Why Banker's algorithm is named so?
Banker's algorithm is named so because it is used in banking system to check
whether loan can be sanctioned to a person or not. Suppose there are n number
of account holders in a bank and the total sum of their money is S. If a person
applies for a loan then the bank first subtracts the loan amount from the total
money that bank has and if the remaining amount is greater than S then only the
loan is sanctioned. It is done because if all the account holders comes to
withdraw their money then the bank can easily do it.
In other words, the bank would never allocate its money in such a way that it
can no longer satisfy the needs of all its customers. The bank would try to be in
safe state always.
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.
Available =
+ tis 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 R,
Max
+ Itis a 2-d array of size ‘n*m’ that defines the maximum demand of each
process in a system.
+ Max{i, |] =k means process P, may request at most ‘k’ instances of
resource type RiAllocation :
+ Itis a 2-d array of size ‘n*m’ that defines the number of resources of each
type currently allocated to each process.
+ Allocationf i, |] = k means process P. is currently allocated ‘k’ instances of
resource type R;
Need =
+ Itis a 2-d array of size ‘n*m’ that indicates the remaining resource need of
each process.
+ Need[i, j]=kmeans process P, currently need ‘k’ instances of resource
type R,
+ Need[i, j]=Max[i, j]—Allocation[i, j]
Allocation, specifies the resources currently allocated to process P, and
Need. specifies the additional resources that process P, may still request to
complete its task.
Banker's algorithm consists of Safety algorithm and Resource request algorithm
Safety Algorithm
PROGRAM :-
// Banker's Algorithm
#include
int main()
{
// PO, P1, P2, P3, P4 are the Process names here
int n, m, i, j, k;
n= 5; // Number of processes
m = 3; // Number of resources
int alloc[5][3] = { { 0, 1,0}, //PO // Allocation Matrixint max[5][3] = {{ 7, 5,3}, //PO // MAX Matrix
{3,2,2},// P41
{9,0,2},//P2
{2,2,2},// P3
{4,3,3}}:// P4
int avail[3] = { 3, 3, 2 }; // Available Resources
int f[n], ans[n], ind = 0;
for (k = 0; k avail[j]){
flag = 1;
break;
}
}
if (flag == 0) {
ans[ind++] = i;
for (y = 0; y < m; y++)
availly] += alloc{illy];
fli] =1;
}
}
}
}int flag = 1;
for(i=O;i", ans[i]);
printf(" P%d", ans[n - 1]);
}
return (0);OUTPUT :-
i) C:\Users\Sanika\Desktop \osprac\osexp\OSexp7.cxe
CONCLUSION :-
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