 
- OS - Home
- OS - Needs
- OS - Overview
- OS - History
- OS - Components
- OS - Structure
- OS - Architecture
- OS - Services
- OS - Properties
- Process Management
- OS - Processes
- OS - Process Control Block
- OS - Operations on Processes
- OS - Inter Process Communication
- OS - Context Switching
- OS - Multi-threading
- Scheduling Algorithms
- OS - Process Scheduling
- Preemptive and Non-Preemptive Scheduling
- Scheduling Algorithms Overview
- FCFS Scheduling Algorithm
- SJF Scheduling Algorithm
- Round Robin Scheduling Algorithm
- HRRN Scheduling Algorithm
- Priority Scheduling Algorithm
- Multilevel Queue Scheduling
- Lottery Scheduling Algorithm
- OS - TAT & WAT
- Predicting Burst Time in SJF Scheduling
- Process Synchronization
- OS - Process Synchronization
- OS - Critical Section Problem
- OS - Critical Section Synchronization
- OS - Mutual Exclusion Synchronization
- OS - Semaphores
- OS - Counting Semaphores
- OS - Mutex
- OS - Turn Variable
- OS - Bounded Buffer Problem
- OS - Reader Writer Locks
- OS - Test and Set Lock
- OS - Peterson's Solution
- OS - Monitors
- OS - Sleep and Wake
- OS - Race Condition
- OS Deadlock
- Introduction to Deadlock in Operating System
- Conditions for Deadlock in Operating System
- Memory Management
- OS - Memory Management
- OS - Contiguous Memory Allocation
- OS - Non-Contiguous Memory Allocation
- OS - First Fit Algorithm
- OS - Next Fit Algorithm
- OS - Best Fit Algorithm
- OS - Worst Fit Algorithm
- OS - Fragmentation
- OS - Virtual Memory
- OS - Segmentation
- OS - Buddy System
- OS - Allocating Kernel Memory
- OS - Overlays
- Paging and Page Replacement
- OS - Paging
- OS - Demand Paging
- OS - Page Table
- OS - Page Replacement Algorithms
- OS - Optimal Page Replacement Algorithm
- OS - Belady's Anomaly
- OS - Thrashing
- Storage and File Management
- OS - File Systems
- OS - File Attributes
- OS - Structures of Directory
- OS - Linked Index Allocation
- OS - Indexed Allocation
- I/O Systems
- OS - I/O Hardware
- OS - I/O Software
- OS Types
- OS - Types
- OS - Batch Processing
- OS - Multiprocessing
- OS - Hybrid
- OS - Monolithic
- OS - Zephyr
- OS - Nix
- OS - Linux
- OS - Blackberry
- OS - Garuda
- OS - Tails
- OS - Clustered
- OS - Haiku
- OS - AIX
- OS - Solus
- OS - Tizen
- OS - Bharat
- OS - Fire
- OS - Bliss
- OS - VxWorks
- OS - Embedded
- OS - Single User
- Miscellaneous Topics
- OS - Security
- OS Questions Answers
- OS - Questions Answers
- OS Useful Resources
- OS - Quick Guide
- OS - Useful Resources
- OS - Discussion
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?
- Steps to Implement Next Fit Algorithm
- C++ Code for Next Fit Algorithm
- Advantages of Next Fit Algorithm
- Disadvantages of Next Fit Algorithm
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.