当前位置:文档之家› 实验三进程调度蔡凤武

实验三进程调度蔡凤武

实验三进程调度蔡凤武进程调度实验目的1、理解有关进程控制块、进程队列的概念。

2、掌握进程优先权调度算法和时间片轮转调度算法的处理逻辑。

实验内容与基本要求1、设计进程控制块PCB的结构,分别适用于优先权调度算法和时间片轮转调度算法。

2、建立进程就绪队列。

3、编制两种进程调度算法:优先权调度算法和时间片轮转调度算法。

实验报告内容一.优先权调度算法和时间片轮转调度算法原理。

对于优先权调度算法,其关键是在于是采用静态优先权还是动态优先权,以及如何确定进程的优先权。

静态优先权是在创建进程是确定的,并且规定它在进程的整个运行期间保持不变。

动态优先权要配合抢占调度方式使用,它是指在创建进程时所赋予的优先权,可以随着进程的推进而发生改变,以便获得更好的调度性能。

在就绪队列中等待调度的进程,可以随着等待时间的增加,其优先权也以某个速率增加。

因此,对于优先权初值很低的进程,在等待足够时间后,其优先权也可能升为最高,从而获得调度,占用处理器并执行。

对已时间片轮转调度算法,系统将所有的就绪进程按进路就绪队列的先后次序排列。

每次调度时把CPU 分配给队首进程,让其执行一个时间片,当时间片用完,由计时器发出时钟中断,调度程序则暂停改程序的执行,使其退出处理器,并将它送人就绪队的末尾,等待下一轮调度执行。

然后,把cpu分配给就绪队列中新的队首进程,同时让它执行一个时间片。

二.程序流程图。

结束就绪队列为空吗三.程序及注释。

#include <stdio、h>#include <dos、h>#include <stdlib、h>#include <conio、h>#include <iostream、h>#include<windows、h>#define P_NUM5#define P_TIME50 enum st{ ready, execute, block, finish};//状态定义进程//struct pcb{ char name[4];//进程名字// int priority;//进程优先权// int cputime;//CPU运行时间// int needtime;//进程运行需要的时间// int count;//进程执行次数// int round;//时间片轮转轮次// st process;//进程状态// pcb *next;};//定义进程//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++; } return p;//输入模拟测试的进程名和执行所需的时间,初始设置可模拟5个进程的调度//}void display (pcb *p){ cout<<"name"<<""<<"cputime"<<" "<<"needtime"<<" "<<"priority"<<""<<"st"<<endl;while(p){ cout<<p->name;cout<<" ";cout<<p->cputime;cout<<" ";cout<<p->needtime;cout<<" ";cout<<p->priority;cout<<" ";switch(p->process){caseready:cout<<"ready"<<endl;break;caseexecute:cout<<"execute"<<endl;break; caseblock:cout<<"block"<<endl;break;casefinish:cout<<"finish"<<endl;break;}p=p->next;}}//显示模拟结果,包含进程名,cpu的时间。

运行所需时间以及优先级//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++;}//选择某一个进程,给它分配cpu//}//计算进程优先级//voidpriority_cal(){pcb*p;system("cls");p=get_process();int cpu=0;system("cls");while(!process_finish(p)){cpu++;cout< <"cputime:"<<cpu<<endl;cpuexe(p);display(p);Sleep(2);}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;inti=0;cout<<"input name andtime"<<endl;while(i<P_NUM){q=(structpcb*)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++;}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;}voidset_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){caseready:cout<<"ready"<<endl;break; caseexecute:cout<<"execute"<<endl;break;casefinish:cout<<"finish"<<endl;break;}p=p->next;}//时间片轮转调度算法输出调度信息//}voidround_cal(){pcb*p;pcb*r;system("cls");p=get_process_round ();intcpu=0;system("cls");r=p;while(!process_finish(p)){cpu+=2; cpu_round(r);r=get_next(r,p);cout<<"cpu"<<cpu<<endl;displ ay_round(p);set_state(p);Sleep(5);}}voidmain(){display_menu();int k;scanf("%d",&k);switch(k){case1:priority_cal();break; case2:round_cal();break;case3:break;display_menu();scanf( "%d",&k);}}四.结果分析本程序用两种方法对五个进程进行调度,每个进程可有三种状态,并假设初始状态为就绪状态。

相关主题