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

Untitled Document

The document defines structures and functions for different memory allocation algorithms. It contains a MemoryBlock structure to represent memory blocks with size, allocation status, and process ID. It defines firstFit, bestFit, and worstFit functions that implement the respective allocation algorithms to allocate an array of processes to memory blocks. The main function provides an example usage with sample data and calls each allocation function followed by a print function to output the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Untitled Document

The document defines structures and functions for different memory allocation algorithms. It contains a MemoryBlock structure to represent memory blocks with size, allocation status, and process ID. It defines firstFit, bestFit, and worstFit functions that implement the respective allocation algorithms to allocate an array of processes to memory blocks. The main function provides an example usage with sample data and calls each allocation function followed by a print function to output the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>

// Structure to represent a memory block


struct MemoryBlock {
int size;
int allocated;
int process_id;
};

// Function to perform First Fit allocation


void firstFit(struct MemoryBlock memory_blocks[], int num_blocks, int processes[], int
num_processes) {
for (int i = 0; i < num_processes; ++i) {
for (int j = 0; j < num_blocks; ++j) {
if (!memory_blocks[j].allocated && memory_blocks[j].size >= processes[i]) {
memory_blocks[j].allocated = 1;
memory_blocks[j].process_id = i + 1;
break;
}
}
}
}

// Function to perform Best Fit allocation


void bestFit(struct MemoryBlock memory_blocks[], int num_blocks, int processes[], int
num_processes) {
for (int i = 0; i < num_processes; ++i) {
int best_fit_index = -1;

for (int j = 0; j < num_blocks; ++j) {


if (!memory_blocks[j].allocated && memory_blocks[j].size >= processes[i]) {
if (best_fit_index == -1 || memory_blocks[j].size < memory_blocks[best_fit_index].size)
{
best_fit_index = j;
}
}
}

if (best_fit_index != -1) {
memory_blocks[best_fit_index].allocated = 1;
memory_blocks[best_fit_index].process_id = i + 1;
}
}
}
// Function to perform Worst Fit allocation
void worstFit(struct MemoryBlock memory_blocks[], int num_blocks, int processes[], int
num_processes) {
for (int i = 0; i < num_processes; ++i) {
int worst_fit_index = -1;

for (int j = 0; j < num_blocks; ++j) {


if (!memory_blocks[j].allocated && memory_blocks[j].size >= processes[i]) {
if (worst_fit_index == -1 || memory_blocks[j].size >
memory_blocks[worst_fit_index].size) {
worst_fit_index = j;
}
}
}

if (worst_fit_index != -1) {
memory_blocks[worst_fit_index].allocated = 1;
memory_blocks[worst_fit_index].process_id = i + 1;
}
}
}

// Function to print memory allocation details


void printAllocation(struct MemoryBlock memory_blocks[], int num_blocks) {
printf("Memory Allocation:\n");
for (int i = 0; i < num_blocks; ++i) {
if (memory_blocks[i].allocated) {
printf("Block %d allocated to Process %d (Size: %d)\n", i + 1,
memory_blocks[i].process_id, memory_blocks[i].size);
}
}
}

int main() {
// Example Usage
struct MemoryBlock memory_blocks[] = {{100, 0, 0}, {200, 0, 0}, {150, 0, 0}};
int processes[] = {120, 80, 200};
int num_blocks = sizeof(memory_blocks) / sizeof(memory_blocks[0]);
int num_processes = sizeof(processes) / sizeof(processes[0]);

// First Fit
firstFit(memory_blocks, num_blocks, processes, num_processes);
printAllocation(memory_blocks, num_blocks);
// Reset Memory Blocks
for (int i = 0; i < num_blocks; ++i) {
memory_blocks[i].allocated = 0;
memory_blocks[i].process_id = 0;
}

// Best Fit
bestFit(memory_blocks, num_blocks, processes, num_processes);
printAllocation(memory_blocks, num_blocks);

// Reset Memory Blocks


for (int i = 0; i < num_blocks; ++i) {
memory_blocks[i].allocated = 0;
memory_blocks[i].process_id = 0;
}

// Worst Fit
worstFit(memory_blocks, num_blocks, processes, num_processes);
printAllocation(memory_blocks, num_blocks);

return 0;
}

You might also like