#include<stdio.h>
int main()
{
int n,p,i,j,complete=0,c,cnt=0,mx[10][3],all[10][3],need[10][3],av[3],done[10];
char safe[10],name[10];
printf("\nEnter the num of process:");
scanf("%d",&n);
printf("\nEnter the num of resources:");
scanf("%d",&p);
for(i=0;i<n;i++)
{
printf("\nEnter the name of process:");
fflush(stdin);
scanf("%c",&name[i]);
done[i]=0;
printf("\nEnter details of process %d",i+1);
for(j=0;j<p;j++)
{
printf("\nEnter max resource %c:",65+j);
scanf("%d",&mx[i][j]);
printf("\nEnter allocated resource %c:",65+j);
scanf("%d",&all[i][j]);
need[i][j]=mx[i][j]-all[i][j];
}
}
for(i=0;i<p;i++)
{
printf("\nEnter the available resource %c:",65+i);
scanf("%d",&av[i]);
}
while(complete!=n && cnt!=n)
{
for(i=0;i<n;i++)
{
cnt++;
if(done[i]!=2)
{
c=0;
for(j=0;j<p;j++)
{
if(need[i][j]<=av[j])
{c++;}
}
if(c==p)
{
for(j=0;j<p;j++)
av[j]+=all[i][j];
safe[complete]=name[i];
complete++;
done[i]=2;
}
}
}
}
if(cnt!=n)
{
printf("\n\nSafe Sequence-->> ");
for(i=0;i<n;i++)
printf("| %c |",safe[i]);
}
else
{
printf("\nNo safe sequence!!");
}
return 0;
int main()
{
int n,p,i,j,complete=0,c,cnt=0,mx[10][3],all[10][3],need[10][3],av[3],done[10];
char safe[10],name[10];
printf("\nEnter the num of process:");
scanf("%d",&n);
printf("\nEnter the num of resources:");
scanf("%d",&p);
for(i=0;i<n;i++)
{
printf("\nEnter the name of process:");
fflush(stdin);
scanf("%c",&name[i]);
done[i]=0;
printf("\nEnter details of process %d",i+1);
for(j=0;j<p;j++)
{
printf("\nEnter max resource %c:",65+j);
scanf("%d",&mx[i][j]);
printf("\nEnter allocated resource %c:",65+j);
scanf("%d",&all[i][j]);
need[i][j]=mx[i][j]-all[i][j];
}
}
for(i=0;i<p;i++)
{
printf("\nEnter the available resource %c:",65+i);
scanf("%d",&av[i]);
}
while(complete!=n && cnt!=n)
{
for(i=0;i<n;i++)
{
cnt++;
if(done[i]!=2)
{
c=0;
for(j=0;j<p;j++)
{
if(need[i][j]<=av[j])
{c++;}
}
if(c==p)
{
for(j=0;j<p;j++)
av[j]+=all[i][j];
safe[complete]=name[i];
complete++;
done[i]=2;
}
}
}
}
if(cnt!=n)
{
printf("\n\nSafe Sequence-->> ");
for(i=0;i<n;i++)
printf("| %c |",safe[i]);
}
else
{
printf("\nNo safe sequence!!");
}
return 0;
}