Let us learn how to implement First Fit Algorithm in C programming language. The memory management program for First Fit Algorithm uses Arrays.
What is First Fit Algorithm?
The First Fit Memory Allocation Algorithm allocates the first free partition available in the memory that is sufficient enough to hold the process within the system.
It does not check for the minimum required space but whichever partition is encountered first that can handle the process is selected.
Advantage
This is a very fast searching algorithm since it does not have to search a lot.
Disadvantage
However, the disadvantage with this memory management algorithm is that the extra space cannot be used by any other process.
1 2 3 4 5 6 7 8 9 10 11 12 | size(memory_block) = n + size(header) Scan free list for <strong>first</strong> memory_block with nWords >= size(memory_block) If [memory_block not found] Failure (time for garbage collection!) ElseIf [free memory_block nWords >= size(block) + threshold] Split into a free memory_block and an in-use block Free memory_block nWords = Free memory_block nWords - size(memory_block) In-use memory_block nWords = size(memory_block) Return block_pointer to in-use block Else Unlink memory_block from free list Return block_pointer to block |
Note: This C Program for First Fit Memory Management Algorithm in operating system is compiled with GNU GCC compiler and written in gEdit Editor in Linux Ubuntu operating system.
C Program To Implement First Fit Algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #include<stdio.h> int main() { static int block_arr[10], file_arr[10]; int fragments[10], blocks[10], files[10]; int m, n, number_of_blocks, number_of_files, temp; printf("\nEnter the Total Number of Blocks:\t"); scanf("%d", &number_of_blocks); printf("Enter the Total Number of Files:\t"); scanf("%d", &number_of_files); printf("\nEnter the Size of the Blocks:\n"); for(m = 0; m < number_of_blocks; m++) { printf("Block No.[%d]:\t", m + 1); scanf("%d", &blocks[m]); } printf("Enter the Size of the Files:\n"); for(m = 0; m < number_of_files; m++) { printf("File No.[%d]:\t", m + 1); scanf("%d", &files[m]); } for(m = 0; m < number_of_files; m++) { for(n = 0; n < number_of_blocks; n++) { if(block_arr[n] != 1) { temp = blocks[n] - files[m]; if(temp >= 0) { file_arr[m] = n; break; } } } fragments[m] = temp; block_arr[file_arr[m]] = 1; } printf("\nFile Number\tBlock Number\tFile Size\tBlock Size\tFragment"); for(m = 0; m < number_of_files; m++) { printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", m, file_arr[m], files[m], blocks[file_arr[m]], fragments[m]); } printf("\n"); return 0; } |
Output

If you have any doubts or compilation errors in this C program to implement First Fit Memory Segment Algorithm in operating system, let us know about it in the comment section below.
Finally I found a working code for Best fit memory management algorithm in C programming. Good work.
There is an advanced version of the first fit algorithm, known as Modified First Fit Algorithm.
Nice code for first fit.
suppose.. if the process is not allocated..then what will be values of..fragments[m]=temp and blockarr[filearr[m]] =1…what ll happen to these statements…
the program is allocating the 2nd block to the 2nd process if the first process size exceedes the size of the 1st block, it should allocate the 2nd process to the block 1 if the process 1 is not allocated….Pls verify