#include<stdio.h>
static int visited[20];
int i,j,np,nr;
int canbeprocessed(int x[],int y[],int z[],int avail[])
{
for(j=0;j<nr;j++)
if(x[j]>avail[j])
return 0;
for(j=0;j<nr;j++)
{
avail[j]+=y[j];
y[j]=z[j]=0;
}
return 1;
}
int safe(int request[10][10],int avail[10],int alloc[10][10],int claim[10][10])
{
int count=0;
printf("\nSafe State Sequence: ");
while(count<np)
{
for(i=0;i<np;i++)
{
if((visited[i]==0) && canbeprocessed(request[i],alloc[i],claim[i],avail))
{
count++;
visited[i]=1;
printf("P%d\t",i+1);
break; //if found break loop
}
}
if(i==np) // all processes found to be not suitable for execution
return 0;
}
return 1;
}
int main()
{
int alloc[10][10],claim[10][10],request[10][10],avail[10],r[10];
printf("\nEnter the no of process: ");
scanf("%d",&np);
printf("\nEnter the no of resources: ");
scanf("%d",&nr);
for(i=0;i<nr;i++)
{
printf("\nTotal Amount of the Resource R%d: ",i+1);
scanf("%d",&r[i]);
}
printf("\nEnter the allocation matrix:");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&alloc[i][j]);
printf("\nEnter the claim matrix:");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&claim[i][j]);
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
request[i][j]=claim[i][j]-alloc[i][j];
/*Available Resource calculation*/
for(j=0;j<nr;j++)
{
avail[j]=r[j];
for(i=0;i<np;i++)
{
avail[j]-=alloc[i][j];
}
}
/*Available Resource Calculation ends*/
if(safe(request,avail,alloc,claim))
printf("\n\nConclusion: System is in safe state ");
else
printf("\n\nConclusion: The system cannot acheive safe state" );
return 0;
}
static int visited[20];
int i,j,np,nr;
int canbeprocessed(int x[],int y[],int z[],int avail[])
{
for(j=0;j<nr;j++)
if(x[j]>avail[j])
return 0;
for(j=0;j<nr;j++)
{
avail[j]+=y[j];
y[j]=z[j]=0;
}
return 1;
}
int safe(int request[10][10],int avail[10],int alloc[10][10],int claim[10][10])
{
int count=0;
printf("\nSafe State Sequence: ");
while(count<np)
{
for(i=0;i<np;i++)
{
if((visited[i]==0) && canbeprocessed(request[i],alloc[i],claim[i],avail))
{
count++;
visited[i]=1;
printf("P%d\t",i+1);
break; //if found break loop
}
}
if(i==np) // all processes found to be not suitable for execution
return 0;
}
return 1;
}
int main()
{
int alloc[10][10],claim[10][10],request[10][10],avail[10],r[10];
printf("\nEnter the no of process: ");
scanf("%d",&np);
printf("\nEnter the no of resources: ");
scanf("%d",&nr);
for(i=0;i<nr;i++)
{
printf("\nTotal Amount of the Resource R%d: ",i+1);
scanf("%d",&r[i]);
}
printf("\nEnter the allocation matrix:");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&alloc[i][j]);
printf("\nEnter the claim matrix:");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&claim[i][j]);
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
request[i][j]=claim[i][j]-alloc[i][j];
/*Available Resource calculation*/
for(j=0;j<nr;j++)
{
avail[j]=r[j];
for(i=0;i<np;i++)
{
avail[j]-=alloc[i][j];
}
}
/*Available Resource Calculation ends*/
if(safe(request,avail,alloc,claim))
printf("\n\nConclusion: System is in safe state ");
else
printf("\n\nConclusion: The system cannot acheive safe state" );
return 0;
}
No comments:
Post a Comment