基于RR算法的CPU调度
具体数据的实现
在分时系统中,最简单也是较常见的是基于时间片的轮转(round robin,RR)调度算法。该算法采取了非常公平的处理机分配方式,即让就绪队列中的每个进程仅运行一个时间片,如果就绪队列上有n个进程,则每个进程每次大约可获得1/n的处理机时间
时间片的大小对系统性能有很大的影响,时间片太小,有利于短作业,但上下文切换频繁,增加系统开销;时间片太长,则退化为FCFS算法,无法满足短作业和交互式用户的需求。
#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;
}