高优先权优先调度算法

上传人:豆浆 文档编号:876257 上传时间:2017-05-20 格式:DOC 页数:16 大小:1.29MB
返回 下载 相关 举报
高优先权优先调度算法_第1页
第1页 / 共16页
高优先权优先调度算法_第2页
第2页 / 共16页
高优先权优先调度算法_第3页
第3页 / 共16页
高优先权优先调度算法_第4页
第4页 / 共16页
高优先权优先调度算法_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《高优先权优先调度算法》由会员分享,可在线阅读,更多相关《高优先权优先调度算法(16页珍藏版)》请在金锄头文库上搜索。

1、1动态高优先权算法实验报告一、 实验目的通过动态优先权算法的模拟加深对进程概念和进程调度过程的理解。提高自己的动手能力,主要是通过自己去思考并自己编码更进一步及更贴切的去理解弄明白动态优先权算法的模拟加深对进程概念和进程调度过程的工作流程及其原理!二、 实验要求1.在运行界面里输入进程名称,进程优先级和进程时间;2.每运行一个时间单位,作业的优先权级数减一;3.在运行出的用户界面中显示初始作业名,作业状态,优先权级数,需要服务的时间,已经运行的时间;4.每次调度前后显示作业队列;三、 实验内容动态优先权是指在创建进程时所赋予的优先权,是可以随着进程的推进或随其等待时间得增加而改变的。实验内容利

2、用 C 语言来实现对 N 个进程采用动态优先权优先算法的进程调度。优先数改变的原则:进程每运行一个时间片,优先数减 1。四、 实验结果登陆界面:2图 1图 23输入进程名字,进程优先数和进程时间:图 34图 45图 5图 66图 7图 87五、 实验小结本次实验代码和基于时间片轮转算法代码是一样的,是在别人代码的基础上,结合自己对高优先权算法和时间片轮转算法的了解,通过 switch 语句把两者合二为一了,当输入 1 的时候,执行 HighPriority 函数,也就是动态高优先权函数。在实现的过程中,使用 for 语句,限制进程数为 5 个:for (int i = 0; i != 5; +

3、i), ,定义 pt 作为临时节点来创建链表,processes 作为头结点来存储链表,psorted 用来存放排序后的链表。这次实验,在最初的时候,遇到了很大的麻烦,毕竟是改的别人代码,所以对代码理解的不是很透彻,但在同学们和老师的帮助下,终于调试成功了。也使我更加明白了高优先权调度的过程。六、 附录#include #include 8struct PCBchar p_name20;int p_priority;int p_needTime;int p_runTime;char p_state;struct PCB* next;void HighPriority();void RoundR

4、obin();void Information();char Choice();struct PCB* SortList(PCB* HL);int main()Information();char choice = Choice();switch(choice)case 1:system(cls);HighPriority();break;case 2:system(cls);RoundRobin();break;9default:break;system(pause);return 0;void Information()printf(nn);printf( * n);printf( 模拟进

5、程调度算法n);printf( * nnn); printf( 班 级: 计科 08-1 班n);printf( 姓 名: 卢彩方n);printf( 学 号: 200807010122n);printf( 实验日期: 2011 年 05 月 17 日nnnnnn);printf( 按回车键进入演示程序);getchar();system(cls);char Choice()printf(nn);printf( * n);10printf( 进程调度演示n);printf( * nnn); printf( 1.演示最高优先数优先算法。n);printf( 2.演示轮转法算法。n);printf

6、( 3.退出程序。nnnn);printf( 选择进程调度方法:); char ch = getchar();return ch;system(cls);void HighPriority()struct PCB *processes, *pt;/pt 作为临时节点来创建链表processes = pt = (struct PCB*)malloc(sizeof(struct PCB); for (int i = 0; i != 5; +i)struct PCB *p = (struct PCB*)malloc(sizeof(struct PCB);printf(进程号 No.%d:n, i);

7、printf(输入进程名:);scanf(%s, p-p_name);printf(输入进程优先数:);scanf(%d, &p-p_priority);printf(输入进程运行时间:);scanf(%d, &p-p_needTime);p-p_runTime = 0;p-p_state = W;p-next = NULL;pt-next = p;11pt = p;printf(nn);getchar(); /接受回车/processes 作为头结点来存储链表processes = processes-next;int cases = 0;struct PCB *psorted = proc

8、esses;while (1)+cases;pt = processes; /对链表按照优先数排序/psorted 用来存放排序后的链表psorted = SortList(psorted);printf(The execute number: %dnn, cases);printf(* 当前正在运行的进程是:%sn, psorted-p_name);psorted-p_state = R;printf(qname state super ndtime runtimen);printf(%st%ct%dt%dt%dtnn, psorted-p_name, psorted-p_state, ps

9、orted-p_priority, psorted-p_needTime, psorted-p_runTime);pt-p_state = W;psorted-p_runTime+;psorted-p_priority-;printf(* 当前就绪状态的队列为:nn);/pt 指向已经排序的队列pt = psorted-next;while (pt != NULL)printf(qname state super ndtime runtimen);12printf(%st%ct%dt%dt%dtnn, pt-p_name, pt-p_state, pt-p_priority, pt-p_nee

10、dTime, pt-p_runTime);pt = pt-next;/pt 指向已经排序的链表,判断链表是否有已用时间啊等于需要时间的pt = psorted;struct PCB *ap;ap = NULL; /ap 指向 pt 的前一个节点while (pt != NULL)if (pt-p_needTime = pt-p_runTime)if (ap = NULL)pt = psorted-next;psorted = pt;elseap-next = pt-next;ap = pt;pt = pt-next;if (psorted-next = NULL)break;getchar()

11、;struct PCB* SortList(PCB* HL)13struct PCB* SL;SL = (struct PCB*)malloc(sizeof(struct PCB);SL = NULL;struct PCB* r = HL;while (r != NULL)struct PCB* t = r-next;struct PCB* cp = SL;struct PCB* ap = NULL;while (cp != NULL)if (r-p_priority cp-p_priority)break;elseap = cp;cp = cp-next;if (ap = NULL)r-ne

12、xt = SL;SL = r;elser-next = cp;ap-next = r;14r = t;return SL;/轮转算法void RoundRobin()struct PCB *processes, *pt;/pt 作为临时节点来创建链表processes = pt = (struct PCB*)malloc(sizeof(struct PCB); for (int i = 0; i != 5; +i)struct PCB *p = (struct PCB*)malloc(sizeof(struct PCB);printf(进程号 No.%d:n, i);printf(输入进程名:

13、);scanf(%s, p-p_name);printf(输入进程运行时间:);scanf(%d, &p-p_needTime);p-p_runTime = 0;p-p_state = W;p-next = NULL;pt-next = p;pt = p;printf(nn);getchar(); /接受回车/processes 作为头结点来存储链表processes = processes-next;int cases = 0;while (1)15+cases;pt = processes; printf(The execute number: %dnn, cases);printf(* 当前正在运行的进程是:%sn, pt-p_name);pt-p_state = R;printf(qname state super ndtime runtimen);printf(%st%ct%dt%dt%dtnn, pt-p_n

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号