Best Fit Program In C++

Best Fit Program in C Plus Plus

 

One of the most important tasks of the operating system is to allocate memory to its different processes. The memory is generally allocated at the processing time of the CPU. The method used to allocate CPU memory to the different process is usually carried out by an algorithm called the scheduling algorithm. 

One of the most important tasks of the operating system is to allocate memory to its different processes. The memory is generally allocated at the processing time of the CPU. The method used to allocate CPU memory to the different process is usually carried out by an algorithm called the scheduling algorithm.  The most common types of algorithms used for this purpose are, best fit, first fit, worst fit, and nest fit.

In the case of best fit memory allocation algorithm, the CPU allocates the memory block that best suits the demanded amount. For this, the CPU searches through all the empty slots to find the slot that best suits the demanded memory without much wastage. The memory management scheme is

Said as best fit as it incurs a minimum amount of memory wastage.  The task is carried out with the help of an algorithm known as a Scheduling Algorithm.

Program in C++ for Best Fit

// C++ implementation code for next fit memory allocation scheme
#include <bits/stdc++.h> 
using namespace std; 
  
// Method to assign memory to a block using the next fit memory allocation scheme 
void NextFit(int b_size[], int m, int p_size[], int n) 
{ 
   //code to store block id of a block to a process demanding memory 
   int allocation[n], j = 0; 
  
    // No process is assigned with memory at the initial phase 
    
    memset(allocate, -1, sizeof(allocate)); 
  
    // pick each process and find suitable blocks 
    // according to its size ad assign to it 
    for (int i = 0; i < n; i++) { 
  
  // Condition to control memory search not from the beginning
 
        while (j < m) { 
  

            if (b_size[j] >= p_size[i]) { 
  
                // code to assign block j to p[i] process 
                allocation[i] = j; 
  
                // Reduce available memory in this block. 
                blockSize[j] -= processSize[i]; 
  
                break; 

            } 
  
           // mod m will help to traverse the free memory blocks from the first block when it reaches end 

            j = (j + 1) % m; 
        } 
    } 

  
    cout << "\nProcess Number\tProcess Size\tBlock Number \n"; 
    for (int i = 0; i < n; i++) { 

        cout << " " << i + 1 << "\t\t" << p_size[i]  
             << "\t\t";
 
        if (allocate[i] != -1) 
            cout << allocate[i] + 1; 
        else

            cout << "Not Allocated"; 

        cout<< endl; 

    } 
} 
  
// Driver program 
int main() 
{ 
    int b_size[] = { 5, 10, 20 }; 
    int p_size[] = { 10, 20, 5 }; 
    int m = sizeof(b_size) / sizeof(b_size[0]); 
    int n = sizeof(p_size) / sizeof(p_size[0]); 
  

    NextFit(b_size, m, p_size, n); 
  
    return 0; 
} 

 

Output

[table id=684 /]