请求分页式存储管理的地址转换过程实现_第1页
请求分页式存储管理的地址转换过程实现_第2页
请求分页式存储管理的地址转换过程实现_第3页
请求分页式存储管理的地址转换过程实现_第4页
请求分页式存储管理的地址转换过程实现_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

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

文档简介

1、第2部分、请求分页式存储管理的地址转换过程实现:基本要求:在第1部分实验基础上实现进程的分页式内存分配和地址转换过程,并进一步实现请求分页式存储分配和地址转换过程。页面置换算法至少应实现先进先出(fifo)、最近最久未使用(lru)等算法。 参考学时:16学时实验提示:1、 建立一个位示图,用来模拟内存的分配情况,位示图的位数与设定的物理块个数相同。程序启动时可利用一组随机0和1填充位示图,表示内存已被占用情况。假设内存容量为64k,块大小为1k,则共有64个块,对应的位示图可能的值如下:第0字节1 0 1 1 0 0 1 1第2字节1 1 1 0 0 1 1 1第4字节0 0 0 1 1 1

2、 1 1第6字节1 1 1 1 1 0 0 0第1字节0 1 1 0 1 1 0 0第3字节1 0 0 0 0 1 1 1第5字节0 1 1 1 0 0 1 1第7字节0 0 0 0 0 0 0 1该位示图表示内存的2(0字节第2位)、3(0字节第3位)、6(0字节第6位)、8(1字节第0位)、9(1字节第1位)、12(1字节第4位)、15(1字节第7位)等块没有被占用。2、 创建进程时输入进程大小,并根据程序中设定的物理块大小为进程分配物理块,同时建立页表。例如,在上图基础上,若要建立一个大小为5000字节的进程,则:l 计算出该进程共有“向上取整(5000/1024)=5”个页,需要占用5

3、个内存块;l 建立空的页表,即长度为5的一维整数数组;l 从位示图中找出前5个“0”位在整个位示图中的位置号(即i字节j位为0,则该位的位置为8*i+j),并将这些号依次填入页表中,同时把前5个“0”改为“1”,以示对应内存块已经分配。3、 输入当前执行进程所要访问的逻辑地址,并将其转换成相应的物理地址。4、 进程退出时,根据其页表内容向位示图反向回填“1”。5、 扩充页表,将其变成支持请求和置换功能的二维页表(增加存在位等)。创建进程时可装入固定的前三页(或键盘输入初始装入页数,不同进程的装入个数可以不同),其余页装入到置换空间内(置换空间大小应为内存空间大小的1.5倍,对其还需建立独立的位

4、示图)。6、 分别采用fifo和lru置换算法对地址转换过程中遇到的缺页现象进行页面置换,可将多次地址转换过程中所涉及到的页号视为进程的页面访问序列,从而计算置换次数和缺页率。import java.util.vector;import java.io.*;class processpage pagetable;vector<page> pagequeue;private int size;private int pagecount;private string name;double visit;double replacement;public void setpage(pag

5、e pagetable) this.pagetable=new pagepagetable.length;for(int i=0;i<pagetable.length;i+) this.pagetablei=pagetablei;public int getpageblock(int pagenum) return pagetablepagenum.getblocknum();public void setsize(int size) this.size=size; public void setpagecount(int pagecount) this.pagecount=pageco

6、unt; public int getpagecount() return pagecount;visit=0;replacement=0;public process() public process(string name)=name;public string tostring()return name;class page int flag;int blocknum;int writed;/xiu gai weiint accessfield;/fang wen zi duanint swapsapce_blockno;/wai cun dizhiint t;bloc

7、knum=0;writed=0;accessfield=0;t=0;public page() flag=1;swapsapce_blockno=-1;public page(int flag,int swapsapce_blockno) this.flag=flag;this.swapsapce_blockno=swapsapce_blockno;public int getblocknum() return blocknum;public int getflag() return flag; class os2 public static vector<process> rea

