Develop a C program to simulate the following contiguous memory allocation Techniques:
b) Best fit
C program to simulate the Best-Fit contiguous memory allocation technique. In this approach,
each incoming process is allocated to the smallest available memory block that can still
accommodate the process, minimizing wasted space within each allocated block.
The program structure is similar to the previous one for Worst-Fit, but the allocation logic now
looks for the smallest suitable block for each process.
Explanation
1. Structures:
o MemoryBlock and Process structures are the same as in the Worst-Fit version.
2. bestFit Function:
o For each process, the function searches for the smallest block that is large
enough to hold the process.
o If a suitable block is found, it is marked as allocated and assigned to the process.
If no suitable block is found, the process is denied allocation.
3. Main Function:
o Initializes arrays of memory blocks and processes.
o Calls bestFit to perform memory allocation based on the Best-Fit strategy.
o Displays the allocation status of each block after processing.
Here's the code:
#include <stdio.h>
#define MAX_BLOCKS 10
#define MAX_PROCESSES 5
// Structure to represent memory blocks
struct MemoryBlock {
int size;
int allocated; // 0 if free, 1 if allocated
};
// Structure to represent processes
struct Process {
int size;
int allocatedBlock; // Index of allocated memory block, -1 if not allocated
};
// Function to perform best-fit memory allocation
void bestFit(struct MemoryBlock blocks[], int numBlocks, struct Process processes[], int
numProcesses) {
for (int i = 0; i < numProcesses; i++) {
int bestIndex = -1; // To track the index of the smallest suitable block
for (int j = 0; j < numBlocks; j++) {
// Check if the block is free and large enough to accommodate the process
if (!blocks[j].allocated && blocks[j].size >= processes[i].size) {
// Find the smallest block that fits
if (bestIndex == -1 || blocks[j].size < blocks[bestIndex].size) {
bestIndex = j;
}
}
}
// Allocate the block to the process if found
if (bestIndex != -1) {
blocks[bestIndex].allocated = 1;
processes[i].allocatedBlock = bestIndex;
printf("Process %d allocated to block %d\n", i + 1, bestIndex + 1);
} else {
processes[i].allocatedBlock = -1;
printf("Process %d cannot be allocated\n", i + 1);
}
}
}
int main() {
struct MemoryBlock blocks[MAX_BLOCKS] = {{100, 0}, {500, 0}, {200, 0}, {300, 0}, {600, 0}};
struct Process processes[MAX_PROCESSES] = {{212, -1}, {417, -1}, {112, -1}, {426, -1}, {95,
-1}};
int numBlocks = 5; // Number of memory blocks
int numProcesses = 5; // Number of processes
printf("Best-Fit Memory Allocation\n");
bestFit(blocks, numBlocks, processes, numProcesses);
printf("\nFinal Memory Allocation:\n");
for (int i = 0; i < numBlocks; i++) {
printf("Block %d: %d KB, %s\n", i + 1, blocks[i].size, blocks[i].allocated ? "Allocated" :
"Free");
}
return 0;
}
Example Output
Best-Fit Memory Allocation
Process 1 allocated to block 4
Process 2 allocated to block 2
Process 3 allocated to block 3
Process 4 allocated to block 5
Process 5 allocated to block 1
Final Memory Allocation:
Block 1: 100 KB, Allocated
Block 2: 500 KB, Allocated
Block 3: 200 KB, Allocated
Block 4: 300 KB, Allocated
Block 5: 600 KB, Allocated
Explanation of Output
Each process is allocated to the smallest block that can hold it, minimizing leftover space
in each allocated block.
For example, Process 1 (212 KB) is allocated to Block 4 (300 KB) as it's the smallest
available block that can accommodate it.