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 First Fit Memory Management Algorithm and also write a program for the First Fit Memory Management algorithm. In the First Fit Memory Management algorithm, the partition is allocated which is first sufficient from the top of Main Memory. It is the simplest form of memory allocation.
We will use C++ to write this algorithm due to the standard template library support. Hence, we will write the program of the First 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 First 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 100 200 300 400 500 Enter number of processes 4 Enter process size 90 200 280 350 Memory 100 200 300 400 500 P. Alloc. P1 P2 P3 P4 Empty Int. Frag. 10 0 20 50 Empty Available Memory: 500 Total Internal Fragmentation: 80
Other memory management algorithms
- Next Fit Memory Management
- Best Fit Memory Management
- Worst Fit Memory Management
- First Fit Memory Management
Let us know in the comments if you are having any questions regarding this First Fit Memory Management Algorithm.
And if you found this post helpful, then please help us by sharing this post with your friends. Thank You