First Fit Algorithm in C and C++

Here you will find out about first fit calculation in C and C++ with program models.

There are different memory the board conspires in working framework like previously fit, best fit and most noticeably awful fit. In this area, we will discuss first fit the plan.

What is the First Fit Memory Management Scheme?

In this plan, we check the squares in a successive way which means we pick the main procedure at that point contrast it’s the size and first square size on the off chance that it does not exactly measure of the square it is designated else we move to the second square, etc.

At the point when the first procedure is dispensed we proceed onward to the following procedure until all procedures are assigned.

First Fit Algorithm

Get no. of Processes and no. of squares.

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

Presently assign forms

if(block size >= procedure size)

/designate the procedure

else

/proceed onward to next square

Show the procedures with the obstructs that are distributed to a separate procedure.

Stop.

Program forĀ First Fit Algorithm in C

#include<stdio.h>
 
void main()
{
	int bsize[10], psize[10], bno, pno, flags[10], allocation[10], i, j;
 
	for(i = 0; i < 10; i++)
	{
		flags[i] = 0;
		allocation[i] = -1;
	}
	
	printf("Enter no. of blocks: ");
	scanf("%d", &bno);
	
	printf("\nEnter size of each block: ");
	for(i = 0; i < bno; i++)
		scanf("%d", &bsize[i]);
 
	printf("\nEnter no. of processes: ");
	scanf("%d", &pno);
	
	printf("\nEnter size of each process: ");
	for(i = 0; i < pno; i++)
		scanf("%d", &psize[i]);
	for(i = 0; i < pno; i++)         //allocation as per first fit
		for(j = 0; j < bno; j++)
			if(flags[j] == 0 && bsize[j] >= psize[i])
			{
				allocation[j] = i;
				flags[j] = 1;
				break;
			}
	
	//display allocation details
	printf("\nBlock no.\tsize\t\tprocess no.\t\tsize");
	for(i = 0; i < bno; i++)
	{
		printf("\n%d\t\t%d\t\t", i+1, bsize[i]);
		if(flags[i] == 1)
			printf("%d\t\t\t%d",allocation[i]+1,psize[allocation[i]]);
		else
			printf("Not allocated");
	}
}

Program for First Fit Algorithm in C++

#include<iostream>
 
using namespace std;
 
int main()
{
	int bsize[10], psize[10], bno, pno, flags[10], allocation[10], i, j;
 
	for(i = 0; i < 10; i++)
	{
		flags[i] = 0;
		allocation[i] = -1;
	}
	
	cout<<"Enter no. of blocks: ";
	cin>>bno;
	
	cout<<"\nEnter size of each block: ";
	for(i = 0; i < bno; i++)
		cin>>bsize[i];
 
	cout<<"\nEnter no. of processes: ";
	cin>>pno;
	
	cout<<"\nEnter size of each process: ";
	for(i = 0; i < pno; i++)
		cin>>psize[i];
	for(i = 0; i < pno; i++)         //allocation as per first fit
		for(j = 0; j < bno; j++)
			if(flags[j] == 0 && bsize[j] >= psize[i])
			{
				allocation[j] = i;
				flags[j] = 1;
				break;
			}
	
	//display allocation details
	cout<<"\nBlock no.\tsize\t\tprocess no.\t\tsize";
	for(i = 0; i < bno; i++)
	{
		cout<<"\n"<< i+1<<"\t\t"<<bsize[i]<<"\t\t";
		if(flags[i] == 1)
			cout<<allocation[i]+1<<"\t\t\t"<<psize[allocation[i]];
		else
			cout<<"Not allocated";
	}
	
	return 0;
}

Remark underneath if have any inquiries or discovered any data mistaken in above first fit calculation in C and C++.

Output

First Fit Algorithm in C and C++

Leave a Comment

error: Alert: Content is protected!!