Write a program to implement best fit algorithm in memory management using C++

  The following c++ program will help you to implement best fit algorithm in memory management using C++

C++ Program Code

   
 

/*;==========================================
; Title: Write a program to implement best fit algorithm in memory management. 
; Owned: codenaive littleboy8506@  <admin@codenaive.com> <s.8506.a@gmail.com>
; Contribution:  Mansi bansal
; Programming Language: C++
; Date:   28 Jan 2021
;==========================================*/
#include<iostream>
 using namespace std;
 int main()
{          int fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=9999;
	static int barray[20],parray[20];
	cout<<"\nEnter the number of blocks: ";
	cin>>nb;
	cout<<"Enter the number of processes: ";
	cin>>np;
	cout<<"\nEnter the size of the blocks:- \n";
	for(i=1;i<=nb;i++)
   { cout<<"Block no."<<i<<":";
        cin>>b[i];
    }
	cout<<"\nEnter the size of the processes :- \n";
	for(i=1;i<=np;i++)
    {
        cout<<"Process no. "<<i<<":";
        cin>>p[i];
    }
	for(i=1;i<=np;i++)
	{	for(j=1;j<=nb;j++)
	{
	if(barray[j]!=1)
{
temp=b[j]-p[i];
	if(temp>=0)
		if(lowest>temp)
			{
			parray[i]=j;
			lowest=temp;
			}
		}
	}
		fragment[i]=lowest;
		barray[parray[i]]=1;
		lowest=10000;
	}
	cout<<"\nProcess_no\tProcess_size\tBlock_no\tBlock_size\tFragment";
	for(i=1;i<=np && parray[i]!=0;i++)
cout<<"\n"<<i<<"\t\t"<<p[i]<<"\t\t"<<parray[i]<<"\t\t"<<b[parray[i]]<<"\t\t"<<fragment[i];
cout<<"\n";
return 0;
}



					
					

Output

~/admin@codenaive/os $ g++ best.cpp -o best 
~/admin@codenaive/os $ ./best 
Enter the number of blocks: 5 
Enter the number of processes: 5 

Enter the size of the blocks:-
Block no.1: 200 
Block no.2: 100 
Block no.3: 300 
Block no.4: 250 
Block no.5: 400

Enter the size of the processes :-
Process no. 1: 80 
Process no. 2: 150 
Process no. 3: 250 
Process no. 4: 280 
Process no. 5: 350 

Process_no     Process_size    Block_no    Block_size    Fragment
1  					80 			  2				100 		20 
2 					150 		  1 			200 		50 
3 					250 		  4 			250 		0
4 					280 		  3 			300 		20
5 					350 		  5             400         50 
  
						 

Leave a Reply

Your email address will not be published. Required fields are marked *