Posts

Search

First Fit

QUESTION DESCRIPTION

Write a C program for implementing the First Fit algorithm in memory partitioning. The First Fit Memory Allocation Algorithm allocates the first free partition available in the memory that is sufficient enough to hold the process within the system.

TEST CASE 1

INPUT

3
2
5
2
7
1
4
OUTPUT
Fileno: 1 2
Filesize: 1 4
Blockno: 3 1
Blocksize: 7 5
Fragment: 6 1

TEST CASE 2

INPUT

6
6
7
8
9
6
2
5
4
3
6
5
7
8
OUTPUT
Fileno: 1 2 3 4 5 6
Filesize: 4 3 6 5 7 8
Blockno: 3 2 1 4 0 0
Blocksize: 9 8 7 6 1 1
Fragment: 5 5 1 1 0 0

Code :

#include<stdio.h>
#define max 25
int main()
{
 int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
 static int bf[max],ff[max];
 scanf("%d",&nb);
 scanf("%d",&nf);
 for(i=1;i<=nb;i++)
  scanf("%d",&b[i]);
 for(i=1;i<=nf;i++)
  scanf("%d",&f[i]);
 for(i=1;i<=nf;i++)
 {
  for(j=1;j<=nb;j++)
  {
   if(bf[j]!=1)
   {
    temp=b[j]-f[i];
    if(temp>=0)
     if(highest<temp)
     {
      ff[i]=j;
      highest=temp;
     }
   }
  }
  frag[i]=highest;
  bf[ff[i]]=1;
  highest=0;
 }
 printf("\nFileno:");
 for(i=1;i<=nf;i++)
  printf(" %d",i);
 printf("\nFilesize:");
 for(i=1;i<=nf;i++)
  printf(" %d",f[i]);
 printf("\nBlockno:");
 for(i=1;i<=nf;i++)
  printf(" %d",ff[i]);
 printf("\nBlocksize:");
 for(i=1;i<=nf;i++)
  printf(" %d",b[ff[i]]);
 printf("\nFragment:");
 for(i=1;i<=nf;i++)
  printf(" %d",frag[i]);
 return 0;
}