国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
CPU調(diào)度算法 - 計(jì)算機(jī)札記大全 - JavaEye技術(shù)網(wǎng)站

兩種進(jìn)程調(diào)度算法:1)優(yōu)先數(shù)調(diào)度;2)循環(huán)輪轉(zhuǎn)調(diào)度

 

①本程序用兩種算法對(duì)五個(gè)進(jìn)程進(jìn)行調(diào)度,每個(gè)進(jìn)程可有三個(gè)狀態(tài),并假設(shè)初始狀態(tài)為就緒狀態(tài)。

②為了便于處理,程序中的某進(jìn)程運(yùn)行時(shí)間以時(shí)間片為單位計(jì)算。各進(jìn)程的優(yōu)先數(shù)或輪轉(zhuǎn)時(shí)間數(shù)以及進(jìn)程需運(yùn)行的時(shí)間片數(shù)的初始值均由用戶給定。

③在優(yōu)先數(shù)算法中,優(yōu)先數(shù)可以先取值為98,進(jìn)程每執(zhí)行一次,優(yōu)先數(shù)減3,CPU時(shí)間片數(shù)加1,進(jìn)程還需要的時(shí)間片數(shù)減1。在輪轉(zhuǎn)算法中,采用固定時(shí)間片(即:每執(zhí)行一次進(jìn)程,該進(jìn)程的執(zhí)行時(shí)間片數(shù)為已執(zhí)行了2個(gè)單位),這時(shí),CPU時(shí)間片數(shù)加2,進(jìn)程還需要的時(shí)間片數(shù)減2,并排列到就緒隊(duì)列的尾上。

對(duì)于遇到優(yōu)先數(shù)一致的情況,采用FIFO策略解決。

 

 

#include<stdio.h>

#include <dos.h>

#include<stdlib.h>

#include<conio.h>

#include<iostream.h>

#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"<<endl;

 

       while (i<P_NUM){

              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"<<endl;

       while(p){

              cout<<p->name;

              cout<<"        ";

              cout<<p->cputime;

              cout<<"          ";

              cout<<p->needtime;

              cout<<"          ";

              cout<<p->priority;

              cout<<"          ";

              switch(p->process){

                     case ready:cout<<"ready"<<endl;break;

                     case execute:cout<<"execute"<<endl;break;

                     case block:cout<<"block"<<endl;break;

                     case finish:cout<<"finish"<<endl;break;

              }

              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(tp<q->priority&&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:"<<cpu<<endl;

              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:"<<endl;

       cout<<"1 PRIORITY"<<endl;

       cout<<"2 ROUNDROBIN"<<endl;

       cout<<"3 EXIT"<<endl;

}

pcb * get_process_round(){

       pcb *q;

       pcb *t;

       pcb *p;

       int i=0;

       cout<<"input name and time"<<endl;

 

       while (i<P_NUM){

              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"<<endl;

       while(p){

              cout<<p->name;

              cout<<"      ";

              cout<<p->cputime;

              cout<<"     ";

              cout<<p->needtime;

              cout<<"         ";

              cout<<p->count;

              cout<<"        ";

              cout<<p->round;

              cout<<"       ";

              switch(p->process){

                     case ready:cout<<"ready"<<endl;break;

                     case execute:cout<<"execute"<<endl;break;

                     case finish:cout<<"finish"<<endl;break;

              }

              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 "<<cpu<<endl;

              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);

       }

       }

 

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
時(shí)間片輪轉(zhuǎn)算法和優(yōu)先級(jí)調(diào)度算法模擬實(shí)現(xiàn)
進(jìn)程調(diào)度
C語(yǔ)言實(shí)現(xiàn)時(shí)間片輪轉(zhuǎn)法的cpu調(diào)度模擬
178 f0602
程序計(jì)時(shí):clock
【C++】用C++編寫最簡(jiǎn)單的計(jì)算器簡(jiǎn)單完整版
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服