Best Fit Algorithm in C and C++

Here you will learn about best fit algorithm in C and C++ with program example.

Memory Management is one of the services provided by OS which is needed for Optimized memory usage of the available memory in a Computer System.

For this purpose OS uses 3 methods:-

  1. First Fit
  2. Best Fit
  3. Worst Fit

In this section we will talk about Best Fit Memory Management Scheme.

What is Best Fit Memory Management Scheme?

Best fit uses the best memory block based on the Process memory request. In best fit implementation the algorithm first selects the smallest block which can adequately fulfill the memory request by the respective process.

Because of this memory is utilized optimally but as it compares the blocks with the requested memory size it increases the time requirement and hence slower than other methods. It suffers from Internal Fragmentation which simply means that the memory block size is greater than the memory requested by the process, then the free space gets wasted.

Once we encounter a process that requests a memory which is higher than block size we stop the algorithm.

Best Fit Algorithm

  1. Get no. of Processes and no. of blocks.
  2. After that get the size of each block and process requests.
  3. Then select the best memory block that can be allocated using the above definition.
  4. Display the processes with the blocks that are allocated to a respective process.
  5. Value of Fragmentation is optional to display to keep track of wasted memory.
  6. Stop.

Program for Best Fit Algorithm in C

Program for Best Fit Algorithm in C++

Output

Best Fit Algorithm in C and C++

Comment below if you have any doubts related to above best fit algorithm in C and C++.

10 thoughts on “Best Fit Algorithm in C and C++”

  1. Amruta. v. pattar

    please send this same program with allocation and also dellocation and also the status of the memory location

  2. What if I sort the blocks in ascending order in the beginning it would be easy to implement after that. Let me know if I am wrong?

Leave a Comment

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