Stuck on Infinite Loop

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "job.h"
#include <iostream>

using namespace 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.

1
2
if((processQueue.front()).getJobType() == 1) { subtract = 0.1; }
else { subtract = 0.2; }
Why don't just use a subtract method (polymorphism), or field?

You could use an alias to avoid this notation (processQueue.front()).setTimeLeft(timeLeft);.
Last edited on
Topic archived. No new replies allowed.