-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbest_fit.c
47 lines (45 loc) · 1.39 KB
/
best_fit.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>
#include <stdlib.h>
void printProcesses(int processes, int allocated[], int processSizes[]) {
printf("\nProcess\tSize\tBlock number");
for (int i = 0; i < processes; i++) {
printf("\n%d\t%d\t", i+1, processSizes[i]);
if (allocated[i] != -1)
printf("%d", allocated[i]+1);
else
printf("Not allocated");
}
}
int main() {
int pNo, bNo, choice;
printf("Enter the number of processes: ");
scanf("%d", &pNo);
printf("Enter the number of blocks: ");
scanf("%d", &bNo);
int processes[pNo], allocated[pNo], blocks[bNo], occupied[bNo];
printf("\nEnter process sizes one by one: ");
for (int i=0; i<pNo; i++) {
scanf("%d", &processes[i]);
allocated[i] = -1;
}
printf("\nEnter block sizes one by one: ");
for (int i=0; i<bNo; i++) {
scanf("%d", &blocks[i]);
occupied[i] = 0;
}
// best fit
for (int i = 0; i<pNo; i++) {
int index = -1;
for (int j = 0; j<bNo; j++) {
if (blocks[j] >= processes[i] && !occupied[j]) {
if (index == -1 || blocks[j] < blocks[index])
index = j;
}
}
if (index != -1) {
allocated[i] = index;
occupied[index] = 1;
}
}
printProcesses(pNo, allocated, processes);
}