# 操作系统实验 先来先服务算法+短作业优先算法+最高响应比算法

https://blog.csdn.net/qq_40159978/article/details/90933768

ps:这个实验真的是，看起来简单，但实现起来是真麻烦

#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
using namespace std;

typedef struct pcb {
string pName;  //进程名
float arriveTime;//到达时间
float serviceTime;//服务时间
float estimatedRunningtime;//估计运行时间
float startTime;//开始运行时间
float finishTime;//完成运行时间
float turnaroundTime;//周转时间
float weightedTuraroundTime;//带权周转时间
float waitServiceTime = 0;
char state;//状态
float s = 0;
}PCB;

vector<PCB>v;

bool cmp1(pcb a, pcb b)
{
return a.arriveTime < b.arriveTime;
}

bool cmp2(pcb a, pcb b)
{
if(a.arriveTime == b.arriveTime)
return a.serviceTime < b.serviceTime;
else
return a.arriveTime < b.arriveTime;
}

bool cmp3(pcb a, pcb b)
{
return a.serviceTime < b.serviceTime;
}

bool cmp4(pcb a, pcb b)
{
return a.s > b.s;
}

void createProcess(int n) {//创建n个进程
cout << endl << endl << "创建进程" << endl;
PCB r;//工作结点
for (int i = 1; i <= n; i ++) {
cout << "请输入第" << i << "个进程的名字、到达时间、服务时间(例如:A 12 8):";
cin >> r.pName;
cin >> r.arriveTime;
cin >> r.serviceTime;
r.startTime = 0;
r.finishTime = 0;
r.estimatedRunningtime = r.serviceTime;
r.turnaroundTime = 0;
r.weightedTuraroundTime = 0;
v.push_back(r);
}
}

void printProcess(vector<PCB>&v1) {//输出所有进程的信息
PCB q;
float ave1 = 0, ave2 = 0;
cout << "进程名\t到达时间 服务时间 开始时间 完成时间 周转时间 带权周转时间" << endl;
for(int i = 0; i < v1.size(); i ++)
{
cout << v1[i].pName << "\t" << v1[i].arriveTime << "\t " << v1[i].serviceTime << "\t  ";
cout << v1[i].startTime << "\t   " << v1[i].finishTime << "\t    " << v1[i].turnaroundTime << "\t     " << v1[i].weightedTuraroundTime << endl;
ave1 += v1[i].turnaroundTime;
ave2 += v1[i].weightedTuraroundTime;
}
cout << endl;
cout << "平均周转时间" << ave1 / v1.size() << endl;
cout << "平均带权周转时间" << ave2 / v1.size() << endl;
}

void runProcess2(vector<PCB>v1, int n) {//运行进程
PCB s;
int cnt = 0;
vector<PCB>v2;
int last = 0;
sort(v1.begin(), v1.end(), cmp2);
float time = v1[0].arriveTime;
for (int i = 0; i < n; i ++)
{
int cnt = 0;
while(i < n && v1[i].arriveTime <= time)
{
v2.push_back(v1[i]);
i++;
cnt++;
}
i--;
sort(v2.begin() + last, v2.begin() + last + cnt, cmp3);
printf("正在运行的进程\n");
int res = last + cnt;
while(last < res)
{
v2[last].startTime = max(time, v2[last].arriveTime);
v2[last].finishTime = v2[last].startTime + v2[last].serviceTime;
v2[last].turnaroundTime = v2[last].finishTime - v2[last].arriveTime;
v2[last].weightedTuraroundTime = float(v2[last].turnaroundTime * 1.0 / v2[last].serviceTime);
while(v2[last].estimatedRunningtime)
{
cout << "进程名 到达时间 服务时间 已运行时间 还剩运行时间\t" << endl;
cout << v2[last].pName << "\t" << v2[last].arriveTime << "\t " << v2[last].serviceTime << "\t  ";
cout << v2[last].serviceTime - v2[last].estimatedRunningtime << "\t   " << v2[last].estimatedRunningtime << endl;
v2[last].estimatedRunningtime --;
}
time = v2[last].finishTime;
last++;
}
if(last < n)
time = max(time, v1[last].arriveTime);
}
cout << "进程" << s.pName << "执行结束之后就绪队列中的进程" << endl;
printProcess(v2);
cout<< endl << endl;
}

