Operating System - Next Fit Algorithm



The Next Fit Algorithm is used in contiguous memory allocation systems to find a suitable block of free space (called a hole) that can accommodate the process. This chapter will explain the Next Fit Algorithm, how it works, and its implementation in operating systems.

What is Next Fit Algorithm?

The Next Fit Algorithm is a memory allocation strategy similar to the First Fit Algorithm. In Next Fit, the search for a free memory block starts from the location of the last allocated block, instead of starting from the beginning of the memory.

The algorithm scans for the next available hole that is large enough to accommodate the process. If a suitable hole is found, then the process is allocated to that hole. If no suitable hole is found by the end of the memory, then the searching pointer moves to the beginning of the memory and continues searching until it reaches the starting point of the search.

Example

Consider the following example where we have three memory blocks of sizes 6, 12, and 25 units, and three processes of sizes 12, 25, and 30 units respectively. We will allocate memory to these processes using the Next Fit Algorithm.

Input :  
blockSize[] = {6, 12, 25};
processSize[] = {12, 25, 30};

Output:
Process No.     Process Size    Block no.
 1              12              2
 2              25              3
 3              30              Not Allocated

The process size 30 cannot be allocated as there is no block large enough to accommodate it.

Steps to Implement Next Fit Algorithm

The Next Fit Algorithm can be implemented using the following steps −

  • Initialize a pointer to keep track of the last allocated block.
  • For each process, start searching for a suitable hole from the last allocated block.
  • If a suitable hole is found, allocate the process to that hole and update the pointer to the current block.
  • If no suitable hole is found by the end of the memory, move to the beginning of the memory and continue searching until reaching the starting point of the search.
  • If no suitable hole is found after a complete scan, mark the process as "Not Allocated".

C++ Code for Next Fit Algorithm

Below is a sample C++ code that demonstrates the implementation of the Next Fit Algorithm −

#include <iostream>
using namespace std;

void nextFit(int blockSize[], int m, int processSize[], int n) {
    int allocation[n];
    for (int i = 0; i < n; i++)
        allocation[i] = -1;

    int j = 0;
    for (int i = 0; i < n; i++) {
        while (j < m) {
            if (blockSize[j] >= processSize[i]) {
                allocation[i] = j;
                blockSize[j] -= processSize[i];
                break;
            }
            j++;
        }
        if (j == m)
            j = 0;
    }

    cout << "Process No.     Process Size    Block no." << endl;
    for (int i = 0; i < n; i++) {
        cout << " " << i + 1 << "              " << processSize[i] << "              ";
        if (allocation[i] != -1)
            cout << allocation[i] + 1;
        else
            cout << "Not Allocated";
        cout << endl;
    }
}

int main() {
    int blockSize[] = {6, 12, 25};
    int processSize[] = {12, 25, 30};
    int m = sizeof(blockSize) / sizeof(blockSize[0]);
    int n = sizeof(processSize) / sizeof(processSize[0]);
    nextFit(blockSize, m, processSize, n);
    return 0;
}

The output of the above code will be −

Process No.     Process Size    Block no.
 1              12              2
 2              25              3
 3              30              Not Allocated

Advantages of Next Fit Algorithm

The Next Fit Algorithm is considered as an improvement over the First Fit Algorithm and have several advantages −

  • Reduced Search Time − By starting the search from the last allocated block, Next Fit can reduce the time taken to find a suitable hole for allocation.
  • Better Memory Utilization − Next Fit can ensure maximum utilization of memory by avoiding the allocation of large holes to small processes.
  • Less Fragmentation − Next Fit can help in reducing fragmentation.

Disadvantages of Next Fit Algorithm

Sometimes, the Next Fit Algorithm is not preferred, because of −

  • Longer Wait Times − In some cases, Next Fit may lead to longer wait times for processes to be allocated memory.
  • Not Always Optimal − Next Fit may not always find the best fit for a process. In some cases, it may skip over suitable holes that are located before the last allocated block.
Advertisements