Let us learn how to implement Best Fit Algorithm in C programming language. The memory management program for Best Fit Algorithm uses Arrays.
What is Best Fit Algorithm?
The Best Fit Memory Allocation Algorithm allocates the smallest free partition available in the memory that is sufficient enough to hold the process within the system.
It searches the complete memory for available free partitions and allocates the process to the memory partition which is the smallest enough to hold the process.
This is a very slow searching algorithm since it searches a lot of memory spaces to find the best fit memory for the process. Therefore, memory utilization is much better as compared to other memory management algorithms.
1 2 3 4 5 6 7 8 9 10 11 12 | size(memory_block) = n + size(header) Scan free list for <strong>smallest</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 Best Fit Memory Management Algorithm in operating system is compiled with GNU GCC compiler and written in gEdit Editor in Linux Ubuntu operating system.
Advantage
The memory utilization in case of best fit allocation program is much better as it searches the smallest free partition first available.
Disadvantage
This algorithm is too slow in execution and may even tend to fill up memory with unallocated memory blocks.
C Program To Implement Best 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 49 50 51 | #include<stdio.h> int main() { int fragments[10], block[10], file[10], m, n, number_of_blocks, number_of_files, temp, lowest = 10000; static int block_arr[10], file_arr[10]; printf("\nEnter the Total Number of Blocks:\t"); scanf("%d", &number_of_blocks); printf("\nEnter 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", &block[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", &file[m]); } for(m = 0; m < number_of_files; m++) { for(n = 0; n < number_of_blocks; n++) { if(block_arr[n] != 1) { temp = block[n] - file[m]; if(temp >= 0) { if(lowest > temp) { file_arr[m] = n; lowest = temp; } } } fragments[m] = lowest; block_arr[file_arr[m]] = 1; lowest = 10000; } } printf("\nFile Number\tFile Size\tBlock Number\tBlock Size\tFragment"); for(m = 0; m < number_of_files && file_arr[m] != 0; m++) { printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", m, file[m], file_arr[m], block[file_arr[m]], fragments[m]); } printf("\n"); return 0; } |
Output

If you have any doubts or compilation errors in this C program to implement Best Fit Memory Segment Algorithm in operating system, let us know about it in the comment section below.
Nice article
Is it important to declare a static array since we do not pass this array into any other functions? It’s value is going to persist in the main function I think.
You could try and implement the same and then verify the ouput.
Thanks for the best fit code.
Why does it only print 2 files when you have 4? I’m a beginner so I might have some problems understanding.
This is a great C program. I had to take a quick backup but after that, I compiled it and it worked for me. Thanks so much for sharing the code.