A1 1
A1 1
h>
#include<string.h>
#include<stdlib.h>
#define false 0
#define true 1
int alloc[10][10],max[10][10],need[10][10],m,n,i,j,avl[10],finish[10],req[10];
void compute_need()
{
printf("\n The Need Matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
printf("%d\t",need[i][j]);
}
printf("\n");
}
}
void accept_avl()
{
for(i=0;i<n;i++)
{
scanf_s("%d",&avl[i]);
}
}
void check_system()
{
int ans[5],cnt=0,flag=0;
for(i=0;i<m;i++)
finish[i]=false;
while(true)
{
flag=false;
for(i=0;i<m;i++)
if(finish[i]==0)
{
for(j=0;j<n;j++)
{
avl[j]=avl[j]+alloc[i][j];
}
}
else
{
printf("\nProcess %d can not be granted resources\
n",i);
}
}
if(flag==false)
break;
if(flag==false)
break;
}
flag=true;
for(i=0;i<m;i++)
if(finish[i]==0)
flag=false;
if(flag==1)
{
printf("\n\nSystem is in Safe State\n\n");
printf("\nSafe Sequence is as follows:");
for(i=0;i<cnt;i++)
{
printf("P%d \t",ans[i]);
}
printf("\n");
}
else
{
printf("\nSystem is not in the safe state");
}
main()
{
printf("\nEnter the no. of processes:-");
scanf_s("%d",&m);
printf("\nEnter the no. of resources:-");
scanf_s("%d",&n);
printf("\n Enter Allocation Matrix:");
accept_data(alloc);
printf("\nEnter the Max Matrix:");
accept_data(max);
printf("\nEnter the available:");
accept_avl();
compute_need();
check_system();
}