生活随笔
收集整理的这篇文章主要介绍了
RR算法 调度
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
RR算法是使用非常广泛的一种调度算法。
首先将所有就绪的队列按FCFS策略排成一个就绪队列,然后系统设置一定的时间片,每次给队首作业分配时间片。如果此作业运行结束,即使时间片没用完,立刻从队列中去除此作业,并给下一个作业分配新的时间片;如果作业时间片用完没有运行结束,则将此作业重新加入就绪队列尾部等待调度。
#include "RR.h" int main() { std::vector<PCB> PCBList; int timeslice; InputPCB(PCBList, timeslice); RR(PCBList, timeslice); show(PCBList); return 0; } #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; void InputPCB(std::vector<PCB> &PCBList, int ×lice); void RR(std::vector<PCB> &PCBList, int timeslice); void show(std::vector<PCB> &PCBList); bool CmpByComeTime(const PCB &p1, const PCB &p2); #endif #include "RR.h" void InputPCB(std::vector<PCB> &PCBList,int ×lice) { std::cout << "输入时间片大小: "; std::cin >> timeslice; do { PCB temp; 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; else break; } while (true); } void RR(std::vector<PCB> &PCBList, int timeslice) { std::sort(PCBList.begin(), PCBList.end(), CmpByComeTime); std::vector<PCB> result; std::queue<PCB> Ready; int BeginTime = (*PCBList.begin()).ComeTime; Ready.push(*PCBList.begin()); PCBList.erase(PCBList.begin()); while (!PCBList.empty() || !Ready.empty()) { if (!PCBList.empty() && BeginTime >= (*PCBList.begin()).ComeTime) { Ready.push(*PCBList.begin()); PCBList.erase(PCBList.begin()); } if (Ready.front().FinishTime + timeslice < Ready.front().ServerTime) { Ready.front().FinishTime += timeslice; Ready.push(Ready.front()); Ready.pop(); BeginTime += timeslice; } else { 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(); } } PCBList = result; std::sort(PCBList.begin(), PCBList.end(), CmpByComeTime); } void show(std::vector<PCB> &PCBList) { int SumTurnoverTime = 0; double SumWeightedTurnoverTime = 0; std::cout.setf(std::ios::left); std::cout << std::setw(20) << "标识符"; for (std::vector<PCB>::iterator it = PCBList.begin(); it < PCBList.end(); ++it) std::cout << std::setw(5) << (*it).ID; std::cout << std::endl; std::cout << std::setw(20) << "到达时间"; for (std::vector<PCB>::iterator it = PCBList.begin(); it < PCBList.end(); ++it) std::cout << std::setw(5) << (*it).ComeTime; std::cout << std::endl; std::cout << std::setw(20) << "服务时间"; for (std::vector<PCB>::iterator it = PCBList.begin(); it < PCBList.end(); ++it) std::cout << std::setw(5) << (*it).ServerTime; std::cout << std::endl; std::cout << std::setw(20) << "完成时间"; for (std::vector<PCB>::iterator it = PCBList.begin(); it < PCBList.end(); ++it) std::cout << std::setw(5) << (*it).FinishTime; std::cout << std::endl; std::cout << std::setw(20) << "周转时间"; for (std::vector<PCB>::iterator it = PCBList.begin(); it < PCBList.end(); ++it) { std::cout << std::setw(5) << (*it).TurnoverTime; SumTurnoverTime += (*it).TurnoverTime;; } std::cout << std::endl; std::cout << std::setw(20) << "带权周转时间"; for (std::vector<PCB>::iterator it = PCBList.begin(); it < PCBList.end(); ++it) { std::cout << std::setw(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; } bool CmpByComeTime(const PCB &p1, const PCB &p2) { return p1.ComeTime < p2.ComeTime; }
总结
以上是生活随笔为你收集整理的RR算法 调度的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。