Next Fit Algorithm C Program

By | November 16, 2016

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

What is Next Fit Algorithm?

The Next Fit Memory Allocation Algorithm is also known as Next Fit Bin Packing Algorithm.

This algorithm keeps a track of the positions where every file is written in the memory. It then allocates the very next available memory block to the succeeding processes.

So, when a process is executed to be stored in the memory, the previous bin or the memory block is checked for its availability.

If it is free, then a process is written in the same memory block or else, the next block is checked. This is a very fast searching algorithm and is also comparatively faster than First Fit and Best Fit Memory Management Algorithms.

The Next Fit Page Replacement Algorithm is a modified version of the First Fit Algorithm. It is, therefore, called as Modified First Fit and is faster than the First Fit Algorithm.

While allocating memory blocks, the algorithm begins as the first fit to find a free partition. Next time when the algorithm is called, it starts searching from where it left off, not from the beginning.

C Program To Implement Next Fit Algorithm in OS

#include <stdio.h>

int main()
{
      int memory_size[10][2], process_size[10][3];
      int i, j, total_processes = 0, total_memory = 0;
      printf("\nEnter the Total Number of Processes:\t");
      scanf("%d", &total_processes);
      printf("\nEnter the Size of Each Process\n");
      for(int i = 0; i < total_processes; i++)
      {
            printf("Enter Size of Process %d:\t", i + 1);
            scanf("%d", &process_size[i][0]);
            process_size[i][1] = 0;
            process_size[i][2] = i;
      }
      printf("\nEnter Total Memory Blocks:\t");
      scanf("%d", &total_memory);
      printf("\nEnter the Size of Each Block:\n");
      for(i = 0; i < total_processes; i++)
      {
            printf("Enter Size of Block %d:\t", i + 1);
            scanf("%d", &memory_size[i][0]);
            memory_size[i][1] = 0;
      }
      for(i = 0; i < total_processes; i++)
      {
            while(j < total_memory)
            {
                  if(memory_size[j][1] == 0 && process_size[i][0] <= memory_size[j][0])
                  {
                        process_size[i][1] = 1;
                        memory_size[j][1] = 1;
                        printf("\nProcess [%d] Allocated to Memory Block:\t%d", i + 1, j + 1);
                        break;
                  }
                  j++;
            }
      }
      for(i = 0; i < total_memory; i++)
      {
            if(process_size[i][1] == 0)
            {
                  printf("\nProcess [%d] Unallocated\n", i + 1);
            }
      }
      printf("\n");
      return 0;
}

Output

C Program For Next Fit Algorithm For Memory Management

If you have any doubts or compilation errors in this C program to implement Next 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
First Fit Memory Management C Program
C Program To Implement Caesar Cipher Algorithm
C Program To Display Digital Clock
C Program To For Preemptive Shortest Job First Algorithm
C Program To Find LCM of N Numbers
C Program To Find First and Follow of Grammar
C Program For Recursive Descent Parsing

4 thoughts on “Next Fit Algorithm C Program

  1. Pooja Mishra

    The Next Fit Program for Memory Management is similar to the First fit algorithm, isn’t it?

    Reply
  2. Rajeev Suri

    Yes. It is a little similar to the first fit but its an upgraded version, we can say.

    Reply
  3. Tatheer Zahra

    NextFit.c:10:15: error: redeclaration of ‘i’ with no linkage
    for(int i = 0; i < total_processes; i++)
    ^
    NextFit.c:6:11: note: previous declaration of ‘i’ was here
    int i, j, total_processes = 0, total_memory = 0;
    ^
    NextFit.c:10:7: error: ‘for’ loop initial declarations are only allowed in C99 mode
    for(int i = 0; i < total_processes; i++)

    I am getting these errors in Next fit

    Reply

Let's Discuss