一、实验要求
1.用可视化编程工具编制程序,在机器上调试运行,并通过上机考核。
2.要求将功能集中在一个界面中,界面设计美观,功能完整,使用方便。
二、设计题目
题目1 进程调度模拟程序
目的:
熟悉进程调度算法及其实现
内容:
编写一个程序完成多道程序的调度
要求:
只考虑1个CPU的资源,其他资源不考虑
使用响应比高者优先算法
程序采用键盘输入,输入格式为:
K
TJ1 YS1
……
TJK YSK
其中K是作业数(>0),TJi提交时间,YSi (i=1~K)是作业预计的运行时间(以分钟计)TJ的输入格式是XXYY,
其中XX是时,YY是分,如10点28分,输入为1028。但内部计算要以60进制来算。要求输出按照作业调度的先后次序输出结果,
每行为一个作业状态,从左到右分别是调度次序,作业号,调度时间,周转时间和带权周转时间,
最后一行输出两个数,第一为平均周转时间,第二为平均带权周转时间。
#include<string.h>
#include<iostream>
using namespace std;
struct PCB {
int arrTime; /* 作业到达时间*/
int serTime; /*作业要求服务时间*/
int waiTime; /*等待时间*/
int begTime; /*开始运行时间*/
int finTime; /*结束运行时间*/
float turTime; /*周转时间*/
float wTuTime; /*带权周转时间*/
int priority;/*优先权*/
int finish; /*是否已经完成*/
}PCB[10];
int cnt;
int runningTime = 0;
int currentTime = 0;
void input()
{
cout << "请输入作业数量: ";
cin >> cnt;
for (int i = 0;i < cnt;i++)
{
cin >> PCB[i].arrTime >> PCB[i].serTime;
PCB[i].priority = 1;
PCB[i].finish = 0;
}
}
int Add(int a, int b)//得到时刻(xx:yy)(用于时刻+分钟)
{
return (a / 100 + (a % 100 + b) / 60) * 100 + (a % 100 + b) % 60;
}
int Sub(int a, int b)//得到时长(min)(用于时刻-时刻)
{
return (a / 100 - b / 100) * 60 + (a % 100 - b % 100);
}
void HRN()
{
int n = 0;
int current, i, j;
int min = 9999999;
for (int m = 0; m< cnt; m++)
{
if (PCB[m].arrTime < min)
{
current = m;
min = PCB[m].arrTime;
}
}
cout << "调度情况如下:" << endl;
cout << "调度次序" << " " << "作业号" << " " << "调度时间" << " " << "周转时间" << " " << "带权周转时间" << endl;
PCB[current].turTime = PCB[current].serTime;
PCB[current].wTuTime = PCB[current].serTime / PCB[current].serTime;
PCB[current].begTime = PCB[current].arrTime;
//输出调度次序,作业号,调度时间,周转时间和带权周转时间
cout << ++n << " " << current + 1 << " " << PCB[current].begTime << " "
<< PCB[current].turTime << " " << PCB[current].wTuTime << endl;
PCB[current].finish = 1;
runningTime += PCB[current].serTime;
currentTime = currentTime + Add(PCB[current].arrTime, PCB[current].serTime);
for (i = 0; i < cnt; i++)
{
PCB[i].waiTime = Sub(currentTime, PCB[i].arrTime);
PCB[i].priority += PCB[i].waiTime / PCB[i].serTime;
}
for (i = 0; i < cnt; i++)
{
if (!PCB[i].finish && PCB[i].arrTime < currentTime)
{
current = i;
for (j = 0; j < cnt; j++)
{
if (!PCB[j].finish && PCB[j].arrTime < currentTime && PCB[j].priority > PCB[current].priority)
current = j;
}
//输出
PCB[current].turTime = Sub(currentTime, PCB[current].arrTime) + PCB[current].serTime;
PCB[current].wTuTime = PCB[current].turTime / PCB[current].serTime;
PCB[current].begTime = currentTime;
cout << ++n << " " << current + 1 << " " << PCB[current].begTime << " " << PCB[current].turTime
<< " " << PCB[current].wTuTime << endl;
PCB[current].finish = 1;
runningTime += PCB[current].serTime;
currentTime = Add(currentTime, PCB[current].serTime);
for (int l = 0; l < cnt; l++)
{
if (!PCB[l].finish)
{
PCB[l].waiTime = Sub(currentTime, PCB[l].arrTime);
PCB[l].priority += PCB[l].waiTime / PCB[l].serTime;
}
}
}
else if (i == cnt - 1)
{
int min = 9999999;
for (int m = 0; m < cnt; m++)
{
if (!PCB[m].finish&&PCB[m].arrTime < min)
{
current = m;
min = PCB[m].arrTime;
}
}
currentTime = Add(currentTime, Sub(PCB[current].arrTime, currentTime));
PCB[current].turTime = PCB[current].serTime;
PCB[current].wTuTime = PCB[current].serTime / PCB[current].serTime;
PCB[current].begTime = PCB[current].arrTime;
//输出调度次序,作业号,调度时间,周转时间和带权周转时间
cout << ++n << " " << current + 1 << " " << PCB[current].begTime << " "
<< PCB[current].turTime << " " << PCB[current].wTuTime << endl;
PCB[current].finish = 1;
runningTime += PCB[current].serTime;
currentTime = Add(currentTime, PCB[current].serTime);
i = -1; //重新开始寻找
}
}
}
void main()
{
input();
HRN();
float tur = 0;
float wtu = 0;
for (int i = 0;i < cnt;i++)
{
tur += PCB[i].turTime;
wtu += PCB[i].wTuTime;
}
cout << "平均周转时间和平均带权周转时间分别为:" << endl;
cout << tur/cnt << " " << wtu/cnt << endl;
}