Simulate Bankers Algorithm for Dead Lock Avoidance
#include<stdio.h>
#include<conio.h>#include<math.h>
void main()
{
int seq[10],m,n,i,j,alloc[10][10],max[10][10],need[10][10]={0},avai[10];
int finish[10],flag, npleft,c,k=0;
printf("enter types of resources : ");
scanf("%d",&m);
printf("enter available resources instance of %d types resources\n", m);
for(i=0;i<m;i++)
scanf("%d",&avai[i]);
printf("enter number of processes : ");
scanf("%d",&n);
printf("enter allocation matrix");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("enter maximum resource matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
scanf("%d",&max[i][j]);
}
printf("the given allocation matrix is\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\n");
}
printf("the need matrix is\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
printf("%d ",need[i][j]);
}
printf("\n");
}
printf("the available resource vector is : ");
for(i=0;i<m;i++)
printf("%d ",avai[i]);
for(i=0;i<n;i++)
finish[i]=0;
npleft=n;
while(npleft!=0)
{
for(i=0;i<n;i++)
{
c=0;
if(finish[i]==0)
{
for(j=0;j<m;j++)
{
if(need[i][j]<=avai[j])
c++;
}
if(c==m)
{
finish[i]=1;
for(j=0;j<m;j++)
avai[j]=avai[j]+alloc[i][j];
seq[k++]=i;
}
}
}
npleft--;
}
flag=0;
for(i=0;i<n;i++)
{
if(finish[i]==0)
flag=1;
}
if(flag==1)
printf("\nthe system is in unsafe state");
else
{
printf("\nsystem is in safe sate and safe sequence is\n");
for(i=0;i<n;i++)
printf("p - %d\t",seq[i]);
printf("\n");
}
}
OUTPUT:
Enter types of resources : 3
Enter available resources instance of 3 types resources
3 3 2
Enter number of processes : 5
Enter allocation matrix
0 1 0
2 0 0
3 0 3
2 1 1
0 0 2
Enter maximum resource matrix
7 5 3
3 2 2
9 0 2
2 2 2
1 3 3
The given allocation matrix is
0 1 0
2 0 0
3 0 3
2 1 1
0 0 2
The need matrix is
7 4 3
1 2 2
6 0 -1
0 1 1
1 3 1
The available resource vector is : 3 3 2
System is in safe sate and safe sequence is
p - 1 p - 3 p - 4 p - 0 p - 2
Comments
Post a Comment