OS Lab 6C
OS Lab 6C
c) First fit
C program to simulate the First-Fit contiguous memory allocation technique. In this approach,
each incoming process is allocated to the first available memory block that can accommodate it.
This method aims to minimize search time by taking the first block that fits, regardless of size.
Explanation
1. Structures:
o MemoryBlock and Process structures are the same as in the previous
versions.
2. firstFit Function:
o For each process, the function iterates over the memory blocks to find the first
block that is large enough to hold the process.
o When a suitable block is found, it is marked as allocated, and the search for that
process stops (using break).
o If no suitable block is found for a process, it is marked as unallocated.
3. Main Function:
o Initializes arrays of memory blocks and processes.
o Calls firstFit to perform memory allocation based on the First-Fit strategy.
o Displays the allocation status of each block after processing.
The code is similar to the previous versions, but the allocation logic now stops as soon as it
finds a suitable block.
#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 first block that fits.
For example, Process 1 (212 KB) is allocated to Block 2 (500 KB), the first block that can
accommodate it.
Process 4 (426 KB) cannot be allocated since no suitable block is found after the
previous allocations.