0% found this document useful (0 votes)
7 views6 pages

Printout 8

Uploaded by

Desmond Krej
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)
7 views6 pages

Printout 8

Uploaded by

Desmond Krej
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/ 6

Experiment 8

#include <iostream>

#include <vector>

#include <iomanip>

struct Block {

int size;

int fragment;

bool allocated;

};

int main() {

int numBlocks, numFiles, blockSize, fileSize;

std::cout << "Enter the number of blocks: ";

std::cin >> numBlocks;

std::vector<Block> blocks(numBlocks);

for (int i = 0; i < numBlocks; ++i) {

std::cout << "Enter size of block " << i + 1 << ": ";

std::cin >> blocks[i].size;

blocks[i].allocated = false;

std::cout << "Enter the number of files: ";

std::cin >> numFiles;

std::cout << std::left << std::setw(10) << "File No" << std::setw(10) << "File Size" << std::setw(10) << "Block
No" << std::setw(12) << "Block Size" << "Fragment" << std::endl;

for (int i = 0; i < numFiles; ++i) {

std::cout << "Enter size of file " << i + 1 << ": ";

std::cin >> fileSize;

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

if (!blocks[j].allocated && blocks[j].size >= fileSize) {

blocks[j].allocated = true;
blocks[j].fragment = blocks[j].size - fileSize;

std::cout << std::left << std::setw(10) << i + 1 << std::setw(10) << fileSize << std::setw(10) << j + 1 <<
std::setw(12) << blocks[j].size << blocks[j].fragment << std::endl;

break;

return 0;

}
#include <iostream>

using namespace std;

struct Block {

int size;

int fragment;

bool allocated;

};

void bestFit(int blockSize[], int m, int processSize[], int n) {

int allocation[n];

for (int i = 0; i < n; ++i)

allocation[i] = -1;

for (int i = 0; i < n; ++i) {

int bestIdx = -1;

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

if (blockSize[j] >= processSize[i]) {

if (bestIdx == -1)

bestIdx = j;

else if (blockSize[bestIdx] > blockSize[j])

bestIdx = j;

if (bestIdx != -1) {

allocation[i] = bestIdx;

blockSize[bestIdx] -= processSize[i];

cout << "\nProcess No.\tProcess Size\tBlock no.\n";

for (int i = 0; i < n; ++i) {

cout << " " << i + 1 << "\t\t" << processSize[i] << "\t\t";
if (allocation[i] != -1)

cout << allocation[i] + 1;

else

cout << "Not Allocated";

cout << endl;

int main() {

int blockSize[] = {100, 500, 200, 300, 600};

int processSize[] = {212, 417, 112, 426};

int m = sizeof(blockSize) / sizeof(blockSize[0]);

int n = sizeof(processSize) / sizeof(processSize[0]);

bestFit(blockSize, m, processSize, n);

return 0;

}
#include <iostream>

using namespace std;

struct Block {

int size;

int fragment;

bool allocated;

};

void worstFit(int blockSize[], int m, int processSize[], int n) {

int allocation[n];

for (int i = 0; i < n; ++i)

allocation[i] = -1;

for (int i = 0; i < n; ++i) {

int worstIdx = -1;

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

if (blockSize[j] >= processSize[i]) {

if (worstIdx == -1)

worstIdx = j;

else if (blockSize[worstIdx] < blockSize[j])

worstIdx = j;

if (worstIdx != -1) {

allocation[i] = worstIdx;

blockSize[worstIdx] -= processSize[i];

cout << "\nProcess No.\tProcess Size\tBlock no.\n";

for (int i = 0; i < n; ++i) {

cout << " " << i + 1 << "\t\t" << processSize[i] << "\t\t";
if (allocation[i] != -1)

cout << allocation[i] + 1;

else

cout << "Not Allocated";

cout << endl;

int main() {

int blockSize[] = {100, 500, 200, 300, 600};

int processSize[] = {212, 417, 112, 426};

int m = sizeof(blockSize) / sizeof(blockSize[0]);

int n = sizeof(processSize) / sizeof(processSize[0]);

worstFit(blockSize, m, processSize, n);

return 0;

You might also like