结构体模拟进程PCB 2.记录进程运行的到达时间与服务时间 3.模拟单处理机下N个进程的先来先服务调度算法、短进程优先调度算法或高响应比优先进程调度算法
时间: 2023-11-22 13:56:42 浏览: 93
好的,请看下面的代码实现:
```
#include<iostream>
using namespace std;
// 进程控制块结构体
struct PCB{
int pid; // 进程ID
int arriveTime; // 到达时间
int serviceTime; // 服务时间
int startTime; // 开始时间
int finishTime; // 完成时间
int turnaroundTime; // 周转时间
float weightTurnaroundTime; // 带权周转时间
};
// 先来先服务调度算法
void FCFS(PCB *process, int n){
int curTime = 0;
for(int i=0; i<n; i++){
if(curTime < process[i].arriveTime){ // 进程未到达
process[i].startTime = process[i].arriveTime;
curTime = process[i].arriveTime;
}
else{ // 进程已到达
process[i].startTime = curTime;
}
process[i].finishTime = process[i].startTime + process[i].serviceTime;
process[i].turnaroundTime = process[i].finishTime - process[i].arriveTime;
process[i].weightTurnaroundTime = (float)process[i].turnaroundTime / process[i].serviceTime;
curTime = process[i].finishTime;
}
}
// 短进程优先调度算法
void SJF(PCB *process, int n){
int curTime = 0;
int index;
for(int i=0; i<n; i++){
int minServiceTime = INT_MAX;
for(int j=0; j<n; j++){
if(process[j].arriveTime <= curTime && process[j].serviceTime < minServiceTime){
minServiceTime = process[j].serviceTime;
index = j;
}
}
process[index].startTime = curTime;
process[index].finishTime = process[index].startTime + process[index].serviceTime;
process[index].turnaroundTime = process[index].finishTime - process[index].arriveTime;
process[index].weightTurnaroundTime = (float)process[index].turnaroundTime / process[index].serviceTime;
curTime = process[index].finishTime;
process[index].serviceTime = INT_MAX; // 标记该进程已经调度完毕
}
}
// 高响应比优先进程调度算法
void HRRN(PCB *process, int n){
int curTime = 0;
for(int i=0; i<n; i++){
int maxPriority = -1;
int index;
for(int j=0; j<n; j++){
if(process[j].arriveTime <= curTime && process[j].serviceTime != INT_MAX){
float priority = (float)(curTime - process[j].arriveTime + process[j].serviceTime) / process[j].serviceTime;
if(priority > maxPriority){
maxPriority = priority;
index = j;
}
}
}
process[index].startTime = curTime;
process[index].finishTime = process[index].startTime + process[index].serviceTime;
process[index].turnaroundTime = process[index].finishTime - process[index].arriveTime;
process[index].weightTurnaroundTime = (float)process[index].turnaroundTime / process[index].serviceTime;
curTime = process[index].finishTime;
process[index].serviceTime = INT_MAX; // 标记该进程已经调度完毕
}
}
int main(){
int n;
cout << "请输入进程数:";
cin >> n;
PCB *process = new PCB[n];
cout << "请依次输入每个进程的到达时间和服务时间:" << endl;
for(int i=0; i<n; i++){
process[i].pid = i+1;
cin >> process[i].arriveTime >> process[i].serviceTime;
}
// 先来先服务调度算法
FCFS(process, n);
cout << "先来先服务调度算法结果:" << endl;
cout << "进程ID\t到达时间\t服务时间\t开始时间\t完成时间\t周转时间\t带权周转时间" << endl;
for(int i=0; i<n; i++){
cout << process[i].pid << "\t" << process[i].arriveTime << "\t\t" << process[i].serviceTime << "\t\t" << process[i].startTime << "\t\t" << process[i].finishTime << "\t\t" << process[i].turnaroundTime << "\t\t" << process[i].weightTurnaroundTime << endl;
}
// 短进程优先调度算法
SJF(process, n);
cout << "短进程优先调度算法结果:" << endl;
cout << "进程ID\t到达时间\t服务时间\t开始时间\t完成时间\t周转时间\t带权周转时间" << endl;
for(int i=0; i<n; i++){
cout << process[i].pid << "\t" << process[i].arriveTime << "\t\t" << process[i].serviceTime << "\t\t" << process[i].startTime << "\t\t" << process[i].finishTime << "\t\t" << process[i].turnaroundTime << "\t\t" << process[i].weightTurnaroundTime << endl;
}
// 高响应比优先进程调度算法
HRRN(process, n);
cout << "高响应比优先进程调度算法结果:" << endl;
cout << "进程ID\t到达时间\t服务时间\t开始时间\t完成时间\t周转时间\t带权周转时间" << endl;
for(int i=0; i<n; i++){
cout << process[i].pid << "\t" << process[i].arriveTime << "\t\t" << process[i].serviceTime << "\t\t" << process[i].startTime << "\t\t" << process[i].finishTime << "\t\t" << process[i].turnaroundTime << "\t\t" << process[i].weightTurnaroundTime << endl;
}
delete[] process;
return 0;
}
```
这里定义了一个进程控制块结构体,其中包括进程ID、到达时间、服务时间、开始时间、完成时间、周转时间和带权周转时间。
然后分别实现了先来先服务调度算法、短进程优先调度算法和高响应比优先进程调度算法。在每个算法中,都按照到达时间和服务时间的要求对进程进行排序,并计算各个进程的开始时间、完成时间、周转时间和带权周转时间。
最后,在主函数中输入进程数和每个进程的到达时间和服务时间,并调用各个算法进行模拟。打印出每个进程的相关信息,包括到达时间、服务时间、开始时间、完成时间、周转时间和带权周转时间。
阅读全文