0% found this document useful (0 votes)
12 views9 pages

22IT3044 - OS - Assignment - Lab 8

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)
12 views9 pages

22IT3044 - OS - Assignment - Lab 8

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/ 9

Assignment 8

Problem 1

#include <stdio.h>

#define MAX_BLOCKS 10
#define MAX_PROCESSES 10

// Function to implement memory allocation


void allocateMemory(int blockSizes[], int numBlocks, int processSizes[], int
numProcesses) {
int i, j;
int allocation[numProcesses];
int originalBlockSizes[numBlocks]; // To keep track of the original block sizes

// Copy the block sizes to preserve the original values for output
for (i = 0; i < numBlocks; i++) {
originalBlockSizes[i] = blockSizes[i];
}

// Initially, all processes are not allocated memory


for (i = 0; i < numProcesses; i++) {
allocation[i] = -1; // -1 indicates no block allocated
}

// Loop through all processes and try to allocate them blocks


for (i = 0; i < numProcesses; i++) {
// Search for a block that can fit the process
for (j = 0; j < numBlocks; j++) {
if (blockSizes[j] >= processSizes[i]) {
allocation[i] = j; // Allocate block j to process i
blockSizes[j] = -1; // Mark block j as used (no longer available)
break; // No need to search further for this process
}
}
}

// Output the allocation results


for (i = 0; i < numProcesses; i++) {
if (allocation[i] != -1) {
// Print the original block size, not the modified one
printf("The Process %d allocated to %d\n", i,
originalBlockSizes[allocation[i]]);
} else {
printf("The Process %d not allocated\n", i);
}
}
}

int main() {
int numBlocks, numProcesses;
int blockSizes[MAX_BLOCKS], processSizes[MAX_PROCESSES];

// Input the number of memory blocks and their sizes


printf("Enter the number of memory blocks: ");
scanf("%d", &numBlocks);

printf("Enter the sizes of the memory blocks:\n");


for (int i = 0; i < numBlocks; i++) {
printf("Block %d: ", i);
scanf("%d", &blockSizes[i]);
}

// Input the number of processes and their required memory sizes


printf("Enter the number of processes: ");
scanf("%d", &numProcesses);

printf("Enter the memory requirements of the processes:\n");


for (int i = 0; i < numProcesses; i++) {
printf("Process %d: ", i);
scanf("%d", &processSizes[i]);
}

// Call the allocateMemory function


allocateMemory(blockSizes, numBlocks, processSizes, numProcesses);

return 0;
}

Problem 2

Best fit Case

#include <stdio.h>

struct Block {
int size;
int is_allocated;
int fragment;
};
struct File {
int size;
int block_index;
int fragment;
};

void bestFit(int num_blocks, int num_files, struct Block blocks[], struct File
files[]) {
for (int i = 0; i < num_files; i++) {
int best_index = -1;

// Find the smallest block that can fit the file


for (int j = 0; j < num_blocks; j++) {
if (!blocks[j].is_allocated && blocks[j].size >= files[i].size) {
if (best_index == -1 || blocks[j].size < blocks[best_index].size) {
best_index = j;
}
}
}

// If a suitable block is found, allocate it


if (best_index != -1) {
files[i].block_index = best_index + 1; // Store block number (1-indexed)
blocks[best_index].is_allocated = 1;
files[i].fragment = blocks[best_index].size - files[i].size;
blocks[best_index].fragment = files[i].fragment;
} else {
files[i].block_index = -1; // No suitable block found
files[i].fragment = -1;
}
}
}

int main() {
int num_blocks, num_files;
// Input number of blocks and files
printf("Enter number of blocks: ");
scanf("%d", &num_blocks);
printf("Enter number of files: ");
scanf("%d", &num_files);

struct Block blocks[num_blocks];


struct File files[num_files];

// Input sizes of blocks


printf("Enter sizes of blocks:\n");
for (int i = 0; i < num_blocks; i++) {
printf("Block %d size: ", i + 1);
scanf("%d", &blocks[i].size);
blocks[i].is_allocated = 0;
blocks[i].fragment = 0;
}

// Input sizes of files


printf("Enter sizes of files:\n");
for (int i = 0; i < num_files; i++) {
printf("File %d size: ", i + 1);
scanf("%d", &files[i].size);
files[i].block_index = -1;
files[i].fragment = -1;
}

// Perform Best Fit allocation


bestFit(num_blocks, num_files, blocks, files);

// Output results
printf("\nFile_no\tFile_size\tBlock_no\tBlock_size\tFragment\n");
for (int i = 0; i < num_files; i++) {
if (files[i].block_index != -1) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\n",
i + 1, files[i].size, files[i].block_index, blocks[files[i].block_index - 1].size,
files[i].fragment);
} else {
printf("%d\t%d\t\tNot Allocated\n", i + 1, files[i].size);
}
}

return 0;
}

Worst fit Case

#include <stdio.h>

struct Block {
int size;
int is_allocated;
int fragment;
};
struct File {
int size;
int block_index;
int fragment;
};

void worstFit(int num_blocks, int num_files, struct Block blocks[], struct File
files[]) {
for (int i = 0; i < num_files; i++) {
int worst_index = -1;

// Find the largest block that can fit the file


for (int j = 0; j < num_blocks; j++) {
if (!blocks[j].is_allocated && blocks[j].size >= files[i].size) {
if (worst_index == -1 || blocks[j].size > blocks[worst_index].size) {
worst_index = j;
}
}
}

// If a suitable block is found, allocate it


if (worst_index != -1) {
files[i].block_index = worst_index + 1; // Store block number (1-indexed)
blocks[worst_index].is_allocated = 1;
files[i].fragment = blocks[worst_index].size - files[i].size;
blocks[worst_index].fragment = files[i].fragment;
} else {
files[i].block_index = -1; // No suitable block found
files[i].fragment = -1;
}
}
}

int main() {
int num_blocks, num_files;
// Input number of blocks and files
printf("Enter number of blocks: ");
scanf("%d", &num_blocks);
printf("Enter number of files: ");
scanf("%d", &num_files);

struct Block blocks[num_blocks];


struct File files[num_files];

// Input sizes of blocks


printf("Enter sizes of blocks:\n");
for (int i = 0; i < num_blocks; i++) {
printf("Block %d size: ", i + 1);
scanf("%d", &blocks[i].size);
blocks[i].is_allocated = 0;
blocks[i].fragment = 0;
}

// Input sizes of files


printf("Enter sizes of files:\n");
for (int i = 0; i < num_files; i++) {
printf("File %d size: ", i + 1);
scanf("%d", &files[i].size);
files[i].block_index = -1;
files[i].fragment = -1;
}

// Perform Worst Fit allocation


worstFit(num_blocks, num_files, blocks, files);

// Output results
printf("\nFile_no\tFile_size\tBlock_no\tBlock_size\tFragment\n");
for (int i = 0; i < num_files; i++) {
if (files[i].block_index != -1) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\n",
i + 1, files[i].size, files[i].block_index, blocks[files[i].block_index - 1].size,
files[i].fragment);
} else {
printf("%d\t%d\t\tNot Allocated\n", i + 1, files[i].size);
}
}

return 0;
}

You might also like