




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、淮海工学院计算机科学系实验报告书课程名: 操 作 系 统 题 目: 虚拟存储器管理 页面置换算法模拟实验 班 级: 学 号: 姓 名: 评语:成绩: 指导教师: 批阅时间: 年 月 日一、实验目的与要求1. 目的:请求页式虚存管理是常用的虚拟存储管理方案之一。通过请求页式虚存管理中对页面置换算法的模拟,有助于理解虚拟存储技术的特点,并加深对请求页式虚存管理的页面调度算法的理解。2. 要求:本实验要求使用C语言编程模拟一个拥有若干个虚页的进程在给定的若干个实页中运行、并在缺页中断发生时分别使用FIFO和LRU算法进行页面置换的情形。其中虚页的个数可以事先给定(例如10个),对这些虚页访问的页地址
2、流(其长度可以事先给定,例如20次虚页访问)可以由程序随机产生,也可以事先保存在文件中。要求程序运行时屏幕能显示出置换过程中的状态信息并输出访问结束时的页面命中率。程序应允许通过为该进程分配不同的实页数,来比较两种置换算法的稳定性。二、实验说明1设计中虚页和实页的表示本设计利用C语言的结构体来描述虚页和实页的结构。pnpfntimepnpfnnext 虚页结构 实页结构在虚页结构中,pn代表虚页号,因为共10个虚页,所以pn的取值范围是09。pfn代表实页号,当一虚页未装入实页时,此项值为-1;当该虚页已装入某一实页时,此项值为所装入的实页的实页号pfn。time项在FIFO算法中不使用,在L
3、RU中用来存放对该虚页的最近访问时间。在实页结构中中,pn代表虚页号,表示pn所代表的虚页目前正放在此实页中。pfn代表实页号,取值范围(0n-1)由动态指派的实页数n所决定。next是一个指向实页结构体的指针,用于多个实页以链表形式组织起来,关于实页链表的组织详见下面第4点。2关于缺页次数的统计为计算命中率,需要统计在20次的虚页访问中命中的次数。为此,程序应设置一个计数器count,来统计虚页命中发生的次数。每当所访问的虚页的pfn项值不为-1,表示此虚页已被装入某实页内,此虚页被命中,count加1。最终命中率=count/20*100%。3LRU算法中“最近最久未用”页面的确定为了能找
4、到“最近最久未用”的虚页面,程序中可引入一个时间计数器countime,每当要访问一个虚页面时,countime的值加1,然后将所要访问的虚页的time项值设置为增值后的当前countime值,表示该虚页的最后一次被访问时间。当LRU算法需要置换时,从所有已分配实页的虚页中找出time值为最小的虚页就是“最近最久未用”的虚页面,应该将它置换出去。4算法中实页的组织因为能分配的实页数n是在程序运行时由用户动态指派的,所以应使用链表组织动态产生的多个实页。为了调度算法实现的方便,可以考虑引入free和busy两个链表:free链表用于组织未分配出去的实页,首指针为free_head,初始时n个实页
5、都处于free链表中;busy链表用于组织已分配出去的实页,首指针为busy_head,尾指针为busy_tail,初始值都为null。当所要访问的一个虚页不在实页中时,将产生缺页中断。此时若free链表不为空,就取下链表首指针所指的实页,并分配给该虚页。若free链表为空,则说明n个实页已全部分配出去,此时应进行页面置换:对于FIFO算法要将busy_head 所指的实页从busy链表中取下,分配给该虚页,然后再将该实页插入到busy链表尾部;对于LRU算法则要从所有已分配实页的虚页中找出time值为最小的虚页,将该虚页从装载它的那个实页中置换出去,并在该实页中装入当前正要访问的虚页。三、程
6、序流程图三个模块的流程图1登录模块开 始打开w_input窗口关闭w_main窗口2参数输入模块开 始打开w_accept窗口关闭w_input窗口输入当前磁头位置输入各磁道号位置开 始打开w_input窗口关闭w_accept窗口调用FCFS算法显示结果调用SSTF算法显示结果调用SCAN算法显示结果调用CSCAN算法 显示结果3算法实现模块四、主要程序清单模块之间的调用关系登录模块参数输入模块算法实现模块#include#include#includeint producerand(int remainder);void initprocess();void chosedisplace()
7、;struct linknode * fifo(struct linknode *head,int randcount);void Optimal(struct linknode*head,int randprocess);struct linknode* LRU(struct linknode *head,int randprocess);struct linknode* initlink();void choicestey();int allotment(struct linknode *head);bool checkfifooptimal(struct linknode* head,i
8、nt checkpage);void recover(struct linknode *head,int randprocess);void recovermemory(); int process1020;/数组的横坐标为进程序列,纵坐标为每个进程的页号 int processallotment6;/存储每个进程已经分配的块数 int finishp6;/标志进程是否完成(1完成0不完成) int finishprocess=0;/进程完成的个数 int findpage6;/每个进程命中的个数 struct linknode *plinkhead6;struct linknode *pli
9、nk6;int memoryallotment6; int stey=0;struct linknodestruct linknode *linkper;/空链表的前驱指针 int page;int processpage;int used;int memorypage; struct linknode *linknext;/空链表的后继指针 struct linknode *processper;/进程的前去指针struct linknode *processnext;/进程的后继指针 ;int main()struct linknode *head=initlink(); initproc
10、ess(); choicestey(); int re=allotment(head); if(re=0)printf(内存分配出现问题。);system(pause);chosedisplace();recovermemory();system(pause);void recovermemory()int n=0;printf(是否回收全部已分配的内存空间?n回收输入 1,不回收输入2n);scanf(%d,&n);if(n=1)for(int i=1;iused=0;head=head-processnext;void choicestey() printf(请选择置换算法n); prin
11、tf(1 表示FIFOn2 表示Optimaln3 表示LRUn); bool flag=true;while(flag)scanf(%d,&stey); switch(stey)case 1:printf(您选择的是FIFO替换算法n);flag=false; break;case 2:printf(您选择的是Optimal替换算法n);flag=false;break;case 3:printf(您选择的是LRU替换算法n);flag=false;break;default :printf(输入错误,请重新输入n); void chosedisplace()/选择置换算法struct li
12、nknode *head;int randcount;/进程序号 bool find;while(finishprocess5) randcount=producerand(5);while(processallotmentrandcountprocessrandcount0)find=false;head=plinkheadrandcount;if(stey=1|stey=2)find=checkfifooptimal(head,processrandcountprocessallotmentrandcount+1);if(find=true)findpagerandcount+;if(fi
13、nd=false)/如果在链表中没找到当前的页数 switch(stey)case 1:plinkheadrandcount=fifo(plinkheadrandcount,randcount);break;case 2: Optimal(plinkheadrandcount,randcount); break; case 3: plinkheadrandcount=LRU(plinkheadrandcount,randcount); break; processallotmentrandcount+; if(finishprandcount=0) finishprocess+;finishp
14、randcount=1; struct linknode *p;printf(进程执行完后内存分配情况:n);for(int i=1;imemorypage,p-processpage,p-page);p=p-processnext; for(int i=1;ipage=checkpage) return true;elsehead=head-processnext;return false;struct linknode* LRU(struct linknode *head,int randprocess) struct linknode *bhead; bhead=head; while(
15、head-processnext!=0) if(head-page=processrandprocessprocessallotmentrandprocess+1) break; else head=head-processnext; if(head-page!=processrandprocessprocessallotmentrandprocess+1)/没找到 bhead-page=processrandprocessprocessallotmentrandprocess+1; head-processnext=bhead; bhead-processper=head; bhead=bh
16、ead-processnext; bhead-processper=0; head=head-processnext; head-processnext=0; plinkrandprocess=plinkrandprocess-processnext; return bhead; else/找到了 if(head=bhead)/头 head-processper=plinkrandprocess; plinkrandprocess-processnext=head; plinkrandprocess=plinkrandprocess-processnext; head=head-process
17、next; head-processper=0; plinkrandprocess-processnext=0; findpagerandprocess+; return head; else if(head-processnext=0)/尾 findpagerandprocess+; return bhead; else/中间 head-processnext-processper=head-processper; head-processper-processnext=head-processnext; head-processnext=0; head-processper=plinkra
18、ndprocess; plinkrandprocess-processnext=head; plinkrandprocess=plinkrandprocess-processnext; findpagerandprocess+; return bhead; void Optimal(struct linknode*head,int randprocess)struct linknode *maxp;maxp=head;int max=1,i;while(head!=0)for(i=processallotmentrandprocess+1;ipage)break;if(imax)max=i;m
19、axp=head;head=head-processnext;maxp-page=processrandprocessprocessallotmentrandprocess+1;struct linknode* fifo(struct linknode*head,int randprocess)struct linknode*phead;/改变后的头指针phead=head; head-page=processrandprocessprocessallotmentrandprocess+1;while(head-processnext!=0)head=head-processnext;head
20、-processnext=phead;phead-processper=head;phead=phead-processnext;head=head-processnext;head-processnext=0;phead-processper=0;return phead;int allotment(struct linknode *head)/为进程分配内存 int allotsum=0;/已经分配完进程的个数 int randprocess;/当前要分配内存的进程标号 bool boolallot6;for(int i=1;i6;i+)processallotmenti=0;boolal
21、loti=false;memoryallotmenti=0;while(allotsumused=0)if(processallotmentrandprocess=0)plinkheadrandprocess=head;plinkrandprocess=head;plinkrandprocess-processper=0;plinkrandprocess-processnext=0;head-processpage=randprocess;plinkrandprocess-page=processrandprocess1;head-used=1;printf(内存块号:%dt进程号:%dt页号
22、:%dn,head-memorypage,head-processpage,head-page);head=head- linknext;memoryallotmentrandprocess+;findpagerandprocess+; elsebool checksame=checkfifooptimal(plinkheadrandprocess,processrandprocessprocessallotmentrandprocess+1);if(checksame=false)head-used=1; head-processnext=0; head-processper=plinkra
23、ndprocess; plinkrandprocess- processnext=head; head-processpage=randprocess; head-page=processrandprocessprocessallotmentrandprocess+1;plinkrandprocess=plinkrandprocess-processnext;printf(内存块号:%dt进程号:%dt页号:%dn,head-memorypage,head-processpage,head-page);head=head-linknext;memoryallotmentrandprocess+
24、;findpagerandprocess+; else if(stey=3) plinkheadrandprocess=LRU(plinkheadrandprocess,randprocess); else findpagerandprocess+; processallotmentrandprocess+;elseprintf(进程%d分配失败n,randprocess);return 0; if(head=0)printf(进程%d分配失败n,randprocess);return 0; if(processallotmentrandprocess=processrandprocess0)
25、printf(进程%d分配成功n,randprocess);allotsum+;boolallotrandprocess=true;finishprocess+;finishprandprocess=1; else if(memoryallotmentrandprocess=4)allotsum+;boolallotrandprocess=true;printf(进程%d分配成功n,randprocess); struct linknode *p; printf(初始内存分配情况:n); for(int i=1;imemorypage,p-processpage,p-page);p=p-processnext; return 1;void initprocess
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 战略合作方销售代理合同范本
- 土地使用权买卖合同样本
- 临时雇佣合同标准文本
- 高校毕业生实习协议合同
- 股份合作企业合同范本
- 婚礼场地租赁合同书
- 度企业信用反担保合同协议
- 企业安全生产责任协议合同
- 劳动合同样本:员工长期雇佣
- 海滨度假村物业销售合同协议
- 2022年陕西省普通高校职业教育单独招生统一考试语文甲(A)试题
- DB11T 212-2017 园林绿化工程施工及验收规范
- 失业保险待遇申领表
- 2024-2025学年初中信息技术(信息科技)第二册河北大学版(第3版)教学设计合集
- 期末测试卷(一)(试题)2023-2024学年二年级上册数学苏教版
- 携程在线能力测评真题
- 感知觉与沟通评估三明医学科技职业
- 人教版(2024)六年级全一册 第17课 设计我的种植园
- 承包商入厂安全培训试题附参考答案【完整版】
- 加盟京东商城合同模板
- 食品安全与质量检测技能大赛考试题库400题(含答案)
评论
0/150
提交评论