Contiguous Memory Allocation
Contiguous Memory Allocation
AIM
To implement and compare First-Fit, Best-Fit, and Worst-Fit memory allocation strategies
using contiguous memory allocation in C programming.
2. THEORY
In Contiguous Memory Allocation, each process is allocated memory in a single
contiguous block.
1. First-Fit:
2. Best-Fit:
3. Worst-Fit:
3. ALGORITHM
First-Fit Allocation
Input:
→ Allocate block
→ Break loop
3. Display allocation result.
Best-Fit Allocation
1. Initialize allocation[] to -1 for all processes.
2. For each process:
a. Find the block with minimum size ≥ process size.
b. If found:
→ Allocate block
→ Reduce block size
3. Display allocation result.
Input:
- blockSize[1...n] = sizes of n memory blocks
- processSize[1...m] = sizes of m processes
1. Initialize allocation[1...m] = -1
Worst-Fit Allocation
1. Initialize allocation[] to -1 for all processes.
2. For each process:
a. Find the block with maximum size ≥ process size.
b. If found:
→ Allocate block
→ Reduce block size
3. Display allocation result.
Input:
- blockSize[1...n] = sizes of n memory blocks
- processSize[1...m] = sizes of m processes
1. Initialize allocation[1...m] = -1
Implementation :
#include <stdio.h>
printf("\nFirst-Fit Allocation:\n");
printf("Process No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < processes; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
}
if (bestIdx != -1) {
allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];
}
}
printf("\nBest-Fit Allocation:\n");
printf("Process No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < processes; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
}
if (worstIdx != -1) {
allocation[i] = worstIdx;
blockSize[worstIdx] -= processSize[i];
}
}
printf("\nWorst-Fit Allocation:\n");
printf("Process No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < processes; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
}
int main() {
int blockSize[MAX], processSize[MAX];
int blocks, processes;
return 0;
}