Created
May 13, 2020 13:44
-
-
Save projjal1/84a8f89b2e6bf21f361dbd5745785948 to your computer and use it in GitHub Desktop.
First Fit memory management scheme in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void firstFit(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 first available block | |
int best_idx=-1; | |
//Running for-loop over blocksizes | |
for (int j=0;j<m;j++) | |
{ | |
//Block has been found | |
if (blockSize[j]>=processSize[i]) | |
{ | |
best_idx=j; | |
break; | |
} | |
} | |
//Cannot be alloted | |
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]); | |
firstFit(blockSize, m, processSize, n); | |
return 0 ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment