0

我正在制作一个时钟驱动的模拟程序,除其他问题外,我的主 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...

4

2 回答 2

5

假设它已经循环了一段时间,现在jobsCompleted是 499(而你的jobsToComplete是 500)。好的,这是最后一个循环,对吧?是的!但是 的递增jobsCompleted发生在另一个嵌套while循环中。因此,如果该嵌套循环发生 6 次,jobsCompleted则为 505,然后外部 while 循环将结束,使您完成的作业总数为 505。

告诉你如何修复它需要理解你的代码的逻辑,但我想弄清楚的东西太多了。也许这会对你有所帮助。

于 2012-12-06T23:25:10.017 回答
1

通过查看代码,很明显会发生这种情况,因为您遇到如下情况

while (x < y) {
  ...
  while (condition) {
    ...
    if (condition) {
      ++x;
    }
  }
}

这意味着对于每次外部迭代,可能会发生x多次递增,因此您输入最后一次迭代 ( x == 499),然后在内部循环中将其递增 6 次。您应该调试该部分代码以了解它发生的原因,明确地您应该检查这两个条件:

while((oneSecond > 0.0) && (!CPUqueue.empty()))
if(top.getTimeRequired() <= top.getTimeGiven())

因为在最后一次迭代中,两者都为真至少 6 次。

于 2012-12-06T23:26:39.120 回答