Basically, once the do while's start, I am stuck in an infinite loop and I do not know what is going on. Basically, this program is for processing CPU "jobs". I know the loop is in the do while. For some reason, the timeLeft never gets subtracted down to 0. It will get subtracted the first time, but then stays at the same constant integer value the entire time and just loops over and over.
If anyone could help it would be greatly appreciated. It's kind of hard to explain what I am trying to do, it is a school project. If you have anyother questions, just ask.
#include "job.h"
#include <iostream>
usingnamespace std;
int main() {
//global clock
int CLOCK = 0;
//total number of jobs
int numberOfJobs;
//number of jobs left
int jobsLeft;
//total seconds of idle time
int idleTime;
//total number of jobs
int ioJobs = 0;
int cpuJobs = 0;
//total time that job was in wait queue
int totalWaitQueueTime = 0;
//waiting queueu
queue<job> waitQueue;
//cpu processing queue
queue<job> processQueue;
//Get all the possible jobs in a vector
cout << "How many jobs do you want?: ";
cin >> numberOfJobs;
jobsLeft = numberOfJobs;
//go into there are no more jobs
for(int x = 0; x < numberOfJobs; x++)
{
//while there is not 10 jobs in the cpu process queue
while(processQueue.size() <= 9 && x < numberOfJobs)
{
//if there is is still jobs left
if(x < numberOfJobs)
{
//create new job
job tempJob = job(CLOCK);
if(tempJob.getJobType() == 1) { ioJobs++; } else { cpuJobs++; }
waitQueue.push(tempJob);
(waitQueue.front()).enterWaitQueue(CLOCK);
x++;
}
//set the time the job left waiting queue
//set the time the job entered CPU process queue
(waitQueue.front()).leaveWaitQueue(CLOCK);
//add to total time job was in wait queue
totalWaitQueueTime += ((waitQueue.front()).getExitWaiting() - (waitQueue.front()).getEnterWaiting());
//actually move the job from wait queue to process queue
processQueue.push(waitQueue.front());
//remove job from wait queue
waitQueue.pop();
}
if(x < numberOfJobs)
{
job tempJob = job(CLOCK);
if(tempJob.getJobType() == 1) { ioJobs++; } else { cpuJobs++; }
waitQueue.push(tempJob);
(waitQueue.front()).enterWaitQueue(CLOCK);
}
}
do {
double oneSecond = 1.00;
do {
double subtract, timeLeft;
timeLeft = (processQueue.front()).getTimeLeft();
if((processQueue.front()).getJobType() == 1) { subtract = 0.1; }
else { subtract = 0.2; }
if(oneSecond == 0.1 && subtract == 0.2)
{
oneSecond = 0;
idleTime += 0.1;
}
else
{
timeLeft = timeLeft - subtract;
oneSecond = oneSecond - subtract;
if(timeLeft <= 0)
{
(processQueue.front()).setTimeLeft(timeLeft);
(processQueue.front()).leaveProcess(CLOCK);
processQueue.pop();
jobsLeft = jobsLeft - 1;
if(!waitQueue.empty())
{
//set the time the job left waiting queue
//set the time the job entered CPU process queue
(waitQueue.front()).leaveWaitQueue(CLOCK);
//add to total time job was in wait queue
totalWaitQueueTime = totalWaitQueueTime + ((waitQueue.front()).getExitWaiting() - (waitQueue.front()).getEnterWaiting());
//actually move the job from wait queue to process queue
processQueue.push(waitQueue.front());
//remove job from wait queue
waitQueue.pop();
}
}
else
{
(processQueue.front()).setTimeLeft(timeLeft);
processQueue.push(processQueue.front());
processQueue.pop();
}
}
} while(oneSecond > 0);
CLOCK += 1;
} while (jobsLeft > 0);
cout << endl << "Jobs Left: " << jobsLeft << endl << endl
<< "Jobs:" << endl
<< "I/O Bound: " << ioJobs << " jobs" << endl
<< "CPU Bound: " << cpuJobs << " jobs" << endl << endl
<< "Average time job spends in wait queue: " << (totalWaitQueueTime/numberOfJobs) << " seconds" << endl
<< "The CPU is busy 0% of the time" << endl
<< "There are " << ((ioJobs+cpuJobs)*3600)/CLOCK << " jobs per performed per hour" << endl << endl;
return 0;
}
I know the loop is in the do while. For some reason, the timeLeft never gets subtracted down to 0
which do-while loop? There is no loop with timeLeft as condition.
1 2 3 4 5 6
if(oneSecond == 0.1 && subtract == 0.2) //don't compare floats for equality. Use fabs(a-b)<TOL instead
{
// oneSecond = 0; //If you just want to break the loop, then use break
idleTime += 0.1;
break;
}
Other things:
Those magic numbers are hard to follow.