Untitled
unknown
plain_text
2 years ago
1.3 kB
8
Indexable
#include <stdio.h>
#define MAX_BLOCKS 100
#define MAX_PROCESS 100
void bestFit(int blocks[], int m, int process[], int n) {
int allocation[n];
int blockAllocated[m];
for (int i = 0; i < m; i++) {
blockAllocated[i] = 0;
}
for (int i = 0; i < n; i++) {
int bestIdx = -1;
for (int j = 0; j < m; j++) {
if (blocks[j] >= process[i] && !blockAllocated[j]) {
if (bestIdx == -1 || blocks[j] < blocks[bestIdx])
bestIdx = j;
}
}
if (bestIdx != -1) {
allocation[i] = bestIdx;
blockAllocated[bestIdx] = 1;
blocks[bestIdx] -= process[i];
} else {
allocation[i] = -1;
}
}
printf("Process No\tProcess Size\tBlock Number\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i + 1, process[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
}
int main() {
int blocks[MAX_BLOCKS] = {100, 50, 30, 120, 35};
int process[MAX_PROCESS] = {40, 10, 30, 60};
int m = 5; // Number of blocks
int n = 4; // Number of processes
bestFit(blocks, m, process, n);
return 0;
}
Editor is loading...
Leave a Comment