/*
Banker Algorithm ( Dynamic )
Author : Charudatta Ekbote ( Space Computer Education, SpaceComp Tutorials)
Note :
1) This program is developed to simulate banker's algorithm to demonstrate deadlock avoidance approach.
2) Program is based on input from user (dynamic data).
3) Program is not doing any validation-check on input data.
4) Program is desgined to work under ubuntu ( or any linux platform but not for window platform)
*/
#include <stdio.h>
#include <stdlib.h>
#define print 1
#define noprint 0
#define notfound 0
int **max,**allocation,**need;
int *available,*request,*finish;
int processes, resources;
void allocateMemory()
{
int i,j;
printf("enter number of processes : ");
scanf("%d",&processes);
printf("enter number of resources : ");
scanf("%d",&resources);
//request array memory allocation
request = malloc(sizeof(int) * resources );
//available array memory allocation
available = malloc(sizeof(int) * resources );
//need array memory allocation
need = malloc(sizeof(int*) * processes);
for(i=0; i<processes; ++i)
need[i] = malloc( sizeof(int) * resources );
//max array memory allocation
max = malloc(sizeof(int*) * processes);
for(i=0; i<processes; ++i)
max[i] = malloc( sizeof(int) * resources );
//allocation array memory allocation
allocation = malloc(sizeof(int*) * processes);
for(i=0; i<processes; ++i)
allocation[i] = malloc( sizeof(int) * resources );
//get inputs for max requirements
printf("\nenter contents for max requirement of each process\n");
for(i=0; i<processes; ++i)
{
for(j=0; j<resources; ++j)
{
printf("max requirement of process P%d for resource%d :",i,j);
scanf("%d",&max[i][j]);
}
printf("\n");
}
//get inputs for allocation requirements
printf("\nenter contents for current allocation of resources for each process\n");
for(i=0; i<processes; ++i)
{
for(j=0; j<resources; ++j)
{
printf("current allocation of process %d for resource%d :",i,j);
scanf("%d",&allocation[i][j]);
}
printf("\n");
}
//get inputs for availablity requirements
printf("\nenter contents for availability of resources\n");
for(j=0; j<resources; ++j)
{
printf("availability for resource%d :",j);
scanf("%d",&available[j]);
}
}
void saveResourceAllocationState()
{
FILE *fp;
fp = fopen("temp.bak","w");
fwrite(available,sizeof(int) * resources,1,fp);
fclose(fp);
}
void restoreResourceAllocationState()
{
FILE *fp;
fp = fopen("temp.bak","r");
fread(available,sizeof(int) * resources,1,fp);
fclose(fp);
}
void calculateNeedArray(int param)
{
int i,j;
if(param == print)
printf("\tR1\tR2\tR3\n");
for(i=0; i<processes; ++i)
{
printf("\t");
for(j=0; j<resources; ++j)
{
need[i][j] = max[i][j] - allocation[i][j];
if( param == print )
printf("%d\t",need[i][j]);
}
if( param == print )
printf("\n");
}
}
int isRequestValid(int i)
{
int j;
for(j=0; j<resources; ++j)
{
if( request[j] > need[i][j] || request[j] > available[j] )
return 0;
}
return 1;
}
int isNeedValid(int i)
{
int j;
for(j=0; j<resources; ++j)
{
if( need[i][j] > available[j] )
return 0;
}
return 1;
}
int safeSequence(int param)
{
int *finish;
int stockChanging, cnt=0, i ,j;
saveResourceAllocationState();
finish = calloc(sizeof(int), processes);
if( param == print )
printf("\t<");
do
{
stockChanging = 0;
for(i=0; i<processes; ++i)
{
if( finish[i] == 1 )
continue;
if( isNeedValid(i) == 1 )
{
for(j=0; j<resources; ++j)
available[j] += allocation[i][j];
finish[i] = 1;
++cnt;
stockChanging = 1;
if( param == print )
printf("P%d, ", i);
}
}
} while(stockChanging);
restoreResourceAllocationState();
if( cnt == processes )
{
if( param == print )
printf(">\n");
return 1;
}
else
{
printf("\n");
return 0;
}
}
int menu()
{
int ch;
system("clear");
printf("\n\t0) Exit");
printf("\n\t1) Accept Resource Request");
printf("\n\t2) Print Available Resources");
printf("\n\t3) Print Safe Sequence");
printf("\n\t4) Print Need Array");
printf("\n\t5) Print Resource Allocation State");
printf("\n\tEnter Choice : ");
scanf("%d",&ch);
return ch;//Banker Algorithm ( Dynamic )
//Author : Charudatta Ekbote ( Space Computer Education, SpaceComp Tutorials)
//Note : This program is developed to simulate banker's algorithm to demonstrate deadlock avoidance approach.
// Program is based on input from user (dynamic data).
// Program is not doing basic validations for max, allocation, available or request array inputs.
// Program is desgined to work under ubuntu ( or any linux platform but not for window platform)
#include <stdio.h>
#include <stdlib.h>
#define print 1
#define noprint 0
#define notfound 0
int **max,**allocation,**need;
int *available,*request,*finish;
int processes, resources;
void allocateMemory()
{
int i,j;
printf("enter number of processes : ");
scanf("%d",&processes);
printf("enter number of resources : ");
scanf("%d",&resources);
//request array memory allocation
request = malloc(sizeof(int) * resources );
//available array memory allocation
available = malloc(sizeof(int) * resources );
//need array memory allocation
need = malloc(sizeof(int*) * processes);
for(i=0; i<processes; ++i)
need[i] = malloc( sizeof(int) * resources );
//max array memory allocation
max = malloc(sizeof(int*) * processes);
for(i=0; i<processes; ++i)
max[i] = malloc( sizeof(int) * resources );
//allocation array memory allocation
allocation = malloc(sizeof(int*) * processes);
for(i=0; i<processes; ++i)
allocation[i] = malloc( sizeof(int) * resources );
//get inputs for max requirements
printf("\nenter contents for max requirement of each process\n");
for(i=0; i<processes; ++i)
{
for(j=0; j<resources; ++j)
{
printf("max requirement of process P%d for resource%d :",i,j);
scanf("%d",&max[i][j]);
}
printf("\n");
}
//get inputs for allocation requirements
printf("\nenter contents for current allocation of resources for each process\n");
for(i=0; i<processes; ++i)
{
for(j=0; j<resources; ++j)
{
printf("current allocation of process %d for resource%d :",i,j);
scanf("%d",&allocation[i][j]);
}
printf("\n");
}
//get inputs for availablity requirements
printf("\nenter contents for availability of resources\n");
for(j=0; j<resources; ++j)
{
printf("availability for resource%d :",j);
scanf("%d",&available[j]);
}
}
void saveResourceAllocationState()
{
FILE *fp;
fp = fopen("temp.bak","w");
fwrite(available,sizeof(int) * resources,1,fp);
fclose(fp);
}
void restoreResourceAllocationState()
{
FILE *fp;
fp = fopen("temp.bak","r");
fread(available,sizeof(int) * resources,1,fp);
fclose(fp);
}
void calculateNeedArray(int param)
{
int i,j;
if(param == print)
printf("\tR1\tR2\tR3\n");
for(i=0; i<processes; ++i)
{
printf("\t");
for(j=0; j<resources; ++j)
{
need[i][j] = max[i][j] - allocation[i][j];
if( param == print )
printf("%d\t",need[i][j]);
}
if( param == print )
printf("\n");
}
}
int isRequestValid(int i)
{
int j;
for(j=0; j<resources; ++j)
{
if( request[j] > need[i][j] || request[j] > available[j] )
return 0;
}
return 1;
}
int isNeedValid(int i)
{
int j;
for(j=0; j<resources; ++j)
{
if( need[i][j] > available[j] )
return 0;
}
return 1;
}
int safeSequence(int param)
{
int *finish;
int stockChanging, cnt=0, i ,j;
saveResourceAllocationState();
finish = calloc(sizeof(int), processes);
if( param == print )
printf("\t<");
do
{
stockChanging = 0;
for(i=0; i<processes; ++i)
{
if( finish[i] == 1 )
continue;
if( isNeedValid(i) == 1 )
{
for(j=0; j<resources; ++j)
available[j] += allocation[i][j];
finish[i] = 1;
++cnt;
stockChanging = 1;
if( param == print )
printf("P%d, ", i);
}
}
} while(stockChanging);
restoreResourceAllocationState();
if( cnt == processes )
{
if( param == print )
printf(">\n");
return 1;
}
else
{
printf("\n");
return 0;
}
}
int menu()
{
int ch;
system("clear");
printf("\n\t0) Exit");
printf("\n\t1) Accept Resource Request");
printf("\n\t2) Print Available Resources");
printf("\n\t3) Print Safe Sequence");
printf("\n\t4) Print Need Array");
printf("\n\t5) Print Resource Allocation State");
printf("\n\tEnter Choice : ");
scanf("%d",&ch);
return ch;
}
void printResourceAllocationState()
{
int r=10, c=5,temp,i,j;
printf("\033[%d;%df",r-1,c);
printf("max required");
for(i=0; i<processes; ++i)
{
c = 5;
for(j=0; j<resources; ++j)
{
printf("\033[%d;%df",r+i,c+=2);
printf("%d",max[i][j]);
}
printf("\n");
}
temp = c+10;
printf("\033[%d;%df",r-1,c+10);
printf("current allocated");
for(i=0; i<processes; ++i)
{
c = temp;
printf("\033[%d;%df",r+i,c);
for(j=0; j<resources; ++j)
{
printf("\033[%d;%df",r+i,c+=2);
printf("%d\t",allocation[i][j]);
}
printf("\n");
}
temp = c+20;
printf("\033[%d;%df",r-1,c+20);
printf("future need");
for(i=0; i<processes; ++i)
{
c = temp;
printf("\033[%d;%df",r+i,c);
for(j=0; j<resources; ++j)
{
printf("\033[%d;%df",r+i,c+=2);
printf("%d\t",max[i][j] - allocation[i][j]);
}
printf("\n");
}
}
main()
{
int ch,i,j;
allocateMemory();
calculateNeedArray(noprint);
if( safeSequence(noprint) == notfound )
{
printf("system is not in Safe State...exiting !");
return;
}
while((ch=menu())!=0)
{
if(ch==5)
{
printResourceAllocationState();
getchar();getchar();
}
else
if(ch==4)
{
calculateNeedArray(print);
getchar();getchar();
}
else
if(ch==3)
{
safeSequence(print);
getchar();getchar();
}
else
if(ch==2)
{
printf("\tR1\tR2\tR3\n");
for(j=0; j<resources; ++j)
printf("\t%d",available[j]);
getchar();getchar();
}
else
if(ch==1)
{
int i;
printf("\n\tenter process number : " );
scanf("%d",&i);
for( j=0; j<resources; ++j )
{
printf("\tresource_%d qty : ",j);
scanf("%d",&request[j]);
}
if( isRequestValid(i) )
{
for(j=0; j<resources; ++j)
{
available[j] -= request[j];
need[i][j] -= request[j];
allocation[i][j] += request[j];
}
if( safeSequence(noprint) == notfound )
{
printf("\tSorry, resources can not be given immediately !\n");
for(j=0; j<resources; ++j)
{
available[j] += request[j];
need[i][j] += request[j];
allocation[i][j] -= request[j];
}
}
else
printf("\tCongratulations process P%d got the resources\n",i);
}
else
printf("\tOops, request is not valid, can not be completed");
getchar();getchar();
}
}
}
}
void printResourceAllocationState()
{
int r=10, c=5,temp,i,j;
printf("\033[%d;%df",r-1,c);
printf("max required");
for(i=0; i<processes; ++i)
{
c = 5;
for(j=0; j<resources; ++j)
{
printf("\033[%d;%df",r+i,c+=2);
printf("%d",max[i][j]);
}
printf("\n");
}
temp = c+10;
printf("\033[%d;%df",r-1,c+10);
printf("current allocated");
for(i=0; i<processes; ++i)
{
c = temp;
printf("\033[%d;%df",r+i,c);
for(j=0; j<resources; ++j)
{
printf("\033[%d;%df",r+i,c+=2);
printf("%d\t",allocation[i][j]);
}
printf("\n");
}
temp = c+20;
printf("\033[%d;%df",r-1,c+20);
printf("future need");
for(i=0; i<processes; ++i)
{
c = temp;
printf("\033[%d;%df",r+i,c);
for(j=0; j<resources; ++j)
{
printf("\033[%d;%df",r+i,c+=2);
printf("%d\t",max[i][j] - allocation[i][j]);
}
printf("\n");
}
}
main()
{
int ch,i,j;
allocateMemory();
calculateNeedArray(noprint);
if( safeSequence(noprint) == notfound )
{
printf("system is not in Safe State...exiting !");
return;
}
while((ch=menu())!=0)
{
if(ch==5)
{
printResourceAllocationState();
getchar();getchar();
}
else
if(ch==4)
{
calculateNeedArray(print);
getchar();getchar();
}
else
if(ch==3)
{
safeSequence(print);
getchar();getchar();
}
else
if(ch==2)
{
printf("\tR1\tR2\tR3\n");
for(j=0; j<resources; ++j)
printf("\t%d",available[j]);
getchar();getchar();
}
else
if(ch==1)
{
int i;
printf("\n\tenter process number : " );
scanf("%d",&i);
for( j=0; j<resources; ++j )
{
printf("\tresource_%d qty : ",j);
scanf("%d",&request[j]);
}
if( isRequestValid(i) )
{
for(j=0; j<resources; ++j)
{
available[j] -= request[j];
need[i][j] -= request[j];
allocation[i][j] += request[j];
}
if( safeSequence(noprint) == notfound )
{
printf("\tSorry, resources can not be given immediately !\n");
for(j=0; j<resources; ++j)
{
available[j] += request[j];
need[i][j] += request[j];
allocation[i][j] -= request[j];
}
}
else
printf("\tCongratulations process P%d got the resources\n",i);
}
else
printf("\tOops, request is not valid, can not be completed");
getchar();getchar();
}
}
}
Banker Algorithm ( Dynamic )
Author : Charudatta Ekbote ( Space Computer Education, SpaceComp Tutorials)
Note :
1) This program is developed to simulate banker's algorithm to demonstrate deadlock avoidance approach.
2) Program is based on input from user (dynamic data).
3) Program is not doing any validation-check on input data.
4) Program is desgined to work under ubuntu ( or any linux platform but not for window platform)
*/
#include <stdio.h>
#include <stdlib.h>
#define print 1
#define noprint 0
#define notfound 0
int **max,**allocation,**need;
int *available,*request,*finish;
int processes, resources;
void allocateMemory()
{
int i,j;
printf("enter number of processes : ");
scanf("%d",&processes);
printf("enter number of resources : ");
scanf("%d",&resources);
//request array memory allocation
request = malloc(sizeof(int) * resources );
//available array memory allocation
available = malloc(sizeof(int) * resources );
//need array memory allocation
need = malloc(sizeof(int*) * processes);
for(i=0; i<processes; ++i)
need[i] = malloc( sizeof(int) * resources );
//max array memory allocation
max = malloc(sizeof(int*) * processes);
for(i=0; i<processes; ++i)
max[i] = malloc( sizeof(int) * resources );
//allocation array memory allocation
allocation = malloc(sizeof(int*) * processes);
for(i=0; i<processes; ++i)
allocation[i] = malloc( sizeof(int) * resources );
//get inputs for max requirements
printf("\nenter contents for max requirement of each process\n");
for(i=0; i<processes; ++i)
{
for(j=0; j<resources; ++j)
{
printf("max requirement of process P%d for resource%d :",i,j);
scanf("%d",&max[i][j]);
}
printf("\n");
}
//get inputs for allocation requirements
printf("\nenter contents for current allocation of resources for each process\n");
for(i=0; i<processes; ++i)
{
for(j=0; j<resources; ++j)
{
printf("current allocation of process %d for resource%d :",i,j);
scanf("%d",&allocation[i][j]);
}
printf("\n");
}
//get inputs for availablity requirements
printf("\nenter contents for availability of resources\n");
for(j=0; j<resources; ++j)
{
printf("availability for resource%d :",j);
scanf("%d",&available[j]);
}
}
void saveResourceAllocationState()
{
FILE *fp;
fp = fopen("temp.bak","w");
fwrite(available,sizeof(int) * resources,1,fp);
fclose(fp);
}
void restoreResourceAllocationState()
{
FILE *fp;
fp = fopen("temp.bak","r");
fread(available,sizeof(int) * resources,1,fp);
fclose(fp);
}
void calculateNeedArray(int param)
{
int i,j;
if(param == print)
printf("\tR1\tR2\tR3\n");
for(i=0; i<processes; ++i)
{
printf("\t");
for(j=0; j<resources; ++j)
{
need[i][j] = max[i][j] - allocation[i][j];
if( param == print )
printf("%d\t",need[i][j]);
}
if( param == print )
printf("\n");
}
}
int isRequestValid(int i)
{
int j;
for(j=0; j<resources; ++j)
{
if( request[j] > need[i][j] || request[j] > available[j] )
return 0;
}
return 1;
}
int isNeedValid(int i)
{
int j;
for(j=0; j<resources; ++j)
{
if( need[i][j] > available[j] )
return 0;
}
return 1;
}
int safeSequence(int param)
{
int *finish;
int stockChanging, cnt=0, i ,j;
saveResourceAllocationState();
finish = calloc(sizeof(int), processes);
if( param == print )
printf("\t<");
do
{
stockChanging = 0;
for(i=0; i<processes; ++i)
{
if( finish[i] == 1 )
continue;
if( isNeedValid(i) == 1 )
{
for(j=0; j<resources; ++j)
available[j] += allocation[i][j];
finish[i] = 1;
++cnt;
stockChanging = 1;
if( param == print )
printf("P%d, ", i);
}
}
} while(stockChanging);
restoreResourceAllocationState();
if( cnt == processes )
{
if( param == print )
printf(">\n");
return 1;
}
else
{
printf("\n");
return 0;
}
}
int menu()
{
int ch;
system("clear");
printf("\n\t0) Exit");
printf("\n\t1) Accept Resource Request");
printf("\n\t2) Print Available Resources");
printf("\n\t3) Print Safe Sequence");
printf("\n\t4) Print Need Array");
printf("\n\t5) Print Resource Allocation State");
printf("\n\tEnter Choice : ");
scanf("%d",&ch);
return ch;//Banker Algorithm ( Dynamic )
//Author : Charudatta Ekbote ( Space Computer Education, SpaceComp Tutorials)
//Note : This program is developed to simulate banker's algorithm to demonstrate deadlock avoidance approach.
// Program is based on input from user (dynamic data).
// Program is not doing basic validations for max, allocation, available or request array inputs.
// Program is desgined to work under ubuntu ( or any linux platform but not for window platform)
#include <stdio.h>
#include <stdlib.h>
#define print 1
#define noprint 0
#define notfound 0
int **max,**allocation,**need;
int *available,*request,*finish;
int processes, resources;
void allocateMemory()
{
int i,j;
printf("enter number of processes : ");
scanf("%d",&processes);
printf("enter number of resources : ");
scanf("%d",&resources);
//request array memory allocation
request = malloc(sizeof(int) * resources );
//available array memory allocation
available = malloc(sizeof(int) * resources );
//need array memory allocation
need = malloc(sizeof(int*) * processes);
for(i=0; i<processes; ++i)
need[i] = malloc( sizeof(int) * resources );
//max array memory allocation
max = malloc(sizeof(int*) * processes);
for(i=0; i<processes; ++i)
max[i] = malloc( sizeof(int) * resources );
//allocation array memory allocation
allocation = malloc(sizeof(int*) * processes);
for(i=0; i<processes; ++i)
allocation[i] = malloc( sizeof(int) * resources );
//get inputs for max requirements
printf("\nenter contents for max requirement of each process\n");
for(i=0; i<processes; ++i)
{
for(j=0; j<resources; ++j)
{
printf("max requirement of process P%d for resource%d :",i,j);
scanf("%d",&max[i][j]);
}
printf("\n");
}
//get inputs for allocation requirements
printf("\nenter contents for current allocation of resources for each process\n");
for(i=0; i<processes; ++i)
{
for(j=0; j<resources; ++j)
{
printf("current allocation of process %d for resource%d :",i,j);
scanf("%d",&allocation[i][j]);
}
printf("\n");
}
//get inputs for availablity requirements
printf("\nenter contents for availability of resources\n");
for(j=0; j<resources; ++j)
{
printf("availability for resource%d :",j);
scanf("%d",&available[j]);
}
}
void saveResourceAllocationState()
{
FILE *fp;
fp = fopen("temp.bak","w");
fwrite(available,sizeof(int) * resources,1,fp);
fclose(fp);
}
void restoreResourceAllocationState()
{
FILE *fp;
fp = fopen("temp.bak","r");
fread(available,sizeof(int) * resources,1,fp);
fclose(fp);
}
void calculateNeedArray(int param)
{
int i,j;
if(param == print)
printf("\tR1\tR2\tR3\n");
for(i=0; i<processes; ++i)
{
printf("\t");
for(j=0; j<resources; ++j)
{
need[i][j] = max[i][j] - allocation[i][j];
if( param == print )
printf("%d\t",need[i][j]);
}
if( param == print )
printf("\n");
}
}
int isRequestValid(int i)
{
int j;
for(j=0; j<resources; ++j)
{
if( request[j] > need[i][j] || request[j] > available[j] )
return 0;
}
return 1;
}
int isNeedValid(int i)
{
int j;
for(j=0; j<resources; ++j)
{
if( need[i][j] > available[j] )
return 0;
}
return 1;
}
int safeSequence(int param)
{
int *finish;
int stockChanging, cnt=0, i ,j;
saveResourceAllocationState();
finish = calloc(sizeof(int), processes);
if( param == print )
printf("\t<");
do
{
stockChanging = 0;
for(i=0; i<processes; ++i)
{
if( finish[i] == 1 )
continue;
if( isNeedValid(i) == 1 )
{
for(j=0; j<resources; ++j)
available[j] += allocation[i][j];
finish[i] = 1;
++cnt;
stockChanging = 1;
if( param == print )
printf("P%d, ", i);
}
}
} while(stockChanging);
restoreResourceAllocationState();
if( cnt == processes )
{
if( param == print )
printf(">\n");
return 1;
}
else
{
printf("\n");
return 0;
}
}
int menu()
{
int ch;
system("clear");
printf("\n\t0) Exit");
printf("\n\t1) Accept Resource Request");
printf("\n\t2) Print Available Resources");
printf("\n\t3) Print Safe Sequence");
printf("\n\t4) Print Need Array");
printf("\n\t5) Print Resource Allocation State");
printf("\n\tEnter Choice : ");
scanf("%d",&ch);
return ch;
}
void printResourceAllocationState()
{
int r=10, c=5,temp,i,j;
printf("\033[%d;%df",r-1,c);
printf("max required");
for(i=0; i<processes; ++i)
{
c = 5;
for(j=0; j<resources; ++j)
{
printf("\033[%d;%df",r+i,c+=2);
printf("%d",max[i][j]);
}
printf("\n");
}
temp = c+10;
printf("\033[%d;%df",r-1,c+10);
printf("current allocated");
for(i=0; i<processes; ++i)
{
c = temp;
printf("\033[%d;%df",r+i,c);
for(j=0; j<resources; ++j)
{
printf("\033[%d;%df",r+i,c+=2);
printf("%d\t",allocation[i][j]);
}
printf("\n");
}
temp = c+20;
printf("\033[%d;%df",r-1,c+20);
printf("future need");
for(i=0; i<processes; ++i)
{
c = temp;
printf("\033[%d;%df",r+i,c);
for(j=0; j<resources; ++j)
{
printf("\033[%d;%df",r+i,c+=2);
printf("%d\t",max[i][j] - allocation[i][j]);
}
printf("\n");
}
}
main()
{
int ch,i,j;
allocateMemory();
calculateNeedArray(noprint);
if( safeSequence(noprint) == notfound )
{
printf("system is not in Safe State...exiting !");
return;
}
while((ch=menu())!=0)
{
if(ch==5)
{
printResourceAllocationState();
getchar();getchar();
}
else
if(ch==4)
{
calculateNeedArray(print);
getchar();getchar();
}
else
if(ch==3)
{
safeSequence(print);
getchar();getchar();
}
else
if(ch==2)
{
printf("\tR1\tR2\tR3\n");
for(j=0; j<resources; ++j)
printf("\t%d",available[j]);
getchar();getchar();
}
else
if(ch==1)
{
int i;
printf("\n\tenter process number : " );
scanf("%d",&i);
for( j=0; j<resources; ++j )
{
printf("\tresource_%d qty : ",j);
scanf("%d",&request[j]);
}
if( isRequestValid(i) )
{
for(j=0; j<resources; ++j)
{
available[j] -= request[j];
need[i][j] -= request[j];
allocation[i][j] += request[j];
}
if( safeSequence(noprint) == notfound )
{
printf("\tSorry, resources can not be given immediately !\n");
for(j=0; j<resources; ++j)
{
available[j] += request[j];
need[i][j] += request[j];
allocation[i][j] -= request[j];
}
}
else
printf("\tCongratulations process P%d got the resources\n",i);
}
else
printf("\tOops, request is not valid, can not be completed");
getchar();getchar();
}
}
}
void printResourceAllocationState()
{
int r=10, c=5,temp,i,j;
printf("\033[%d;%df",r-1,c);
printf("max required");
for(i=0; i<processes; ++i)
{
c = 5;
for(j=0; j<resources; ++j)
{
printf("\033[%d;%df",r+i,c+=2);
printf("%d",max[i][j]);
}
printf("\n");
}
temp = c+10;
printf("\033[%d;%df",r-1,c+10);
printf("current allocated");
for(i=0; i<processes; ++i)
{
c = temp;
printf("\033[%d;%df",r+i,c);
for(j=0; j<resources; ++j)
{
printf("\033[%d;%df",r+i,c+=2);
printf("%d\t",allocation[i][j]);
}
printf("\n");
}
temp = c+20;
printf("\033[%d;%df",r-1,c+20);
printf("future need");
for(i=0; i<processes; ++i)
{
c = temp;
printf("\033[%d;%df",r+i,c);
for(j=0; j<resources; ++j)
{
printf("\033[%d;%df",r+i,c+=2);
printf("%d\t",max[i][j] - allocation[i][j]);
}
printf("\n");
}
}
main()
{
int ch,i,j;
allocateMemory();
calculateNeedArray(noprint);
if( safeSequence(noprint) == notfound )
{
printf("system is not in Safe State...exiting !");
return;
}
while((ch=menu())!=0)
{
if(ch==5)
{
printResourceAllocationState();
getchar();getchar();
}
else
if(ch==4)
{
calculateNeedArray(print);
getchar();getchar();
}
else
if(ch==3)
{
safeSequence(print);
getchar();getchar();
}
else
if(ch==2)
{
printf("\tR1\tR2\tR3\n");
for(j=0; j<resources; ++j)
printf("\t%d",available[j]);
getchar();getchar();
}
else
if(ch==1)
{
int i;
printf("\n\tenter process number : " );
scanf("%d",&i);
for( j=0; j<resources; ++j )
{
printf("\tresource_%d qty : ",j);
scanf("%d",&request[j]);
}
if( isRequestValid(i) )
{
for(j=0; j<resources; ++j)
{
available[j] -= request[j];
need[i][j] -= request[j];
allocation[i][j] += request[j];
}
if( safeSequence(noprint) == notfound )
{
printf("\tSorry, resources can not be given immediately !\n");
for(j=0; j<resources; ++j)
{
available[j] += request[j];
need[i][j] += request[j];
allocation[i][j] -= request[j];
}
}
else
printf("\tCongratulations process P%d got the resources\n",i);
}
else
printf("\tOops, request is not valid, can not be completed");
getchar();getchar();
}
}
}
No comments:
Post a Comment