zoukankan      html  css  js  c++  java
  • 操作系统 RR轮转调度算法(C++实现)

    1. 基本原理

      在轮转(RR)法中,系统根据FCFS策略,将所有的就绪进程排成一个就绪队列,并可设置每隔一定时间间隔(即时间片)即产生一次中断,激活系统中的进程调度程序,完成一次调度,将CPU分配给队首进程,令其执行。

      进程切换时机:

    • 若一个时间片尚未用完,进程就已经结束,则立即激活调度程序,将其从队列中删除,并启动一个新的时间片。
    • 在一个时间片用完时,进程尚未结束,则将其送往队尾。

    2. 代码实现

      2.1 初始化数据

    作业情况

    时间片

    进程名 0  1 2 3 4 平均
    到达时间 0 1 2 3 4  
    服务时间 4 3 4 2 4  

    RR

    q = 4

    完成时间 4 7 11 13 17  
    周转时间 4 6 9 10 13 8.4
    带权周转时间 1 2 2.25 5 3.25 2.5

      2.2 RR实现函数

     1 //RR轮转调度算法
     2 void RoundRobin( vector<int> T, vector<double> S, vector<int> &FT, vector<int> &WT 
     3             , vector<double> &WWT){
     4     int q , CurTime = 0, count = 0;
     5     printf("Please enter the number of piece:
    ");
     6     cin >> q;
     7     queue<int> list;
     8     vector<double> _S = S; //用来存储服务时间
     9     while(CurTime < 17){
    10         while( T[count] <= CurTime && count < T.size())
    11             list.push(count ++);
    12         // 利用队列完成时间片轮转
    13         if( ! list.empty() ){
    14             int temp = list.front();
    15             list.pop();
    16             for( int i = 0; i < q; i ++, S[temp] --, CurTime ++){
    17                 if( S[temp] > 0)
    18                     printf("Time %d : Program %d is Running.
    ",CurTime ,temp);
    19                 else break;
    20             }
    21             //先判断是否有新的就绪进程可以入队
    22             while( T[count] <= CurTime && count < T.size())
    23                 list.push(count ++);
    24             //再将之前未完成的进程入队
    25             if(S[temp] > 0)
    26                 list.push(temp);
    27             else{
    28                 printf("Time %d : Program %d is over.
    ", CurTime ,temp);
    29                 FT[temp] = CurTime; WT[temp] = FT[temp] - T[temp]; WWT[temp] = WT[temp] / _S[temp];
    30             }
    31         }
    32         else
    33             printf( "Time %d : No Program is Running.
    ", CurTime ++);
    34     }
    35 
    36 }
  • 相关阅读:
    重学SQL Server 笔记(二)
    H.E mapreduce操作HBase(转载)
    Paxos算法能帮助我们做什么呢? 如下几点:
    Hive入门3–Hive与HBase的整合(转载)
    Hadoop的I / O管道剖析
    install jdk in ubuntu( please notice diffrent verstion and change the name in the configuration)
    sudo bin/hadoop namenode format
    asp.net + ext grid程序示例(提供源码下载)
    摩尔定律
    it's time go to sleep ,i will be continue!
  • 原文地址:https://www.cnblogs.com/john1015/p/12980008.html
Copyright © 2011-2022 走看看