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

Memory Allocation Algorithms in C++

Uploaded by

ms.inahus
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Memory Allocation Algorithms in C++

Uploaded by

ms.inahus
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <iostream>

#include <climits>
using namespace std;

// ------------------------------------------------------
// FIRST FIT FUNCTION
// ------------------------------------------------------
void firstFit(int blockSize[], int m, int processSize[], int n) {
int blockRem[100], allocation[100];

// Copy block sizes to remaining array


for (int i = 0; i < m; i++)
blockRem[i] = blockSize[i];

// Initialize all processes as not allocated


for (int i = 0; i < n; i++)
allocation[i] = -1;

// First Fit logic


for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (blockRem[j] >= processSize[i]) {
allocation[i] = j;
blockRem[j] -= processSize[i];
break;
}
}
}

// Print result
cout << "\nFirst Fit:\nProcess\tSize\tBlock\n";
for (int i = 0; i < n; i++) {
cout << i + 1 << "\t" << processSize[i] << "\t";
if (allocation[i] != -1)
cout << allocation[i] + 1;
else
cout << "Not allocated";
cout << endl;
}
}

// ------------------------------------------------------
// BEST FIT FUNCTION
// ------------------------------------------------------
void bestFit(int blockSize[], int m, int processSize[], int n) {
int blockRem[100], allocation[100];

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


blockRem[i] = blockSize[i];

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


allocation[i] = -1;

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


int bestIdx = -1, bestRem = INT_MAX;

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


if (blockRem[j] >= processSize[i] &&
blockRem[j] - processSize[i] < bestRem) {
bestRem = blockRem[j] - processSize[i];
bestIdx = j;
}
}

if (bestIdx != -1) {
allocation[i] = bestIdx;
blockRem[bestIdx] -= processSize[i];
}
}

cout << "\nBest Fit:\nProcess\tSize\tBlock\n";


for (int i = 0; i < n; i++) {
cout << i + 1 << "\t" << processSize[i] << "\t";
if (allocation[i] != -1)
cout << allocation[i] + 1;
else
cout << "Not allocated";
cout << endl;
}
}

// ------------------------------------------------------
// WORST FIT FUNCTION
// ------------------------------------------------------
void worstFit(int blockSize[], int m, int processSize[], int n) {
int blockRem[100], allocation[100];

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


blockRem[i] = blockSize[i];

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


allocation[i] = -1;

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


int worstIdx = -1, worstRem = -1;

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


if (blockRem[j] >= processSize[i] &&
blockRem[j] - processSize[i] > worstRem) {
worstRem = blockRem[j] - processSize[i];
worstIdx = j;
}
}

if (worstIdx != -1) {
allocation[i] = worstIdx;
blockRem[worstIdx] -= processSize[i];
}
}

cout << "\nWorst Fit:\nProcess\tSize\tBlock\n";


for (int i = 0; i < n; i++) {
cout << i + 1 << "\t" << processSize[i] << "\t";
if (allocation[i] != -1)
cout << allocation[i] + 1;
else
cout << "Not allocated";
cout << endl;
}
}

// ------------------------------------------------------
// MAIN
// ------------------------------------------------------
int main() {
int m, n;
int blockSize[100], processSize[100];

cout << "Enter number of memory blocks: ";


cin >> m;

cout << "Enter sizes of blocks:\n";


for (int i = 0; i < m; i++)
cin >> blockSize[i];

cout << "Enter number of processes: ";


cin >> n;

cout << "Enter sizes of processes:\n";


for (int i = 0; i < n; i++)
cin >> processSize[i];

firstFit(blockSize, m, processSize, n);


bestFit(blockSize, m, processSize, n);
worstFit(blockSize, m, processSize, n);

return 0;
}

You might also like