计算机操作系统-多道程序设计的实现

设计实例

设在内存中有三道程序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,双通道的情况下运行,为抢占式

代码设计

代码思路及重点分为以下几点

  1. 使用队列模拟CPU与两个通道
  2. 自定义结构体存储程序信息
  3. 设置总时间变量和当前程序变量
  4. 计算函数runPro,处理当前程序(计算和转通道)
  5. IO函数runIO,处理当前程序(IO和转CPU)
  6. 打印信息函数printStatus,以刷新屏幕打印信息的方式实现信息动态显示
  7. 主函数处理当前程序变量和CPU所需要执行的程序

详细代码

#include <iostream>
#include <cstdio>
#include <queue>
#include <Windows.h> 

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<program> CPU;
queue<program> PassageOne;
queue<program> 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.level<CPU.front().level)
			{
				CPU.push(temp);
				temp=CPU.front();
				pro[temp.level]=temp;
				CPU.pop();
			}
			if(CPU.empty())
			{
				CPU.push(temp);
			}
		}
	}
	if(!PassageTwo.empty())
	{
		PassageTwo.front().IO--;
		if(PassageTwo.front().IO==0)
		{
			temp=PassageTwo.front();
			PassageTwo.pop();
			pro[temp.level]=temp;
			if(temp.level<CPU.front().level)
			{
				CPU.push(temp);
				temp=CPU.front();
				pro[temp.level]=temp;
				CPU.pop();
			}
			if(CPU.empty())
			{
				CPU.push(temp);
			}
		}
	}
	
}

void printStatus()
{
	Sleep(500);
	system("cls");
	if(!CPU.empty() && CPU.front().level!=100)
	{
		if(CPU.front().level!=100)
		{
			printf("\t\t\t\t\t\t-CPU 正在执行 程序%c-\n", 'A'+CPU.front().level);
			printf("\t\t\t\t\t\t------%2d %2d %2d------\n\n", CPU.front().countOne, CPU.front().IO, CPU.front().countTwo);
		}
	}
	else
	{
		printf("\t\t\t\t\t\t-CPU 处于空闲-\n"); 
		printf("\t\t\t\t\t\t--------------------\n\n");
	}
	if(!PassageOne.empty())
	{
		printf("\t\t\t\t\t\t程序%c 正在使用通道1\n", 'A'+PassageOne.front().level);
		printf("\t\t\t\t\t\t------%2d %2d %2d------\n\n", PassageOne.front().countOne, PassageOne.front().IO, PassageOne.front().countTwo);
	}
	else
	{
		printf("\t\t\t\t\t\t通道1 正处在空闲状态\n"); 
		printf("\t\t\t\t\t\t--------------------\n\n");
	}
	if(!PassageTwo.empty())
	{
		printf("\t\t\t\t\t\t程序%c 正在使用通道2\n", 'A'+PassageTwo.front().level);
		printf("\t\t\t\t\t\t------%2d %2d %2d------\n\n", PassageTwo.front().countOne, PassageTwo.front().IO, PassageTwo.front().countTwo);
	}
	else
	{
		printf("\t\t\t\t\t\t通道2 正处在空闲状态\n"); 
		printf("\t\t\t\t\t\t--------------------\n\n");
	}
}

int main()
{
//	freopen("data.out","w",stdout);
	CPU.push(pro[0]);
	while(true)
	{
		nowTime++;
		runPro();
		runIO();
		printStatus();

		while(CPU.empty())
		{
			if(pro[nowPro].countOne!=0)	CPU.push(pro[nowPro]);
			else if(pro[nowPro].countOne==0 && pro[nowPro].IO!=0)	CPU.push(pro[nowPro+1]);
			else if(pro[nowPro].countOne==0 && pro[nowPro].countTwo==0)	nowPro++;
			if(nowPro>2)	break;
		}
		if(pro[2].countTwo==0)	break;
	}
	cout<<"三道程序运行结束需要 "<<nowTime+1<<" MS"<<endl;
	return 0;
}
posted @ 2021-11-17 22:09  Conan-jine  阅读(929)  评论(0编辑  收藏  举报