April 22, 2004
 
« Programming? Not funny! (There's nothing funny down that road) »

Just had to show you what a monstrosity of a program I had to do last night and this afternoon:

#include <iostream.h>
#include <queue>
#include <deque>
#include <string>
#include "process.h"

void reInitt();
void FCFS(); // other file
void SJN(); // other file
void RR(); // this file
void SRTN(); // other file
void HRRN(); // this file
void print(string, deque<Process>); // this file

Process *CPU;
Process *procs[50];
Process *ptemp[50];

int totalTime;
int cpuBusy;
int aveRespTime;
int aveTurnaround;
int aveWait;
int highWait;

int main() {
// initialize processes
for (int x = 0; x < 50; x++)
procs[x] = new Process(x);

reInitt();

FCFS();
reInitt();

SJN();
reInitt();

RR();
reInitt();

SRTN();
reInitt();

HRRN();

for (int x = 0; x < 50; x++) // deallocation
delete procs[x];

return 0;
} // end of main

void reInitt() {
for (int x = 0; x < 50; x++) {
ptemp[x] = procs[x];
ptemp[x]->reInit();
}
totalTime = 0;
cpuBusy = 0;
highWait = 0;
} // end of function

void RR() {
int q = 10; // time quantum
deque<Process> rrQ;
deque<Process> finishQ;
int inCPU;
while (1)
{
for (int n = 0; n < 50; n++)
if (ptemp[n]->getEntry() == totalTime) // time to come in
rrQ.push_back(*ptemp[n]); // add to the ready queue

if (rrQ.size() == 0) {
++totalTime;
}
for (int x = 0; x < rrQ.size(); x++) {
inCPU = x;
for (int y = 0; y < rrQ.size(); y++) {
if (x == y) { // current process is in CPU
for (int r = 0; r < q; r++) {
rrQ[x].service(1);
++totalTime;
++cpuBusy;
if (rrQ[x].getServTime() == rrQ[x].getFirstOutput()) // if it produces a response now
rrQ[x].setResponseTime(totalTime - rrQ[x].getEntryTime()); // set the response time to now

if (rrQ[x].getCPU() == 0) { // process is done
r = q;
deque<Process>::iterator itr = rrQ.begin();
itr += x;
finishQ.push_back(rrQ[x]); // add to finish queue
rrQ.erase(itr); // remove from ready queue
}

} // end service loop
} // end if x = y statement
else
rrQ[y].waiting(1);
} // end looking-for-x loop
} // end go-through-ready-queue-once for loop

if (finishQ.size() == 50)
break; // done
} // end CPU loop
print("Round Robin", finishQ);


} // end of RR

void HRRN() {
deque<Process> readyQ;
deque<Process> finishQ;
float hhr; // holds the highest response ratio (wait time / service time)
int hhrLoc;
while(1) {
hhr = -1;
hhrLoc = 0;
for (int x = 0; x < 50; x++)
if (ptemp[x]->getEntry() == totalTime) // time to come in
readyQ.push_back(*ptemp[x]); // add it to the ready queue

for (int x = 0; x < readyQ.size(); x++) { // "loop 1" -- find HHR
if ((float) readyQ[x].getWait() / readyQ[x].getServTime() > hhr) { // if this has a higher HHR so far
hhr = (float) readyQ[x].getWait() / readyQ[x].getServTime(); // make it the highest HHR
hhrLoc = x; // and remember the location
}
}


for (int x = 0; x < readyQ.size(); x++) { // "loop 2" -- look for HHR, process it
if (x == hhrLoc) {
readyQ[x].service(1); // service it by one tick
++cpuBusy;
if (readyQ[x].getServTime() == readyQ[x].getFirstOutput()) // if it produces a response now
readyQ[x].setResponseTime(totalTime - readyQ[x].getEntryTime()); // set the response time to now
if (readyQ[x].getCPU() == 0) { // if CPU time = 0, therefore done
deque<Process>::iterator itr = readyQ.begin();
itr += x;
finishQ.push_back(readyQ[x]); // put in finish queue
readyQ.erase(itr); //rid from ready queue
}
}
else
readyQ[x].waiting(1); // increase the wait time by 1
}
++ totalTime;
if (finishQ.size() == 50)
break;
}
print("Highest Resp. Ratio Next", finishQ);
} // end of HRRN

void print(string alg, deque<Process> finishQ) {
aveWait = 0;
aveTurnaround = 0;
aveRespTime = 0;
for (int x = 0; x < 50; x++) {
aveWait += finishQ[x].getWait();
aveTurnaround += finishQ[x].getTurnaround();
aveRespTime += finishQ[x].getResponseTime();
if (highWait < finishQ[x].getWait())
highWait = finishQ[x].getWait();
}
aveWait /= 50;
aveTurnaround /= 50;
aveRespTime /= 50;

cout << "Results for '" << alg << "' algorithm:" << endl
<< "Total Time: " << totalTime << " ms" << endl
<< " Busy Time: " << cpuBusy << " ms" << endl
<< "Avg. Wait: " << aveWait << " ms " <<endl
<< "Avg. Turnaround: " << aveTurnaround << " ms" << endl
<< "Avg. Response Time: " << aveRespTime << " ms" << endl
<< "Highest Wait Time: " << highWait << " ms" << endl
<< "Throughput: 1 process done per " << (totalTime/50) << " ms" << endl << endl;
}




Powered by Blogger

Weblog Commenting and Trackback by HaloScan.com