Write a program using C to implement first-fit, best-fit and worst-fit allocation strategies.
Below is the C program that implements the First-Fit, Best-Fit, and Worst-Fit memory allocation strategies for a given set of memory blocks and a set of processes.
#include <stdio.h>
#define MAX_PROCESSES 100
#define MAX_BLOCKS 100
void firstFit(int blockSize[], int m, int processSize[], int n) {
int allocation[n];
for (int i = 0; i < n; i++) {
allocation[i] = -1; // Initially, no process is allocated
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
allocation[i] = j; // Allocate block j to process i
blockSize[j] -= processSize[i]; // Reduce available size in block
break;
}
}
}
printf("\nFirst Fit Allocation:\n");
for (int i = 0; i < n; i++) {
if (allocation[i] != -1) {
printf("Process %d allocated to Block %d\n", i + 1, allocation[i] + 1);
} else {
printf("Process %d not allocated\n", i + 1);
}
}
}
void bestFit(int blockSize[], int m, int processSize[], int n) {
int allocation[n];
for (int i = 0; i < n; i++) {
allocation[i] = -1; // Initially, no process is allocated
}
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; // Find the best fit block
}
}
}
if (bestIdx != -1) {
allocation[i] = bestIdx; // Allocate block bestIdx to process i
blockSize[bestIdx] -= processSize[i]; // Reduce available size in block
}
}
printf("\nBest Fit Allocation:\n");
for (int i = 0; i < n; i++) {
if (allocation[i] != -1) {
printf("Process %d allocated to Block %d\n", i + 1, allocation[i] + 1);
} else {
printf("Process %d not allocated\n", i + 1);
}
}
}
void worstFit(int blockSize[], int m, int processSize[], int n) {
int allocation[n];
for (int i = 0; i < n; i++) {
allocation[i] = -1; // Initially, no process is allocated
}
for (int i = 0; i < n; i++) {
int worstIdx = -1;
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
if (worstIdx == -1 || blockSize[j] > blockSize[worstIdx]) {
worstIdx = j; // Find the worst fit block
}
}
}
if (worstIdx != -1) {
allocation[i] = worstIdx; // Allocate block worstIdx to process i
blockSize[worstIdx] -= processSize[i]; // Reduce available size in block
}
}
printf("\nWorst Fit Allocation:\n");
for (int i = 0; i < n; i++) {
if (allocation[i] != -1) {
printf("Process %d allocated to Block %d\n", i + 1, allocation[i] + 1);
} else {
printf("Process %d not allocated\n", i + 1);
}
}
}
int main() {
int blockSize[MAX_BLOCKS], processSize[MAX_PROCESSES];
int m, n;
// Read number of memory blocks
printf("Enter the number of memory blocks: ");
scanf("%d", &m);
printf("Enter the sizes of the memory blocks:\n");
for (int i = 0; i < m; i++) {
printf("Block %d: ", i + 1);
scanf("%d", &blockSize[i]);
}
// Read number of processes
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the sizes of the processes:\n");
for (int i = 0; i < n; i++) {
printf("Process %d: ", i + 1);
scanf("%d", &processSize[i]);
}
// Call allocation functions
firstFit(blockSize, m, processSize, n);
bestFit(blockSize, m, processSize, n);
worstFit(blockSize, m, processSize, n);
return 0;
}