C++ Program for First Fit algorithm in Memory Management

C++Server Side ProgrammingProgramming

Given with the n number of processes and m size of memory blocks and the task is to find the best fit memory block for the corresponding process using the first fit memory management algorithm.

What is First Fit Memory Management Algorithm?

There is multiple memory partitioning algorithms available which are used by the operating system to allocate the memory blocks to the processes like −

  • First Fit Algorithm
  • Next Fit Algorithm
  • Best Fit Algorithm
  • Worst Fit Algorithm
  • Quick Fit Algorithm

First Fit Algorithm is the simplest technique of allocating the memory block to the processes amongst all. In this algorithm, the pointer keeps track of all the free blocks in the memory and accepts the request of allocating a memory block to the coming process. After that pointer start searching for the largest first free block for the process and allocate that memory block to the coming process. In this, two partitions are created one is for the hole and the one will store the processes.

Advantage of using First Fit Algorithm is the fastest memory allocation to the coming processes as it allocates the largest first fit algorithm to the new processes.

Disadvantage of using First Fit Algorithm is the wastage of memory which will lead to the deficiency of memory for other processes.

Example

Input-: block_size[] = {300, 50, 200, 350, 70}
process_size[] = {200, 47, 212, 426, 10}
Output-:
Process No. Process Size Block no.
1             200       1
2             47       1
3             212       4
4             426       Not Allocated
5             10       1

Approach used in the following program is as follows

  • Input the blocks and the processes in an array
  • Set all the memory blocks to free
  • Check if the (size of a process) <= (size of a memory block) then allocate the process to a memory block
  • Else keep traversing the other blocks until the (size of a process) <= (size of a memory block) will not hold true.

Algorithm

Start
Step 1->  declare function to calculate the best fit memory block
   void First_Fit(int block_size[], int total_blocks, int process_size[], int total_process)
   Declare int allocation[total_process]
   Call function memset(allocation, -1, sizeof(allocation))
   Loop For i = 0 and i < total_process and i++
      Loop for j = 0 and j < total_blocks and j++
         IF block_size[j] >= process_size[i]
            Set allocation[i] = j
            Set block_size[j] -= process_size[i]
         End
      End
   End
   Loop For i = 0 and i < total_process and i++
      IF allocation[i] != -1
         Set allocation[i] + 1
      End
      Else
         Print Not Allocated
      End
   End
Step 2-> In main()
   Declare an array for blocks as int block_size[] = {300, 50, 200, 350, 70}
   Declare an array for processes int process_size[] = {200, 47, 212, 426, 10}
   Calculate total blocks int total_blocks = sizeof(block_size) / sizeof(block_size[0]
   Calculate total processes int total_process = sizeof(process_size) / sizeof(process_size[0])
   Call First_Fit(block_size, total_blocks, process_size, total_process)
Stop

Example

 Live Demo

#include<bits/stdc++.h>
using namespace std;
// Function to allocate memory to  
// blocks as per First fit algorithm
void First_Fit(int block_size[], int total_blocks, int process_size[], int total_process) {
   int allocation[total_process];
   memset(allocation, -1, sizeof(allocation));
   //this for loop wll pick eact process and allocate a first fit block to it
   for (int i = 0; i < total_process; i++) {
      for (int j = 0; j < total_blocks; j++) {
         if (block_size[j] >= process_size[i]) {
            allocation[i] = j;
            block_size[j] -= process_size[i];
            break;
         }
      }
   }
   cout << "\nProcess No.\tProcess Size\tBlock no.\n";
   for (int i = 0; i < total_process; i++) {
      cout << " " << i+1 << "\t\t" << process_size[i] << "\t\t";
      if (allocation[i] != -1)
         cout << allocation[i] + 1;
      else
         cout << "Not Allocated";
         cout << endl;
   }
}
int main() {
   //create array to store block sizes
   int block_size[] = {300, 50, 200, 350, 70};  
    //create array to store process sizes
   int process_size[] = {200, 47, 212, 426, 10};
    //variable total_blocks that contain total number of blocks
   int total_blocks = sizeof(block_size) / sizeof(block_size[0]);
    //variable total_process that contain total number of blocks
   int total_process = sizeof(process_size) / sizeof(process_size[0]);
    //calling the function First_fit
   First_Fit(block_size, total_blocks, process_size, total_process);
   return 0 ;
}

Output

Process No.    Process Size   Block no.  
1                  200            1  
2                  47             1          
3                  212            4  
4                  426         Not Allocated  
5                  10            1
raja
Published on 23-Dec-2019 06:26:47
Advertisements