Please write a program that can examine the Banker’s algorithm to check a reques
ID: 3874692 • Letter: P
Question
Please write a program that can examine the Banker’s algorithm to check a request. When the system allocates the resource according to the request and the system is in a safe state, the request will be accepted.
The sample program can check only one request. Please write a program( in C ) to check a serial of requests with different command types:
Sample codes
#include
#define MAXP 10
#define MAXR 10
int maximal[MAXP][MAXR],allocation[MAXP][MAXR],available[MAXR];
int command[MAXR+2];
//command[0]:cmd type; [1]:process id; [2]-[n]:resource type
//command type 0: finish 1:request 2:release
int p,r; //p: amout of process; r: amount of resource
void set_init()
{
FILE *init;
int i,j;
if((init=fopen("init.txt","r"))<0)
{
perror("init file open error");
return;
}
fscanf(init,"%d",&p);
fscanf(init,"%d",&r);
printf("%d %d ",p,r);
for (i=0;i
{
for (j=0;j
{
fscanf(init,"%d",&allocation[i][j]);
}
}
for (i=0;i
{
for (j=0;j
{
fscanf(init,"%d",&maximal[i][j]);
}
}
for (j=0;j
{
fscanf(init,"%d",&available[j]);
}
fclose(init);
}
void get_command()
{
FILE *cmd;
int i;
if((cmd=fopen("cmd.txt","r"))<0)
{
perror("command file open error");
return;
}
printf("command");
for (i=0;i
{
fscanf(cmd,"%d",&command[i]);
printf("[%d]",command[i]);
}
printf(" ");
}
init.txt
5 3
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
3 3 2
cmd.txt
1 4 3 3 0
Explanation / Answer
#include <stdio.h>
int current[5][5], maximum_claim[5][5], available[5];
int allocation[5] = {0, 0, 0, 0, 0};
int maxres[5], running[5], safe = 0;
int counter = 0, i, j, exec, resources, processes, k = 1;
int main()
{
printf(" **Enter number of processes you want:** ");
scanf("%d", &processes);
for (i = 0; i < processes; i++)
{
running[i] = 1;
counter++;
}
printf(" Enter number of resources: ");
scanf("%d", &resources);
printf(" Enter Claim Vector:");
for (i = 0; i < resources; i++)
{
scanf("%d", &maxres[i]);
}
printf(" ** Kindly enter the allocated Resource Table:** ");
for (i = 0; i < processes; i++)
{
for(j = 0; j < resources; j++)
{
scanf("%d", ¤t[i][j]);
}
}
printf(" **Kindly Enter Maximum Claim Table:** ");
for (i = 0; i < processes; i++)
{
for(j = 0; j < resources; j++)
{
scanf("%d", &maximum_claim[i][j]);
}
}
printf(" **The Claim Vector is:** ");
for (i = 0; i < resources; i++)
{
printf(" %d", maxres[i]);
}
printf(" **The Allocated Resource Table:** ");
for (i = 0; i < processes; i++)
{
for (j = 0; j < resources; j++)
{
printf(" %d", current[i][j]);
}
printf(" ");
}
printf(" **The Maximum Claim Table:** ");
for (i = 0; i < processes; i++)
{
for (j = 0; j < resources; j++)
{
printf(" %d", maximum_claim[i][j]);
}
printf(" ");
}
for (i = 0; i < processes; i++)
{
for (j = 0; j < resources; j++)
{
allocation[j] += current[i][j];
}
}
printf(" **The Allocated resources are:**");
for (i = 0; i < resources; i++)
{
printf(" %d", allocation[i]);
}
for (i = 0; i < resources; i++)
{
available[i] = maxres[i] - allocation[i];
}
printf(" **The Available resources are:**");
for (i = 0; i < resources; i++)
{
printf(" %d", available[i]);
}
printf(" ");
while (counter != 0)
{
safe = 0;
for (i = 0; i < processes; i++)
{
if (running[i])
{
exec = 1;
for (j = 0; j < resources; j++)
{
if (maximum_claim[i][j] - current[i][j] > available[j])
{
exec = 0;
break;
}
}
if (exec)
{
printf(" Now the Process%d is executing ", i + 1);
running[i] = 0;
counter--;
safe = 1;
for (j = 0; j < resources; j++)
{
available[j] += current[i][j];
}
break;
}
}
}
if (!safe)
{
printf(" The processes are in unsafe state and therefore it cant be accepted ");
break;
}
else
{
printf(" The process is in safe state");
printf(" For these processes the Available vector is:");
for (i = 0; i < resources; i++)
{
printf(" %d", available[i]);
}
printf(" ");
}
}
return 0;
}