Program:
// Banker's Algorithm
// Resource Allocation Algorithm
/*
5
3
1 1 2
2 1 2
4 0 1
0 2 0
1 1 2
4 3 3
3 2 2
9 0 2
7 5 3
1 1 2
2 1 0
*/
#include <iostream>
#include <unistd.h>
using namespace std;
int n, m;
// Function to display Matrix
void display(int mat[5][3])
{
for (int i = 0; i < n; i++)
{
cout << endl;
for (int j = 0; j < m; j++)
{
cout << mat[i][j] << " ";
}
}
}
// Function to check safety
bool safety(int available[5], int need[5][3], int alloc[5][3])
{
// Step 2: Creating Flag variable to keep track of which process complete
int flag[n];
for (int i = 0; i < n; i++)
{
flag[i] = 0;
}
// Step 3: Allocating Resources if available is greater than need
int safe_seq[n], ct = 0;
int i, j, k, f;
for (k = 0; k < n; k++)
{
for (i = 0; i < n; i++)
{
f = 0;
if (flag[i] == 1)
{
continue;
}
for (j = 0; j < m; j++)
{
if (available[j] < need[i][j])
{
f = 0;
break;
}
else
{
f = 1;
}
}
if (f == 1)
{
flag[i] = 1;
break;
}
}
if (i == n)
{
continue;
}
if (flag[i] == 1)
{
safe_seq[ct] = i;
ct++;
for (j = 0; j < m; j++)
{
available[j] += alloc[i][j];
}
}
}
for (int i = 0; i < n; i++)
{
if (flag[i] == 0)
{
return false;
}
}
return true;
}
bool check(int resources[3], int need[5][3], int available[3], int p_no)
{
// Check if request is less than need
for (int j = 0; j < m; j++)
{
if (resources[j] > need[p_no][j])
{
cout << "\n\n Request is greater than Need";
return false;
}
}
// Check if request is less than available
for (int j = 0; j < m; j++)
{
if (resources[j] > available[j])
{
cout << "\n\n Request is greater than Available";
return false;
}
}
return true;
}
int main()
{
// freopen("input.txt", "r", stdin);
cout << "\n\n Enter number of process:";
cin >> n;
cout << "\n\n Enter number of resources:";
cin >> m;
int alloc[n][3], need[n][3], max[n][3];
int available[m];
cout << "\n\n Enter Allocation Matrix:";
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> alloc[i][j];
}
}
cout << "\n\n Enter Max Matrix:";
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> max[i][j];
}
}
cout << "\n\n Enter available Array:";
for (int i = 0; i < m; i++)
{
cin >> available[i];
}
cout << "\n\n Displaying Allocation Matrix:";
display(alloc);
cout << "\n\n Displaying Max Matrix:";
display(max);
cout << "\n\n Displaying Available Array:";
for (int i = 0; i < m; i++)
{
cout << available[i] << " ";
}
// Step 1: Creating Need matix
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
need[i][j] = max[i][j] - alloc[i][j];
}
}
cout << "\n\n Displaying Need Matrix:";
display(need);
// Taking Input for Resource Request
int p_no, resources[m];
cout << "\n\n Enter Process Number you want to allocate resource:";
cin >> p_no;
cout << "\n\n Enter Resouces " << m << ":";
for (int i = 0; i < m; i++)
{
cin >> resources[i];
}
cout << "\n Requested for Process:" << p_no;
cout << "\n\n Requested Resources:";
for (int i = 0; i < m; i++)
{
cout << resources[i] << " ";
}
p_no = p_no - 1;
if (!check(resources, need, available, p_no))
{
return 0;
}
// Now Request is less than available and need
// allocate the resources
for (int i = 0; i < m; i++)
{
alloc[p_no][i] += resources[i];
available[i] -= resources[i];
need[p_no][i] -= resources[i];
}
if (safety(available, need, alloc))
{
cout << "\n\n Requested Resources Can be Allocated!";
}
else
{
cout << "\n\n Requested Resources Cannot be Allocated!";
}
return 0;
}
Output:
1) Request Can Be Granted:


