《操作系统-加深对进程概念和进程调度的过程,算法的理解.docx》由会员分享,可在线阅读,更多相关《操作系统-加深对进程概念和进程调度的过程,算法的理解.docx(13页珍藏版)》请在三一办公上搜索。
1、山东工商学院实验(上机)报告实验题目:进程调度模拟程序2014年4月20日一. 实验目的加深对进程概念和进程调度的过程/算法的理解二. 实验内容(1) 给出进程调度算法的描述(如基于动态优先级和时间片轮 转调度算法的描述)(2) 用C语言设计一个对n个并发进程进行调度的程序,每个进 程由一个进程控制块PCB结构表示,该进程控制块应包含 下述描述信息:进程标识ID,进程优先数priority(并规定优先 数和优先权成正比),时间片数chip,进程已经占用CPU的时 间cputime,进程还需要运行的时间alltime(当进程运行完毕 时,其值为0),进程的状态state(为简化起见,设每个进程处
2、 于运行 E(Executing),就绪 R(Ready)和完成 F(Finished)3 中状 态之一,并假设起始状态都是就绪状态R),以及进程队列指 针next(用来将PCB排成队列)等,可按照调度算法的不同而 增删.(3) 调度程序应当包含两种不同的调度算法,运行时可以任选 一种,以利于各种方法的分析和比较.(4) 程序应能显示或打印各种进程状态和参数变化情况,便于 观察.既要显示每个时间片内各进程的情况,并且指出运行 进程就绪和阻塞队列中的内容三. 实验步骤(1)算法描述时间片轮转调度算法描述:使用LENGTH(宏定义为100)个元素的PCB数组作为进程队列, 使用一次所有进程状态输出
3、作为一个时间片.从队列的队头开 始,一个时间片改变一个进程信息(cputime+,alltime-,state由 Ready转为Executing,最后转为Ready或者Finished),直到所有 的进程状态全部为Finished(宏定义为F) 动态优先级调度算法描述:使用LENGTH个元素的PBC数组作为进程队列,使用一次所有 进程的状态输出作为一个时间片,定义十个优先级,从1到10,10 为最高优先级,定义了 10个优先级队列,分别名为one , two , three , four , five , six , seven , eight , nine ,ten,队列长度为 LENGT
4、H,在进程信息输入到进程队列后,进行进程优先级排序, 将相应优先级进程的id值存入相应优先级队列,在调度处理函 数中,从ten优先级队列开始遍历,如果不空,则取优先级进程队 列队首元素作为进程队列id,调度处理该id值的进程(priority-, cputime+ , alltime- ,state 由 Ready 转为 Executing,最后转为 Ready或者Finished;将该进程id从原优先级队列移除,然后添加 到低一优先级队列的队尾),同一进程队列的进程id,从队头开始 调度,如果进程优先级全部将为1,则进行时间片轮转调度,直到 所有进程状态为Finished(宏定义为F).编译
5、环境在Linux下使用gcc进行编译调试代码#include #define EXECUTING 69#define READY 82#define FINISHED 70#define TRUE 1#define FALSE 0#define LENGTH 100 typedef struct int id;/进程 IDint priority;进程优先数int chip;需要的时间片数int cputime;进程已占用CPU的时间int alltime;进程还需要运行的时间int state;进程状态/struct process *next; /Next 指针PCB;PCB q_proc
6、essLENGTH;int top=0;指向队头int last=0;指向队尾int flag=1;动态优先级调度算法使用的int oneLENGTH,twoLENGTH,threeLENGTH,fourLENGTH,fiveLENGTH,sixL ENGTH,sevenLENGTH,eightLENGTH,nineLENGTH,tenLENGTH;inttop_one,last_one,top_two,last_two,top_three,last_three,top_four,last_four;inttop_five,last_five,top_six,last_six,top_seve
7、n,last_seven,top_eight,last_eight;int top_nine,last_nine,top_ten,last_ten;int dy_time=0;void time_round();void dy_priority();int if_empty_dy();int if_empty_time();void enter_last(int *topPoint,int *lastPoint,int id,int x);void del_top(int *topPoint,int x);void deal_proc(int top_m);/*时间片轮转调度算法*/void
8、time_round(int t)int time=t;int i;while(if_empty_time()for(i=0;ilast;i+)if(q_processi.state = READY)q_processi.state=EXECUTING;printf(第 %d 个时间片:n,time);time+;int j;for(j=0;jlast;j+)printf( %dt %dtt%dtt %dttt%dt %cn”,q_processj.id,q_proce ssj.priority,q_processj.chip,q_processj.cputime,q_processj.all
9、time,q_pr ocessj.state);q_processi.cputime+;q_processi.alltime-;if(q_processi.alltime = 0)q_processi.state=FINISHED;elseq_processi.state=READY;printf(第 %d 个时间片:n,time);int j;for(j=0;jlast;j+)printf( %dt %dtt%dtt %dttt%dt %cn”,q_processj.id,q_proce ssj.priority,q_processj.chip,q_processj.cputime,q_pr
10、ocessj.alltime,q_pr ocessj.state);/*如果队列为bu空,返回TRUE,否则返回FALSE*/int if_empty_time()int i=0;for(;ilast;i+)if(q_processi.state != FINISHED) return TRUE;return FALSE;int if_empty_dy()int i;for(i=0;ilast;i+)if(q_processi.state != FINISHED) return TRUE;return FALSE;/*动态优先级调度算法*/10个优先级,从1到10,分别设10个数组队列,分别存
11、储这些 优先级进程id,void print(int x)int i;for(i=0;iLENGTH;i+)if(xi != 0) printf( %d,xi);void del_top(int *topPoint,int x) x*topPoint=0;(*topPoint)+;void enter_last(int *topPoint,int *lastPoint,int id,int x) if(*lastPoint = LENGTH-1)int flag,i,point;*topPoint =0;for(i=0;xi = 0;i+) point=flag=i;if(!i)(printf
12、(进程队列已满! n);return;for(i=0;iflag;i+,point+)xi=xpoint;xpoint=0;*lastPoint=-i;x*lastPoint=id;(*lastPoint)+;void dy_priority()int i;for(i=0;ilast;i+)switch(q_processi.priority)case1:enter_last(&top_one,&last_one,q_processi.id,one);break;case2:enter_last(&top_two,&last_two,q_processi.id,two);break;case3
13、:enter_last(&top_three,&last_three,q_processi.id,three);break;case4:enter_last(&top_four,&last_four,q_processi.id,four);break;case5:enter_last(&top_five,&last_five,q_processi.id,five);break;case6:enter_last(&top_six,&last_six,q_processi.id,six);break;case7:enter_last(&top_seven,&last_seven,q_process
14、i.id,seven);break;case8:enter_last(&top_eight,&last_eight,q_processi.id,eight);break;case9:enter_last(&top_nine,&last_nine,q_processi.id,nine);break;case10:enter_last(&top_ten,&last_ten,q_processi.id,ten);break;default:printf(n 进程优先级错误!n);return;while(if_empty_dy()if(top_ten != last_ten)/ten shifou
15、wei kongdeal_proc(tentop_ten);enter_last(&top_nine,&last_nine,q_processtentop_ten.id,nine);del_top(&top_ten,ten);else if(top_nine != last_nine)/nine shi fou wei kong deal_proc(ninetop_nine);enter_last(&top_eight,&last_eight,q_processninetop_nine.id,eight);del_top(&top_nine,nine);else if(top_eight !=
16、 last_eight)/eight shi fou wei kong deal_proc(eighttop_eight);enter_last(&top_seven,&last_seven,q_processeighttop_eight.id,seven);del_top(&top_eight,eight);else if(top_seven != last_seven)/seven shi fou wei kong deal_proc(seventop_seven);enter_last(&top_six,&last_six,q_processseventop_seven.id,six);
17、del_top(&top_seven,seven);else if(top_six != last_six)/six shi fou wei kong deal_proc(sixtop_six);enter_last(&top_five,&last_five,q_processsixtop_six.id,five);del_top(&top_six,six);else if(top_five != last_five)/five shi fou wei kong deal_proc(fivetop_five);enter_last(&top_four,&last_four,q_processf
18、ivetop_five.id,four);del_top(&top_five,five);else if(top_four != last_four)/four shi fou wei kong deal_proc(fourtop_four);enter_last(&top_three,&last_three,q_processfourtop_four.id,three);del_top(&top_four,four);else if(top_three != last_three)/three shi fou wei kong deal_proc(threetop_three);enter_
19、last(&top_two,&last_two,q_processthreetop_three.id,two);del_top(&top_three,three);else if(top_two != last_three)/two shi fou wei kong deal_proc(twotop_two);enter_last(&top_one,&last_one,q_processtwotop_two.id,one);del_top(&top_two,two);else if(top_one != last_one )flag=0;time_round(dy_time);break;vo
20、id deal_proc(int top_m)if(q_processtop_m.state = READY)printf(第 %d 个时间片:n,+dy_time);q_processtop_m.state=EXECUTING;int j;for(j=0;jlast;j+)printf( %dt %dtt%dtt %dttt%dt %cn”,q_processj.id,q_proce ssj.priority,q_processj.chip,q_processj.cputime,q_processj.alltime,q_pr ocessj.state);q_processtop_m.cput
21、ime+;q_processtop_m.alltime-;q_processtop_m.priority-;if(q_processtop_m.alltime = 0)q_processtop_m.state=FINISHED;elseq_processtop_m.state=READY;return;int main()int p_id=0,i=0;int p_priority=0,p_chip=0;printf(-输入进程信息(进程优先数,时间片数),以-1 -1结束输入 n);while(TRUE)scanf(%d%d”,&p_priority,&p_chip);if(p_priorit
22、y 0 | p_chip 0)break;q_processi.id=p_id+;q_processi.priority=p_priority;q_processi.chip=p_chip;q_processi.cputime=0;q_processi.alltime=p_chip;q_processi.state=READY;last+;i+;printf(-选择调度算法(1.动态优先级调度算法2.时间片轮转调 度算法):n);int choosed=0;scanf(%d”,&choosed);printf(-进程ID进程优先级进程所需时间片进程已占用CPU时间 还需要运行时间进程状态n);
23、if(choosed = 1)printf(动态优先级调度算法:n);dy_priority();if(flag)int j;printf(第 %d 个时间片:n,+dy_time);for(j=0;jiy neriu.*榔山x虹Tdn户厦禁入进拒信息t进芹忧免鞭,时间片以r -1皓束肃人 454312-1-1透城闻星翼法1.动态忧先吸阊国算法!in进点优丸殁 片花特K度鼻楂 个个肘倘片二个肘间斥:个时间片;个时间片;个时间片;十时间片;四.存在的问题及解决方法存在的问题:1)如何去模拟一个时间片2)队列的维护问题3)函数参数值传递并且改变的问题4)重复输出最终状态问题解决方法:(1) 使用一次所有进程状态的输出模拟一个时间片(2) 设置全局变量,使用全局变量维护队列(3) 函数参数传递时使用传递参数地址的方法(4) 设置flag,根据flag的值判断是否进行输出五.体会通过进程调度模拟程序,再一次认识到了 C语言的强大.在程序实 现过程中,清楚地明白进程调度的过程很重要,有了相应的运行过 程,才能合理地设计算法,然后实现算法.使用合理的数据结构,在算 法实现时也是很重要的,它可以使算法实现变得更加容易,运行时 效率更高!