Tuesday, 28 February 2017

Banker Algorithm (Static Code for Slip 8, 23 and 24)

/*
Banker Algorithm (Static i.e. user need not input values)
Author : Charudatta Ekbote ( Space Computer Education, SpaceComp Tutorials)
Note about program :
1) This program is developed to simulate banker's algorithm to demonstrate deadlock avoidance approach.
2) Program is using some static data for 5 processes and 3 resources.
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>
#define processes 5
#define resources 3
#define print 1
#define noprint 0
#define notfound 0

int max[processes][resources] = { {7,5,3}, {3,2,2}, {9,0,2}, {2,2,2}, {4,3,3} };
int allocation[processes][resources] = { {0,1,0}, {2,0,0}, {3,0,2}, {2,1,1}, {0,0,2} };
int need[processes][resources];
int available[resources] = { 3,3,2 };
int request[resources] = { 0,0,0 };

void saveResourceAllocationState()
{
FILE *fp;
fp = fopen("temp.bak","w");
fwrite(available,sizeof(available),1,fp);
fclose(fp);
}

void restoreResourceAllocationState()
{
FILE *fp;
fp = fopen("temp.bak","r");
fread(available,sizeof(available),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[processes] = {0,0,0,0,0};
int stockChanging, cnt=0, i ,j;
saveResourceAllocationState();

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;
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();
}33
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