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++.

2 thoughts on “Best Fit Algorithm in C and C++

Leave a Reply

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