Concept:Allocate the smallest hole that is big enough. We must search the entire list, unless the list is ordered by size. This strategy produces the smallest left over hole.
SOURCE CODE :
// * Best- Fit * //
#include
#include
void main()
{
int m[5],n,mr[10],i,j,t,rm=0,pr[5]={1,2,3,4,5},q;
char p[10][10];
clrscr();
printf("Enter the no.of memory partitions \n");
scanf("%d",&q);
for(i=0;i
{
printf("the memory for partition %d \t",i++);
scanf("%d",&m[i]);
}
printf("Enter no.of process \n:");
scanf("%d",&n);
for(i=0;i
{
printf(" process name \t ");
scanf("%s",&p[i]);
printf("Enter memory required \t");
scanf("%d",&mr[i]);
}
for(i=0;i
{
for(j=i+1;j
{
if(m[i]>m[j])
{
t=m[i];
m[i]=m[j];
m[j]=t;
t=pr[i];
pr[i]=pr[j];
pr[j]=t;
}
}
}
for(i=0;i
{
for(j=0;j
if(m[j]>mr[i])
{
printf(" Process %s has been allocated partitio p[i],pr[j],m[j]);
m[j]=m[j]-mr[i];
break;
}
}
for(i=0;i
rm=rm+m[i];
printf("remaining
free space is %d kb \n", rm);
}
OUTPUT Process p1 has been allocated partition 4 with space 300kb Process p2 has been allocated partition 2 with space 500kb Process p3 has been allocated partition 3 with space 200kb Process p4 has been allocated partition 5 with space 600kb Remaining free space is 533kb.