Worst 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 Worst Fit Memory Management Algorithm and also write a program for the Worst Fit Memory Management algorithm. In the Worst Fit Memory Management algorithm, it allocates a process to the partition which is largest sufficient among the freely available partitions available in the main memory. If a large process comes at a later stage, then memory will not have space to accommodate it.

We will use C++ to write this algorithm due to the standard template library support. Hence, we will write the program of the Worst 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 Worst 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 300


Unallocated Process P4

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

Total External Fragmentation: 300
Total Internal Fragmentation: 630

Other memory management algorithms

Let us know in the comments if you are having any questions regarding this Worst 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