【实验目的、要求】 (1)通过编写程序实现请求分页存储管理页面Optimal、FIFO、LRU调度算法,使学生掌握虚拟存储管理中有关缺页处理方法等内容,巩固有关虚拟存储管理的教学内容。 (2)了解Windows2000/XP中内存管理机制,掌握页式虚拟存储技术。 (3)理解内存分配原理,特别是以页面为单位的虚拟内存分配方法。 |
【实验内容】 在Windows XP、Windows 2000等操作系统下,使用的VC、VB、java或C等编程语言,实现请求分页存储管理页面Optimal、FIFO、LRU调度算法。 |
【实验环境】(含主要设计设备、器材、软件等) VC、VB、java或C等编程语言 windows xp |
【实验步骤、过程】(含原理图、流程图、关键代码,或实验过程中的记录、数据等) 1.利用随机数产生一个指令序列,共320条指令。其地址按下述原则生成: ①50%的指令是顺序执行的; ②25%的指令是均匀分布在前地址部分; ③25%的指令是均匀分布在后地址部分; 2.指令序列变换成页地址流 设:页面大小为1K; 用户虚存容量为32K。 在用户虚存中,按每1K存放10条指令排列虚存地址,即320条指令在虚存中的存放方式为: 第0条—第9条指令为第0页(对应虚存地址为[0,9]); 第10条—第19条指令为第1页(对应虚存地址为[10,19]); 310条—第319条指令为第31页(对应虚存地址为[310,319]); 按以上方式,用户指令可组成32页。 3.计算并输出下述各种算法在不同内存容量(用户内存容量为4页到32页)下的缺页率。 a) OPT最佳淘汰算法: OPT.java源代码: package instructions.algorithm; import instructions.*; /** * The implementation of "Optimal" algorithm. * @author DigitalSonic */ public class OPT extends Algorithm { /** * Creates a new instance of OPT */ public OPT(int UserPagesCount,SequenceSeq, VirtualMemory VM) { super(UserPagesCount, Seq, VM); this.name = "OPT"; } /** * Replace a page with the new one. * @param NewPage The page ready to be inserted. */ public void replacePage(Page NewPage) { if (VM.getCurrentEmptyIndex() != VM.UserPagesCount) { VM.getUserPages()[VM.getCurrentEmptyIndex()]=NewPage; VM.setCurrentEmptyIndex(VM.getCurrentEmptyIndex() + 1); } else { VM.getUserPages()[findUnusedPage(VM.getUserPages(), NewPage.getVisitTime())] = NewPage; } } private int findUnusedPage(Page[] Pages, int CurrentInstruction) { int[] Pos = new int[Pages.length]; Page page; for (int m = 0; m < Pages.length; m++) { Pos[m] = 0; page = Pages[m]; for (int n = CurrentInstruction; n < seq.getList().size(); n++) { if (page == VM.getPages()[Integer.parseInt(seq.getList().get(n).getName()) / Page.PAGE_SIZE]) { Pos[m]=n; break; } } } return findMax(Pos); } private int findMax(int[] Count) { int Max = 0; int Index = 0; for (int i = 0; i < Count.length; i++) { if (Count[i] > Max) { Max = Count[i]; Index = i; } } return Index; } } b) FIFO先进先出的算法: FIFO.java源代码: package instructions.algorithm;import instructions.*; /** * The implementation of "First In First Out" algorithm. * @author DigitalSonic */ public class FIFO extends Algorithm { /** * Creates a new instance of FIFO */ public FIFO(int UserPagesCount, Sequence Seq, VirtualMemory VM) { super(UserPagesCount, Seq, VM); this.name = "FIFO"; } /** * Replace a page with the new one. * @param NewPage The page ready to be inserted. */ public void replacePage(Page NewPage) { if(VM.getCurrentEmptyIndex()!=VM.UserPagesCount) { VM.getUserPages()[VM.getCurrentEmptyIndex()]=NewPage; VM.setCurrentEmptyIndex(VM.getCurrentEmptyIndex() + 1); } else { VM.getUserPages()[findFIPage(VM.getUserPages())] = NewPage; } } private int findFIPage(Page[] Pages) { int Index = Page.PAGE_SIZE * VirtualMemory.TOTAL_PAGE_COUNT; for (int i = 0; i < Pages.length; i++) { if (Pages[i].getVisitTime() < Index) Index = i; } return Index; } } c) LRU最近最少使用算法: LRU.java源代码: package instructions.algorithm;import instructions.*; /** * The implementation of "Least Recently Used" algorithm. * @author DigitalSonic */ public class LRU extends Algorithm { /** * Creates a new instance of LRU */ public LRU(int UserPagesCount, Sequence Seq, VirtualMemory VM) { super(UserPagesCount, Seq, VM); this.name = "LRU"; } /** * Replace a page with the new one. * @param NewPage The page ready to be inserted.*/ public void replacePage(Page NewPage) { if(VM.getCurrentEmptyIndex()!=VM.UserPagesCount) { VM.getUserPages()[VM.getCurrentEmptyIndex()]=NewPage; VM.setCurrentEmptyIndex(VM.getCurrentEmptyIndex() + 1); } else { VM.getUserPages()[findLRUPage(VM.getUserPages(), NewPage.getVisitTime())] = NewPage; } } private int findLRUPage(Page[] Pages, int CurrentInstruction) { Int[] Count = new int[Pages.length]; Page page; for (int m = 0; m < Pages.length; m++) { Count[m] = 0; page = Pages[m]; for (int n = 0; n < CurrentInstruction && n < seq.getList().size(); n++) { if (page == VM.getPages()[Integer.parseInt(seq.getList().get(n).getName()) / Page.PAGE_SIZE]) Count[m]++; } } return findMin(Count); } private int findMin(int[] Count) { int Min = Page.PAGE_SIZE * VirtualMemory.TOTAL_PAGE_COUNT; int Index = 0; for (int i = 0; i < Count.length; i++) { if (Count[i] < Min) { Min = Count[i]; Index = i; } } return Index; } } |
【实验结果或总结】(对实验结果进行相应分析,或总结实验的心得体会,并提出实验的改进意见) 通过编写程序实现请求分页存储管理页面Optimal、FIFO、LRU调度算法,使我们掌握了虚拟存储管理中有关缺页处理方法等内容,巩固有关虚拟存储管理的教学内容。也了解Windows2000/XP中内存管理机制,掌握页式虚拟存储技术,同时也理解了内存分配原理,特别是以页面为单位的虚拟内存分配方法。 这次实验使我们在理论的基础上时间运用了我们所学的知识,提高了我们的动手能力. 指导教师签名: 20 年 月 日 |
08-21
05-06
9240
12-16
4694