2024-11-05 00:21:16 +05:30
|
|
|
#include <iostream>
|
2024-10-12 17:44:02 +05:30
|
|
|
using namespace std;
|
|
|
|
|
2024-11-05 00:21:16 +05:30
|
|
|
int blocks[] = {100, 500, 200, 300, 600};
|
2024-10-12 17:44:02 +05:30
|
|
|
int processes[] = {212, 417, 112, 426};
|
2024-11-05 00:21:16 +05:30
|
|
|
int blkSize = sizeof(blocks) / sizeof(blocks[0]);
|
|
|
|
int prSize = sizeof(processes) / sizeof(processes[0]);
|
2024-10-12 17:44:02 +05:30
|
|
|
|
2024-11-05 00:21:16 +05:30
|
|
|
void FirstFit() {
|
|
|
|
cout<<endl<<"FIRST FIT:";
|
|
|
|
for (int i=0; i<prSize; i++) {
|
|
|
|
bool check = false;
|
|
|
|
for(int j=0; j<blkSize; j++) {
|
|
|
|
if ((blocks[j] - processes[i] >= 0)) {
|
|
|
|
blocks[j] -= processes[i];
|
|
|
|
check = true;
|
|
|
|
cout<<endl<<"Process "<<processes[i]<<" has been allocated to block "<<j+1;
|
|
|
|
break;
|
|
|
|
}
|
2024-10-12 17:44:02 +05:30
|
|
|
}
|
2024-11-05 00:21:16 +05:30
|
|
|
if (check == false) {
|
|
|
|
cout<<endl<<"Process "<<processes[i]<<" has not been allocated.";
|
|
|
|
}
|
|
|
|
}
|
2024-10-12 17:44:02 +05:30
|
|
|
}
|
|
|
|
|
2024-11-05 00:21:16 +05:30
|
|
|
void NextFit() {
|
|
|
|
int j = 0;
|
|
|
|
cout<<endl<<"NEXT FIT:";
|
|
|
|
for (int i=0; i<prSize; i++) {
|
|
|
|
bool check = false;
|
|
|
|
for (int k=0; k<blkSize; k++) {
|
|
|
|
if ((blocks[j] - processes[i]) >= 0) {
|
|
|
|
blocks[j] = blocks[j] - processes[i];
|
|
|
|
cout<<endl<<"Process "<<processes[i]<<" has been allocated to block "<<j+1;
|
|
|
|
check = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
j = (j + 1) % blkSize;
|
|
|
|
}
|
|
|
|
if (check == false) {
|
|
|
|
cout<<endl<<"Process "<<processes[i]<<" has not been allocated.";
|
2024-10-12 17:44:02 +05:30
|
|
|
}
|
2024-11-05 00:21:16 +05:30
|
|
|
}
|
2024-10-12 17:44:02 +05:30
|
|
|
}
|
|
|
|
|
2024-11-05 00:21:16 +05:30
|
|
|
void BestFit() {
|
2024-11-05 00:28:33 +05:30
|
|
|
cout<<endl<<"BEST FIT:";
|
2024-11-05 00:21:16 +05:30
|
|
|
for (int i=0; i<prSize; i++) {
|
|
|
|
bool check = false;
|
|
|
|
int k, store = 999;
|
|
|
|
for (int j=0; j<blkSize; j++) {
|
|
|
|
if ((blocks[j] - processes[i]) >= 0 && (blocks[j] - processes[i]) < store) {
|
|
|
|
k = j + 1;
|
|
|
|
store = blocks[j] - processes[i];
|
|
|
|
check = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (check == false) {
|
|
|
|
cout<<endl<<"Process "<<processes[i]<<" was not allocated to any block.";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
blocks[k - 1] -= processes[i];
|
|
|
|
cout<<endl<<"Process "<<processes[i]<<" was allocated to block "<<k;
|
2024-10-12 17:44:02 +05:30
|
|
|
}
|
2024-11-05 00:21:16 +05:30
|
|
|
}
|
2024-10-12 17:44:02 +05:30
|
|
|
}
|
|
|
|
|
2024-11-05 00:21:16 +05:30
|
|
|
void WorstFit() {
|
2024-11-05 00:28:33 +05:30
|
|
|
cout<<endl<<"WORST FIT:";
|
2024-11-05 00:21:16 +05:30
|
|
|
for (int i=0; i<prSize; i++) {
|
2024-11-05 00:28:33 +05:30
|
|
|
bool check = false;
|
2024-11-05 00:21:16 +05:30
|
|
|
int k, store = -1;
|
|
|
|
for (int j=0; j<blkSize; j++) {
|
|
|
|
if ((blocks[j] - processes[i] >= 0 && (blocks[j] - processes[i] > store))) {
|
|
|
|
k = j + 1;
|
|
|
|
store = blocks[j] - processes[i];
|
|
|
|
check = true;
|
|
|
|
}
|
2024-10-12 17:44:02 +05:30
|
|
|
}
|
2024-11-05 00:21:16 +05:30
|
|
|
if (check == false) {
|
|
|
|
cout<<endl<<"Process "<<processes[i]<<" was not allocated to any block.";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
blocks[k - 1] -= processes[i];
|
|
|
|
cout<<endl<<"Process "<<processes[i]<<" was allocated to block "<<k;
|
|
|
|
}
|
|
|
|
}
|
2024-10-12 17:44:02 +05:30
|
|
|
}
|
|
|
|
|
2024-11-05 00:21:16 +05:30
|
|
|
int main () {
|
|
|
|
// ONLY RUN ONE FUNCTION AT A TIME.
|
|
|
|
// FirstFit();
|
|
|
|
// NextFit();
|
|
|
|
BestFit();
|
|
|
|
// WorstFit();
|
|
|
|
return 0;
|
2024-10-12 17:44:02 +05:30
|
|
|
}
|