




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验二进程调度1.目的和要求通过这次实验,理解进程调度的过程,进一步掌握进程状态的转变、进程调度的策略,进一步体会多道程序并发执行的特点,并分析具体的调度算法的特点,掌握对系统性能的评价方法。.实验内容编写程序模拟实现进程的轮转法调度过程,模拟程序只对PCB进行相应的调度模拟操作,不需要实际程序。假设初始状态为:有n个进程处于就绪状态,有m个进程处于阻塞状态。采用轮转法进程调度算法进行调度(调度过程中,假设处于执行状态的进程不会阻塞),且每过t个时间片系统释放资源,唤醒处于阻塞队列队首的进程。程序要求如下:1) 输出系统中进程的调度次序;2) 计算CPU利用率。.实验环境Windows操作系统、VC++6.0C语言4.实验要求:1) 上机前认真使用C语言编写好程序,采用VisualC++6.0作为编译环境;2) 上机时独立调试程序3) 根据具体实验要求,填写好实验报告(包括目的和要求、实验内容、实验环境、设计思想、源程序、实例运行结果、总结)。4) 测试用数据:n=2m=3t=5dispath()算法流程图:use_cpu=0x=0unuse_cpu=0/*use_cpu中记录CPU运行时间源程序:#include<stdio.h>#include<stdlib.h>structPCB_type{intpid; 〃进程名intstate; 〃进程状态//2--表示”执行”状态//1--表示”就绪”状态//0--表示”阻塞”状态intcpu_time;〃运行需要的CPU时间(需运行的时间片个数)};structQueueNode{structPCB_typePCB;structQueueNode*next;};//ready队列队首指针//ready队列队首指针//ready队列队尾指针//blocked队列队首指针//blocked队列队尾指针*ready_tail=NULL,*block_head=NULL,*block_tail=NULL;intuse_cpu,unuse_cpu;voidstart_state()〃读入假设的数据,设置系统初始状态{intn,m;inti;structQueueNode*p,*q;printf("输入就绪节点个数n:");scanf("%d”,&n);printf("输入阻塞节点个数m:");scanf("%d”,&m);p=(structQueueNode*)malloc(sizeof(structQueueNode));p->next=NULL;ready_head=ready_tail=p;for(i=0;i<n;i++){p=(structQueueNode*)malloc(sizeof(structQueueNode));p->next=NULL;p->PCB.state=1;printf("输入就绪进程%d的pid和cpu_time:",i+1);scanf("%d%d”,&p->PCB.pid,&p->PCB.cpu_time);ready_tail->next=p;ready_tail=p;}q=(structQueueNode*)malloc(sizeof(structQueueNode));q->next=NULL;block_head=block_tail=q;for(i=0;i<m;i++){q=(structQueueNode*)malloc(sizeof(structQueueNode));q->next=NULL;q->PCB.state=0;printf("输入阻塞进程%d的pid和cpu_time:",i+1);scanf("%d%d”,&q->PCB.pid,&q->PCB.cpu_time);block_tail->next=q;block_tail=q;}printf("\n处于就绪状态的进程有:\n");p=ready_head->next;i=1;while(p){printf("进程%d的pid和state和cpu_time:%5d%5d%5d\n”,i,p->PCB.pid,p->PCB.state,p->PCB.cpu_time);p=p->next;i++;}}voiddispath() //模拟调度{intx=0,t;use_cpu=0;unuse_cpu=0;printf('输入t:");scanf("%d”,&t);printf("开始调度\n");while(ready_head!=ready_tail||block_head!=block_tail){structQueueNode*p,*q;if(ready_head!=ready_tail)p=ready_head->next;ready_head->next=p->next;p->next=NULL;if(ready_head->next==NULL){ready_tail=ready_head;}p->PCB.state=2;printf("进程%d调度\t",p->PCB.pid);use_cpu++;x++;p->PCB.cpu_time--;if(p->PCB.cpu_time){ready_tail->next=p;ready_tail=p;}else{printf("进程%d完成\t",p->PCB.pid);free(p);}}else{unuse_cpu++;x++;printf("空闲一个时间片\『);}if(x==t&&block_head!=block_tail){q=block_head->next;block_head->next=q->next;q->next=NULL;if(block_head->next==NULL){block_tail=block_head;}ready_tail->next=q;ready_tail=q;x=0;
}voidcalculate()〃计算CPU利用率{printf("\ncpu的利用率%.2f\n",(float)use_cpu/(use_cpu+unuse_cpu));}voidmain()(start_state();dispath();calculate();}111片片一个时间片£就绪状态的进程有二pid^Ustatecpu_time:呈2的pid^Ustate^Qcpu_time:^:5_time:1_time:2_time:111片片一个时间片£就绪状态的进程有二pid^Ustatecpu_time:呈2的pid^Ustate^Qcpu_time:^:5_time:1_time:2_time:3_time:4_time:5145.运行结果片可.1 成^^度度23-45SSS进进空进进度度度成成兀13445SS11S1S1S1S1S.卜_1.■.►_1.■.►_1.■.►_1.■.►_1.■uuuuUppppPCCCCCnnnnn23-木丰木丰木■■■■nniiiii蜘数^pftpftpftpftp.ru12123点点曲进进进进.*■W04-I-7V.ILcpu的利用率@-79Pressanykeyt1成度度度一1345
H
进进进进7.实验小结通过模拟进程调度的实验,熟悉了进程调度算法,对进程调度的原理有了进步的认识,巩固了理论知识,同时复习了C语言的知识。实验三可变分区存储管理1.目的和要求通过这次实验,加深对内存管理的认识,进一步掌握内存的分配、回收算法的思想。.实验内容编写程序模拟实现内存的动态分区法存储管理。内存空闲区使用自由链管理,采用最坏适应算法从自由链中寻找空闲区进行分配,内存回收时假定不做与相邻空闲区的合并。假定系统的内存共640K,初始状态为操作系统本身占用64K。在t1时间之后,有作业A、B、C、D分别请求8K、16K、64K、124K的内存空间;在t2时间之后,作业C完成;在t3时间之后,作业E请求50K的内存空间;在t4时间之后,作业D完成。要求编程序分别输出t1、t2、t3、t4时刻内存的空闲区的状态。.实验环境Windows操作系统、VC++6.0C语言4.实验要求:1) 上机前认真使用C语言编写好程序,采用VisualC++6.0作为编译环境;2) 上机时独立调试程序3) 根据具体实验要求,填写好实验报告(包括目的和要求、实验内容、实验环境、设计思想、源程序、实例运行结果、总结)。
requireMemo(charname,intrequire)流程图如下:requireMemo(charname,intrequire)流程图如下:endfreeMemo(charname)流程图如下:if(p==busy_tail)busy_tail=q;printf("%cisnotexist”,name)q->next=p->next;len=p->len;address=p->address;free(p) 】ri是 -end: 十是
w=(structfreelink*)malloc(..・);w->len=len;w->address=address;1Fend源程序:#include<stdio.h>#include<stdlib.h>structfreelinkNode(intlen;intaddress;structfreelinkNode*next;};structbusylinkNode(charname;intlen;intaddress;structbusylinkNode*next;};structfreelinkNode*free_head=NULL;//自由链队列(带头结点)队首指针structbusylinkNode*busy_head=NULL;〃占用区队列队(带头结点)首指针structbusylinkNode*busy_tail=NULL;〃占用区队列队尾指针voidstart(void)/*设置系统初始状态*/{structfreelinkNode*p;structbusylinkNode*q;free_head=(structfreelinkNode*)malloc(sizeof(structfreelinkNode));free_head->next=NULL;//创建自由链头结点busy_head=busy_tail=(structbusylinkNode*)malloc(sizeof(structbusylinkNode));busy_head->next=NULL;//创建占用链头结点p=(structfreelinkNode*)malloc(sizeof(structfreelinkNode));p->address=64;p->len=640-64;//OS占用了64Kp->next=NULL;free_head->next=p;q=(structbusylinkNode*)malloc(sizeof(structbusylinkNode));q->name='S';/*S表示操作系统占用*/q->len=64;q->address=0;q->next=NULL;busy_head->next=q;busy_tail=q;}voidrequireMemo(charname,intrequire)/*模拟内存分配*/{freelinkNode*w,*u,*v;busylinkNode*p;if(free_head->next->len>=require){p=(structbusylinkNode*)malloc(sizeof(structbusylinkNode));p->name=name;p->address=free_head->next->address;p->len=require;p->next=NULL;busy_tail->next=p;busy_tail=p;}elseprintf("Can'tallocate");w=free_head->next;free_head->next=w->next;if(w->len==require){free(w);}else{w->address=w->address+require;w->len=w->len-require;}u=free_head;v=free_head->next;while((v!=NULL)&&(v->len>w->len)){u=v;v=v->next;}u->next=w;w->next=v;}voidfreeMemo(charname)/*模拟内存回收*/{intlen;intaddress;busylinkNode*q,*p;freelinkNode*w,*u,*v;q=busy_head;p=busy_head->next;while((p!=NULL)&&(p->name!=name)){q=p;p=p->next;}if(p==NULL){printf("%cisnotexist",name);else{if(p==busy_tail){busy_tail=q;}else{q->next=p->next;len=p->len;address=p->address;free(p);w=(structfreelinkNode*)malloc(sizeof(structfreelinkNode));w->len=len;w->address=address;u=free_head;v=free_head->next;while((v!=NULL)&&(v->len>len)){u=v;v=v->next;}u->next=w;w->next=v;}}}voidpast(inttime)/*模拟系统过了time时间*/{printf("过了时间%d后:\n”,time);}voidprintlink()/*输出内存空闲情况(自由链的结点)*/{freelinkNode*p;printf("内存的空闲情况为:\n");p=(structfreelinkNode*)malloc(sizeof(structfreelinkNode));p=free_head->next;while(p!=NULL){ printf("内存的起始地址和内存的大小%5d\t%5d:\n”,p->address,p->len);p=p->next;}}voidmain(){intt1=1,t2=2,t3=3,t4=4;start();
past(tl);requireMemo('A',8);requireMemo('B',16);requireMemo('C',64);requireMemo('D',124);printlink();past(t2);f
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025别墅装修合同范本
- 2025园林绿化养护合同
- 2025国内贸易合同模板
- 2025标准土地租用协议合同
- 湖南省娄底市部分学校2024-2025学年高一下学期4月期中英语试卷(图片版)
- 画册平面设计合同协议
- 玻璃瓶定制合同协议
- 环保渣土车出售合同协议
- 瓦工砌墙清包合同协议
- 生病解除劳动合同协议
- 【高考真题】2022年新高考物理真题试卷-河北卷(含答案)
- 社保系统保密培训
- 急诊一科一品一特色护理
- 物流行业招聘流程及人员配置
- 液化气充装站建站可行性研究报告
- 电力安全工作规程(完整版)
- 2024-2030年中国临近空间飞行器发展规划及未来前景展望研究报告
- 瑞幸咖啡认证考试题库(值班主管)
- 《广东省智慧高速公路建设指南(试行)》
- 工厂自动化规划报告
- 《分布式生活垃圾中转站臭气处理技术规程》
评论
0/150
提交评论