操作系统-使用动态优先权的进程的调度算法的模拟

/*  使用动态优先权的进程的调度算法的模拟     20154350315   */
#include <iostream>

using namespace std;
int Ready[5];
int Block[5];
int runningID;

struct PCB{
   int id;//进程标识数
   int priority;//优先数
   int cputime;//已经占用cpu时间
   int alltime;//还需占用cpu时间
   int startblock;//阻塞时间
   int blocktime;//已经被阻塞时间
   string  state;//当前状态
   PCB*  next;//下个进程的指针
};

void printf(PCB t[]);
void  readyList(PCB t[]);
int main()
{
    //给进程赋值
    PCB thread1={0,9,0,3,2,3,"Ready"};
    PCB thread2={1,38,0,3,-1,0,"Ready"};
    PCB thread3={2,30,0,6,-1,0,"Ready"};
    PCB thread4={3,29,0,3,-1,0,"Ready"};
    PCB thread5={4,0,0,4,-1,0,"Ready"};

    PCB thread[5] = {thread1,thread2,thread3,thread4,thread5};
    int countEnd=0;//当前结束的线程数量
    int runningID;//正在运行的线程的ID
    //先打印初值
        printf(thread);
        while(countEnd<5){
         //就绪列表第一个即正在运行的线程
         runningID =Ready[0];
         //修改正在运行的线程的情况
         thread[runningID].alltime-=1;
         thread[runningID].cputime+=1;
         //判断是否结束
         if(thread[runningID].alltime==0)
           {
                      thread[runningID].priority=0;
                      thread[runningID].state="End";
                      countEnd++;
           }
         else{
              thread[runningID].priority-=3;
              thread[runningID].state="Running";
         }
         //判断是否进行阻塞
         if(thread[runningID].startblock>-1&&thread[runningID].blocktime>0){
            //阻塞时间减到0就阻塞
            thread[runningID].startblock--;
            if(thread[runningID].startblock==0){
                thread[runningID].state="Block";
            }
         }

         //修改不在运行的线程情况
         for(int i=0;i<5;i++){
            if(i!=runningID&&thread[i].state!="End"&&thread[i].state!="Block")
            {
                thread[i].priority++;
                thread[i].state="Ready";
            }
            //被阻塞时间为0时解除阻塞
            if(thread[i].state=="Block"){
                thread[i].blocktime--;
                if(thread[i].blocktime==0){
                    thread[i].state="Ready";
                }
            }
         }
          //打印
          printf(thread);
        }

}

//打印函数
void printf(PCB t[]){
    //找到就绪队列
     readyList(t);
   //找到阻塞队列
    int Block[5];
    for(int i=0;i<5;i++){
        Block[i]=-1;
    }
    int j=0;
    for(int i=0;i<5;i++){
       if(t[i].state=="Block")
        Block[j++]=i;
    }
    //开始打印部分
    cout<<"RUNNING PROG:";
    for(int i=0;i<5;i++)
    {
        if(t[i].state=="Running")
            cout<<i;
    }
    cout<<endl<<"READY_QUEUE:";
    for(int i=0;i<5;i++){
        if(Ready[i]!=-1&&t[Ready[i]].state!="Running")cout<<"->"<<Ready[i];
    }
    cout<<endl<<"BLOCK_QUEUE:";
    for(int i=0;i<5;i++){
        if(Block[i]!=-1)cout<<"->"<<Block[i];
    }
    cout<<endl<<"====================================================="<<endl;
         cout<<"ID           ";
    for(int i=0;i<5;i++){
        cout<<"             "<<i;
    }
    cout<<endl<<"PRIORITY    ";
    for(int i=0;i<5;i++){
        cout<<"             "<<t[i].priority;
    }
    cout<<endl<<"CPUTIME     ";
    for(int i=0;i<5;i++){
        cout<<"             "<<t[i].cputime;
    }
    cout<<endl<<"ALLTIME     ";
    for(int i=0;i<5;i++){
        cout<<"             "<<t[i].alltime;
    }
    cout<<endl<<"STARTBLOCK  ";
    for(int i=0;i<5;i++){
        cout<<"             "<<t[i].startblock;
    }
    cout<<endl<<"BLOCKTIME   ";
    for(int i=0;i<5;i++){
        cout<<"             "<<t[i].blocktime;
    }
    cout<<endl<<"STATE       ";
    for(int i=0;i<5;i++){
        cout<<"           "<<t[i].state;
    }
    cout<<endl<<"******************************************************************************************"<<endl;

}
//寻找并排序就绪列表
void readyList(PCB t[]){
    int j=0;
    //重置队列
     for(int i=0;i<5;i++){
        Ready[i]=-1;
     }
     //添加元素
    for(int i=0;i<5;i++)
   {
       if(t[i].state=="Ready"||t[i].state=="Running")
        Ready[j++]=i;
   }
   //逆序排序
    for(int i=0;i<4;i++){
      for(int k=i+1;k<5;k++)
      {
          if(t[Ready[i]].priority<t[Ready[k]].priority){
            int tmp = Ready[k];
            Ready[k]=Ready[i];
            Ready[i]=tmp;
          }
      }
   }
}



(1)用C语言来实现对N个进程采用动态优先权优先算法进程调度。 (2)每个用来标识进程进程控制块PCB用结构来描述,包括以下字段: •••• 进程标识数 ID。 •••• 进程优先数 PRIORITY,并规定优先数越大的进程,其优先权越高。 •••• 进程已占用的CPU时间CPUTIME。 •••• 进程还需占用的CPU时间ALLTIME。当进程运行完毕时,ALLTIME变为0。•••• 进程的阻塞时间STARTBLOCK,表示当进程再运行STARTBLOCK个时间片后,将进入阻塞状态。 •••• 进程被阻塞的时间BLOCKTIME,表示已足赛的进程再等待BLOCKTIME个时间片后,将转换成就绪状态。 •••• 进程状态START。 •••• 队列指针NEXT,用来将PCB排成队列。 (3)优先数改变的原则: •••进程在就绪队列中呆一个时间片,优先数加1。 •••进程每运行一个时间片,优先数减3。 (4)假设在调度前,系统中有5个进程,它们的初始状态如下: ID 0 1 2 3 4 PRIORITY 9 38 30 29 0 CPUTIME 0 0 0 0 0 ALLTIME 3 3 6 3 4 STARTBLOCK 2 -1 -1 -1 -1 BLOCKTIME 3 0 0 0 0 STATE READY READY READY READY READY (5)为了清楚的观察各进程调度过程,程序应将每个时间片内的情况显示出来,参照的具体格式如下: RUNNING PROG:i READY-QUEUE:->id1->id2 BLOCK-QUEUE:->id3->id4 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ID 0 1 2 3 4 PRIORITY P0 P1 P2 P3 P4 CUPTIME C0 C1 C2 C3 C4 ALLTIME A0 A1 A2 A3 A4 STARTBLOCK T0 T1 T2 T3 T4 BLOCKTIME B0 B1 B2 B3 B4 STATE S0 S1 S2 S3 S4
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值