我将如何实现循环调度模拟器
How would I implement a Round Robin scheduling simulator?
使用如下结构:
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 <= time
和burstTime > 0
找到合适的流程,请返回 - 如果您再次点击
currentProcess
,请在currentProcess
和closestCandidate
之间决定处理并返回哪个更好
最后要做的一件事是有效地实现循环条件。我会把它留给你来弄清楚。
注意:我不确定deque
是否是这里最好的容器,我很可能会使用forward_list
并在进程完成时删除它们。你也可以在deque中这样做,但这是O(n)运算。
相关文章:
- 如何在c++中实现处理器调度模拟器
- 在 c++ 中实现嵌套循环的更短方法吗?
- 如何在C++中实现带有packaged_task的异步等待循环?
- 基于范围的变换以实现正常循环
- 实现基于链表的堆栈的基于范围的 for 循环
- 避免在基于反向范围的for循环实现中悬挂参考
- C++ 模板使用递归实现循环
- 是否有可能实现O(N)时间和O(1)空间解决方案,以实现C++中的字符串循环移位
- 如何实现阻塞处理循环?
- 如何实现从 Windows 脚本主机到脚本的事件调度
- 实现循环阵列队列
- 为什么Visual Studio Compiler不在我的Mersenne-Twister实现中循环展开?
- 如何在不停滞主循环的情况下实现对话系统?
- 如何准确测量和比较opencl速度以实现循环功能的简单速度
- 如何实现循环以增加数据
- 我将如何实现循环调度模拟器
- 我将如何实现SJF和循环调度模拟器
- 当我事先不知道所有类时,如何实现双重调度?
- 使用 deque 在C++中实现循环缓冲区
- 用openmp实现循环并行化的嵌套c++