#include<stdio.h>
#include<conio.h>
struct
process
{
int
flag,max[10],alloc[10],need[10];
}p[10];
void
main()
{
int
i,j,ch,temp[10],sum,avail[10],tot[10],n,m,x,c=0,k=0,a=0;
clrscr();
printf("enter
no of process");
scanf("%d",&n);
printf("enter
no of resources");
scanf("%d",&m);
printf("enter
max no.of resources needed by process");
for(i=0;i<n;i++)
{
printf("\n
p[%d]:",i);
for(j=0;j<m;j++)
scanf("%d",&p[i].max[j]);
}
printf("enter
the no.of resources allocated to process");
for(i=0;i<n;i++)
{
printf("\n
p[%d]:",i);
for(j=0;j<m;j++)
scanf("%d",&p[i].alloc[j]);
}
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
p[i].need[j]=p[i].max[j]-p[i].alloc[j];
p[i].flag=0;
}
printf("no.of
resources needed by process");
for(i=0;i<n;i++)
{
printf("\n
p[%d]:",i);
for(j=0;j<m;j++)
printf("%d",p[i].need[j]);
printf("\n");
}
printf("total
no of resources");
for(j=0;j<m;j++)
scanf("%d",&tot[j]);
for(j=0;j<m;j++)
{
sum=0;
for(i=0;i<n;i++)
sum=sum+p[i].alloc[j];
avail[j]=tot[j]-sum;
}
printf("available
resources");
for(j=0;j<m;j++)
printf("%d",avail[j]);
while(a<n)
{
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
if((p[i].need[j]<=avail[j]&&(p[i].flag==0)))
c++;
if(c==m)
{
p[i].flag=1;
for(j=0;j<m;j++)
avail[j]=avail[j]+p[i].alloc[j];
printf("p%d->",i);
}
c=0;
}
a++;
}
for(i=0;i<n;i++)
if(p[i].flag==1)
k++;
if(k==n)
printf("system
is in safe state");
else
printf("unsafe
state");
getch();
}
కామెంట్లు లేవు:
కామెంట్ను పోస్ట్ చేయండి