Banker's Algorithm For Deadlock Avoidance

#include <stdio.h>
main()
{
int Max[10][10],Alloc[10][10],Need[10][10],Avail[10], Check[10], SafeSequence[10];
int i,j,p,r,c=1,ch;
printf("Enter the no. of Processes : ");
scanf("%d",&p);
for(i=1;i<=p;i++)
Check[i] = 0;
printf("\n\nEnter the no. of Resources : ");
scanf("%d",&r);
printf("\n\nEnter the Max Resources instances : ");
for(i =1; i <=r; i++)
scanf("%d",&Avail[i]);
printf("\n\nEnter the Max Allocation for each process : \n");
for(i =1; i <=p; i++)
{
printf("\nprocess %d : ", i);
for(j =1; j <=r; j++)
scanf("%d", &Max[i][j]);
}
printf("\n\nEnter the Allocation for each process : \n");
for(i = 1; i <=p; i++)
{
printf("\nprocess %d : ",i );
for(j =1; j <=r; j++)
scanf("%d", &Alloc[i][j]);
}
for(i = 1; i <=p; i++)
{
for(j =1; j <=r; j++)
Avail[j]-=Alloc[i][j];
}
for(i = 1; i <=p; i++)
for(j = 1;j <=r;j++)
Need[i][j]=(Max[i][j]-Alloc[i][j]);
do
{
printf("\n Max Allocation:\t  Allocated:\t  Need:\t   \t Available:\n");

for(i = 1; i <=p; i++)
{
printf("\n");
for( j =1; j <=r; j++)
printf("  %d ",Max[i][j]);
printf("\t\t");
for( j = 1; j <=r; j++)
printf("  %d ",Alloc[i][j]);
printf("\t");
for( j = 1; j <=r; j++)
printf("  %d ",Need[i][j]);
printf("\t");
}
for( j = 1; j <=r;j++)
printf("  %d ",Avail[j]);
printf("\n");
ch=0;
for(i = 1; i <=p; i++)
{
if(Check[i] == 0)
{
ch=i ;
for(j = 1; j <=r; j++)
{
if(Avail[j] < Need[i][j])
{
ch=0;
break;
}
}
}
if(ch!=0)
break;
}
if(ch!= 0)
{
printf("\nProcess %d completed\n", i);
SafeSequence[c] = i;
c++;

            for(j = 1; j <=r; j++)
{
Avail[j] += Alloc[i][j];
Alloc[i][j] = 0;
Max[i][j] = 0;
Need[i][j] = 0;
Check[i] = 1;
}
}
else if(i==p+1)
                break;

}while(c<=p);
if(c== p+1)
{
printf("\n\nThe system is in a Safe State\n");
printf("\n\nSafe Sequence :  ");
for( i = 1; i <=p; i++)
printf("%d ", SafeSequence[i]);
}
else
printf("\n\nThe System is in unsafe State\n");

}



SHARE

About Santhosh

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment