First Fit Algorithm C Program

By | November 4, 2016

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

What is 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.

It does not check for the minimum required space but whichever partition is encountered first that can handle the process is selected.

Advantage

This is a very fast searching algorithm since it does not have to search a lot.

Disadvantage

However, the disadvantage with this memory management algorithm is that the extra space cannot be used by any other process.

size(memory_block) = n + size(header) 
Scan free list for <strong>first</strong> 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 First Fit Memory Management Algorithm in operating system is compiled with GNU GCC compiler and written in gEdit Editor in Linux Ubuntu operating system.

C Program To Implement First Fit Algorithm

#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;
      printf("\nEnter the Total Number of Blocks:\t");
      scanf("%d", &number_of_blocks);
      printf("Enter 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", &blocks[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", &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\tBlock Number\tFile Size\tBlock Size\tFragment");
      for(m = 0; m < number_of_files; m++)
      {
            printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", m, file_arr[m], files[m], blocks[file_arr[m]], fragments[m]);
      }
      printf("\n");
      return 0;
}

Output

C Program To Implement First Fit Algorithm For Memory Management

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

Recommended Programs
Best Fit Memory Management C Program
Worst Fit Memory Management C Program
C Program For Least Frequently Used Page Replacement Algorithm
Next Fit Memory Management C Program
C Program To Implement Preemptive Shortest Job First Algorithm
C Program To Find Symmetric Matrix
C Program To Add Two Complex Numbers
C Program For Recursive Descent Parsing
C Program To Find LCM of N Numbers
C Program To Evaluate a Postfix String using Stack
C Program To Implement Preemptive Priority Scheduling Algorithm

5 thoughts on “First Fit Algorithm C Program

  1. Mahesh Asalkar

    Finally I found a working code for Best fit memory management algorithm in C programming. Good work.

    Reply
  2. Krishna Rathod

    There is an advanced version of the first fit algorithm, known as Modified First Fit Algorithm.

    Reply
  3. Anonymous

    suppose.. if the process is not allocated..then what will be values of..fragments[m]=temp and blockarr[filearr[m]] =1…what ll happen to these statements…

    Reply
  4. Anurag Prakash

    the program is allocating the 2nd block to the 2nd process if the first process size exceedes the size of the 1st block, it should allocate the 2nd process to the block 1 if the process 1 is not allocated….Pls verify

    Reply

Let's Discuss