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

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


Enter number of blocks: 5
Enter size of each block: 200 400 300 550 120
Enter no. of process: 4
Enter size of each process: 250 500 100 150

Block no. 		size  		process no. 		size 
1 				200 			3 				100 
2 				400 			1 				250 
3 				300 			4				150 
4 				550 			2 				500 
5 				120 		Not allocated 

Table of Contents

C++ Program Code

   
 

/*;==========================================
; Title: Write a program to implement first fit algorithm in memory management
; Owned: codenaive littleboy8506@  <admin@codenaive.com> <s.8506.a@gmail.com>
; Contribution:  Mansi bansal
; Programming Language: C++
; Date:   9 Feb 2021
;==========================================*/
#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 number of blocks: ";
cin>>bno;
cout<<"\nEnter size of each block: ";
for(i = 0; i < bno; i++)
cin>>bsize[i];
cout<<"\nEnter no. of process: ";
cin>>pno;
cout<<"\nEnter size of each process: ";
for(i = 0; i < pno; i++)
 cin>>psize[i];
for(i = 0; i < pno; i++)         
   for(j = 0; j < bno; j++)
     if(flags[j] == 0 && bsize[j] >= psize[i])
       {
	allocation[j] = i;
	flags[j] = 1;
	break;
      }
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";
}
cout<<"\n";
 return 0;
}

			 
						 		  
			 
						 					

Output

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

Enter size of each block: 200 400 300 550 120 

Enter no. of process: 4 

Enter size of each process: 250 500 100 150

Block no. 		size  		process no. 		size 
1 				200 			3 				100 
2 				400 			1 				250 
3 				300 			4				150 
4 				550 			2 				500 
5 				120 		Not allocated 


						

Leave a Reply

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