Untitled
unknown
plain_text
2 years ago
1.5 kB
10
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], process[MAX_PROCESS];
int m, n;
printf("Enter the number of blocks: ");
scanf("%d", &m);
printf("Enter the block sizes:\n");
for (int i = 0; i < m; i++)
scanf("%d", &blocks[i]);
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the process sizes:\n");
for (int i = 0; i < n; i++)
scanf("%d", &process[i]);
bestFit(blocks, m, process, n);
return 0;
}Editor is loading...
Leave a Comment