Best 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 Best Fit Memory Management Algorithm and also write a program for the Best Fit Memory Management algorithm. In the Best Fit Memory Management algorithm, the allocation of the process is done to a partition which is the smallest sufficient partition among the free available partitions.

We will use C++ to write this algorithm due to the standard template library support. Hence, we will write the program of the Best 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 Best 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";
    sort(m,m+nm,memSort);
    int globalFlag=0;

    for(i=0;i<np;i++){
        int flag=0;
        for(j=0;j<nm;j++){
            if(p[i]<=m[j].memsize && m[j].allocp==-1){
                m[j].allocp=i;
                m[j].allocSize=p[i];
                flag=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
500 400 300 200 100
Enter number of processes
4
Enter process size
90 200 280 350



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

Available Memory: 500
Total Internal Fragmentation: 80

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

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

Jazib
Hey guys, I have created this platform to share my knowledge with others and to keep you guys updated about the latest technologies.
Leave a Reply