zoukankan      html  css  js  c++  java
  • 优先调度和轮转调度

    优先调度中,优先级为100-needtime,没执行一次,优先级将5

    轮转调度的时间片为2

    View Code
    #include<iostream>
    using namespace std;
    #include "string.h"
    typedef struct node
    {
       char name[10];  //进程的名称 
       int prio;   //进程的优先级 
       int round; //CPU的时间片 
       int cputime; //进程占用CPU时间
       int needtime; //进程到完成还要的时间*
       int count;  //计数器
       char state; //进程的状态,运行态'R',就绪态'W',完成态'F' 
       struct node *next; //指向下一个进程的指针 
    }PCB;
    PCB *finish,//完成队列的指针 
         *ready,//就绪队列的指针 
         *tail,//队列的尾指针 
         *run; //执行成队列的指针 
         
    int N; //进程数
    
    
    void FirstRun()//执行就绪队列中的第一个进程
    {
       run=ready;   //就绪队列头指针赋值给运行头指针
       run->state='R';  //把现运行的进程的状态改为‘R’ 
       ready=ready->next;  //就绪对列头指针后移到下一进程
    }
    //标题输出函数
    void PrintfTile(char a)
    {
       if(a=='P') /*优先数法*/
          cout<<"name     cputime  needtime  priority  state"<<endl;
       else
          cout<<"name     cputime  needtime  count  round   state"<<endl;
    }
    /*进程PCB输出*/
    void PrintfPCB(char a,PCB *q)
    {
       if(a=='P')  /*优先数法的输出*/
           cout<<q->name<<"         "<<
                 q->cputime<<"        "<<
                 q->needtime<<"        "<<
                 q->prio<<"        "<<
                 q->state<<"       "<<endl;
       else/*轮转法的输出*/
           cout<<q->name<<"         "<<
                 q->cputime<<"        "<<
                 q->needtime<<"         "<<
                 q->count<<"      "<<
                 q->round<<"      "<<
                 q->state<<"    "<<endl;
    }
    /*输出函数*/
    void Output(char algo)
    {
       PCB *p;
       PrintfTile(algo); 
        
       if(run!=NULL) //如果允许进程不为空 
           PrintfPCB(algo,run); //输出当前正在运行的进程情况 
       p=ready;   //输出就绪队列的进程情况 
       while(p!=NULL)
       {
          PrintfPCB(algo,p);
          p=p->next;
       }
       p=finish;  //输出完成队列的进程 
       while(p!=NULL)
       {
          PrintfPCB(algo,p);
          p=p->next;
       }
       getchar(); 
    }
    //优先数的插入算法
    void InsertPriority(PCB *q)
    {
       PCB *p1,*s,*r;
       int flag=1;
       s=q;  
       p1=ready; //就绪队列头指针
       r=p1; //r做p1的前驱指针
       while((p1!=NULL)&&flag)  /*根据优先数确定插入位置*/
          if(p1->prio>=s->prio)
          {
             r=p1;
             p1=p1->next;
          }
          else
          flag=0;
       if(r!=p1)  /*如果条件成立说明插入在r与p1之间*/
       {
          r->next=s;
          s->next=p1;
       }
       else
       {
          s->next=p1;  /*否则插入在就绪队列的头*/
          ready=s;
       }
    }
    //轮转法插入函数
    void InsertRount(PCB *p2)
    {
       tail->next=p2;  //将新的PCB插入在当前就绪队列的尾
       tail=p2;
       p2->next=NULL;
    }
    /*优先数创建初始PCB信息*/
    void create1(char alg)
    {
       PCB *p;
       int i,time;
       cout<<"请输入进程的ID和它需要的运行时间:"<<endl; 
       for(i=1;i<=N;i++)
       {
          p=(PCB *)malloc(sizeof(PCB));
          cin>>p->name;
          cin>>p->needtime;
          p->cputime=0;
          p->state='w';
          p->prio=100-p->needtime;
          if(ready!=NULL) /*就绪队列不空调用插入函数插入*/
          InsertPriority(p);
          else
          {
         p->next=ready; /*创建就绪队列的第一个PCB*/
         ready=p;
          }
       }
       cout<<"                 PriotityAlgorithm:"<<endl;
       cout<<"========================================================"<<endl;
       Output(alg);  /*输出进程PCB信息*/
       run=ready; /*将就绪队列的第一个进程投入运行*/
       ready=ready->next;
       run->state='R';
    }
    //轮转法创建进程
    void CreateRount(char alg)
    {
       PCB *p;
       int i,time;
       char na[10];
       ready=NULL;
       finish=NULL;
       run=NULL;
    
       cout<<"请输入进程的ID和它需要的运行时间:"<<endl; 
       for(i=1;i<=N;i++)
       {
          p=(PCB *)malloc(sizeof(PCB));
    
          cin>>p->name; 
          cin>>p->needtime;
          p->cputime=0;
          p->count=0; /*计数器*/
          p->state='w';
          p->round=2;  /*时间片*/
          if(ready!=NULL)
          InsertRount(p);
          else
          {
            p->next=ready;
            ready=p;
            tail=p;
          }
       }
       
       cout<<"                 PoundyAlgorithm:"<<endl;
       cout<<"========================================================"<<endl;
       Output(alg);   /*输出进程PCB信息*/
       run=ready;  /*将就绪队列的第一个进程投入运行*/
       ready=ready->next;
       run->state='R';
    }
    //优先数调度算法
    void PriorityAlgorthm(char alg)
    {
       while(run!=NULL)  //当运行队列不空时
       {
          run->cputime++;
          run->needtime--;
          run->prio=run->prio-5; //每运行一次优先数降低5
          if(run->needtime==0)  //所需时间为0时将其插入完成队列
          {
         run->next=finish;
         finish=run;
         run->state='F';  //状态变为为完成 
         run=NULL;       //运行队列头指针为空
         if(ready!=NULL) //如就绪队列不空
            FirstRun();  //将就绪对列的第一个进程运行
          }
          else //没有运行完同时优先数不是最大,则将其变为就绪态插入到就绪队列
         if((ready!=NULL)&&(run->prio<ready->prio))
         {
            run->state='W';
            InsertPriority(run);
            FirstRun(); //将就绪队列的第一个进程投入运行
         }
          Output(alg); //输出进程PCB信息
       }
    }
    //时间片轮转法
    void RoundAlgorthm(char alg)
    {
       while(run!=NULL)
       {
          run->cputime++;
          run->needtime--;
          run->count=run->count+1;
          if(run->needtime==0)//运行完将其变为完成态,插入完成队列*/
          {
           run->next=finish;
           finish=run;
           run->state='F';
           run=NULL;
           //run->needtime=0;
           if(ready!=NULL)
             FirstRun(); //就绪对列不空,将第一个进程投入运行*/
          }
          else
         if(run->count==run->round)  //如果时间片到
         {
            run->count=0;  //计数器置0
            if(ready!=NULL) //就绪队列不空时 
            {
               run->state='W'; //将进程插入到就绪队列中等待轮转
               InsertRount(run);
               FirstRun(); /*将就绪对列的第一个进程投入运行*/
            }
         }
          Output(alg); //输出进程信息
       }
    }
    
    int main()
    {
       char choose;  //算法标记*/
       cout<< "请输入你要选择的执行算法:P/R(优先级算法或者轮转法)"<<endl;
       cin>>choose;
    
       cout<<"进程的数目:";
       cin>>N;
       if(choose=='P'||choose=='p')
       {
          create1(choose); //优先数法
          PriorityAlgorthm(choose);
       }
       else
          if(choose=='R'||choose=='r')
          {
           CreateRount(choose); //轮转法
           RoundAlgorthm(choose);
          }
          return 0;
    }
  • 相关阅读:
    11. Container With Most Water
    9. Palindrome Number
    375. 猜数字大小 II leetcode java
    leetcode 72 编辑距离 JAVA
    73. 矩阵置零 leetcode JAVA
    快速排序 JAVA实现
    63. 不同路径 II leetcode JAVA
    重写(override)与重载(overload)
    62 不同路径 leetcode JAVA
    leetcode 56 合并区间 JAVA
  • 原文地址:https://www.cnblogs.com/aijianiula/p/2785447.html
Copyright © 2011-2022 走看看