0% found this document useful (0 votes)
11 views3 pages

OS Lab 6C

The document provides a C program that simulates the First-Fit contiguous memory allocation technique, where each process is allocated to the first available memory block that can accommodate it. It includes structures for memory blocks and processes, a firstFit function to perform the allocation, and a main function to initialize data and display allocation results. The output illustrates the allocation status of each process and memory block after execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

OS Lab 6C

The document provides a C program that simulates the First-Fit contiguous memory allocation technique, where each process is allocated to the first available memory block that can accommodate it. It includes structures for memory blocks and processes, a firstFit function to perform the allocation, and a main function to initialize data and display allocation results. The output illustrates the allocation status of each process and memory block after execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Develop a C program to simulate the following contiguous memory allocation Techniques:

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.

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 first-fit memory allocation


void firstFit(struct MemoryBlock blocks[], int numBlocks, struct Process processes[], int
numProcesses) {
for (int i = 0; i < numProcesses; i++) {
int allocated = 0; // Flag to indicate if the process is allocated
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) {
// Allocate the first suitable block found
blocks[j].allocated = 1;
processes[i].allocatedBlock = j;
allocated = 1;
printf("Process %d allocated to block %d\n", i + 1, j + 1);
break; // Stop searching once the block is found
}
}

// If no block was found, mark process as unallocated


if (!allocated) {
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("First-Fit Memory Allocation\n");


firstFit(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

First-Fit Memory Allocation


Process 1 allocated to block 2
Process 2 allocated to block 5
Process 3 allocated to block 3
Process 4 cannot be allocated
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, Free
Block 5: 600 KB, Allocated

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.

You might also like