生活随笔
收集整理的这篇文章主要介绍了
OS轮转调度算法RR的C++实现
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
在分时系统中,最简单也是较常见的是基于时间片的轮转(round robin,RR)调度算法。该算法采取了非常公平的处理机分配方式,即让就绪队列中的每个进程仅运行一个时间片,如果就绪队列上有n个进程,则每个进程每次大约可获得1/n的处理机时间
时间片的大小对系统性能有很大的影响,时间片太小,有利于短作业,但上下文切换频繁,增加系统开销;时间片太长,则退化为FCFS算法,无法满足短作业和交互式用户的需求。
一个较为可取的时间片大小是略大于一次典型的交互所需要的时间,使大多数交互式进程能在一个时间片内完成,从而可以获得很小的响应时间。 ——出自《计算机操作系统》,西电出版的那本,但是好像还是没有说明时间片具体应该怎么取0.0
源代码如下:
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define MAXSIZE 5
int ID[MAXSIZE], COME_TIME[MAXSIZE], RUN_TIME[MAXSIZE];
struct processing
{int pid; //作业号int sequence; //顺序号double come_time; //到达时double run_time; //运行时double last_run_time; //剩余运行时间double over_time; //完成时double round_time; //周转时double avg_time; //带权周转时
}pc[MAXSIZE]; //作业数
queue<processing > q;
bool CmpByComeTime(processing p1, processing p2) {return p1.come_time<p2.come_time;
}
void push_in_queue() { //进程入队列for (int i = 0; i < MAXSIZE; ++i){q.push(pc[i]);}
}
void info_to_process() {for (int i = 0; i<MAXSIZE; ++i) {pc[i].sequence = i;pc[i].pid = ID[i];pc[i].come_time = COME_TIME[i];pc[i].run_time = RUN_TIME[i];pc[i].last_run_time = pc[i].run_time;}sort(pc, pc + MAXSIZE, CmpByComeTime);push_in_queue();
}
void get_info() { //输入进程信息for (int i = 0; i<MAXSIZE; i++) {cin >> ID[i] >> COME_TIME[i] >> RUN_TIME[i];}info_to_process();}
void print(double avg_sum_round_time, double avg_sum_avg_time) {cout << "执行顺序:" << endl;for (int i = 0; i < MAXSIZE; ++i){/* code */cout << pc[i].pid << " ";}cout << endl;cout << "作业号" << '\t' << "到达时" << '\t' << "运行时" << '\t' << "完成时" << '\t' << "周转时" << '\t' << "带权周转时" << '\t' << endl;for (int i = 0; i < MAXSIZE; ++i){cout << pc[i].pid << '\t' << pc[i].come_time << '\t'<< pc[i].run_time << '\t' << pc[i].over_time << '\t'<< pc[i].round_time << '\t'<< pc[i].avg_time << endl;}cout << "平均周转时: " << avg_sum_round_time << endl<< "平均带权周转时: " << avg_sum_avg_time << endl << endl;
}
void get_RoundAndAvgTime() {double sum_round_time = 0;double avg_sum_round_time = 0.0;double sum_avg_time = 0.0;double avg_sum_avg_time = 0.0;for (int i = 0; i<MAXSIZE; i++) {sum_round_time += pc[i].round_time;sum_avg_time += pc[i].avg_time;}avg_sum_round_time = sum_round_time * 1.0 / MAXSIZE;avg_sum_avg_time = sum_avg_time * 1.0 / MAXSIZE;print(avg_sum_round_time, avg_sum_avg_time);
}
void calculate(int TimePeace) {int NowTime = 0;while (!q.empty()) {int NowPcNum = q.front().sequence;if (TimePeace >= pc[NowPcNum].last_run_time) {pc[NowPcNum].over_time = NowTime + pc[NowPcNum].last_run_time; //完成时间pc[NowPcNum].round_time = pc[NowPcNum].over_time - pc[NowPcNum].come_time; //周转时 = 完成时 - 到达时pc[NowPcNum].avg_time = pc[NowPcNum].round_time / pc[NowPcNum].run_time; //带权周转时 = 周转时 / 运行时NowTime += pc[NowPcNum].last_run_time;q.pop();}else {pc[NowPcNum].last_run_time -= TimePeace; //该进程剩余需要运行的时间NowTime += TimePeace;q.push(pc[NowPcNum]);q.pop();}}get_RoundAndAvgTime();
}int main(int argc, char const *argv[])
{get_info();int TimePeace;char ch;while (true) {cout << "时间片大小" << endl;cin >> TimePeace;calculate(TimePeace);cout << "continue? [y/n]" << endl;cin >> ch;if (ch == 'y') {info_to_process();continue;}else {break;}}system("pause");return 0;}
运行结果如下:
总结
以上是生活随笔为你收集整理的OS轮转调度算法RR的C++实现的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。