Skip to content

Instantly share code, notes, and snippets.

@projjal1
Created May 13, 2020 13:44
Show Gist options
  • Save projjal1/ac60619791bac10471b6b99927f97135 to your computer and use it in GitHub Desktop.
Save projjal1/ac60619791bac10471b6b99927f97135 to your computer and use it in GitHub Desktop.
Best Fit Memory Management in C
#include <stdio.h>
void bestFit(int blockSize[],int m,int processSize[],int n)
{
//Block allocations for process
int allocation[n];
//Initially allocate the index of -1 to all allocation blocks
for (int i=0;i<n;i++)
{
allocation[i]=-1;
}
//Fetching the process
for (int i=0;i<n;i++)
{
//Setting the index with minimal wastage
int best_idx=-1;
//Keeping the track of free space in best block
int diff;
//Running for-loop over blocksizes
for (int j=0;j<m;j++)
{
if (blockSize[j]>=processSize[i])
{
//Choosing first block as the best allocated block
if (best_idx==-1)
{
diff=blockSize[j]-processSize[i];
best_idx=j;
}
else
{
int inter=blockSize[j]-processSize[i];
//Check if this free space is less than that of current best
if (diff>inter)
{
diff=inter;
best_idx=j;
}
}
}
}
if (best_idx==-1)
continue;
//At the end allot the block to process and re-calculate free-space
blockSize[best_idx]-=processSize[i];
allocation[i]=best_idx;
}
//Now printing the allocation array
for(int i=0;i<n;i++)
{
if (allocation[i]!=-1)
printf("\nBlock for Process No %d with Size %d is %d",i+1,processSize[i],allocation[i]+1);
else
printf("\nBlock for Process No %d with Size %d cannot be allocated",i+1,processSize[i]);
}
printf("\n\nEnd of process\n");
}
int main()
{
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize)/sizeof(blockSize[0]);
int n = sizeof(processSize)/sizeof(processSize[0]);
bestFit(blockSize, m, processSize, n);
return 0 ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment