/* Bankers Algorithm */
#include<stdio.h>
#include<conio.h>
void main()
{
int np,nr,i,j,k,flag=0,count=0;
int fin[5],avalres[3],work[3],max[5][3],alloc[5][3],need[5][3];
printf("Enter no. of processes : ");
scanf("%d",&np);
printf("Enter no. of resources : ");
scanf("%d",&nr);
printf("Enter available resouces %d",nr);
for(j=0;j<nr;j++)
{
scanf("%d",&avalres[j]);
}
printf("\nEnter allocation for each process : ");
for(i=0;i<np;i++)
{
for(j=0;j<nr;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("\n Enter max of each process : ");
for(i=0;i<np;i++)
{
for(j=0;j<nr;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("\n Allocation : ");
for(i=0;i<np;i++)
{
printf("\n");
for(j=0;j<nr;j++)
{
printf(" %d ",alloc[i][j]);
}
}
printf("\nMax : ");
for(i=0;i<np;i++)
{
printf("\n");
for(j=0;j<nr;j++)
{
printf(" %d ",max[i][j]);
}
}
printf("\nNeed : ");
for(i=0;i<np;i++)
{
printf("\n");
for(j=0;j<nr;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
printf(" %d ",need[i][j]);
}
}
printf("\nAvailable Resorces : ");
for(j=0;j<nr;i++)
{
printf(" %d ",avalres[j]);
}
printf("\n\n");
for(i=0;i<np;i++)
{
fin[i]=0;
}
for(i=0;i<nr;i++)
work[i]=avalres[i];
printf("\n\n Safety sequence : ");
while(1)
{
for(i=0;i<np;i++)
{
flag=0;
if(fin[i]==0)
{
for(j=0;j<nr;j++)
{
if(work[j]<need[i][j])
{
flag=1;
break;
}
else
flag=0;
}
if(flag==0)
{
for(k=0;k<nr;k++)
{
fin[i]=1;
work[k]=work[k]+alloc[i][k];
}
printf("\t P %d",i);
count++;
}
}
}
if(count==np)
break;
}// end of while
}// end of main
#include<stdio.h>
#include<conio.h>
void main()
{
int np,nr,i,j,k,flag=0,count=0;
int fin[5],avalres[3],work[3],max[5][3],alloc[5][3],need[5][3];
printf("Enter no. of processes : ");
scanf("%d",&np);
printf("Enter no. of resources : ");
scanf("%d",&nr);
printf("Enter available resouces %d",nr);
for(j=0;j<nr;j++)
{
scanf("%d",&avalres[j]);
}
printf("\nEnter allocation for each process : ");
for(i=0;i<np;i++)
{
for(j=0;j<nr;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("\n Enter max of each process : ");
for(i=0;i<np;i++)
{
for(j=0;j<nr;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("\n Allocation : ");
for(i=0;i<np;i++)
{
printf("\n");
for(j=0;j<nr;j++)
{
printf(" %d ",alloc[i][j]);
}
}
printf("\nMax : ");
for(i=0;i<np;i++)
{
printf("\n");
for(j=0;j<nr;j++)
{
printf(" %d ",max[i][j]);
}
}
printf("\nNeed : ");
for(i=0;i<np;i++)
{
printf("\n");
for(j=0;j<nr;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
printf(" %d ",need[i][j]);
}
}
printf("\nAvailable Resorces : ");
for(j=0;j<nr;i++)
{
printf(" %d ",avalres[j]);
}
printf("\n\n");
for(i=0;i<np;i++)
{
fin[i]=0;
}
for(i=0;i<nr;i++)
work[i]=avalres[i];
printf("\n\n Safety sequence : ");
while(1)
{
for(i=0;i<np;i++)
{
flag=0;
if(fin[i]==0)
{
for(j=0;j<nr;j++)
{
if(work[j]<need[i][j])
{
flag=1;
break;
}
else
flag=0;
}
if(flag==0)
{
for(k=0;k<nr;k++)
{
fin[i]=1;
work[k]=work[k]+alloc[i][k];
}
printf("\t P %d",i);
count++;
}
}
}
if(count==np)
break;
}// end of while
}// end of main
No comments:
Post a Comment