版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上 处理机调度算法实验报告学 号姓 名时 间专 业班 级实验题目:处理机调度算法一、实验目的在了解操作系统的基础上全面了解处理机调度算法的实现以及过程,详细了解处理机调度算法的机制,充分了解调度的过程及状态,采用优先数法进程调度算法来模拟演示进程调度 二、实验内容与步骤:1. 了解进程的三种状态状态:ready、running、finish2.了解进程需要的CPU时间以时间片为单位确定3.编写一段程序#include <stdio.h>#include <stdlib.h>#define P_NUM 5#define P_TIME 50enum
2、stateready,execute,block,finish;struct pcbbchar name4;int priority; int cputime; int needtime; int count;enum state process;struct pcbb *next;typedef struct pcbb pcb;void display_menu()printf("CHOOSE THE ALGORITHM:n");printf("1 PRIORITYn");printf("2 ROUNDROBINn");printf
3、("3 EXITn");pcb* get_process()pcb *q;pcb *p; pcb *t; int i = 0;printf("input name and timen");while (i < P_NUM)q=(pcb *)malloc(sizeof(pcb);scanf("%s",q->name);scanf("%d",&q->needtime);q->cputime = 0;q->priority = P_TIME - q->needtime;q-&g
4、t;process = ready;q->next = NULL;if(i=0)p = q;t = q;elset->next = q;t = q;i+;return p;void free_process(pcb *p)pcb *q;while(p!= NULL)q = p;p = p->next;free(q);void display(pcb *p)printf("name cputime needtimepriority staten");while(p)printf("%s",p->name);printf("
5、 ");printf("%d",p->cputime);printf(" ");printf("%d",p->needtime);printf(" ");printf("%d",p->priority);printf(" ");switch(p->process)case ready:printf("readyn");break;case execute:printf("executen"); brea
6、k;case block:printf("blockn"); break;case finish:printf("finishn"); break;p = p->next;int process_finish(pcb *q)int b1 = 1;while(b1&&q)b1 = b1&&q->needtime=0;q = q->next;return b1;void cpuexe(pcb *q)pcb *t = q;int tp = 0;while(q) if (q->process!=finish
7、)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+;void priority_cal()pcb *p;p = get_process
8、();int cpu = 0;while(!process_finish(p) cpu+;printf("cputime:%dn",cpu);cpuexe(p); display(p); sleep(2);free_process(p); printf("All processes have finishedn");pcb *get_process_round()pcb *q;pcb *p; pcb *t; int i = 0;printf("input name and timen");while (i<P_NUM)q=(pc
9、b *)malloc(sizeof(pcb);scanf("%s",q->name);scanf("%d",&q->needtime);q->cputime = 0;q->count = 0;q->process = ready;q->next = NULL;if(i=0)p = q;t = q;elset->next = q;t = q;i+;return p;void cpu_round(pcb *q)if(q->needtime=1) q->cputime+; else q->cp
10、utime +=2;q->needtime -=2;if(q->needtime<0)q->needtime = 0;q->count+;q->process = execute;pcb *get_next(pcb *k,pcb *head)pcb *t;t = k;dot =t->next;while ( t && t->process = finish);if(t = NULL)t = head;while(t->next!=k && t->process = finish)t = t->ne
11、xt;return t;void set_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)printf("name cputime needtimecount staten");while(p)printf("%s",p->name);printf(" ");print
12、f("%d",p->cputime);printf(" ");printf("%d",p->needtime);printf(" ");printf("%d",p->count);printf(" ");switch(p->process)case ready:printf("readyn");break;case execute:printf("executen"); break;case block:prin
13、tf("blockn"); break;case finish:printf("finishn"); break;p = p->next;void round_cal()pcb *p;pcb *r;p = get_process_round();int cpu = 0;r=p;while(!process_finish(p) if(r->needtime=1)cpu+=1;elsecpu+=2; cpu_round(r);r = get_next(r,p); printf("cputime:%dn",cpu);displa
14、y_round(p);set_state(p);sleep(2);free_process(p); main()display_menu();int k;scanf("%d",&k);switch(k)case 1:priority_cal();break;case 2:round_cal();break;case 3:break;default:printf("YOU HAVE NOT CHOOSE ANY ALGORITHM!n");运行后结果如下:rootrhel5hbzy # gcc -o chuliji chuliji.crootrhe
15、l5hbzy # ./mCHOOSE THE ALGORITHM:1 PRIORITY2 ROUNDROBIN3 EXIT1input name and timejing 2aaaa 8bbbb5ffff4ggg 6cputime:1name cputime needtime priority statejing- 1 1 45 executeaaaa* 0 8 42 readybbbb2 0 0 50 finishffff2 0 0 50 finishggg 0 6 44 readyrtyucputime:2name cputime needtime priority statejing*
16、2 0 42 executeaaaa* 0 8 42 readybbbb2 0 0 50 finishffff2 0 0 50 finishggg 0 6 44 ready5cputime:3name cputime needtime priority statejing* 2 0 42 finishaaaa* 0 8 42 readybbbb2 0 0 50 finishffff2 0 0 50 finishggg 1 5 41 execute2cputime:4name cputime needtime priority statejing* 2 0 42 finishaaaa'
17、1 7 39 executebbbb2 0 0 50 finishffff2 0 0 50 finishggg 1 5 41 readycputime:5name cputime needtime priority statejing* 2 0 42 finishaaaa' 1 7 39 readybbbb2 0 0 50 finishffff2 0 0 50 finishggg 2 4 38 executecputime:6name cputime needtime priority statejing* 2 0 42 finishaaaa$ 2 6 36 executebbbb2
18、0 0 50 finishffff2 0 0 50 finishggg 2 4 38 readycputime:7name cputime needtime priority statejing* 2 0 42 finishaaaa$ 2 6 36 readybbbb2 0 0 50 finishffff2 0 0 50 finishggg 3 3 35 executecputime:8name cputime needtime priority statejing* 2 0 42 finishaaaa! 3 5 33 executebbbb2 0 0 50 finishffff2 0 0 5
19、0 finishggg 3 3 35 readycputime:9name cputime needtime priority statejing* 2 0 42 finishaaaa! 3 5 33 readybbbb2 0 0 50 finishffff2 0 0 50 finishggg 4 2 32 executecputime:10name cputime needtime priority statejing* 2 0 42 finishaaaa 4 4 30 executebbbb2 0 0 50 finishffff2 0 0 50 finishggg 4 2 32 ready
20、cputime:11name cputime needtime priority statejing* 2 0 42 finishaaaa 4 4 30 readybbbb2 0 0 50 finishffff2 0 0 50 finishggg 5 1 29 executecputime:12name cputime needtime priority statejing* 2 0 42 finishaaaa 5 3 27 executebbbb2 0 0 50 finishffff2 0 0 50 finishggg 5 1 29 readycputime:13name cputime needtime priority statejing* 2 0 42 finishaaaa 5 3 27 readybbbb2 0 0 50 finishffff2 0 0 50 finishggg 6 0 26 executecputime:14name cputime needtime priority
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026广东中山大学孙逸仙纪念医院心血管内科医教研岗位人员招聘4人考试备考题库及答案详解
- 2026年湖南高速养护工程有限公司第二批招聘46人笔试历年备考题库附带答案详解
- 2026年开封市水投水生态有限公司面向社会招聘工作人员4人笔试历年常考点试题专练附带答案详解
- 2026年中国葛洲坝集团易普力股份有限公司禹州分公司招聘22人(河南)笔试历年常考点试题专练附带答案详解
- 2026山东高速集团有限公司博士后科研工作站全职博士后招聘3人笔试历年常考点试题专练附带答案详解
- 2026年广西财经学院高层次人才招聘138人考试参考题库及答案详解
- 2026年昆明市盘龙区公务员招聘笔试模拟试题及答案详解
- 2025年中央美术学院初试考题解析
- 2025年监理工程师土建控制考试真题(附答案)
- 2026年柳州市鱼峰区公务员招聘笔试参考题库及答案详解
- 《油罐讲义》课件
- 《产业经济学》期末考试复习题库(含答案)
- 自编MSA(计数型)自动分析表
- 北京工业大学《嵌入式系统》2022-2023学年期末试卷
- GB/T 2977-2024载重汽车轮胎规格、尺寸、气压与负荷
- 深圳市步行和自行车交通系统规划设计导则
- 2024年浙江省中考英语试题卷(含答案解析)
- 经鼻高流量湿化氧疗护理
- 天津耀华嘉诚国际中学2024届高一下数学期末质量检测试题含解析
- 传染病相关知识及上报流程
- 人工智能在金融营销中的应用
评论
0/150
提交评论