高优先权优先调度算法.doc_第1页
高优先权优先调度算法.doc_第2页
高优先权优先调度算法.doc_第3页
高优先权优先调度算法.doc_第4页
高优先权优先调度算法.doc_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

动态高优先权算法实验报告一、 实验目的 通过动态优先权算法的模拟加深对进程概念和进程调度过程的理解。提高自己的动手能力,主要是通过自己去思考并自己编码更进一步及更贴切的去理解弄明白动态优先权算法的模拟加深对进程概念和进程调度过程的工作流程及其原理!二、 实验要求1. 在运行界面里输入进程名称,进程优先级和进程时间;2. 每运行一个时间单位,作业的优先权级数减一;3. 在运行出的用户界面中显示初始作业名,作业状态,优先权级数,需要服务的时间,已经运行的时间;4. 每次调度前后显示作业队列;三、 实验内容动态优先权是指在创建进程时所赋予的优先权,是可以随着进程的推进或随其等待时间得增加而改变的。实验内容利用C语言来实现对N个进程采用动态优先权优先算法的进程调度。优先数改变的原则:进程每运行一个时间片,优先数减1。四、 实验结果 登陆界面:图1图2输入进程名字,进程优先数和进程时间:图3图4图5图6图7图8五、 实验小结本次实验代码和基于时间片轮转算法代码是一样的,是在别人代码的基础上,结合自己对高优先权算法和时间片轮转算法的了解,通过switch语句把两者合二为一了,当输入1的时候,执行HighPriority函数,也就是动态高优先权函数。在实现的过程中,使用for语句,限制进程数为5个:for (int i = 0; i != 5; +i),定义pt作为临时节点来创建链表,processes作为头结点来存储链表,psorted用来存放排序后的链表。这次实验,在最初的时候,遇到了很大的麻烦,毕竟是改的别人代码,所以对代码理解的不是很透彻,但在同学们和老师的帮助下,终于调试成功了。也使我更加明白了高优先权调度的过程。六、 附录#include #include struct PCB char p_name20; int p_priority; int p_needTime; int p_runTime; char p_state; struct PCB* next;void HighPriority();void RoundRobin();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; default: break; system(pause); return 0;void Information() printf(nn); printf( * n); printf( 模拟进程调度算法n); printf( * nnn); printf( 班 级: 软件测试10-02班n); printf( 姓 名: 林文n); printf( 学 号: 541013110224n); printf( 实验日期: 2013年04月9日nnnnnn); printf( 按回车键进入演示程序); getchar(); system(cls);char Choice() printf(nn); printf( * n); printf( 进程调度演示n); printf( * nnn); printf( 1.演示最高优先数优先算法。n); printf( 2.演示轮转法算法。n); printf( 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); 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; pt = p; printf(nn); getchar(); /接受回车 /processes作为头结点来存储链表 processes = processes-next; int cases = 0; struct PCB *psorted = processes; 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, psorted-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); printf(%st%ct%dt%dt%dtnn, pt-p_name, pt-p_state, pt-p_priority, pt-p_needTime, 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; else ap-next = pt-next; ap = pt; pt = pt-next; if (psorted-next = NULL) break; getchar(); struct PCB* SortList(PCB* HL) struct 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; else ap = cp; cp = cp-next; if (ap = NULL) r-next = SL; SL = r; else r-next = cp; ap-next = r; r = 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(输入进程名:); 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) +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_name, pt-p_state, pt-p_priority, pt-p_needTime, pt-p_runTime); pt-p_state = W; pt-p_runTime+; pt-p_priority-; printf(* 当前就绪状态的队列为:nn); pt = pt-next; while (pt != NULL) printf(qname state super ndtime runtimen); printf(%st%ct%dt%dt%dtnn, pt-p_name, pt-p_state, pt-p_priority, pt-p_needTime, pt-p_runTime); pt = pt-next; /检测是否运行时间等于需要时间,是的话从队列里面删除,不是的话加到队列最尾部 pt = processes; if (pt-p_needTime = pt-p_runTime) pt-p_state = C; pt = processes-next; processes = pt; else i

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论