最新计算机操作系统试验模拟比较面置换算法及缺率_第1页
最新计算机操作系统试验模拟比较面置换算法及缺率_第2页
最新计算机操作系统试验模拟比较面置换算法及缺率_第3页
最新计算机操作系统试验模拟比较面置换算法及缺率_第4页
最新计算机操作系统试验模拟比较面置换算法及缺率_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

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

文档简介

1、成绩计算机操作系统实验模拟比较页面置换页算法及缺页率学号 200510020220姓名乔峰 班级 信息052实验名称:模拟比较页面置换页算法及缺页率实验目的:(1)掌握先进先出页面置换算法;(2) 掌握最近未用页面置换算法;(3) 了解最近最久未使用页面置换算法以及其他页面置换算法:(4) 熟悉C/C+编程。实验学时:6学时实验内容:编写程序,设置不同的贝面数,使用不同的页面替换策略算法进行 模拟页面替换。先进先出,最近未用页面置换算法等,并计算缺页 率。实验环境:(1) . PC微机(2) . Windows操作系统(3) . C/C+开发环境实验原理及算法参考程序段:#includc #i

2、ncludc #include #include include int add256/* 地址 */,page256/* 页而*/;int k.j,ram,t;float rale;/*缺页率*/struct si int page;int free;int tag; fifo33,opt33Jru33;stmet s2j int time;void addressO;float FIFO(int ram)尸先进先出勺float LRU(int ram);/*最近最久未使用页而置换*/void addrcssO/*产生指令地址*/ int i;add0=1000;for (i=l; i=0)

3、&(x=512)&(x=768)&(x1024)add i=add i-l+random(30* 1024-add i-l-l)+l;float FIFO(int ram) int absent=O.t=O.i,z,Lyn;for (i=0; iram; i+) fifoi.page=-l;fifoij.free=l;fifoi.tag=0;i=0;while (ij) yn=o;for (z=0; zram; z+) /*the page is in the ram?*/if (fifbz.page=pageij)yn=l;for (z=0; zvram; z+)if (fifoz.free

4、=0) fifoz.tag+=l; jif (yn!=l) absent+= 1; /*count the absent page*/1=0;while (lvram)&(fifolfrcc=0)1+;if (lvram)&(fifolfrcc=l)/*any free ram?*/ fifol.page=pagei:fifol.free=0;for (1=0; lvram; 1+)if (fifol.firee=0) fifol.tag+=l;)else /*there is no free ram*/t=0;for (1=0; lram; 1+)if ( fifol.tagfifo|t.t

5、ag)t=l; fifot.page=pagei; fifot.free=0; fifot.tag=l;1=0;)i+;rate=( float )absent/j * 100; return rate; float LRU(int ram) int absent=0,yn.tj,l.z,now=0;struct s2 P250;for (i=0;ij;i+)P(i.time=0;for (i=0; iram; i+) lrui.page=-l;lru(i.free=l;i=0;while(ij) for(l=0; lram; 1+)yn=0; for(z=0; zram; z+)if (lr

6、uz.page=pageij) now+=l;Plruz.page.time=now;yn=l;)if (yn!=l) absent+=l; now+=l;1=0;while (l=ram)&(lrul.frec=O)1+;if (I=ram)&(Irul.free=l) /*any free ram?*/ Irul.page=pagei:Plrul.page.time=now: lrul.free=0;else /*there is no ram*/t=0;for (1=0: lram; 1+)if (Plrul.page.timePlrut.page.time)t=I;lrut.page=

7、pagei;PlrutJ.pageJ.time=now;)i+;rate=(float)absent/j* 100:return rate;void main() int i,p2567* 页号 */clrscr();address();for (k=l; k=8;)/#页面大小:lk,2k,4k,8K*/ printf(Mthe size of a page is %d kn ,k); printf(uthe page num is .An”); for (i=0; i256; i+)(pi=addi/(k*1024)尸将指令地址生成相应的页号*/ printf(H%dj=0;for (i=

8、0; i256; i+) while (pi=pi+lj)i+;pagej=pi;j+; printf(unafter connect the same pages the page num is:nH);for (i=0; ij; i+)printf(H%d *pagei);printf(HnH);getch();for (ram=l; ramhead-nextpagey/当前页struct page * replacedpage;/要淘汰的页在当前页中寻找要访问的页号while(currentpage¤tpage-number!=accesspage)currcntpage=c

9、urrentpage ncx(pagc;)找到页号则打印J,并返回0if(currcntpage!=NULL&cinrcmpagcnumber=acccsspagc)print Jf,);return 0;没找到则判断页表是否已经满了,没满则调入新页并打印X返回1 else if(pt-pagenumpagetablesize)newpage=(struct page *)malloc(sizeof(struct page); newpage-number=accesspage;newpage-nextpage=newpage-priorpage=NULL; newpage-nextpage=

10、pt-head-nextpage;if(pt-head-nextpage)pt-head-nextpage priorpagc=ncwpagc;pt-head-nextpage=newpage;newpage-priorpage=pt-head;pt-pagenum+;printf(MxH);return 1:如页表已经满,则采用fifo算法进行淘汰选择elsecurrentpage=pt-head-nextpage; replacedpage=NULL;while(currentpage-nextpage)currentpage=currentpage-ncxtpage;replacedpa

11、ge=currentpage;if(replaccdpage-nextpage)(replacedpage-nextpage-priorpage=replacedpage-priorpage;replacedpage-prioipage ncx(page=rcplaccdpagcncx(pagc; free(replaccdpage);newpage=(struct page *)malloc(sizeof(stnict page); newpage-number=accesspage; newpage-nextpage=newpage-priorpage=NULL; newpage-next

12、page=pt-head-nextpage; if(pt-head-nextpage)pt-head-nextpage-priorpage=newpage;pt-head-nextpage=newpage;ne wpage-priorpage=pt-head:printf(MxH);return 1;main()struct pagetable pt;unsigned int totalabsence=0,totalpagenum= 10;unsigned int restpage 100;unsigned int i=0;pt.pagenum=O;pt.pagetablesize=3;pt.

13、head=(struct page*)malloc(sizeof(struct page); pt.head-nextpage=pt.head-priorpage=NULL;从文件获取要读入的页而流HLE *fp:fp=fopen(MIN.dafVru);if(fp=NULL)printf(Hcan not open file IN.DAT!H);retum: while(!feof(fp)fscanf(fp/,%d,&restpagei);i+;fclose(fp);totalpagenum=i;printffthis program for fifo nnM);打印要访问的页for(i=0;itotalpagenum;i+)printf(,%2dH,restpa

温馨提示

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

评论

0/150

提交评论