私はクロック駆動のシミュレーションプログラムを作成していますが、他の問題の中でも、メインのwhileループwhile(jobsCompleted < jobsToComplete)
が、予想/必要以上にループしています。たとえば、500をに割り当てるとするjobsToComplete
と、プログラムの最後の出力から、505個のジョブが完了したことがわかります。私はこの1つの問題を少なくとも1時間デバッグしようとしましたが、役に立ちませんでした。どんな助けでも大歓迎です。ありがとう!
#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
正しく渡すように懇願することもできません...