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. [BOI2004]Sequence 数字序列

    Description: Hint: \(n<=10^5\) Solution: 首先考虑b不严格递增时的做法 发现当\(a[i]\)递增时\(b[i]\)直接取\(a[i]\)即可,否则此时需 ...

  2. 老菜鸟学习:Javascript 将html转成pdf

    起因:处理某个项目,需要把页面上的数据(订单.运单)等导出pdf. 第一个想法:从 Java 层去想.但是经过各种资料查询和实践,第一个想法宣告放弃: 幸好客户的要求是:导出的 pdf 尺寸要和打印的 ...

  3. 基于Grunt构建一个JavaScript库

    现在公认的JavaScript典型项目需要运行单元测试,合并压缩.有些还会使用代码生成器,代码样式检查或其他构建工具. Grunt.js是一个开源工具,可以帮助你完成上面的所有步骤.它非常容易扩展,并 ...

  4. Javascript 函数声明先提升还是变量先提升

    大家都知道js 分为词法阶段 和执行阶段 也知道它是因为var变量和函数声明会提升 但是你知道他们两个谁先提升的吗 测试一下 function test(){ alert(4); } var test ...

  5. 集合(4)—Collection之Set的使用方法

    定义 set接口及其实现类–HashSet Set是元素无序且不可重复的集合,被称为集. HashSet是哈希集,是Set的一个重要实现类 set中循环只能使用foreach和iterator这两个, ...

  6. 简单理解Linux的Loopback接口

    Linu支持环回接口( Loopback Interface),以允许运行在同一台主机上的客户程序和服务器程序通TCP/IP进行通信. A 类网络127就是为环回接口预留的 .根据惯例,大多数系统把I ...

  7. 初识zookeeper(1)之zookeeper的安装及配置

    初识zookeeper(一)之zookeeper的安装及配置 1.简要介绍 zookeeper是一个分布式的应用程序协调服务,是Hadoop和Hbase的重要组件,是一个树型的目录服务,支持变更推送. ...

  8. Delphi不注册COM直接使用ActiveX控件并绑定事件

    文笔不行,直接上源码: 主窗口: unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System ...

  9. web架构延变

    在现代的软件系统中,几乎所有的系统都使用到了数据库,不论是关系型数据,例如MySql.SQLite.Oracle.SQLServer等,还是非关系性数据,例如mongoDB.redis等.本文已web ...

  10. 【iOS开发】关于显示一连串图片组成动画效果UIImageView的使用

    关于使用UIImageView显示一串图片组成动画效果的总结: 1>创建装这一串图片的容器,使用NSArray NSMutableArray *images = [NSMutableArray ...