Best Fit Algorithm in C and C++

Here you will find out about best-fit calculation in C and C++ with the program model.

Memory Management is one of the administrations given by OS which is required for Optimized memory use of the accessible memory in a Computer System.

For this reason, OS utilizes 3 strategies:-

First Fit

Best Fit

Most exceedingly terrible Fit

In this area, we will discuss the Best Fit Memory Management Scheme.

What is the Best Fit Memory Management Scheme?

Best fit uses the best memory square dependent on the Process memory demand. In best fit usage the calculation initially chooses the littlest square which can enough satisfy the memory demand by the individual procedure.

Due to this memory is used ideally yet as it contrasts the squares and the mentioned memory size it builds the time necessary and henceforth more slow than different techniques.

It experiences Internal Fragmentation which just implies that the memory square size is more prominent than the memory mentioned by the procedure, at that point the free space gets squandered.

When we experience a procedure that demands a memory which is higher than the square size we stop the calculation.

Best Fit Algorithm

Get no. of Processes and no. of squares.

After that get the size of each square and procedure demands.

At that point select the best memory hinder that can be apportioned utilizing the above definition.

Show the procedures with the obstructs that is allotted to an individual procedure.

Estimation of Fragmentation is discretionary to show to monitor squandered memory.

Stop.

Program for Best Fit Algorithm in C

#include<stdio.h>
 
void main()
{
	int fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=9999;
	static int barray[20],parray[20];
	
	printf("\n\t\t\tMemory Management Scheme - Best Fit");
	printf("\nEnter the number of blocks:");
	scanf("%d",&nb);
	printf("Enter the number of processes:");
	scanf("%d",&np);
	
	printf("\nEnter the size of the blocks:-\n");
	for(i=1;i<=nb;i++)
    {
		printf("Block no.%d:",i);
        scanf("%d",&b[i]);
    }
	
	printf("\nEnter the size of the processes :-\n");
	for(i=1;i<=np;i++)
    {
        printf("Process no.%d:",i);
        scanf("%d",&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;
	}
	
	printf("\nProcess_no\tProcess_size\tBlock_no\tBlock_size\tFragment");
	for(i=1;i<=np && parray[i]!=0;i++)
		printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,p[i],parray[i],b[parray[i]],fragment[i]);
}

Program for Best Fit Algorithm in C++

#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<<"\n\t\t\tMemory Management Scheme - Best Fit";
	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];
	
	return 0;
}

Output

Best Fit Algorithm in C and C++

Remark underneath on the off chance that you have any questions identified with above best fit calculation in C and C++.

Recent Posts

error: Alert: Content is protected!!