-
Notifications
You must be signed in to change notification settings - Fork 0
/
5.BestFit.c
47 lines (40 loc) · 1.15 KB
/
5.BestFit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<stdio.h>
void bestFit(int blockSize[],int m,int processSize[],int n)
{
int allocation[n];
for(int i=0;i<n;i++)allocation[i]=-1;
for(int i=0;i<n;i++){
int bestIdx=-1;
for(int j=0;j<m;j++){
if(blockSize[j]>=processSize[i])
{
if(bestIdx==-1 || blockSize[j]<blockSize[bestIdx]){
bestIdx=j;
}
}
}
if(bestIdx!=-1){
allocation[i]=bestIdx;
blockSize[bestIdx]-=processSize[i];
}
}
// Output the allocation table
printf("\nProcess No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1) {
printf("%d\n", allocation[i] + 1); // Block numbers start from 1
} else {
printf("Not Allocated\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;
}