先来先服务+短作业优先 用c++实现的代码
时间: 2023-11-16 08:06:49 浏览: 147
【进程调度实现C++】先来先服务 短进程优先 优先级调度(
好的,下面是先来先服务+短作业优先算法的C++实现代码:
```c++
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
struct Request {
int id; // 请求的id
int track; // 请求所在磁道
int arrive_time; // 请求到达时间
int start_time; // 请求开始服务时间
int finish_time; // 请求完成时间
int wait_time; // 请求等待时间
int turnover_time; // 请求周转时间
double weight_turnover_time; // 请求带权周转时间
};
bool compare_arrive_time(Request a, Request b) {
return a.arrive_time < b.arrive_time;
}
bool compare_track(Request a, Request b) {
return a.track < b.track;
}
bool compare_start_time(Request a, Request b) {
return a.start_time < b.start_time;
}
void FCFS(vector<Request> requests) {
int n = requests.size();
int total_track = 0;
int total_wait_time = 0;
int total_turnover_time = 0;
double total_weight_turnover_time = 0;
int current_time = 0;
for (int i = 0; i < n; i++) {
Request r = requests[i];
if (r.arrive_time > current_time) {
current_time = r.arrive_time;
}
r.start_time = current_time;
r.finish_time = current_time + abs(r.track - total_track);
r.wait_time = r.start_time - r.arrive_time;
r.turnover_time = r.finish_time - r.arrive_time;
r.weight_turnover_time = (double)r.turnover_time / r.finish_time;
total_track = r.track;
total_wait_time += r.wait_time;
total_turnover_time += r.turnover_time;
total_weight_turnover_time += r.weight_turnover_time;
current_time = r.finish_time;
requests[i] = r;
}
cout << "先来先服务算法结果:" << endl;
cout << "请求编号\t到达时间\t服务时间\t完成时间\t等待时间\t周转时间\t带权周转时间" << endl;
for (int i = 0; i < n; i++) {
Request r = requests[i];
cout << r.id << "\t\t" << r.arrive_time << "\t\t" << r.start_time << "\t\t" << r.finish_time << "\t\t" << r.wait_time << "\t\t" << r.turnover_time << "\t\t" << r.weight_turnover_time << endl;
}
double average_wait_time = (double)total_wait_time / n;
double average_turnover_time = (double)total_turnover_time / n;
double average_weight_turnover_time = total_weight_turnover_time / n;
cout << "总等待时间:" << total_wait_time << endl;
cout << "平均等待时间:" << average_wait_time << endl;
cout << "总周转时间:" << total_turnover_time << endl;
cout << "平均周转时间:" << average_turnover_time << endl;
cout << "总带权周转时间:" << total_weight_turnover_time << endl;
cout << "平均带权周转时间:" << average_weight_turnover_time << endl << endl;
}
void SJF(vector<Request> requests) {
int n = requests.size();
int total_track = 0;
int total_wait_time = 0;
int total_turnover_time = 0;
double total_weight_turnover_time = 0;
int current_time = 0;
priority_queue<Request, vector<Request>, function<bool(Request, Request)>> pq(compare_track);
for (int i = 0; i < n; i++) {
Request r = requests[i];
if (r.arrive_time > current_time) {
current_time = r.arrive_time;
}
while (!pq.empty() && pq.top().finish_time <= current_time) {
Request r = pq.top();
pq.pop();
r.wait_time = r.start_time - r.arrive_time;
r.turnover_time = r.finish_time - r.arrive_time;
r.weight_turnover_time = (double)r.turnover_time / r.finish_time;
total_wait_time += r.wait_time;
total_turnover_time += r.turnover_time;
total_weight_turnover_time += r.weight_turnover_time;
requests[r.id] = r;
}
pq.push(r);
if (pq.size() == 1) {
Request r = pq.top();
r.start_time = current_time;
r.finish_time = current_time + abs(r.track - total_track);
total_track = r.track;
current_time = r.finish_time;
pq.pop();
pq.push(r);
}
}
while (!pq.empty()) {
Request r = pq.top();
pq.pop();
r.wait_time = r.start_time - r.arrive_time;
r.turnover_time = r.finish_time - r.arrive_time;
r.weight_turnover_time = (double)r.turnover_time / r.finish_time;
total_wait_time += r.wait_time;
total_turnover_time += r.turnover_time;
total_weight_turnover_time += r.weight_turnover_time;
requests[r.id] = r;
}
cout << "短作业优先算法结果:" << endl;
cout << "请求编号\t到达时间\t服务时间\t完成时间\t等待时间\t周转时间\t带权周转时间" << endl;
for (int i = 0; i < n; i++) {
Request r = requests[i];
cout << r.id << "\t\t" << r.arrive_time << "\t\t" << r.start_time << "\t\t" << r.finish_time << "\t\t" << r.wait_time << "\t\t" << r.turnover_time << "\t\t" << r.weight_turnover_time << endl;
}
double average_wait_time = (double)total_wait_time / n;
double average_turnover_time = (double)total_turnover_time / n;
double average_weight_turnover_time = total_weight_turnover_time / n;
cout << "总等待时间:" << total_wait_time << endl;
cout << "平均等待时间:" << average_wait_time << endl;
cout << "总周转时间:" << total_turnover_time << endl;
cout << "平均周转时间:" << average_turnover_time << endl;
cout << "总带权周转时间:" << total_weight_turnover_time << endl;
cout << "平均带权周转时间:" << average_weight_turnover_time << endl << endl;
}
int main() {
int n;
cout << "请输入请求个数:" << endl;
cin >> n;
int start_track, max_track;
cout << "请输入起始扫描磁道号和最大磁道号数:" << endl;
cin >> start_track >> max_track;
vector<Request> requests;
for (int i = 0; i < n; i++) {
Request r;
r.id = i;
r.track = rand() % max_track + start_track;
r.arrive_time = rand() % (n + 1);
requests.push_back(r);
}
sort(requests.begin(), requests.end(), compare_arrive_time);
FCFS(requests);
sort(requests.begin(), requests.end(), compare_track);
SJF(requests);
return 0;
}
```
这段代码实现了先来先服务+短作业优先算法,其中先来先服务算法使用了最简单的按照请求到达时间排序的方法,短作业优先算法使用了优先队列来维护待处理请求队列,并按照请求所在磁道排序来实现短作业优先。在实现过程中,我们还计算了每个请求的等待时间、周转时间和带权周转时间,并输出了这些指标的总和和平均值。
阅读全文