3
1
SystemsProgrammingAndOperat.../Codes/Group B/Code-B6.cpp

99 lines
2.5 KiB
C++
Raw Normal View History

#include <iostream>
2024-10-12 17:44:02 +05:30
using namespace std;
int blocks[] = {100, 500, 200, 300, 600};
2024-10-12 17:44:02 +05:30
int processes[] = {212, 417, 112, 426};
int blkSize = sizeof(blocks) / sizeof(blocks[0]);
int prSize = sizeof(processes) / sizeof(processes[0]);
2024-10-12 17:44:02 +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
}
if (check == false) {
cout<<endl<<"Process "<<processes[i]<<" has not been allocated.";
}
}
2024-10-12 17:44:02 +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-10-12 17:44:02 +05:30
}
void BestFit() {
cout<<endl<<"BEST FIT:";
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-10-12 17:44:02 +05:30
}
void WorstFit() {
cout<<endl<<"WORST FIT:";
for (int i=0; i<prSize; i++) {
bool check = false;
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
}
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
}
int main () {
// ONLY RUN ONE FUNCTION AT A TIME.
// FirstFit();
// NextFit();
BestFit();
// WorstFit();
return 0;
2024-10-12 17:44:02 +05:30
}