五邑大学实验报告操作系统课程2016~2017年度第1学期实验题目:进程调度院系:计算机学院班级: 140801学号: 3114002472姓名:黄凯鑫任课教师:白明成绩评定:实验二题目:进程调度完成日期:2016年12 月11 日1、实验目的(1)设计一个有n个进程工行的进程调度程序。
每个进程由一个进程控制块(PCB)表示。
进程控制块通常应包含下述信息:进程名、进程优先数、进程需要运行的时间、占用CPU的时间以及进程的状态等,且可按调度算法的不同而增删。
(2)调度程序应包含2~3种不同的调度算法,运行时可任意选一种,以利于各种算法的分析比较。
(3)系统应能显示或打印各进程状态和参数的变化情况,便于观察诸进程的调度过程2、实验内容(1)编制和调试示例给出的进程调度程序,并使其投入运行。
(2)自行设计或改写一个进程调度程序,在相应机器上调试和运行该程序,其功能应该不亚于示例。
(3)直观地评测各种调度算法的性能。
3、算法设计算法:(1) 优先数法。
进程就绪链按优先数大小从高到低排列,链首进程首先投入运行。
每过一个时间片,运行进程所需运行的时间片数减1,说明它已运行了一个时间片,优先数也减3,理由是该进程如果在一个时间片中完成不了,优先级应该降低一级。
接着比较现行进程和就绪链链首进程的优先数,如果仍是现行进程高或者相同,就让现行进程继续进行,否则,调度就绪链链首进程投入运行。
原运行进程再按其优先数大小插入就绪链,且改变它们对应的进程状态,直至所有进程都运行完各自的时间片数。
(2) 简单轮转法。
进程就绪链按各进程进入的先后次序排列,进程每次占用处理机的轮转时间按其重要程度登入进程控制块中的轮转时间片数记录项(相当于优先数法的优先数记录项位置)。
每过一个时间片,运行进程占用处理机的时间片数加1,然后比较占用处理机的时间片数是否与该进程的轮转时间片数相等,若相等说明已到达轮转时间,应将现运行进程排到就绪链末尾,调度链首进程占用处理机,且改变它们的进程状态,直至所有进程完成各自的时间片。
实验源代码:#include <stdio.h>#include <dos.h>#include <stdlib.h>#include <conio.h>#include <iostream.h>#include <time.h>enum state //进程的状态{Ready,Working,Finish};struct pcb //PCB数据结构{int pid;int priority;int cputime;int needtime;int round;state process;pcb *next;};int timepiece;pcb *get_process(){ //优先数算法--输入进程个数int proc;pcb *q;pcb *t;pcb *p;int i=0;cout << "Input Process Number(1-10): ";cin >> proc;while (proc<1 || proc>10){cout << endl << "Illegal Input!" << endl << endl << "Input Process Number(1-10): "; cin>>proc;}//cout << endl << endl<< "Start Scheduling!!!\n\n";getch();srand((unsigned)time(NULL)); //初始化随机数种子发生器while (i<proc){q=(struct pcb *)malloc(sizeof(pcb));q->pid=rand()%10000;q->needtime=rand()%10+1;q->cputime=0;q->priority=rand()%100;q->process=Ready;q->next=NULL; //利用随机数生成进程信息if (i==0){p=q;t=q;}else{t->next=q;t=q;} //尾插法建立PCB节点i++;} //whilereturn p;}void display(pcb *p){ //优先数算法结果输出cout<<"ProcessID"<<" "<<"Cputime"<<" "<<"Needtime"<<" "<<"Priority"<<" "<<"State"<<endl;while(p){cout<<" "<< p->pid;cout<<"\t\t";cout<<p->cputime;cout<<"\t";cout<<p->needtime;cout<<"\t";if(p->needtime==0)cout<<"Done";elsecout<<p->priority;cout<<"\t\t";switch(p->process){case Ready:cout<<"Ready"<<endl;break;case Working:cout<<"\b\b->Working<-"<<endl;break;case Finish:cout<<"Finish"<<endl;break;}p=p->next;}}int process_finish(pcb *q) //判断是否所有进程都已完成,是则返回1{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=-1;while(q){if (q->process!=Finish){ //未完成的进程置Ready,完成的进程置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){ //修改正在执行的进程的信息,并置其状态为Working t->priority-=3;if(t->priority<0) t->priority=0;t->needtime--;t->process=Working;t->cputime++;}}void priority_cal(){ //优先数算法主控函数pcb *p;system("cls");p=get_process();int cpu=0;char key;system("cls");cout<<"CPUTime:"<<cpu<<endl;display(p);cout<<endl;getch();while(!process_finish(p)){ //当不是所有进程都完成时不断执行进程并显示信息cpu++;cout<<"CPUTime:"<<cpu<<endl;cpuexe(p);display(p);cout<<endl;key=getch();if(key=='q') exit(0);}printf("All processes are finished!");getch();}pcb *get_process_round(){ //时间片算法--输入进程个数及CPU时间片int proc;pcb *q;pcb *t;pcb *p;int i=0;cout<<"Input Process Number(1-10): ";cin>>proc;while(proc<1 || proc>10){cout<<endl<<"Your process is out of order,please try again!"<<endl<<endl<<"Input Process Number(1-10): ";cin>>proc;}cout<<"CPU TimePiece(1-5)?";cin>>timepiece;while(timepiece<1 || timepiece>5){cout<<endl<<"Illegal Input!"<<endl<<endl<<"CPU TimePiece(1-5)?";cin>>timepiece;}//cout<< endl << endl << "Start Scheduling!!!\n\n";getch();srand((unsigned)time(NULL)); //初始化随机数种子发生器while (i<proc){q=(struct pcb *)malloc(sizeof(pcb)); //利用随机数建立进程信息q->pid=rand()%10000;q->needtime=rand()%10+1;q->cputime=0;q->round=0;q->process=Ready;q->next=NULL;if (i==0){ //尾插法建立PCB节点p=q;t=q;}else{t->next=q;t=q;}i++;} //whilereturn p;}void cpu_round(pcb *p,pcb *q){ //时间片算法模拟进程执行函数while(p){if (p->needtime==0){ //完成的进程置Finish,其它置Ready p->process=Finish;}if (p->process==Working){p->process=Ready;}p=p->next;}q->cputime+=timepiece; //修改正在执行进程的信息,并置其状态为Workingq->needtime-=timepiece;if(q->needtime<0) {q->needtime=0;}q->round++;q->process=Working;}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!=k && t->process==Finish){t=t->next;}}return t;}void display_round(pcb *p){ //时间片算法输出结果cout<<"ProcessID"<<" "<<"Cputime"<<" "<<"Needtime"<<" "<<"Round"<<" "<<"State"<<endl;while(p){cout<<" "<<p->pid;cout<<"\t\t";cout<<p->cputime;cout<<"\t";cout<<p->needtime;cout<<"\t";cout<<p->round;cout<<"\t";switch(p->process){case Ready:cout<<"Ready"<<endl;break;case Working:cout<<"\b\b->Working<-"<<endl;break;case Finish:cout<<"Finish"<<endl;break;}p=p->next;}}void round_cal(){pcb * p;pcb * r;system("cls");p=get_process_round();int cpu=0;char key;system("cls");cout<<"CPUTime:"<<cpu<<endl;display_round(p);cout<<endl;getch();r=p;while(!process_finish(p)){cpu+=timepiece;cpu_round(p,r);r=get_next(r,p);cout<<"CPUTime:"<<cpu<<endl;display_round(p);cout<<endl;key=getch();if(key=='q') exit(0);}}void display_menu(){cout<<"1 Priority"<<endl;cout<<"2 Round Robin"<<endl;cout<<"3 Exit"<<endl;cout<<"Choice: ";}int main(){char key;display_menu();cin >> key;switch(key){case '1':priority_cal();break;case '2':round_cal();break;case '3':exit(0);}return 0;}4、运行与测试P算法:输入进程数4以后,由srand函数随机给出各个进程的needtime和priority。