Next Fit Memory Management Algorithm Program in C/C++

Memory management is a form of resource management applied to computer memory. The essential requirement of memory management is to provide ways to dynamically allocate portions of memory to programs at their request, and free it for reuse when no longer needed. This is critical to any advanced computer system where more than a single process might be underway at any time.

In this post, we will discuss the Next Fit Memory Management Algorithm and also write a program for the Next Fit Memory Management algorithm. In the Next Fit Memory Management algorithm, it begins as the first fit to find a free partition but when called next time it starts searching from where it left off, not from the beginning. This policy makes use of a roving pointer. The pointer moves along the memory chain to search for a next fit. This helps in, to avoid the usage of memory always from the head (beginning) of the free blockchain.

We will use C++ to write this algorithm due to the standard template library support. Hence, we will write the program of the Next Fit Memory Management Algorithm in C++, although, it’s very similar to C.

INPUT:
The first line is the number of blocks(nm).
The second line is an array of block sizes (m[nm]).
The third line is the number of processes (np).
The fourth line is an array of process sizes (p[np]).

OUTPUT:
Print the matrix for memory and process allocated.
Also, print the total external fragmentation and total internal fragmentation.

The following is the Next Fit Memory Management program in C++.

#include<iostream>
#include<algorithm>
using namespace std;

struct node{
    int memsize;
    int allocp=-1;
    int pos;
    int allocSize;
}m[200];


bool posSort(node a,node b){
    return a.pos < b.pos; 
}

bool memSort(node a,node b){
    return a.memsize < b.memsize; 
}

int main(){
    int nm,np,choice, i, j, p[200];
    cout<<"Enter number of blocks\n";
    cin>>nm;
    cout<<"Enter block size\n";
    for(i=0;i<nm;i++){
        cin>>m[i].memsize;
        m[i].pos=i;
    }

    cout<<"Enter number of processes\n";
    cin>>np;

    cout<<"Enter process size\n";
    for(i=0;i<np;i++){
        cin>>p[i];
    }
    cout<<"\n\n";
    int globalFlag=0;
    int pos = -1;
    for(i=0;i<np;i++){
        int flag=0;
        for(j=pos+1;j<nm;j++){
            if(j==nm){
                j=0;

            }
            if(j==pos)
                break;
            
            if(p[i]<=m[j].memsize && m[j].allocp==-1){
                m[j].allocp=i;
                m[j].allocSize=p[i];
                flag=1;
                pos = j;
                if(j==nm-1){
                    j=0;
                    pos = -1;
                }
                break;
            }
        }
        if(flag==0){
                cout<<"Unallocated Process P"<<i+1<<"\n";
                globalFlag=1;    
            }
        }
    sort(m,m+nm,posSort);
    cout<<"\n";
    int intFrag=0,extFrag=0;
    cout<<"Memory\t\t";
    for(i=0;i<nm;i++){
        cout<<m[i].memsize<<"\t";
    }
    cout<<"\n";
    cout<<"P. Alloc.\t";
    for(i=0;i<nm;i++){
        if(m[i].allocp!=-1){
            cout<<"P"<<m[i].allocp+1<<"\t";
        }
        else{
            cout<<"Empty\t";
        }
    }
    cout<<"\n";
    cout<<"Int. Frag.\t";
    for(i=0;i<nm;i++){
            if(m[i].allocp!=-1){
                cout<<m[i].memsize-m[i].allocSize<<"\t";
                intFrag+=m[i].memsize-m[i].allocSize;
            }
            else{
                extFrag+=m[i].memsize;
                cout<<"Empty\t";
            }
    }
    cout<<"\n";
    cout<<"\n";

    if(globalFlag==1)
        cout<<"Total External Fragmentation: "<<extFrag<<"\n";
    else
    {
        cout<<"Available Memory: "<<extFrag<<"\n";   
    }
    
    cout<<"Total Internal Fragmentation: "<<intFrag<<"\n";
    

    return 0;
}

OUTPUT:

Enter number of blocks
5
Enter block size
200 100 300 400 500
Enter number of processes
4
Enter process size
250 200 100 350

Unallocated Process P4

Memory          200     100     300     400     500
P. Alloc.       Empty   Empty   P1      P2      P3
Int. Frag.      Empty   Empty   50      200     400

Total External Fragmentation: 300
Total Internal Fragmentation: 650

Other memory management algorithms

Let us know in the comments if you are having any questions regarding this Next Fit Memory Management Algorithm.

And if you found this post helpful, then please help us by sharing this post with your friends. Thank You

Leave a Reply