如何在c++中实现处理器调度模拟器
How would I implement a processor scheduling simulator in c++?
我的输入文件看起来像这个
in.txt
1 0 10
其中,每一行都是单个结构的ID(任意,不表示到达顺序(、arrivalTime和burstTime
我想在从用户那里获得要使用的算法的输入后,实现下面这样的输出。
例如用户输入FSCS,然后输入
S.algorithm: FCFS
Total 1 tasks are read
<system time 0> process 1 is running
<system time 1> process 1 is running
<system time 2> process 1 is running
<system time 3> process 1 is finished
任何实际代码中的帮助都将不胜感激!!!
我的工作尚未
while (file)
{
file >> temp.ID >> temp.arrivalTime >> temp.burstTime;
d.push_back(temp);
int i = 0, time = 0;
while (i < d.size())
{
if (d[i].arrivalTime > time)
cout << "Time " << time << "process is idle";
time += d[i].arrivalTime;
cout << "Time " << time << " Process " << d[i].ID << " is running" << endl;
time += d[i].burstTime;
i++;
}
}
Input.txt
1 0 5
2 5 8
3 9 6
输出
<system time <0> process 1 is running
<system time <1> process 1 is running
<system time <2> process 1 is running
<system time <3> process 1 is running
<system time <4> process 1 is running
<system time <5> process 1 finished
<system time <5> process 2 is running
<system time <6> process 2 is running
<system time <7> process 2 is running
<system time <8> process 2 is running
<system time <9> process 2 is running
<system time <10> process 2 is running
<system time <11> process 2 is running
<system time <12> process 2 is running
<system time <13> process 2 finished
<system time <13> process 3 is running
<system time <14> process 3 is running
<system time <15> process 3 finished
Press any key to continue . . .
对于文件处理部分,使用类似的东西
ifstream file("input.txt")
if(!file)
{
std::cerr<<"Cannot open file"<<std::endl;
exit(1);
}
myData temp;
while(file)
{
file >> temp.Id >> temp.arrivalTime >> temp.burstTime;
d.push_back(temp);
}
int time,i;
time = i = 0;
while( i < d.size())
{
if( time < d[i].arrivalTime )
{
std::cout<<"<system time <"<<time<<"> system is idle"<<std::endl;
time++;
}
else if( time < d[i].arrivalTime + d[i].burstTime )
{
std::cout<<"<system time <"<<time<<"> process "<<i<<" is running"<<std::endl;
time++;
}
else{
std::cout<<"<system time <"<<time<<"> process "<<i<<" finished"<<std::endl;
i++;
}
}
编辑:
test.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>
using namespace std;
struct myData {
int Id;
int arrivalTime;
int burstTime;
int finishTime;
};
int main()
{
ifstream file("input.txt");
vector<myData> d;
if(!file)
{
std::cerr<<"Cannot open file"<<std::endl;
exit(1);
}
myData temp;
temp.finishTime = -1;
while(file)
{
if(file.good())
file >> temp.Id >> temp.arrivalTime >> temp.burstTime;
d.push_back(temp);
}
int time,i;
time = i = 0;
while( i < d.size() - 1 )
{
if( time < d[i].arrivalTime )
{
std::cout<<"<system time <"<<time<<"> system is idle"<<std::endl;
time++;
}
else if( time < d[i].finishTime || d[i].finishTime==-1 )
{
if(d[i].finishTime == -1)
d[i].finishTime = d[i].burstTime + time;
std::cout<<"<system time <"<<time<<"> process "<<i+1<<" is running"<<std::endl;
time++;
}
else{
std::cout<<"<system time <"<<time<<"> process "<<i+1<<" finished"<<std::endl;
i++;
}
}
}
和input.txt
1 0 3
相关文章:
- 如何在c++中实现处理器调度模拟器
- 如何在 x86 处理器上实现"锁定添加"
- 如何实现从 Windows 脚本主机到脚本的事件调度
- 如何使用C++中的处理器指令来实现快速算术运算
- 没有虚拟调度的默认方法实现
- 有没有办法让 C 预处理器更改我正在使用的 STL 实现?
- 如何使用Microsoft PPL轻量级任务调度程序实现后退
- 我将如何实现循环调度模拟器
- 我将如何实现SJF和循环调度模拟器
- 如何实现FCFS处理器调度模拟器
- 使用 Boost 协程实现多任务调度程序和执行程序
- 当我事先不知道所有类时,如何实现双重调度?
- 为多处理器找到DAG的静态调度-库
- 您将如何在调度程序中实现这种自适应的"软糖因子"?
- 是否可以在没有预处理器宏的情况下为"virtual" get-setter 定义默认实现
- 以下哪种实现应该用于标记调度?为什么?
- 如何实现/模拟作业调度器
- 分布式仿真中如何实现动态TCP端口调度
- 用模板实现替换预处理器#ifdef块
- 用C++实现两个类层次结构的双重调度