OS Lab 6B
OS Lab 6B
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.
#include <stdio.h>
#define MAX_BLOCKS 10
#define MAX_PROCESSES 5
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}};
return 0;
}
Example Output
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.