日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

RR算法 调度

發布時間:2023/12/20 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 RR算法 调度 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

RR算法是使用非常廣泛的一種調度算法。

首先將所有就緒的隊列按FCFS策略排成一個就緒隊列,然后系統設置一定的時間片,每次給隊首作業分配時間片。如果此作業運行結束,即使時間片沒用完,立刻從隊列中去除此作業,并給下一個作業分配新的時間片;如果作業時間片用完沒有運行結束,則將此作業重新加入就緒隊列尾部等待調度。

?

?

?

?

  • //main.cpp
  • #include "RR.h"
  • int main()
  • {
  • std::vector<PCB> PCBList;
  • int timeslice;
  • //輸入時間片大小,作業信息
  • InputPCB(PCBList, timeslice);
  • //RR算法
  • RR(PCBList, timeslice);
  • //顯示結果
  • show(PCBList);
  • return 0;
  • }
  • //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升序排列
  • 參數說明:
  • 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 {
  • 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);
  • }
  • //RR算法
  • 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();
  • }
  • }
  • //按ComeTime升序排序,便于顯示結果
  • 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;
  • }
  • //比較函數,按ComeTime升序排列
  • bool CmpByComeTime(const PCB &p1, const PCB &p2)
  • {
  • return p1.ComeTime < p2.ComeTime;
  • }
  • ?

  • 總結

    以上是生活随笔為你收集整理的RR算法 调度的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。