一、 实验要求
1、 编写并调试一个单道处理系统的作业调度模拟程序。(必做) 作业调度算法:分别采用先来先服务(FCFS),最短作业优先(SJF)、响应比高者优先(HRRN)的调度算法。
对每种调度算法都要求打印每个作业开始运行时刻、完成时刻、周转时间、带权周转时间,以及这组作业的平均周转时间及带权平均周转时间,以比较各种算法的优缺点。
#include <iostream>
#include <math.h>
#include <vector>
#include <stdlib.h>
#include <iomanip>
#include <algorithm>
using namespace std;
using std::cout;
using std::sort;
int n;//job count
string s[3] = { "ready" ,"running","finish" };
struct jcb
{
int id;//作业编号
int needtime;//需要多少时间
int allneedtime;//一共最开始是需要多长时间
int startruntime = 0;//开始运行时间
int status = 0;//是否完成
float priority = 1;//优先级
};
//设置运行所需时间从小到大排列
int cmp(jcb a, jcb b)
{
return a.needtime < b.needtime;
}
//设置优先级从高到低
int cmp1(jcb a, jcb b)
{
return a.priority > b.priority;
}
//输出,遍历数组来输出全部的运行过程,使用于FCFS和SJF算法
int run(vector <jcb> job)
{
int runtime = 0;
int alltime = 0;
float allwtime = 0;
while (!job.empty())
{
cout << "JOB" << job[0].id
<< "\tneedtime:" << job[0].needtime
<< "\tstartruntime:" << runtime;
runtime += job[0].needtime;
alltime += runtime;
allwtime += runtime * 1.0 / job[0].needtime;
cout << "\tfinishtime:" << runtime
<< "\tround time:" << runtime
<< "\tweight round time:"
<< setiosflags(ios::fixed) << setprecision(3)
<< runtime * 1.0 / job[0].needtime << endl;
job.erase(job.begin(), job.begin() + 1);
}
cout << "\n average round time:" << alltime * 1.0 / n
<< " average weight round time:" << allwtime / n << endl;
cout << endl;
return 1;
}
//输出数组的全部元素的状态信息,仅仅是当前时间各个作业的状态信息
int print1(vector <jcb> job)
{//输出就绪队列的进程
while (!job.empty())
{
cout << "JOB" << job[0].id
<< "\tneedtime:" << job[0].needtime
<< "\tstatus:" << s[job[0].status]
<< "\tpriority:" << job[0].priority << endl;
job.erase(job.begin(), job.begin() + 1);
}
cout << endl;
return 1;
}
int FCFS(vector <jcb> job)
{
run(job);
return 1;
}
int SJF(vector <jcb> job)
{
sort(job.begin(), job.end(), cmp);
run(job);
return 1;
}
int HRRN(vector <jcb> job)
{
sort(job.begin(), job.end(), cmp);
int count = n;//还有多少个作业尚未完成
int runtime = 0, alltime = 0;
float allwtime = 0;
int isstarted[100] = { 0 };
cout << "初始的作业状态:" << endl;
print1(job);
cout << "运行状态:" << endl;
while (true)//就绪队列非空
{
if (isstarted[job[0].id - 1] == 0)
{
isstarted[job[0].id - 1] = 1;
job[0].startruntime = runtime;
}
//运行队列的第一个作业
job[0].status = 1;
job[0].needtime -= 1;
runtime++;
for (int i = 1; i < count; i++)
{
job[i].priority = 1 + runtime * 1.0 / job[i].needtime;
}
cout << "time:" << runtime << endl;
print1(job);//输出作业的状态
if (job[0].needtime == 0)//当前作业完成
{
count--;
job[0].status = 2;//运行结束
alltime += runtime;
allwtime += runtime * 1.0 / job[0].allneedtime;
cout << "JOB" << job[0].id << " has finished!!\n" << "startruntime:" << job[0].startruntime
<< "\tfinishtime:" << runtime << "\troundtime:" << runtime
<< "\tround weight time:" << runtime * 1.0 / job[0].allneedtime << endl;
cout << endl;
job.erase(job.begin(), job.begin() + 1);
if (job.empty())
break;
}
else
job[0].status = 0;
sort(job.begin(), job.end(), cmp1);
}
cout << endl;
cout << " average round time:" << alltime * 1.0 / n
<< " average weight round time:" << allwtime / n << endl;
return 1;
}
int main() {
cout << "input job counts:";
cin >> n;
vector <jcb> job(n);
cout << "input jobs needtime:\n";
for (int i = 0; i < n; i++)
{
cout << "请输入第" << i + 1 << "个作业所需要的时间:";
job[i].id = i + 1;
cin >> job[i].needtime;
job[i].allneedtime = job[i].needtime;
}
cout << "FCFS:" << endl;
FCFS(job);
cout << "SJF" << endl;
SJF(job);
cout << "HRRN" << endl;
HRRN(job);
}
运行结果:
图有点多,就不都附上了
这是最后一张: