Posts

Search

First Fit

QUESTION DESCRIPTION

Write a program to implement First fit algorithm.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
5 5
10 20 20 35 10
23 34 23 12 35
OUTPUT
File Number Block Number File Size Block Size Fragment
0 3 23 35 12
1 0 34 10 -24
2 0 23 10 -13
3 1 12 20 8
4 0 35 10 -25

TEST CASE 2

INPUT
3 6
10 40 15
21 22 20 10 5 8
OUTPUT
File Number Block Number File Size Block Size Fragment
0 1 21 40 19
1 0 22 10 -7
2 0 20 10 -5
3 2 10 15 5
4 0 5 10 5
5 0 8 10 5

Code :

#include<stdio.h>
int main()
{
      static int block_arr[10], file_arr[10];
      int fragments[10], blocks[10], files[10];
      int m, n, number_of_blocks, number_of_files, temp;
      scanf("%d", &number_of_blocks);
      scanf("%d", &number_of_files);
      for(m = 0; m < number_of_blocks; m++)
            scanf("%d", &blocks[m]);
      for(m = 0; m < number_of_files; m++)
            scanf("%d", &files[m]);
      for(m = 0; m < number_of_files; m++)
      {
            for(n = 0; n < number_of_blocks; n++)
            {
                  if(block_arr[n] != 1)
                  {
                        temp = blocks[n] - files[m];
                        if(temp >= 0)
                        {
                              file_arr[m] = n;
                              break;
                        }
                  }
            }
            fragments[m] = temp;
            block_arr[file_arr[m]] = 1;
      }
      printf("\nFile Number Block Number File Size Block Size Fragment");
      for(m = 0; m < number_of_files; m++)
      {
            printf("\n%d %d %d %d %d", m, file_arr[m], files[m], blocks[file_arr[m]], fragments[m]);
      }
      printf("\n");
      return 0;
}