计算机操作系统-多道程序设计的实现
设计实例
设在内存中有三道程序A、B和C,并按A、B、C的优先次序运行,其内部计算和I/O操作的时间如下所示:
A: [计算 30ms] -- [I/O 40ms] -- [计算 10ms]
B: [计算 60ms] -- [I/O 30ms] -- [计算 10ms]
B: [计算 20ms] -- [I/O 40ms] -- [计算 20ms]
程序环境
程序将在单CPU,双通道的情况下运行,为抢占式
代码设计
代码思路及重点分为以下几点
- 使用队列模拟CPU与两个通道
- 自定义结构体存储程序信息
- 设置总时间变量和当前程序变量
- 计算函数runPro,处理当前程序(计算和转通道)
- IO函数runIO,处理当前程序(IO和转CPU)
- 打印信息函数printStatus,以刷新屏幕打印信息的方式实现信息动态显示
- 主函数处理当前程序变量和CPU所需要执行的程序
详细代码
#include
#include
#include
#include
using namespace std;
struct program
{
int countOne,IO,countTwo;
int level;
};
program pro[100]={{30,40,10,0},{60,30,10,1},{20,40,20,2},{0,0,0,100}};
queue CPU;
queue PassageOne;
queue PassageTwo;
program temp;
int nowTime,nowPro=0;
void runPro()
{
if(CPU.front().level==100) return;
if(!PassageOne.empty())
{
if(PassageOne.front().level==CPU.front().level)
{
CPU.pop();
return;
}
}
if(!PassageTwo.empty())
{
if(PassageTwo.front().level==CPU.front().level)
{
CPU.pop();
return;
}
}
if(CPU.front().countOne>0)
{
CPU.front().countOne--;
if(CPU.front().countOne==0)
{
temp=CPU.front();
CPU.pop();
pro[temp.level]=temp;
if(PassageOne.empty())
PassageOne.push(temp);
else if(PassageTwo.empty())
PassageTwo.push(temp);
}
}
else
{
if(CPU.front().countTwo>0)
{
CPU.front().countTwo--;
if(CPU.front().countTwo==0)
{
temp=CPU.front();
CPU.pop();
pro[temp.level]=temp;
}
}
}
}
void runIO()
{
if(!PassageOne.empty())
{
PassageOne.front().IO--;
if(PassageOne.front().IO==0)
{
temp=PassageOne.front();
PassageOne.pop();
pro[temp.level]=temp;
if(temp.level2) break;
}
if(pro[2].countTwo==0) break;
}
cout<<"三道程序运行结束需要 "<