我将如何实现循环调度模拟器

How would I implement a Round Robin scheduling simulator?

本文关键字:实现 循环调度 模拟器 何实现      更新时间:2023-10-16

使用如下结构:

struct{
int ID;
int arrivalTime;
int burstTime;
};

我该如何通过结构的deque,这样如果输入如下所示:

0 0 3
1 5 2
3 8 4 

其中每一行分别是结构的ID、arrivalTime和burstTime,我可以打印出这样的内容:

Time 0 Process 0 is running
Time 2 Process 0 is running
Time 3 Processor is Idle
Time 5 Process 1 is running
Time 7 Processor is Idle
Time 8 Process 3 is running
Time 10 Process 3 is running

该输出假设时间量为2。有没有一种方法可以只使用一个数据块来实现这一点,或者创建另一个数据组作为FIFO队列来处理这一点会更容易?我知道我需要一个整数来记录经过的时间,但除此之外,这个问题真的让我很困惑。空闲时间让我很沮丧。C++代码甚至伪代码中的任何帮助都会很有帮助。谢谢

我知道我需要一个整数来跟踪经过了多少时间

我将从三个值开始——运行时间、当前进程和下一个进程。您的日程安排循环可能如下所示。为了简单起见,我将选择下一个进程的逻辑放在了一个独立的函数中:

time = 0;
currentProcess = deque.end();
while(some processes remaining)
{
nextProcess = getNextProcess(time, currentProcess, deque);
if(nextProcess->arrivalTime > time)
{
// nothing to do now
// advance time by smaller of quota or nextProcess->arrivalTime
} else {
// at least one process has work ready
if(currentProcess != nextProcess)
{
// preemt currentProcess
// start nextProcess
// advance time by the smaller of quota or nextProcess->burstTime
// reduce nextProcess->burstTime by the time advanced
} else {
// continue with current process for quota or its remaining burstTime
// reduce its burstTime
}
}
currentProcess = nextProcess;
}

实现getNextProcess取决于您的优先级标准,一种天真的方法可能看起来像这样:

  • 您从位置currentProcess + 1开始执行deque。当你到达终点时,从头开始
  • 注意最小arrivalTime大于time的过程。让我们称之为closestCandidate
  • 如果您使用arrivalTime <= timeburstTime > 0找到合适的流程,请返回
  • 如果您再次点击currentProcess,请在currentProcessclosestCandidate之间决定处理并返回哪个更好

最后要做的一件事是有效地实现循环条件。我会把它留给你来弄清楚。

注意:我不确定deque是否是这里最好的容器,我很可能会使用forward_list并在进程完成时删除它们。你也可以在deque中这样做,但这是O(n)运算。