c++实现时间片轮转的代码
时间: 2023-08-06 19:15:41 浏览: 170
好的,下面是时间片轮转算法的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; // 请求带权周转时间
int execution_time; // 请求执行时间
int remaining_time; // 请求剩余执行时间
};
bool compare_arrive_time(Request a, Request b) {
return a.arrive_time < b.arrive_time;
}
void RR(vector<Request> requests, int time_slice) {
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;
queue<Request> q;
for (int i = 0; i < n; i++) {
Request r = requests[i];
r.remaining_time = r.execution_time;
if (r.arrive_time > current_time) {
current_time = r.arrive_time;
}
q.push(r);
}
while (!q.empty()) {
Request r = q.front();
q.pop();
r.start_time = current_time;
if (r.remaining_time <= time_slice) {
current_time += r.remaining_time;
r.finish_time = current_time;
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.execution_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;
requests[r.id] = r;
} else {
current_time += time_slice;
r.remaining_time -= time_slice;
q.push(r);
}
while (!q.empty() && q.front().arrive_time <= current_time) {
Request r = q.front();
q.pop();
q.push(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;
int max_execution_time;
cout << "请输入请求最大执行时间:" << endl;
cin >> max_execution_time;
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);
r.execution_time = rand() % max_execution_time + 1;
requests.push_back(r);
}
sort(requests.begin(), requests.end(), compare_arrive_time);
int time_slice;
cout << "请输入时间片大小:" << endl;
cin >> time_slice;
RR(requests, time_slice);
return 0;
}
```
这段代码实现了时间片轮转算法,其中每个请求有一个执行时间和一个剩余执行时间,如果剩余执行时间小于等于时间片大小,则该请求执行完毕,否则该请求只执行一个时间片的时间,然后重新进入队列等待下一次执行。在实现过程中,我们还计算了每个请求的等待时间、周转时间和带权周转时间,并输出了这些指标的总和和平均值。
阅读全文