8、dy,blocked; public static process running; public static bufferedreader br; static int a=new int88; static intd=new int1010;/置换空间 static int count=0; public os2() ready=new vector<process>(); blocked=new vector<process>();br=new bufferedreader(new inputstreamreader(system.in);for(int i=0

9、;i<a.length;i+) for(int j=0;j<ai.length;j+) if(math.random()<0.5) aij=0; else aij=1; for(int i=0;i<d.length;i+) for(int j=0;j<di.length;j+) if(math.random()<0.5) dij=0; else dij=1; system.out.println("位示图-"); print(a); system.out.println("置换空间-"); print(d); publ

10、ic static void go() while(true) system.out.println("="); system.out.println("1:进程创建 "); system.out.println("2:进程到时 "); system.out.println("3:进程阻塞 "); system.out.println("4:进程唤醒 "); system.out.println("5:进程结束 "); system.out.println("6:逻

11、辑地址转换为物理地址 "); system.out.println("7:查看页表"); system.out.println("8:显示位示图和置换空间"); system.out.println("9:查看置换次数和缺页率"); system.out.println("0:退出->"); try int i=integer.parseint(br.readline(); switch(i) case 0: system.exit(0); case 1: createnewprocess(); b

12、reak; case 2: switchcurrentprocess(); break; case 3: blockcurrentprocess(); break; case 4: wakeupblockedprocess(); break; case 5: terminatecurrentprocess(); break; case 6: transform(); break; case 7: showpage(); break; case 8: system.out.println("位示图-"); print(a); system.out.println("

13、置换空间-"); print(d); break; case 9: look(); break; catch(exception e) system.out.println(e); system.out.println("执行进程:"+(running=null?"none":running);system.out.print("就绪进程:"); printlist(ready);system.out.print("阻塞进程:"); printlist(blocked); public static vo

14、id printlist(vector v) for(int i=0;i<v.size();i+) system.out.print(v.elementat(i)+"t"); system.out.println(); public static void createnewprocess() try system.out.print("进程名称:"); string name=br.readline(); process process=new process(name); ready.add(process); system.out.print

15、("进程大小:"); double size=integer.parseint(br.readline(); int numpage=0; numpage=(int)math.ceil(size/1024); process.setpagecount(numpage); page p=new pagenumpage; process.pagequeue=new vector<page>(); st: for(int i=0;i<a.length;i+) for(int j=0;j<ai.length;j+) if(aij=0) if(count<

16、;3) /page p1=new page(); /pcount=p1; pcount=new page(); pcount.blocknum=8*i+j; process.pagequeue.add(pcount); pcount.t=3-count; count+; aij=1; if(count=numpage) break st; else for(int m=0;m<d.length;m+) for(int n=0;n<dm.length;n+) if(dmn=0) /page p2=new page(); /pcount=p2; pcount=new page(0,-1

17、); pcount.blocknum=-1; pcount.swapsapce_blockno=8*m+n; count+; dmn=1; if(count=numpage) count=0; break st; if(running=null) running=(process)ready.elementat(0); ready.removeelementat(0); process.setpage(p); catch(exception e) system.out.println(e); public static void switchcurrentprocess() if(runnin

18、g!=null) ready.add(running); if(ready.size()>0) running=(process)ready.elementat(0); ready.removeelementat(0); else running=null; public static void blockcurrentprocess() if(running!=null) blocked.add(running); if(ready.size()>0) running=(process)ready.elementat(0); ready.removeelementat(0); e

19、lse running=null; public static void wakeupblockedprocess() if(blocked.size()>0) /running=(process)blocked.elementat(0); ready.add(process)blocked.elementat(0); blocked.removeelementat(0); if(running=null) running=(process)ready.elementat(0); ready.removeelementat(0); else /blocked=null; public s

20、tatic void terminatecurrentprocess() whileexit(); if(running!=null) running=null; if(ready.size()>0) running=(process)ready.elementat(0); ready.removeelementat(0); else if(blocked.size()>0) running=(process)blocked.elementat(0); blocked.removeelementat(0); else running=null; public static void

21、 transform() try if(running!=null) system.out.print("输入逻辑地址:");int logicadds=integer.parseint(br.readline();int pagenumber=logicadds/1024;if(pagenumber>running.getpagecount()-1) while(true) /if(pagenumber>running.getpagecount()-1) system.out.println("逻辑地址超出范围,请重新输入:");logic

22、adds=integer.parseint(br.readline();pagenumber=logicadds/1024;if(pagenumber<=running.getpagecount()-1) break;/int offset=logicadds%1024;int pageblocknum=running.getpageblock(pagenumber);+running.pagetablepagenumber.accessfield;+running.visit;if(running.pagetablepagenumber.getflag()=0) +running.re

23、placement;system.out.println("请选择你所想要置换的算法(提示:1代表fifo算法,2代表lru算法):");int ch=integer.parseint(br.readline();if(ch=1) system.out.println("正在fifo置换算法,对地址进行转换");fifo(pagenumber);else system.out.println("正在lru置换算法,对地址进行转换");lru(pagenumber);int phyadds=(running.getpageblock(p

24、agenumber)*1024+offset;system.out.println("物理地址:"+phyadds);else int phyadds=(running.getpageblock(pagenumber)*1024+offset;system.out.println("物理地址:"+phyadds);else system.out.println("no running process");catch(exception e) system.out.println(e); public static void while

25、exit() for(int i=0;i<running.pagetable.length;i+) if(running.pagetablei.flag=1) int m=running.pagetablei.blocknum/8;int n=running.pagetablei.blocknum%8;amn=0;else int m=running.pagetablei.swapsapce_blockno/8;int n=running.pagetablei.swapsapce_blockno%8;dmn=0;public static void fifo(int pagenumber

26、) running.pagetablepagenumber.blocknum=(page)running.pagequeue.elementat(0).blocknum;st:for(int i=0;i<d.length;i+) for(int j=0;j<di.length;j+) if(dij=0&&running.pagequeue.size()>0) dij=1;(page)running.pagequeue.elementat(0).flag=0;(page)running.pagequeue.elementat(0).blocknum=-1;(pa

27、ge)running.pagequeue.elementat(0).writed=0;(page)running.pagequeue.elementat(0).accessfield=0;(page)running.pagequeue.elementat(0).swapsapce_blockno=8*i+j;(page)running.pagequeue.elementat(0).t=0;running.pagequeue.removeelementat(0);running.pagequeue.add(running.pagetablepagenumber);break st;/sb:for

28、(int m=0;m<a.length;m+) /for(int n=0;n<am.length;n+) /if(amn=0) /amn=1;running.pagetablepagenumber.flag=1;/running.pagetablepagenumber.blocknum=8*m+n;running.pagetablepagenumber.writed=0;/+running.pagetableq.accessfield;/running.pagetableq.swapsapce_blockno=8*m+n;/break sb;/public static void

29、lru(int pagenumber) /process.pagetablecount.tint k=getpagetime();st:for(int i=0;i<d.length;i+) for(int j=0;j<di.length;j+) if(dij=0) dij=1;running.pagetablek.flag=0;running.pagetablek.blocknum=-1;running.pagetablek.writed=0;running.pagetablek.accessfield=0;running.pagetablek.swapsapce_blockno=

30、8*i+j;running.pagetablek.t=1;break st;sb:for(int m=0;m<a.length;m+) for(int n=0;n<am.length;n+) if(amn=0) amn=1;running.pagetablepagenumber.flag=1;running.pagetablepagenumber.blocknum=running.pagetablek.blocknum;running.pagetablepagenumber.writed=0;/+running.pagetableq.accessfield;/running.pag

31、etableq.swapsapce_blockno=8*m+n;break sb;for(int i=0;i<running.pagetable.length;i+) if(i!=k) +running.pagetablei.t;public static int getpagetime() int max=running.pagetable0.t;int place=0;for(int i=0;i<running.pagetable.length;i+) if(max<running.pagetablei.t) max=running.pagetablei.t;place=i;return place;public static void print(int a) for(int i=0;i<a.leng

温馨提示

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

评论

0/150

提交评论