protected override MemBlock GetFirstMem(out int?dispage, int[] cmd = null, int current = -1) { MemBlock block = blocks.Max(); dispage = block.Page; return(block); }
protected override MemBlock GetFirstMem(out int?dispage, int[] cmds = null, int current = 0) { //FIFO状态下,新装入的内存永远在最后一位,最早装入的永远在最前位 MemBlock block = blocks[0]; dispage = block.Page; blocks.RemoveAt(0); blocks.Add(block); return(block); }
protected virtual MemBlock GetEmptyMem() { if (IsFull) { throw new Exception("Memory is full, no empty block"); } var newBlock = new MemBlock(); blocks.Add(newBlock); IsFull = blocks.Count >= count; return(newBlock); }
/// <summary> /// 使用给定指令序列和内存块数进行制定模式的测试 /// </summary> /// <param name="mc">内存块数</param> /// <param name="cmd">指令序列</param> /// <param name="mode">调度模式</param> /// <returns>缺页率</returns> public static double RunTest(int mc, int[] cmd, DispatchMode mode) { List <PageItem> table = new List <PageItem>(); //页表 MemoryList memory = MemoryListFactory.Create(mc, mode); //初始化内存和调度模式 for (int i = 0; i < cmd.Length / 10; i++) { table.Add(new PageItem() { Page = i }); } var accCount = 0; //命中次数 var failCount = 0; //缺页次数 for (int i = 0; i < cmd.Length; i++) { var next = table[cmd[i] / 10]; if (next.InMemory) { //指令在内存中,命中 accCount++; if (mode == DispatchMode.LRU) { //若使用LRU调度模式,清空内存驻留时间 var mem = memory.GetMemByPage(next.Page); mem.Time = 0; } } else { //指令不在内存中,缺页中断,调页 failCount++; MemBlock mem = null; //可用内存单元 int? dis = null; //移出页页号 mem = memory.GetMem(out dis, cmd, i); next.InMemory = true; mem.Page = next.Page; //目标页进入内存 mem.Time = 0; //清空内存驻留时间 if (dis != null) { //若有页被调出,将其在页表中的标志位设置false table[(int)dis].InMemory = false; } } memory.Refresh(); //刷新内存状态 #if DEBUG Console.WriteLine("当前指令 -> {0}", cmd[i]); memory.Show(); #endif } return(failCount * 100.0 / (failCount + accCount)); }