CPU调度算法

2019-07-14 09:17发布

两种进程调度算法:1)优先数调度;2)循环轮转调度   ①本程序用两种算法对五个进程进行调度,每个进程可有三个状态,并假设初始状态为就绪状态。 ②为了便于处理,程序中的某进程运行时间以时间片为单位计算。各进程的优先数或轮转时间数以及进程需运行的时间片数的初始值均由用户给定。 ③在优先数算法中,优先数可以先取值为98,进程每执行一次,优先数减3CPU时间片数加1,进程还需要的时间片数减1。在轮转算法中,采用固定时间片(即:每执行一次进程,该进程的执行时间片数为已执行了2个单位),这时,CPU时间片数加2,进程还需要的时间片数减2,并排列到就绪队列的尾上。 对于遇到优先数一致的情况,采用FIFO策略解决。     #include #include #include #include #include #define P_NUM 5 #define P_TIME 50 enum state{        ready,        execute,        block,        finish };   struct pcb{        char name[4];        int priority;        int cputime;        int needtime;        int count;        int round;        state process;        pcb * next; }; pcb * get_process(); pcb * get_process(){        pcb *q;        pcb *t;        pcb *p;        int i=0;        cout<<"input name and time"<          while (i               q=(struct pcb *)malloc(sizeof(pcb));               cin>>q->name;               cin>>q->needtime;               q->cputime=0;               q->priority=P_TIME-q->needtime;               q->process=ready;               q->next=NULL;               if (i==0){                      p=q;                      t=q;               }               else{                      t->next=q;                      t=q;               }               i++;        }  //while        return p; } void  display(pcb *p){        cout<<"name"<<"    "<<"cputime"<<"    "<<"needtime"<<"    "<<"priority"<<"    "<<"state"<        while(p){               cout<name;               cout<<"        ";               cout<cputime;               cout<<"          ";               cout<needtime;               cout<<"          ";               cout<priority;               cout<<"          ";               switch(p->process){                      case ready:cout<<"ready"<                      case execute:cout<<"execute"<                      case block:cout<<"block"<                      case finish:cout<<"finish"<               }               p=p->next;        } }   int process_finish(pcb *q){        int bl=1;        while(bl&&q){               bl=bl&&q->needtime==0;               q=q->next;        }        return bl; }   void cpuexe(pcb *q){        pcb *t=q;        int tp=0;        while(q){               if (q->process!=finish){                      q->process=ready;                      if(q->needtime==0){                             q->process=finish;                      }               }               if(tppriority&&q->process!=finish){                      tp=q->priority;                      t=q;               }               q=q->next;        }        if(t->needtime!=0){               t->priority-=3;               t->needtime--;               t->process=execute;               t->cputime++;        } }   void priority_cal(){        pcb * p;        clrscr();        p=get_process();        int cpu=0;        clrscr();        while(!process_finish(p)){               cpu++;               cout<<"cputime:"<               cpuexe(p);               display(p);               sleep(2);               clrscr();        }        printf("All processes have finished,press any key to exit");        getch(); }   void display_menu(){        cout<<"CHOOSE THE ALGORITHM:"<        cout<<"1 PRIORITY"<        cout<<"2 ROUNDROBIN"<        cout<<"3 EXIT"< } pcb * get_process_round(){        pcb *q;        pcb *t;        pcb *p;        int i=0;        cout<<"input name and time"<          while (i               q=(struct pcb *)malloc(sizeof(pcb));               cin>>q->name;               cin>>q->needtime;               q->cputime=0;               q->round=0;               q->count=0;               q->process=ready;               q->next=NULL;               if (i==0){                      p=q;                      t=q;               }               else{                      t->next=q;                      t=q;               }               i++;        }  //while        return p; }   void cpu_round(pcb *q){        q->cputime+=2;        q->needtime-=2;        if(q->needtime<0) {               q->needtime=0;        }        q->count++;        q->round++;        q->process=execute;   }   pcb * get_next(pcb * k,pcb * head){        pcb * t;        t=k;        do{         t=t->next;        }        while (t && t->process==finish);            if(t==NULL){               t=head;                 while (t->next!=k && t->process==finish){                      t=t->next;               }        }        return t; } void set_state(pcb *p){        while(p){               if (p->needtime==0){                      p->process=finish;                 }               if (p->process==execute){                      p->process=ready;               }               p=p->next;        } } void display_round(pcb *p){        cout<<"NAME"<<"  "<<"CPUTIME"<<"  "<<"NEEDTIME"<<"  "<<"COUNT"<<"  "<<"ROUND"<<"  "<<"STATE"<        while(p){               cout<name;               cout<<"      ";               cout<cputime;               cout<<"     ";               cout<needtime;               cout<<"         ";               cout<count;               cout<<"        ";               cout<round;               cout<<"       ";               switch(p->process){                      case ready:cout<<"ready"<                      case execute:cout<<"execute"<                      case finish:cout<<"finish"<               }               p=p->next;        } }   void round_cal(){        pcb * p;        pcb * r;        clrscr();        p=get_process_round();        int cpu=0;        clrscr();        r=p;        while(!process_finish(p)){               cpu+=2;               cpu_round(r);               r=get_next(r,p);               cout<<"cpu "<               display_round(p);               set_state(p);               sleep(5);               clrscr();        } }   void main(){        display_menu();        int k;        scanf("%d",&k);        switch(k){                      case 1:priority_cal();break;                      case 2:round_cal();break;                      case 3:break;                      display_menu();                      scanf("%d",&k);        }        }    

热门文章