Here you will learn about first fit algorithm in C and C++ with program examples.
There are various memory management schemes in operating system like first fit, best fit and worst fit. In this section we will talk about first fit scheme.
What is First Fit Memory Management Scheme?
In this scheme we check the blocks in a sequential manner which means we pick the first process then compare it’s size with first block size if it is less than size of block it is allocated otherwise we move to second block and so on.
When first process is allocated we move on to the next process until all processes are allocated.
Also Read: Best Fit Algorithm in C and C++
First Fit Algorithm
- Get no. of Processes and no. of blocks.
- After that get the size of each block and process requests.
- Now allocate processes
if(block size >= process size)
//allocate the process
else
//move on to next block - Display the processes with the blocks that are allocated to a respective process.
- 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; }
Output
Comment below if have any queries or found any information incorrect in above first fit algorithm in C and C++.
sir send me about function in c
Thank you
it has helped me thanks alot
Thank you! Your code is brief to understand, and helps me a lot!
Hello Niraj sir….
I typed ur code for first fit c program….
But the output is different from ur output….with the same values…..
Please help me out sir with this bug…
As soon as possible
Yo bro learn coding bro
this program is wrong
what is the use of flag here plz clear it out
sir please do all the program in three file concepts .It will help us a lot
nice
the code written here is wrong. in the first block only there is enough memory to save the second processor size but it takes processor 2 to allocate 2nd memory size wats the use of this then? please figure it out.
Write a C program that manages memory by dynamic multiple partitions
choose the size of the central memory and the work unit
display
the search for a free area for a waiting program is done according to the first fit algorithm