轮转调度算法(RR)
RR算法是使⽤⾮常⼴泛的⼀种调度算法。
⾸先将所有就绪的队列按FCFS策略排成⼀个就绪队列,然后系统设置⼀定的时间⽚,每次给队⾸作业分配时间⽚。如果此作业运⾏结束,即使时间⽚没⽤完,⽴刻从队列中去除此作业,并给下⼀个作业分配新的时间⽚;如果作业时间⽚⽤完没有运⾏结束,则将此作业重新加⼊就绪队列尾部等待调度。
//main.cpp
heeljob
简历的英文#include "RR.h"
int main()
{
std::vector<PCB> PCBList;std
int timeslice;
//输⼊时间⽚⼤⼩,作业信息
InputPCB(PCBList, timeslice);
//RR算法
RR(PCBList, timeslice);
//显⽰结果
show(PCBList);
}
//RR.h
#ifndef RR_H_
#define RR_H_
#include <iostream>
天津新东方#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
//作业结构体
typedef struct PCB
{
int ID; //标识符埃及总统穆巴拉克
int ComeTime; //到达时间
int ServerTime; //服务时间
int FinishTime; //完成时间
int TurnoverTime; //周转时间
double WeightedTurnoverTime; //带权周转时间
}PCB;
/*
函数功能:输⼊作业信息
参数说明:
PCBList std::vector<PCB>& PCB链
timeslice int 时间⽚
*/
void InputPCB(std::vector<PCB> &PCBList, int ×lice);
/*
函数功能:RR算法
参数说明:
PCBList std::vector<PCB>& PCB链
*/
void RR(std::vector<PCB> &PCBList, int timeslice);
/
*
函数功能:显⽰结果
参数说明:
PCBList std::vector<PCB>& PCB链
*/
void show(std::vector<PCB> &PCBList);
/*
函数功能:⽐较函数,⽤于sort(),按ComeTime升序排列参数说明:
ivy是什么意思p1 const PCB& PCB从早到晚
p2 const PCB& PCB
*/
bool CmpByComeTime(const PCB &p1, const PCB &p2); #endif
//RR.cpp
#include "RR.h"
//输⼊作业信息
void InputPCB(std::vector<PCB> &PCBList,int ×lice)
{
std::cout << "输⼊时间⽚⼤⼩: ";
std::cin >> timeslice;
do {
std::cout << "输⼊标识符: ";
std::cin >> temp.ID;
std::cout << "输⼊到达时间: ";
std::cin >> temp.ComeTime;
std::cout << "输⼊服务时间: ";
std::cin >> temp.ServerTime;
temp.FinishTime = 0; //暂时存放运⾏了多少时间,来判断此作业是否运⾏结束
PCBList.push_back(temp);
std::cout << "继续输⼊?Y/N: ";
char ans;
std::cin >> ans;
if ('Y' == ans || 'y' == ans)
continue;
el
break;
} while (true);
}
//RR算法
void RR(std::vector<PCB> &PCBList, int timeslice)
{
std::sort(PCBList.begin(), d(), CmpByComeTime); //按到达时间排序
std::vector<PCB> result; //保存结果
std::queue<PCB> Ready; //就绪队列
int BeginTime = (*PCBList.begin()).ComeTime; //第⼀个作业开始时间
Ready.push(*PCBList.begin());
while (!pty() || !pty())
{
if (!pty() && BeginTime >= (*PCBList.begin()).ComeTime) //有新作业到达,加⼊就绪队列
{
Ready.push(*PCBList.begin());
}league是什么意思
if (Ready.front().FinishTime + timeslice < Ready.front().ServerTime) //时间⽚⽤完没运⾏完,加⼊队尾
{
Ready.front().FinishTime += timeslice;
Ready.push(Ready.front());
Ready.pop();
BeginTime += timeslice;
}
el //此作业运⾏完
{
BeginTime += Ready.front().ServerTime - Ready.front().FinishTime;
Ready.front().FinishTime = BeginTime;
Ready.front().TurnoverTime = Ready.front().FinishTime - Ready.front().ComeTime;
Ready.front().WeightedTurnoverTime = (double)Ready.front().TurnoverTime / Ready.front().ServerTime;
//从就绪队列中移除作业
result.push_back(Ready.front());
Ready.pop();
}
}
//按ComeTime升序排序,便于显⽰结果
奢侈怎么读PCBList = result;
std::sort(PCBList.begin(), d(), CmpByComeTime);
}
//显⽰结果
void show(std::vector<PCB> &PCBList)
{
int SumTurnoverTime = 0;
double SumWeightedTurnoverTime = 0;
std::cout.tf(std::ios::left);
std::cout << std::tw(20) << "标识符";
for (std::vector<PCB>::iterator it = PCBList.begin(); it < d(); ++it)
std::cout << std::tw(5) << (*it).ID;
std::cout << std::endl;
std::cout << std::tw(20) << "到达时间";
for (std::vector<PCB>::iterator it = PCBList.begin(); it < d(); ++it)
新车保养常识std::cout << std::tw(5) << (*it).ComeTime;
std::cout << std::endl;
std::cout << std::tw(20) << "服务时间";
for (std::vector<PCB>::iterator it = PCBList.begin(); it < d(); ++it)
std::cout << std::tw(5) << (*it).ServerTime;
std::cout << std::endl;
std::cout << std::tw(20) << "完成时间";
for (std::vector<PCB>::iterator it = PCBList.begin(); it < d(); ++it)
std::cout << std::tw(5) << (*it).FinishTime;
std::cout << std::endl;
std::cout << std::tw(20) << "周转时间";
for (std::vector<PCB>::iterator it = PCBList.begin(); it < d(); ++it)
{
std::cout << std::tw(5) << (*it).TurnoverTime;
SumTurnoverTime += (*it).TurnoverTime;;
}
std::cout << std::endl;
std::cout << std::tw(20) << "带权周转时间";
for (std::vector<PCB>::iterator it = PCBList.begin(); it < d(); ++it)
{
std::cout << std::tw(5) << (*it).WeightedTurnoverTime;
SumWeightedTurnoverTime += (*it).WeightedTurnoverTime;;
}
std::cout << std::endl;
std::cout << "平均周转时间: " << (double)SumTurnoverTime / PCBList.size() << std::endl;
std::cout << "平均带权周转时间: " << SumWeightedTurnoverTime / PCBList.size() << std::endl; }
//⽐较函数,按ComeTime升序排列
bool CmpByComeTime(const PCB &p1, const PCB &p2)
{
return p1.ComeTime < p2.ComeTime;
}