进程调度的优先数法和简单轮转法

来源:互联网 发布:淘宝卖家一般在哪上货 编辑:程序博客网 时间:2024/06/16 03:37
#include<iostream>#include<string>#include<time.h>using namespace std;int n;class PCB{public:int pri;//进程优先数int runtime;//进程运行CPU时间int pieceOftime;//轮转时间片int needOftime;//还需要时间int Counter;string procname;//进程名string state;//进程状态PCB * next;};PCB * run = NULL;PCB * ready = NULL;PCB * finish = NULL;PCB * tial = ready;void Dtime(int t);void Prinft(int a){if(a==1){cout<<"进程名称"<<"\t"<<"优先数"<<"\t"<<"还需要时间"<<"\t"<<"已运行时间"<<"\t"<<"状态:"<<endl;}elsecout<<"进程名称"<<"\t"<<"已运行时间"<<"\t"<<"还需要时间"<<"\t"<<"计数器"<<"\t"<<"时间片"<<"\t"<<"状态"<<endl;}void Prinft(int b,PCB * p){if(b==1){cout<<p->procname<<"\t\t"<<p->pri<<"\t"<<p->needOftime<<"\t\t"<<p->runtime<<"\t\t"<<p->state<<endl;}elsecout<<p->procname<<"\t\t"<<p->runtime<<"\t\t"<<p->needOftime<<"\t\t"<<p->Counter<<"\t"<<p->pieceOftime<<"\t"<<p->state<<endl;}void display(int c){PCB *p;if(run!=NULL)Prinft(c,run);//Dtime(2);p=ready;while(p!=NULL){Prinft(c,p);p=p->next;}//Dtime(2);p=finish;while(p!=NULL){Prinft(c,p);p=p->next;}}void insert(PCB *p)//插入就绪队列按Pri大小{PCB *S1,*S2;if(ready==NULL){p->next = NULL;ready = p;}else{S1 = ready;S2 = S1;while(S1!=NULL){if(S1->pri >= p->pri){S2 = S1;S1 = S1->next;}elsebreak;}if(S2->pri >= p->pri){S2->next = p;p->next = S1;}else{p->next = ready;ready = p;}}}bool CTProcessOfPri(){PCB * Node;cout <<"输入创建进程的数目:"<<endl;cin >>n;for(int j = 0;j < n; j++){Node = new PCB;if(Node==NULL)return false;else{cout <<"输入进程的名称,进程需CPU时间:"<<endl;cin >>Node->procname>>Node->needOftime;Node->runtime = 0;Node->state ="就绪";Node->pri =Node->needOftime;cout <<"进程"<<Node->procname<<"创建完毕!"<<endl;}insert(Node);}return true;}void priority(int i){run = ready;ready = ready->next;run->state = "运行";Prinft(i);while(run!=NULL){run->runtime=run->runtime+1;run->needOftime=run->needOftime-1;run->pri=run->pri-1;if(run->needOftime==0){run->state = "完成";run->next = finish;finish = run;run=NULL;if(ready!=NULL){run = ready;run->state = "运行";ready = ready->next;}}else if((ready!=NULL)&&(run->pri<ready->pri)){run->state="就绪";insert(run);run = ready;run->state = "运行";ready = ready->next;}display(i);}}void queue(PCB *p){if(ready==NULL){p->next = NULL;ready = p;tial = p;}else{tial->next = p;tial = p;p->next = NULL;}}bool CTProcessOfRuntime(){PCB * Node;int m;cout <<"输入创建进程的数目:"<<endl;cin >>n;cout <<"输入时间片:"<<endl;cin >>m;for(int j = 0;j < n; j++){Node = new PCB;if(Node==NULL)return false;else{cout <<"输入进程的名称,进程需CPU时间:"<<endl;cin >>Node->procname>>Node->needOftime;Node->runtime = 0;Node->state ="就绪";Node->Counter = 0;Node->pieceOftime = m;cout <<"进程"<<Node->procname<<"创建完毕!"<<endl;}queue(Node);}return true;}void Runtime(int c){run = ready;ready = ready->next;run->state = "运行";Prinft(c);while(run!=NULL){run->runtime=run->runtime+1;run->needOftime=run->needOftime-1;run->Counter = run->Counter + 1;if(run->needOftime==0){run->state = "完成";run->next = finish;finish = run;run = NULL;if(ready!=NULL){run = ready;ready = ready->next;}}else if(run->Counter == run->pieceOftime){run->Counter = 0;run->state = "就绪";queue(run);run=NULL;if(ready!=NULL){run = ready;run->state = "运行";ready = ready->next;}}display(c);}}int main(){int i;cout <<"菜单:"<<endl;cout <<"输入0 退出"<<endl;cout <<"输入1 优先数调度算法"<<endl;cout <<"输入2 循环时间片轮转算法"<<endl;cin >>i;switch(i){case 1:CTProcessOfPri();priority(i);break;case 2:CTProcessOfRuntime();Runtime(i);break;default:break;}return 0;}void Dtime(int t){time_t current_time;time_t start_time;time(&start_time);do{time(& current_time);}while((current_time-start_time)<t);}

1 0
原创粉丝点击