In: Computer Science
Implement a C++ program to implement the Banker’s algorithm for deadlock avoidance. Number of process 5, number of resources 3 and the number of instances of each given resource is in available. You should complete the functionalities for safe state check and resource request processing.
To Do
1. Complete the definition of isSafe function. The function take, the process array, 1D array of available resources, 2D array storing current allocation, and 2D array of current need. The function does not return any value. If the system is not in safe state, the function prints “System is not in safe state”, else the function prints “System is in safe state. Safe sequence is:” with the safe sequence printed subsequently.
2. Complete the definition of processReq function. The function does not return any value. The function takes the process id, corresponding 1D array specifying resource request, 1D array of available resources, 2D array storing current allocation, and 2D array of current need.
The incomplete code:
#include<iostream>
using namespace std;
// Number of processes
const int numP = 5;
// Number of resources
const int numR = 3;
// Max resource allocation to each process
int maxAlloc[][numR] = {{ 7, 5, 3 }, { 3, 2, 2 }, { 9, 0, 2 }, { 2,
2,2 }, { 4, 3, 3 }};
// Calculate the resource need for a process
void calcNeed(int need[][numR], int allot[][numR])
{
// Calculating Need of each Process
for (int i = 0; i < numP; i++)
for (int j = 0; j < numR; j++)
// Need of instance = maxm instance - allocated instance
need[i][j] = maxAlloc[i][j] - allot[i][j];
}
// Function to find the system is in safe state or not
void isSafe(int processes[], int avail[], int allot[][numR], int
need[][numR])
{
// Place Your Code here
}
// Function to check if given resource request can be
processed.
void processReq(int proID, int req[], int avail[], int
allot[][numR], int need[][numR])
{
// Place Your Code here
}
// Main Function
int main()
{
int processes[] = {0, 1, 2, 3, 4};
// Resources allocated to processes
int allot[][numR] = {{0, 1, 0},{2, 0, 0},{3, 0, 2},{2, 1, 1},{0, 0,
2}};
// Resource Need
int procNeed[numP][numR];
// Available instances of resources
int avail[] = {3, 3, 2};
cout << "System information at time - T0" <<
endl;
cout << "Maximum allocation to different processes " <<
endl;
for(int i = 0; i < numP; i++)
{
for(int j = 0; j < numR; j++)
cout << maxAlloc[i][j] << " ";
cout << endl;
}
cout << "Available resouces instances at time - T0"
<< endl;
for(int j = 0; j < numR; j++)
cout << avail[j] << " ";
cout << endl;
cout << "Allocation to different processes at - T0 " <<
endl;
for(int i = 0; i < numP; i++)
{
for(int j = 0; j < numR; j++)
cout << allot[i][j] << " ";
cout << endl;
}
calcNeed(procNeed, allot);
// Check system is in safe state or not
isSafe(processes, avail, allot, procNeed);
return 0;
}
void processReq(int proID, int req[], int avail[], int
allot[][numR], int need[][numR])
{
bool flag_request=true; for (int j = 0; j < Num_R ; j++) |
{ |
if (request[j] > need[process_request][j]) |
{ |
flag_request=false; |
break; |
} |
if (request[j] > available[j]) |
{ |
flag_request=false; |
break; |
} |
} |
if (flag_request == false) |
cout<<"\nTHE REQUEST CAN NOT BE GRANTED .... X "<<endl; |
else |
{ |
for (int j = 0; j < Num_R ; j++) |
{ |
available[j]-=request[j]; |
allocation[process_request][j]+=request[j]; |
need[process_request][j]-=request[j]; |
} |
bool safe_request= safe( allocation ,need ,available ); |
if (safe_request == true) |
{ |
cout<<"\nTHE REQUEST CAN BE GRANTED ...."<<endl; |
|
cout<<"\nThe Available Vector is..."<<endl; |
cout<<" "; |
for (int j = 0 ; j < Num_R ; j++) |
cout<<' '<< r[j] ; |
cout<<"\n "; |
for (int j = 0; j < Num_R ; j++) |
{ |
cout<<work[j] <<' ';} |
cout<<endl; |
} |
else |
cout<<"\nTHE REQUEST CAN NOT BE GRANTED BECAUSE \nTHE SYSTEM IS NOT IN A SAFE STATE! ...."<<endl; |
|
} |
}
bool
isSafe(
int
processes[],
int
avail[],
int
allot[][R],
int
need[][R])
{
// Mark all processes
as infinish
bool
finish[P] = {0};
// To store safe
sequence
int
safeSeq[P];
// Make a copy of
available resources
int
work[R];
for
(
int
i = 0; i < R ;
i++)
work[i]
= avail[i];
// While all
processes are not finished
// or system is not
in safe state.
int
count = 0;
while
(count < P)
{
//
Find a process which is not finish and
//
whose needs can be satisfied with current
//
work[] resources.
bool
found =
false
;
for
(
int
p = 0; p < P;
p++)
{
//
First check if a process is finished,
//
if no, go for next condition
if
(finish[p] == 0)
{
//
Check if for all resources of
//
current P need is less
//
than work
int
j;
for
(j = 0; j < R; j++)
if
(need[p][j] > work[j])
break
;
//
If all needs of p were satisfied.
if
(j == R)
{
//
Add the allocated resources of
//
current P to the available/work
//
resources i.e.free the resources
for
(
int
k = 0 ; k < R ;
k++)
work[k]
+= allot[p][k];
//
Add this process to safe sequence.
safeSeq[count++]
= p;
//
Mark this p as finished
finish[p]
= 1;
found
=
true
;
}
}
}
//
If we could not find a next process in safe
//
sequence.
if
(found ==
false
)
{
cout
<<
"System is not in safe
state"
;
return
false
;
}
}
// If system is in
safe state then
// safe sequence will
be as below
cout <<
"System is in safe state.\nSafe"
"
sequence is: "
;
for
(
int
i = 0; i < P ;
i++)
cout
<< safeSeq[i] <<
"
"
;
return
true
;
}