Posts

Search

Worst Fit

QUESTION DESCRIPTION

Write a program to implement Worst Fit algorithm.The Worst Fit Memory Allocation Algorithm allocates the largest free partition available in the memory that is sufficient enough to hold the process within the system.It searches the complete memory for available free partitions and allocates the process to the memory partition which is the largest out of all.

TEST CASE 1

INPUT

6 8
18 33 12 36 19 23
17 11 20 28 25 10 6 9
OUTPUT
File Number File Size Block Number Block Size Fragment
0 17 5 23 6
1 11 2 12 0
2 20 0 18 0
3 28 0 18 0
4 25 0 18 0
5 10 0 18 0
6 6 0 18 0
7 9 0 18 0

TEST CASE 2

INPUT

5 7
50 22 30 10 15
17 22 14 11 34 13 19
OUTPUT
File Number File Size Block Number Block Size Fragment
0 17 2 30 0
1 22 0 50 0
2 14 4 15 1
3 11 0 50 0
4 34 0 50 0
5 13 0 50 0
6 19 0 50 0

Code :

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