生活随笔
收集整理的這篇文章主要介紹了
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算法 调度的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。