// First Fit Program with Comments
#include <stdio.h>
int main() {
int bsize[20], psize[20], allocation[20]; // Arrays to hold block sizes, process sizes, and al
int bno, pno, i, j;
printf("First Fit Memory Allocation\n");
// Input number of memory blocks
printf("Enter number of blocks: ");
scanf("%d", &bno);
printf("Enter size of each block:\n");
for (i = 0; i < bno; i++)
scanf("%d", &bsize[i]);
// Input number of processes
printf("Enter number of processes: ");
scanf("%d", &pno);
printf("Enter size of each process:\n");
for (i = 0; i < pno; i++)
scanf("%d", &psize[i]);
// Initially mark all processes as not allocated
for (i = 0; i < pno; i++)
allocation[i] = -1;
// Try to allocate each process to the first suitable block
for (i = 0; i < pno; i++) {
for (j = 0; j < bno; j++) {
if (bsize[j] >= psize[i]) { // If block can hold process
allocation[i] = j; // Allocate block j to process i
bsize[j] -= psize[i]; // Reduce available size in block
break; // Exit loop after first fit found
}
}
}
// Output allocation results
printf("\nProcess No.\tProcess Size\tBlock No.\n");
for (i = 0; i < pno; i++) {
printf("%d\t\t%d\t\t", i + 1, psize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1); // +1 for block numbering starting at 1
else
printf("Not Allocated\n");
}
return 0;
}
// Best Fit Program with Comments
#include <stdio.h>
int main() {
int bsize[20], psize[20], allocation[20];
int bno, pno, i, j, best;
printf("Best Fit Memory Allocation\n");
// Input memory blocks
printf("Enter number of blocks: ");
scanf("%d", &bno);
printf("Enter size of each block:\n");
for (i = 0; i < bno; i++)
scanf("%d", &bsize[i]);
// Input processes
printf("Enter number of processes: ");
scanf("%d", &pno);
printf("Enter size of each process:\n");
for (i = 0; i < pno; i++)
scanf("%d", &psize[i]);
// Mark all processes as not allocated
for (i = 0; i < pno; i++)
allocation[i] = -1;
// Best fit logic
for (i = 0; i < pno; i++) {
best = -1;
for (j = 0; j < bno; j++) {
if (bsize[j] >= psize[i]) { // Block can hold process
if (best == -1 || bsize[j] < bsize[best]) // Find block with smallest sufficient size
best = j;
}
}
if (best != -1) {
allocation[i] = best;
bsize[best] -= psize[i]; // Reduce block size
}
}
// Print result
printf("\nProcess No.\tProcess Size\tBlock No.\n");
for (i = 0; i < pno; i++) {
printf("%d\t\t%d\t\t", i + 1, psize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
return 0;
}
// Worst Fit Program with Comments
#include <stdio.h>
int main() {
int bsize[20], psize[20], allocation[20];
int bno, pno, i, j, worst;
printf("Worst Fit Memory Allocation\n");
// Input block details
printf("Enter number of blocks: ");
scanf("%d", &bno);
printf("Enter size of each block:\n");
for (i = 0; i < bno; i++)
scanf("%d", &bsize[i]);
// Input process details
printf("Enter number of processes: ");
scanf("%d", &pno);
printf("Enter size of each process:\n");
for (i = 0; i < pno; i++)
scanf("%d", &psize[i]);
// Initialize allocations to -1
for (i = 0; i < pno; i++)
allocation[i] = -1;
// Worst fit logic
for (i = 0; i < pno; i++) {
worst = -1;
for (j = 0; j < bno; j++) {
if (bsize[j] >= psize[i]) {
if (worst == -1 || bsize[j] > bsize[worst]) // Find block with largest size
worst = j;
}
}
if (worst != -1) {
allocation[i] = worst;
bsize[worst] -= psize[i];
}
}
// Show final allocations
printf("\nProcess No.\tProcess Size\tBlock No.\n");
for (i = 0; i < pno; i++) {
printf("%d\t\t%d\t\t", i + 1, psize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
return 0;
}