void runProcess1(vector<PCB>v1, int n) {//运行进程
PCB s;
vector<PCB>v2;
sort(v1.begin(), v1.end(), cmp1);
float time = v1[0].arriveTime;
for (int i = 0; i < n; i ++)
{
cout << "正在运行的进程" << endl;
v1[i].startTime = max(time, v1[i].arriveTime);
v1[i].finishTime = v1[i].startTime + v1[i].serviceTime;
v1[i].turnaroundTime = v1[i].finishTime - v1[i].arriveTime;
v1[i].weightedTuraroundTime = float(v1[i].turnaroundTime * 1.0 / v1[i].serviceTime);
while(v1[i].estimatedRunningtime)
{
cout << "进程名 到达时间 服务时间 已运行时间 还剩运行时间\t" << endl;
cout << v1[i].pName << "\t" << v1[i].arriveTime << "\t " << v1[i].serviceTime << "\t  ";
cout << v1[i].serviceTime - v1[i].estimatedRunningtime << "\t     " << v1[i].estimatedRunningtime << endl;
v1[i].estimatedRunningtime --;
}
time = v1[i].finishTime;
}
cout << "进程" << s.pName << "执行结束之后就绪队列中的进程" << endl;
printProcess(v1);
cout<< endl << endl;
}

void runProcess3(vector<PCB>v1, int n) {//运行进程
PCB s;
int cnt = 0;
vector<PCB>v2;
int last = 0;
sort(v1.begin(), v1.end(), cmp2);
float time = v1[0].arriveTime;
for (int i = 0; i < n; i ++)
{
int cnt = 0;
while(i < n && v1[i].arriveTime <= time)
{
v2.push_back(v1[i]);
i++;
cnt++;
}
i--;
int res = last + cnt;
for(int j = last; j < res; j ++)
{
v2[j].waitServiceTime = time - v2[j].arriveTime;
v2[j].s = (v2[j].waitServiceTime + v2[j].serviceTime) / v2[j].serviceTime;
cout << v2[j].s << " ";
}
sort(v2.begin() + last, v2.begin() + last + cnt, cmp4);
printf("正在运行的进程\n");

while(last < res)
{
v2[last].startTime = max(time, v2[last].arriveTime);
v2[last].finishTime = v2[last].startTime + v2[last].serviceTime;
v2[last].turnaroundTime = v2[last].finishTime - v2[last].arriveTime;
v2[last].weightedTuraroundTime = float(v2[last].turnaroundTime * 1.0 / v2[last].serviceTime);
while(v2[last].estimatedRunningtime)
{
cout << "进程名 到达时间 服务时间 已运行时间 还剩运行时间\t" << endl;
cout << v2[last].pName << "\t" << v2[last].arriveTime << "\t " << v2[last].serviceTime << "\t  ";
cout << v2[last].serviceTime - v2[last].estimatedRunningtime << "\t   " << v2[last].estimatedRunningtime << endl;
v2[last].estimatedRunningtime --;
}
time = v2[last].finishTime;
last++;
for(int j = last; j < res; j ++)
{
v2[j].waitServiceTime = time - v2[j].arriveTime;
v2[j].s = (v2[j].waitServiceTime + v2[j].serviceTime) / v2[j].serviceTime;
cout << v2[j].s << " ";
}
cout << endl;
sort(v2.begin() + last, v2.begin() + res, cmp4);
}
if(last < n)
time = max(time, v1[last].arriveTime);
}
cout << "进程" << s.pName << "执行结束之后就绪队列中的进程" << endl;
printProcess(v2);
cout<< endl << endl;
}

{
cout << "1.先来先服务算法" << endl;
cout << "2.短作业优先算法" << endl;
cout << "3.高响应比优先算法" << endl;
cout << "0.退出程序" << endl;
}

int main()
{
int n;
cout << "请输入进程的个数:";
cin >> n;
createProcess(n);
int flag = 0;
while(1)
{
cin >> flag;
switch(flag)
{
case 1: runProcess1(v, n); break;
case 2: runProcess2(v, n); break;
case 3: runProcess3(v, n); break;
case 0: return 0;
}
}
return 0;
}


05-26
05-28 2580
04-30 337
03-07
08-09 2440
10-31 6235
01-04 824
06-27 4万+
07-05 1万+
06-05 8470