1、进程调度实验报告天津大学仁爱学院操作系统实验报告实验类型:必修实验日期: 2014年 4月 18日实验名称:进程调度实验地点:二实验楼 504学生姓名:李帅帅 指导教师:张 磊班 级:计科一班计算机科学与技术系实验报告内容:1) 实验目的用c语言编写和调试一个进程调度程序,以加深对进程的概念及进程 调度算法的理解。2) 实验器材和设备硬 件:二实验楼504计算机开发工具: Microsoft Visual C+ 6.03) 实验任务本实验模拟单处理器系统的进程调度,加深对进程的概念及进程调度算 法的理解。用c语言编写和调试一个进程调度的算法程序,有一些简单的界 面,能够运行,仿真操作系统中进程
2、调度的原理和过程。通过对调度算法的 模拟,进一步理解进程的基本概念,加深对进程运行状态和进程调度4) 实验原理无论是在批处理系统还是分时系统中,用户进程数一般都多于处理机 数、这将导致它们互相争夺处理机。另外,系统进程也同样需要使用处理 机。这就要求进程调度程序按一定的策略,动态地把处理机分配给处于就 绪队列中的某一个进程,以使之执行。 基本状态:1.等待态:等待某个事件的完成;2.就绪态:等待系统分配处理器以便运行;3.运行态:占有处理器正在运行。运行态-等待态 往往是由于等待外设,等待主存等资源分配或等待人工干 预而引起的。等待态-就绪态则是等待的条件已满足,只需分配到处理器后就能运行。
3、运行态-就绪态 不是由于自身原因,而是由外界原因使运行状态的进程让 出处理器,这时候就变成就绪态。例如时间片用完,或有更高优先级的进程 来抢占处理器等。就绪态-运行态 系统按某种策略选中就绪队列中的一个进程占用处理器, 此时就变成了运行态5) 实验过程描述a) 打开 Microsoft Visual C+ 6.0 ,创建工程。b) 根据要求用c语言代码实现应用程序,并调试完成c) 运行程序,根据提示输入相应的字符。d) 输入实验测试内容,并观察执行窗口显示的过程。e)重复c、d过程,认真体会领悟。6)实验代码 / Iss.cpp : Defines the entry point for th
4、e con sole applicatio n. /#include stdafx.h#include #include vconio.h#include #include #define P_NUM 3 #define P_TIME 50 enu mstate ready, execute, block, fin ish;structpcbchar n ame4;intpriority;intcputime;intn eedtime;intcount;intround;state process;pcb* n ext;pcb * get_process() _pcb *q;pcb *t;pc
5、b *p;int i=0;t=( struct pcb *)malloc( sizeof (pcb); p=(struct pcb *)malloc( sizeof (pcb); cout Input Name and Time endl;while (iq-n ame;cinq-n eedtime;q-cputime=O;q-priority=P_TIME-q-n eedtime;q-process=ready;q-n ext=NULL;if (i=0)p=q;t-n ext=q;elseq-n ext=t- n ext;t=q;q=p;i+;return p;void display(pc
6、b * p)coutname cputime needtime priority state endl;while (p)coutn ame;cout;coutcputime;cout;coutn eedtime;cout;coutpriority;coutprocess)case ready:cout ready endl; break;case execute:cout execute endl; break;case block:cout block endl; break;case finish:cout finish n ext;int process_finish(pcb *q)
7、_int b1=1;while (b1 &q)b1=b1 &q-n eedtime=O; q=q-n ext;return b1;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=fi ni sh;if (tppriority&q-process!=finish) tp=q-priority; t=q; q=q-n ext;if (t-needtime!=0) t-priority-=3; t-n eedtime-;t-
8、process=execute; t-cputime+;void priority_cal() _pcb * p;system(cls);P=get_process();int cpu=0;system( cls);while (!process_finish(p) _cpu+;cout cuptime: cpuendl;cpuexe(p);display(p);Sleep(1000);printf( All processes have fini shed,press any key to exit );getch();void display_menu() _coutnCHOOSE THE
9、 ALGORITHMKe ndl;cout1 PRIORITYe ndl;cout 2 roun drob in e ndl;cout 3 EXIT endl;/显示调度算法菜单,可供用户选择优先权调度算法和时间轮转调度算法pcb *get_process_r oun d() 一 一pcb *q;pcb *t;pcb *p;int i=0;t=( struct pcb *)malloc( sizeof (pcb);p=(struct pcb *)malloc( sizeof (pcb);cout in put n ame and time e ndl;while (iq-n ame;cinq-
10、n eedtime;q-cputime=0;q-roun d=0;q-co un t=0;q-process=ready;q-n ext=NULL;if (i=0)p=q;t-n ext=q;elseq-n ext=t- n ext; t=q; q=p;i+;return p;void cpu_round(pcb *q) _q-cputime+=2;q-n eedtime-=2;if (q-needtimen eedtime=0;q-co un t+;q-roun d+; q-process=execute;pcb * get_ next(pcb * k,pcb * head) _pcb *
11、t;t=k;dot=t- n ext;while (t&t-process=finish);if (t=NULL)t=head;while (t-next!=k&t-process=finish) return t; void set_state(pcb *p) _while (p)if (p-needtime=0)p-process=fi ni sh;/如果所需执行时间为0,则设置运行状态为结束if (p-process=execute) p-process=ready; /如果未执行状态则设置为就绪p-n ext;/设置队列中进程执行状态void displayound(pcb *p) _
12、coutvvNAME QPUTIMEV vvNEEDTIME COUNTROUNDSTATEendl;while (p)coutn ame;cout ;coutcputime;cout ;coutn eedtime;cout ;coutco unt;cout ;coutr ound;coutprocess)case ready:cout ready endl; break; case execute:cout execute endl; break; case finish:cout finish n ext;时间片轮转调度算法输出调度信息void round_cal() _pcb * p;p
13、cb * r;system(cls);P=get_process_r oun d();int cpu=0;system( cls);r=p;while (!process_finish(p) _cpu+=2;cpu_ro un d(r);r=get_ next(r,p);cout cpu cpuendl; display_r oun d(p);set_state(p);Sleep(1000);int main( int argc, char* argv)display_me nu();int k;scanf( %d,&k);while (1)switch (k)case 1:priority_
14、cal(); break;case 2:ro un d_cal(); break;case 3: return 0;display_me nu(); scanf( %d,&k);printf( n本调度算法成功结束! n;);return 0;7)实验结果截图g 作系统上机lssDebuglss. ezeeput ime-1name eputineneedt imepriority staA 111 35executeeputinename eput imeneedt imepriority statea 210 32executec put ime: 3name eputimeneedt i
15、meprior ity statea 39 29executecputime:4name eputineneedtinepriority statea 48 26executeeput inc-5Iname eput imeneedt imepriority state卜57 23executeleput imeIname Cput imeinepriority StateU 66 20executeeput irie: 7namQ cpu,t ineneedt imepriority staA 75 17executeeput ime-Sname eput imeneedt imeprior
16、ity statea 84 14exedutfteput ime:9name c put imeneedtimepriority state鬲 ?3 11executeeput line : 18nan? cpu,t inene ed incpr*ioi*i ty s ta 七日a 102 8execueput ime-11name eputineneedt imepriority state& 111 Sexecuteeputime:12name eput imeneedt imepriority statea 120 2executehave f in is hcd, press an*/ key to exitAll processesCHOOSE THE ALGORITHM:1PRIORITY2POUNVROBIN3EXITJ8)对实验的总结本次实验,任务是用c语言代码实现进程调度模拟系统,从而观察进 程的运行过程及加深对进程的了解。这次实验,加深了我对进程概念及进 程调度的理解;熟悉了进程调度算法。使得理论知识得到的实践,也使我 的编程能力得到了进一步提高。实验中,我们小组共同学习、共同努力, 虽然在实验中遇到了一些问题,但在查阅资料后都解决了。实验过程也让 我认识到自己的不足,好多知识已经模糊不清,在今后自己要多一些实验 及相关的小项目,来提高自己的编程能力。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1