Simplified and improved code B6.
- Changed variable names and made prSize and blkSize global variables - Added note in main function
This commit is contained in:
parent
c8304d11c5
commit
f598faff49
@ -1,117 +1,96 @@
|
|||||||
#include<iostream>
|
#include <iostream>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int blksize[] = {100, 500, 200, 300, 600};
|
int blocks[] = {100, 500, 200, 300, 600};
|
||||||
int processes[] = {212, 417, 112, 426};
|
int processes[] = {212, 417, 112, 426};
|
||||||
|
int blkSize = sizeof(blocks) / sizeof(blocks[0]);
|
||||||
|
int prSize = sizeof(processes) / sizeof(processes[0]);
|
||||||
|
|
||||||
void firstfit(int pr[], int pr_size, int blk[], int blk_size)
|
void FirstFit() {
|
||||||
{
|
cout<<endl<<"FIRST FIT:";
|
||||||
for(int i = 0; i < pr_size; i++)
|
for (int i=0; i<prSize; i++) {
|
||||||
{
|
bool check = false;
|
||||||
bool check = false;
|
for(int j=0; j<blkSize; j++) {
|
||||||
for(int j = 0; j < blk_size; j++)
|
if ((blocks[j] - processes[i] >= 0)) {
|
||||||
{
|
blocks[j] -= processes[i];
|
||||||
if((blk[j] - pr[i]) >= 0)
|
check = true;
|
||||||
{
|
cout<<endl<<"Process "<<processes[i]<<" has been allocated to block "<<j+1;
|
||||||
blk[j] = blk[j] - pr[i];
|
break;
|
||||||
cout<<"\nProcess with value "<<pr[i]<<" allocated at block "<<j+1;
|
}
|
||||||
check = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(!check)
|
|
||||||
{
|
|
||||||
cout<<"\nProcess with value "<<pr[i]<<" not allocated";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
if (check == false) {
|
||||||
|
cout<<endl<<"Process "<<processes[i]<<" has not been allocated.";
|
||||||
void bestfit(int pr[], int pr_size, int blk[], int blk_size)
|
|
||||||
{
|
|
||||||
for(int i = 0; i < pr_size; i++)
|
|
||||||
{
|
|
||||||
bool check = false;
|
|
||||||
int k, store = 999;
|
|
||||||
for(int j = 0; j < blk_size; j++)
|
|
||||||
{
|
|
||||||
if((blk[j] - pr[i]) >= 0 && (blk[j] - pr[i]) < store)
|
|
||||||
{
|
|
||||||
k = j + 1;
|
|
||||||
store = blk[j] - pr[i];
|
|
||||||
check = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(!check)
|
|
||||||
{
|
|
||||||
cout<<"\nProcess with value "<<pr[i]<<" not allocated";
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
blk[k - 1] = blk[k - 1] - pr[i];
|
|
||||||
cout<<"\nProcess with value "<<pr[i]<<" allocated at block "<<k;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void worstfit(int pr[], int pr_size, int blk[], int blk_size)
|
void NextFit() {
|
||||||
{
|
int j = 0;
|
||||||
for(int i = 0; i < pr_size; i++)
|
cout<<endl<<"NEXT FIT:";
|
||||||
{
|
for (int i=0; i<prSize; i++) {
|
||||||
bool check = false;
|
bool check = false;
|
||||||
int k, store = -1;
|
for (int k=0; k<blkSize; k++) {
|
||||||
for(int j = 0; j < blk_size; j++)
|
if ((blocks[j] - processes[i]) >= 0) {
|
||||||
{
|
blocks[j] = blocks[j] - processes[i];
|
||||||
if((blk[j] - pr[i]) >= 0 && (blk[j] - pr[i]) > store)
|
cout<<endl<<"Process "<<processes[i]<<" has been allocated to block "<<j+1;
|
||||||
{
|
check = true;
|
||||||
k = j + 1;
|
break;
|
||||||
store = blk[j] - pr[i];
|
}
|
||||||
check = true;
|
j = (j + 1) % blkSize;
|
||||||
}
|
|
||||||
}
|
|
||||||
if(!check)
|
|
||||||
{
|
|
||||||
cout<<"\nProcess with value "<<pr[i]<<" not allocated";
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
blk[k - 1] = blk[k - 1] - pr[i];
|
|
||||||
cout<<"\nProcess with value "<<pr[i]<<" allocated at block "<<k;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
if (check == false) {
|
||||||
|
cout<<endl<<"Process "<<processes[i]<<" has not been allocated.";
|
||||||
void nextfit(int pr[], int pr_size, int blk[], int blk_size)
|
|
||||||
{
|
|
||||||
int j = 0;
|
|
||||||
for(int i = 0; i < pr_size; i++)
|
|
||||||
{
|
|
||||||
bool check = false;
|
|
||||||
for(int k = 0; k < blk_size; k++)
|
|
||||||
{
|
|
||||||
if((blk[j] - pr[i]) >= 0)
|
|
||||||
{
|
|
||||||
blk[j] = blk[j] - pr[i];
|
|
||||||
cout<<"\nProcess with value "<<pr[i]<<" allocated at block "<<j+1;
|
|
||||||
check = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
j = (j + 1) % blk_size;
|
|
||||||
}
|
|
||||||
if(!check)
|
|
||||||
{
|
|
||||||
cout<<"\nProcess with value "<<pr[i]<<" not allocated";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
void BestFit() {
|
||||||
{
|
for (int i=0; i<prSize; i++) {
|
||||||
int blk_size = sizeof(blksize)/sizeof(blksize[0]);
|
bool check = false;
|
||||||
int pr_size = sizeof(processes)/sizeof(processes[0]);
|
int k, store = 999;
|
||||||
|
for (int j=0; j<blkSize; j++) {
|
||||||
////Note: Use only one function at a time to get the correct output
|
if ((blocks[j] - processes[i]) >= 0 && (blocks[j] - processes[i]) < store) {
|
||||||
|
k = j + 1;
|
||||||
//firstfit(processes, pr_size, blksize, blk_size);
|
store = blocks[j] - processes[i];
|
||||||
//bestfit(processes, pr_size, blksize, blk_size);
|
check = true;
|
||||||
//worstfit(processes, pr_size, blksize, blk_size);
|
}
|
||||||
nextfit(processes, pr_size, blksize, blk_size);
|
}
|
||||||
return 0;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WorstFit() {
|
||||||
|
bool check = false;
|
||||||
|
for (int i=0; i<prSize; i++) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main () {
|
||||||
|
// ONLY RUN ONE FUNCTION AT A TIME.
|
||||||
|
// FirstFit();
|
||||||
|
// NextFit();
|
||||||
|
BestFit();
|
||||||
|
// WorstFit();
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user