You are on page 1of 3

#include true 1

#include false 0
#include<stdio.h>
int m=0,n=0,avail[10],i=0,alloc[10][10],j=0,max[10][10],work[10],finish[10],need
[10][10],count=0,process=0,sequence[10],a=0;
void init()
{
printf("\n enter the no of processes");
scanf("%d",&m);
printf("\n enter the n of resources");
scanf("%d",&n);
printf("\n enter available");
readvector(avail);
printf("\n enter allocation");
readmatrix(alloc);
printf("\n enter max");
readmatrix(max);
for(i=0;i<n;i++)
{
work[i]=avail[i];
}
for(i=0;i<m;i++)
{
finish[i]=false;
}
}
void readmatrix(int t[][])
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&t[i][j]);
}
}
}
void readvector(int v[])
{
for(i=0;i<n;i++)
{
scanf("%d",&v[i]);
}
}
void findneed()
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
printmatrix(need);
}
void printmatrix(int t[][])
{

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d \t",t[i][j]);
}
printf("\n");
}
}
void selectprocess()
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(need[i][j]<=work[j])
flag=1;
else
{
flag=0;
break;
}
}
if(finish[i]=false && flag==1)
{
process=i;
count++;
break;
}
}
if(flag==0)
{
printf("system unsafe\n");
exit(1);
}
printf("\n sytem safe);
}
void executeprocess(int p)
{
printf("%d execute",p);
sequence[a]=p;
a++;
}
void releaseresource()
{
for(i=0;i<n;i++)
{
work[i]=work[i]+alloc[process[i]];
}
}
void main()
{

init();
findneed();
do
{
selectprocess();
executepocess(process);
releaseresource();
finish[process]=true;
} while(count<m);
for(i=0;i<m;i++)
printf("\n p%d \t",sequence[i]);
}

You might also like