RR算法 调度

RR算法是使用非常广泛的一种调度算法。

首先将所有就绪的队列按FCFS策略排成一个就绪队列,然后系统设置一定的时间片,每次给队首作业分配时间片。如果此作业运行结束,即使时间片没用完,立刻从队列中去除此作业,并给下一个作业分配新的时间片;如果作业时间片用完没有运行结束,则将此作业重新加入就绪队列尾部等待调度。

  1.  
    //main.cpp
  2.  
    #include "RR.h"
  3.  
     
  4.  
    int main()
  5.  
    {
  6.  
    std::vector<PCB> PCBList;
  7.  
    int timeslice;
  8.  
     
  9.  
    //输入时间片大小,作业信息
  10.  
    InputPCB(PCBList, timeslice);
  11.  
     
  12.  
    //RR算法
  13.  
    RR(PCBList, timeslice);
  14.  
     
  15.  
    //显示结果
  16.  
    show(PCBList);
  17.  
     
  18.  
    return 0;
  19.  
    }
  20.  
     
  21.  
    //RR.h
  22.  
    #ifndef RR_H_
  23.  
    #define RR_H_
  24.  
     
  25.  
    #include <iostream>
  26.  
    #include <algorithm>
  27.  
    #include <iomanip>
  28.  
    #include <vector>
  29.  
    #include <queue>
  30.  
     
  31.  
    //作业结构体
  32.  
    typedef struct PCB
  33.  
    {
  34.  
    int ID; //标识符
  35.  
    int ComeTime; //到达时间
  36.  
    int ServerTime; //服务时间
  37.  
    int FinishTime; //完成时间
  38.  
    int TurnoverTime; //周转时间
  39.  
    double WeightedTurnoverTime; //带权周转时间
  40.  
    }PCB;
  41.  
     
  42.  
    /*
  43.  
    函数功能:输入作业信息
  44.  
    参数说明:
  45.  
    PCBList std::vector<PCB>& PCB链
  46.  
    timeslice int 时间片
  47.  
    */
  48.  
    void InputPCB(std::vector<PCB> &PCBList, int ×lice);
  49.  
     
  50.  
    /*
  51.  
    函数功能:RR算法
  52.  
    参数说明:
  53.  
    PCBList std::vector<PCB>& PCB链
  54.  
    */
  55.  
    void RR(std::vector<PCB> &PCBList, int timeslice);
  56.  
     
  57.  
    /*
  58.  
    函数功能:显示结果
  59.  
    参数说明:
  60.  
    PCBList std::vector<PCB>& PCB链
  61.  
    */
  62.  
    void show(std::vector<PCB> &PCBList);
  63.  
     
  64.  
    /*
  65.  
    函数功能:比较函数,用于sort(),按ComeTime升序排列
  66.  
    参数说明:
  67.  
    p1 const PCB& PCB
  68.  
    p2 const PCB& PCB
  69.  
    */
  70.  
    bool CmpByComeTime(const PCB &p1, const PCB &p2);
  71.  
     
  72.  
    #endif
  73.  
     
  74.  
    //RR.cpp
  75.  
    #include "RR.h"
  76.  
     
  77.  
    //输入作业信息
  78.  
    void InputPCB(std::vector<PCB> &PCBList,int ×lice)
  79.  
    {
  80.  
    std::cout << "输入时间片大小: ";
  81.  
    std::cin >> timeslice;
  82.  
    do {
  83.  
    PCB temp;
  84.  
    std::cout << "输入标识符: ";
  85.  
    std::cin >> temp.ID;
  86.  
    std::cout << "输入到达时间: ";
  87.  
    std::cin >> temp.ComeTime;
  88.  
    std::cout << "输入服务时间: ";
  89.  
    std::cin >> temp.ServerTime;
  90.  
    temp.FinishTime = 0; //暂时存放运行了多少时间,来判断此作业是否运行结束
  91.  
    PCBList.push_back(temp);
  92.  
     
  93.  
    std::cout << "继续输入?Y/N: ";
  94.  
    char ans;
  95.  
    std::cin >> ans;
  96.  
    if ('Y' == ans || 'y' == ans)
  97.  
    continue;
  98.  
    else
  99.  
    break;
  100.  
    } while (true);
  101.  
    }
  102.  
     
  103.  
    //RR算法
  104.  
    void RR(std::vector<PCB> &PCBList, int timeslice)
  105.  
    {
  106.  
    std::sort(PCBList.begin(), PCBList.end(), CmpByComeTime); //按到达时间排序
  107.  
    std::vector<PCB> result; //保存结果
  108.  
    std::queue<PCB> Ready; //就绪队列
  109.  
    int BeginTime = (*PCBList.begin()).ComeTime; //第一个作业开始时间
  110.  
    Ready.push(*PCBList.begin());
  111.  
    PCBList.erase(PCBList.begin());
  112.  
     
  113.  
    while (!PCBList.empty() || !Ready.empty())
  114.  
    {
  115.  
    if (!PCBList.empty() && BeginTime >= (*PCBList.begin()).ComeTime) //有新作业到达,加入就绪队列
  116.  
    {
  117.  
    Ready.push(*PCBList.begin());
  118.  
    PCBList.erase(PCBList.begin());
  119.  
    }
  120.  
    if (Ready.front().FinishTime + timeslice < Ready.front().ServerTime) //时间片用完没运行完,加入队尾
  121.  
    {
  122.  
    Ready.front().FinishTime += timeslice;
  123.  
    Ready.push(Ready.front());
  124.  
    Ready.pop();
  125.  
    BeginTime += timeslice;
  126.  
    }
  127.  
    else //此作业运行完
  128.  
    {
  129.  
    BeginTime += Ready.front().ServerTime - Ready.front().FinishTime;
  130.  
    Ready.front().FinishTime = BeginTime;
  131.  
    Ready.front().TurnoverTime = Ready.front().FinishTime - Ready.front().ComeTime;
  132.  
    Ready.front().WeightedTurnoverTime = (double)Ready.front().TurnoverTime / Ready.front().ServerTime;
  133.  
     
  134.  
    //从就绪队列中移除作业
  135.  
    result.push_back(Ready.front());
  136.  
    Ready.pop();
  137.  
    }
  138.  
     
  139.  
    }
  140.  
     
  141.  
    //按ComeTime升序排序,便于显示结果
  142.  
    PCBList = result;
  143.  
    std::sort(PCBList.begin(), PCBList.end(), CmpByComeTime);
  144.  
    }
  145.  
     
  146.  
    //显示结果
  147.  
    void show(std::vector<PCB> &PCBList)
  148.  
    {
  149.  
    int SumTurnoverTime = 0;
  150.  
    double SumWeightedTurnoverTime = 0;
  151.  
     
  152.  
    std::cout.setf(std::ios::left);
  153.  
     
  154.  
    std::cout << std::setw(20) << "标识符";
  155.  
    for (std::vector<PCB>::iterator it = PCBList.begin(); it < PCBList.end(); ++it)
  156.  
    std::cout << std::setw(5) << (*it).ID;
  157.  
    std::cout << std::endl;
  158.  
     
  159.  
    std::cout << std::setw(20) << "到达时间";
  160.  
    for (std::vector<PCB>::iterator it = PCBList.begin(); it < PCBList.end(); ++it)
  161.  
    std::cout << std::setw(5) << (*it).ComeTime;
  162.  
    std::cout << std::endl;
  163.  
     
  164.  
    std::cout << std::setw(20) << "服务时间";
  165.  
    for (std::vector<PCB>::iterator it = PCBList.begin(); it < PCBList.end(); ++it)
  166.  
    std::cout << std::setw(5) << (*it).ServerTime;
  167.  
    std::cout << std::endl;
  168.  
     
  169.  
    std::cout << std::setw(20) << "完成时间";
  170.  
    for (std::vector<PCB>::iterator it = PCBList.begin(); it < PCBList.end(); ++it)
  171.  
    std::cout << std::setw(5) << (*it).FinishTime;
  172.  
    std::cout << std::endl;
  173.  
     
  174.  
    std::cout << std::setw(20) << "周转时间";
  175.  
    for (std::vector<PCB>::iterator it = PCBList.begin(); it < PCBList.end(); ++it)
  176.  
    {
  177.  
    std::cout << std::setw(5) << (*it).TurnoverTime;
  178.  
    SumTurnoverTime += (*it).TurnoverTime;;
  179.  
    }
  180.  
    std::cout << std::endl;
  181.  
     
  182.  
    std::cout << std::setw(20) << "带权周转时间";
  183.  
    for (std::vector<PCB>::iterator it = PCBList.begin(); it < PCBList.end(); ++it)
  184.  
    {
  185.  
    std::cout << std::setw(5) << (*it).WeightedTurnoverTime;
  186.  
    SumWeightedTurnoverTime += (*it).WeightedTurnoverTime;;
  187.  
    }
  188.  
    std::cout << std::endl;
  189.  
     
  190.  
    std::cout << "平均周转时间: " << (double)SumTurnoverTime / PCBList.size() << std::endl;
  191.  
    std::cout << "平均带权周转时间: " << SumWeightedTurnoverTime / PCBList.size() << std::endl;
  192.  
    }
  193.  
     
  194.  
    //比较函数,按ComeTime升序排列
  195.  
    bool CmpByComeTime(const PCB &p1, const PCB &p2)
  196.  
    {
  197.  
    return p1.ComeTime < p2.ComeTime;
  198.  
    }
原文地址:https://www.cnblogs.com/qiaoyanlin/p/9435858.html