我正在制作一个时钟驱动的模拟程序,除了其他问题之外,我的主要while循环,while(jobsCompleted < jobsToComplete)
循环次数超过预期/需要。例如,如果我将500分配给jobsToComplete
,则程序结束时的输出将告诉我已完成505个作业。我现在尝试调试这个问题至少一个小时,但无济于事。任何帮助表示赞赏。谢谢!
#include <iostream>
#include <string>
#include <stdlib.h>
#include <queue>
#include <fstream>
#include "job.cpp"
using namespace std;
int main()
{
ofstream cpuSim;
cpuSim.open("cpuSim.out.txt");
int clock = 0, jobsCompleted = 0, jobsToComplete = 0, probUser = 0, probability, id = 0;
jobType_t job_type;
int inWQ, outWQ, inCPUQ, outCPUQ, required, given, jobTypeInt, timeSpentInCPUqueue = 0, timeSpentInWaitQueue = 0, CPUidle = 0;
queue<job> CPUqueue, waitQueue;
int numIO = 0, numCPU = 0;
srand(time(NULL));
cout << "Enter how many jobs need to be completed: ";
cin >> jobsToComplete;
cout << endl << "Enter the probability that a new job is created: ";
cin >> probUser;
cout << endl;
while(jobsCompleted < jobsToComplete)
{
clock++;
probability = rand() % 100 + 1;
if(probability > probUser)
{
for(int i=0; i<jobsToComplete; i++)
{
id = rand() % 1000 + 1;
jobTypeInt = rand() % 100 + 1;
if(jobTypeInt >= 50)
job_type = IO_bound;
else
job_type = CPU_bound;
required = rand() % 10;
job *newJob = new job(id, job_type, inWQ, outWQ, inCPUQ, outCPUQ, required, given);
waitQueue.push(*newJob);
}
while((CPUqueue.size() <= 10) && waitQueue.empty() == false)
{
waitQueue.front();
job temp = waitQueue.back();
waitQueue.pop();
temp.setTimeExitedWQueue(clock);
temp.setTimeEnteredCPUQueue(clock);
CPUqueue.push(temp);
}
double oneSecond = 1.0, timeSpent = 0;
while((oneSecond > 0.0) && (!CPUqueue.empty()))
{
job top = CPUqueue.front();
CPUqueue.pop();
if(top.getJobType() == IO_bound)
{
top.setTimeGiven(top.getTimeGiven() + .1);
timeSpent = .1;
numIO++;
}
else
{
top.setTimeGiven(top.getTimeGiven() + .2);
timeSpent = .2;
numCPU++;
}
if(top.getTimeRequired() <= top.getTimeGiven())
{
top.setTimeExitedCPUQueue(clock);
jobsCompleted++;
timeSpentInWaitQueue += (top.getTimeExitedWQueue() - top.getTimeEnteredWQueue());
timeSpentInCPUqueue += (top.getTimeExitedCPUQueue() - top.getTimeEnteredCPUQueue());
}
else
CPUqueue.push(top);
oneSecond -= timeSpent;
if((clock%60 == 0) && (clock > 600)) //every 60 seconds after the first 10 minutes
{
cout << "After the first 10 minutes:" << endl;
cout << "Time: " << clock << endl;
cout << "Number of jobs in the wait queue: " << waitQueue.size() << endl;
cout << "Number of jobs in the CPU queue: " << CPUqueue.size() << endl;
job temp1 = waitQueue.front();
job temp2 = CPUqueue.front();
cout << "Job number of front wait job: " << temp1.getID() << endl;
cout << "Job number of front CPU job: " << temp2.getID() << endl;
}
else
{
cout << "Job Number: " << jobsCompleted << endl;
cout << "Job ID: " << top.getID() << endl;
cout << "Job Type: " << top.getJobType() << endl;
cout << "Time in CPU Queue: " << timeSpentInCPUqueue << endl;
cout << "Time Entered CPU Queue: " << top.getTimeEnteredCPUQueue() << endl << endl;
}
}
if((oneSecond > 0) && (CPUqueue.empty()))
CPUidle += oneSecond;
}
}
cout << "I/O_bound jobs: " << numIO << endl;
cout << "CPU_bound jobs: " << numCPU << endl;
cout << "*****JOBS COMPLETED: " << jobsCompleted << " *****" << endl << endl;
return 0;
}
作为一个不太相关的问题,我无法正确打印出我的枚举数据类型,也无法正确地将我的ID传递到*newJob
...
答案 0 :(得分:5)
假设它已经循环了一段时间,现在jobsCompleted
是499(而你的jobsToComplete
是500)。好的,这是最后一个循环吗?是!但是jobsCompleted
的递增发生在另一个嵌套的while
循环中。因此,如果嵌套循环发生6次,jobsCompleted
将为505,然后外部while循环将结束,从而使您在505处完成的作业总数。
告诉你如何修复它需要理解你的代码的逻辑,但是我有点太多想弄清楚了。也许这会对你有帮助。
答案 1 :(得分:1)
通过查看代码,很明显发生这种情况是因为您遇到类似以下情况的情况
while (x < y) {
...
while (condition) {
...
if (condition) {
++x;
}
}
}
这意味着对于每次外部迭代,您可能会多次递增x
,因此您输入最后一次迭代(x == 499
)然后在内部循环内递增6次。你应该调试那部分代码来理解它发生的原因,明确你应该检查这两个条件:
while((oneSecond > 0.0) && (!CPUqueue.empty()))
if(top.getTimeRequired() <= top.getTimeGiven())
因为在最后一次迭代中,这两次都是真的。