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.  
    }

RR算法 调度的更多相关文章

  1. 操作系统概念学习笔记 10 CPU调度

    操作系统概念学习笔记 10 CPU调度 多道程序操作系统的基础.通过在进程之间切换CPU.操作系统能够提高计算机的吞吐率. 对于单处理器系统.每次仅仅同意一个进程执行:不论什么其它进程必须等待,直到C ...

  2. [Linux]Linux系统调用列表

    本文列出了大部分常见的Linux系统调用,并附有简要中文说明. 以下是Linux系统调用的一个列表,包含了大部分常用系统调用和由系统调用派生出的的函数.这可能是你在互联网上所能看到的唯一一篇中文注释的 ...

  3. LVS负载均衡集群服务搭建详解(二)

    lvs-nat模型构建 1.lvs-nat模型示意图 本次构建的lvs-nat模型的示意图如下,其中所有的服务器和测试客户端均使用VMware虚拟机模拟,所使用的CentOS 7 VS内核都支持ipv ...

  4. 常用的Linux系统调用命令

    常用的Linux系统调用命令   下面一些函数已经过时,被新的更好的函数所代替了(gcc在链接这些函数时会发出警告),但因为兼容的原因还保留着,这些函数将在前面标上“*”号以示区别.   一.进程控制 ...

  5. Linux系统调用(转载)

    目录: 1. Linux系统调用原理 2. 系统调用的实现 3. Linux系统调用分类及列表 4.系统调用.用户编程接口(API).系统命令和内核函数的关系 5. Linux系统调用实例 6. Li ...

  6. Linux系统调用列表

    转自Linux系统调用列表 一.进程控制: fork 创建一个新进程 clone 按指定条件创建子进程 execve 运行可执行文件 exit 中止进程 _exit 立即中止当前进程 getdtabl ...

  7. Linux常用系统调用

    转载 http://www.ibm.com/developerworks/cn/linux/kernel/syscall/part1/appendix.html#icomments 按照惯例,这个列表 ...

  8. 【转】Linux系统调用列表

    一.进程控制: fork 创建一个新进程 clone 按指定条件创建子进程 execve 运行可执行文件 exit 中止进程 _exit 立即中止当前进程 getdtablesize 进程所能打开的最 ...

  9. Linux系统常见调用及其分类

    Linux系统调用主要可以分为以下几类: 进程控制  fork 创建一个新进程 clone 按指定条件创建子进程 execve 运行可执行文件 exit 中止进程 _exit 立即中止当前进程 get ...

随机推荐

  1. Open Source Book For ML

    The following is a list of free, open source books on machine learning, statistics, data-mining, etc ...

  2. centos 7 安装 php 5.5 5.6 7.0

    查看当前安装的PHP包 [root@node1 ~]# yum list installed | grep php php56w.x86_64 -.w7 @webtatic php56w-cli.x8 ...

  3. 【转】大数据分析中Redis怎么做到220万ops

    原文:http://www.cnblogs.com/nnhy/archive/2018/01/16/Redis220.html 大数据时代,海量数据分析就像吃饭一样,成为了我们每天的工作.为了更好的为 ...

  4. Java多线程学习(吐血超详细总结)(转)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 写在前面的话:此文只能说是java多线程的一个入门,其实Java里头线程完全可以写一本书了,但 ...

  5. from String value ('{}'); no single-String constructor/factory

    需要为类增加一个接受String的构造函数: 例如: public class B { private String name; public B(String b) { } public Strin ...

  6. Mysql 8 常用命令测试

    1.创建数据库,帐号及授权 create database testdb; CREATE USER 'rusking'@'%' IDENTIFIED BY '12345678'; CREATE USE ...

  7. [BetterExplained]书写是为了更好的思考

    我经常在走路和睡前总结所学过的内容,思考遗留的问题,一段时间的阅读和思考之后,一个总体的知识框架就会逐渐浮现在脑海中.然后我会将它书写下来,然而,我往往非常惊讶地发现,当我书写的时候,新的内容仍然源源 ...

  8. 【转载】ASP.NET MVC的过滤器【Filters】

    文章来自: http://www.cnblogs.com/HopeGi/p/3342083.html 这篇对Filters讲的很详细.正好我自己也不用写了,真的很棒的一篇文章 APS.NET MVC中 ...

  9. MUI class="mui-switch"开关 JQuery 控制开关

    <div class="mui-switch mui-active"> <div class="mui-switch-handle">& ...

  10. android 显示gif图片

    在android中不支持gif格式的图片,但是由于我希望在我的程序中刚刚加载的时候有一个小人在跑步表示正在加载.而这个小人跑就是一个gif图片.也就是希望程序一启动时就加载gif图片.在网上查找了一些 ...