Best Fit Algorithm C Program

By | November 4, 2016

Let us learn how to implement Best Fit Algorithm in C programming language. The memory management program for Best Fit Algorithm uses Arrays.

What is Best Fit Algorithm?

The Best Fit Memory Allocation Algorithm allocates the smallest 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 smallest enough to hold the process.

This is a very slow searching algorithm since it searches a lot of memory spaces to find the best fit memory for the process. Therefore, memory utilization is much better as compared to other memory management algorithms.

size(memory_block) = n + size(header) 
Scan free list for smallest memory_block with nWords >= size(memory_block) 
If [memory_block not found]
      Failure (time for garbage collection!) 
ElseIf [free memory_block nWords >= size(block) + threshold]
      Split into a free memory_block and an in-use block 
      Free memory_block nWords = Free memory_block nWords - size(memory_block) 
      In-use memory_block nWords = size(memory_block) 
      Return block_pointer to in-use block 
Else 
      Unlink memory_block from free list
      Return block_pointer to block

Note: This C program for Best Fit Memory Management Algorithm in operating system is compiled with GNU GCC compiler and written in gEdit Editor in Linux Ubuntu operating system.

Advantage

The memory utilization in case of best fit allocation program is much better  as it searches the smallest free partition first available.

Disadvantage

This algorithm is too slow in execution and may even tend to fill up memory with unallocated memory blocks.

C Program To Implement Best Fit Algorithm

#include<stdio.h>

int main()
{
      int fragments[10], block[10], file[10], m, n, number_of_blocks, number_of_files, temp, lowest = 10000;
      static int block_arr[10], file_arr[10];
      printf("\nEnter the Total Number of Blocks:\t");
      scanf("%d", &number_of_blocks);
      printf("\nEnter the Total Number of Files:\t");
      scanf("%d", &number_of_files);
      printf("\nEnter the Size of the Blocks:\n");
      for(m = 0; m < number_of_blocks; m++) 
      {
            printf("Block No.[%d]:\t", m + 1);
            scanf("%d", &block[m]);
      }
      printf("Enter the Size of the Files:\n");
      for(m = 0; m < number_of_files; m++) 
      {
            printf("File No.[%d]:\t", m + 1);	
            scanf("%d", &file[m]);
      }
      for(m = 0; m < number_of_files; m++)
      {
            for(n = 0; n < number_of_blocks; n++)
            {
                  if(block_arr[n] != 1)
                  {
                        temp = block[n] - file[m];
                        if(temp >= 0)
                        {
                              if(lowest > temp)
                              {
                                    file_arr[m] = n;
                                    lowest = temp;
                              }
                        }
                  }
                  fragments[m] = lowest;
                  block_arr[file_arr[m]] = 1;
                  lowest = 10000;
            }
      }
      printf("\nFile Number\tFile Size\tBlock Number\tBlock Size\tFragment");
      for(m = 0; m < number_of_files && file_arr[m] != 0; m++)
      {
            printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", m, file[m], file_arr[m], block[file_arr[m]], fragments[m]);
      }
      printf("\n");
      return 0;
}

Output

C Program To Implement Best Fit Algorithm For Memory Management

If you have any doubts or compilation errors in this C program to implement Best Fit Memory Segment Algorithm in operating system, let us know about it in the comment section below.

Recommended Programs
C Program For Worst Fit Memory Management Algorithm
C Program For Next Fit Memory Management Algorithm
C Program For First Fit Memory Management Algorithm
C Program For First In First Out Page Replacement Algorithm
C Program For Optimal Page Replacement Algorithm
C Program For FCFS Algorithm
C Program To Check Skew Symmetric Matrix
C Program To Print Map of India
C Program For Producer Consumer Problem
C Program To Convert Infix Expression into Postfix using Stack
C Program To Implement Multi Level Feedback Queue Scheduling Algorithm

6 thoughts on “Best Fit Algorithm C Program

  1. Sachin Bhoi

    Is it important to declare a static array since we do not pass this array into any other functions? It’s value is going to persist in the main function I think.

    Reply
  2. Amee

    Why does it only print 2 files when you have 4? I’m a beginner so I might have some problems understanding.

    Reply
  3. Dennis

    This is a great C program. I had to take a quick backup but after that, I compiled it and it worked for me. Thanks so much for sharing the code.

    Reply

Let's Discuss