/// <summary> /// 恢复设置启用状态 /// </summary> /// <param name="type">算法类型</param> private void EnableState(Models.ArithmeticType type) { TimeSetting_GroupBox.Enabled = true; OtherSettings_GroupBox.Enabled = true; switch (type) { case Models.ArithmeticType.FIFO: FIFO_Start.Enabled = true; FIFO_Pause.Enabled = false; FIFO_Result.Visible = true; panel_ControlLRU.Enabled = true; panel_ControlOPT.Enabled = true; panel_ControlAll.Enabled = true; if (AllThread_Flag && (!Thread_LRU.IsAlive && !Thread_OPT.IsAlive)) //适用于全部算法线程启用时,判断所有线程是否结束 { ALL_Start.Enabled = true; ALL_Pause.Enabled = false; } break; case Models.ArithmeticType.LRU: LRU_Start.Enabled = true; LRU_Pause.Enabled = false; LRU_Result.Visible = true; panel_ControlFIFO.Enabled = true; panel_ControlOPT.Enabled = true; panel_ControlAll.Enabled = true; if (AllThread_Flag && (!Thread_FIFO.IsAlive && !Thread_OPT.IsAlive)) { ALL_Start.Enabled = true; ALL_Pause.Enabled = false; } break; case Models.ArithmeticType.OPT: OPT_Start.Enabled = true; OPT_Pause.Enabled = false; OPT_Result.Visible = true; panel_ControlFIFO.Enabled = true; panel_ControlLRU.Enabled = true; panel_ControlAll.Enabled = true; if (AllThread_Flag && (!Thread_FIFO.IsAlive && !Thread_LRU.IsAlive)) { ALL_Start.Enabled = true; ALL_Pause.Enabled = false; } break; } InputText.Enabled = true; RandomGenerate.Enabled = true; }
/// <summary> /// 禁用时间设置以及其他设置 /// </summary> private void DisableState(Models.ArithmeticType type) { TimeSetting_GroupBox.Enabled = false; OtherSettings_GroupBox.Enabled = false; InputText.Enabled = false; RandomGenerate.Enabled = false; switch (type) { //将输入序列、随机生成序列按钮、时间设置组、杂项设置组以及除了本算法之外的控制按钮全部禁用 case Models.ArithmeticType.FIFO: FIFO_Start.Enabled = false; FIFO_Pause.Enabled = true; FIFO_Result.Visible = false; panel_ControlLRU.Enabled = false; panel_ControlOPT.Enabled = false; panel_ControlAll.Enabled = false; break; case Models.ArithmeticType.LRU: LRU_Start.Enabled = false; LRU_Pause.Enabled = true; LRU_Result.Visible = false; panel_ControlFIFO.Enabled = false; panel_ControlOPT.Enabled = false; panel_ControlAll.Enabled = false; break; case Models.ArithmeticType.OPT: OPT_Start.Enabled = false; OPT_Pause.Enabled = true; OPT_Result.Visible = false; panel_ControlFIFO.Enabled = false; panel_ControlLRU.Enabled = false; panel_ControlAll.Enabled = false; break; case Models.ArithmeticType.All: ALL_Start.Enabled = false; ALL_Pause.Enabled = true; FIFO_Result.Visible = false; LRU_Result.Visible = false; OPT_Result.Visible = false; panel_ControlFIFO.Enabled = false; panel_ControlLRU.Enabled = false; panel_ControlOPT.Enabled = false; break; } }
/// <summary> /// 根据输入地址数组创建新的表格表头 /// </summary> private void UpdateDataGrid(Models.ArithmeticType type) { DataGridView DGV = new DataGridView(); switch (type) { case Models.ArithmeticType.FIFO: DGV = dataGridView_FIFO; break; case Models.ArithmeticType.LRU: DGV = dataGridView_LRU; break; case Models.ArithmeticType.OPT: DGV = dataGridView_OPT; break; case Models.ArithmeticType.All: UpdateDataGrid(Models.ArithmeticType.FIFO); UpdateDataGrid(Models.ArithmeticType.LRU); UpdateDataGrid(Models.ArithmeticType.OPT); return; } //除了第一列(页框号)之外移除其他列 while (DGV.Columns.Count != 1) { DGV.Columns.RemoveAt(1); } //逻辑地址取页号 List <int> tmpList = new List <int>(); foreach (var item in InputAddresses) { tmpList.Add(Helper.GetPageBlock(item)); } foreach (var item in tmpList) { DataGridViewColumn column = new DataGridViewColumn() { Width = 30, Name = Guid.NewGuid().ToString(), HeaderText = item.ToString("X0"), CellTemplate = new DataGridViewTextBoxCell() }; DGV.Columns.Add(column); } CellsUnselected(); }
/// <summary> /// 向单元格添加提示文本 /// </summary> /// <param name="GDV">需要进行添加的表格</param> /// <param name="type">算法类型</param> /// <param name="flag">是否缺页</param> /// <param name="blockNum">被替换的页框号</param> /// <param name="threadSleepTime">本次线程休眠时间</param> private void AddToolTip2Cells(DataGridView GDV, Models.ArithmeticType type, bool flag, int blockNum, int threadSleepTime) { int count_Type = 0; switch (type) { case Models.ArithmeticType.FIFO: count_Type = count_FIFO; break; case Models.ArithmeticType.LRU: count_Type = count_LRU; break; case Models.ArithmeticType.OPT: count_Type = count_OPT; break; case Models.ArithmeticType.All: break; } StringBuilder sb = new StringBuilder(); if (flag) { sb.Append($"发生缺页,"); if (blockNum == 0) { sb.Append("添加了新的一块,"); } else { sb.Append($"替换了{GDV.Rows[blockNum - 1].Cells[count_Type - 2].Value},"); } } else { sb.Append("未发生缺页,"); } sb.Append($"共耗时 {threadSleepTime} ms"); for (int i = 0; i < MemoryBlockNum; i++) { GDV.Rows[i].Cells[count_Type - 1].ToolTipText = sb.ToString(); } }
/// <summary> /// 读取内存树,并将其中的内容更新至表格中 算法执行的步数的列 中 /// </summary> /// <param name="type">算法类型</param> /// <param name="flag">是否缺页</param> /// <param name="blockNum">需要进行高亮的内存块号</param> private void UpdateDGV(Models.ArithmeticType type, bool flag, int blockNum = 0) { //队列中有效的个数 int count = 0; int count_Type = 0; DataGridView DGV = new DataGridView(); Dictionary <int, int> Memory_Type = new Dictionary <int, int>(); switch (type) { case Models.ArithmeticType.FIFO: Memory_Type = GlobalVariable.Memory_FIFO; count_FIFO++; count_Type = count_FIFO - 1; DGV = dataGridView_FIFO; break; case Models.ArithmeticType.LRU: Memory_Type = GlobalVariable.Memory_LRU; count_LRU++; count_Type = count_LRU - 1; DGV = dataGridView_LRU; break; case Models.ArithmeticType.OPT: Memory_Type = GlobalVariable.Memory_OPT; count_OPT++; count_Type = count_OPT - 1; DGV = dataGridView_OPT; break; } foreach (var item in Memory_Type) { if (item.Value != -1) { count++; } } //读取算法内存 for (int i = 0; i < MemoryBlockNum; i++) { if (i < count)//仍旧是有效的值,将内存的值写入表格 { DGV.Rows[i].Cells[count_Type].Value = Memory_Type[i + 1].ToString("X0"); } if (i == blockNum - 1)//为需要高亮的块号 { DGV.Rows[i].Cells[count_Type].Style.ForeColor = Color.Red; } if (count_Type >= 28) { DGV.FirstDisplayedCell = DGV.Rows[i].Cells[count_Type - 28]; } } if (flag)//缺页标志 { DGV.Rows[MemoryBlockNum].Cells[count_Type].Value = "√"; } else { DGV.Rows[MemoryBlockNum].Cells[count_Type].Value = "×"; } }
/// <summary> /// 算法环境初始化,进行内存队列的清空、内存树的初始化以及算法线程的实例化 /// </summary> /// <param name="type">算法的类型</param> private void ArithmeticInit(Models.ArithmeticType type) { switch (type) { case Models.ArithmeticType.FIFO: //重置算法进行步数 count_FIFO = 1; //算法环境初始化 GlobalVariable.MemoryQueue_FIFO = new Queue <MemoryBlock>(); GlobalVariable.Memory_FIFO = new Dictionary <int, int>(); for (int i = 0; i < MemoryBlockNum; i++) { GlobalVariable.Memory_FIFO.Add(i + 1, -1); } //算法线程实例化 Thread_FIFO = new Thread(() => { MakeFIFO(out int pageFaultCount, out int timeSpent); //显示最终结果 FIFO_Result.Invoke(new MethodInvoker(() => { FIFO_Result.Text = $"共用时间:{timeSpent}ms 缺页次数:{pageFaultCount} / {InputAddresses.Count} " + $"缺页率:{pageFaultCount / (double)InputAddresses.Count * 100:f2}%"; EnableState(Models.ArithmeticType.FIFO); })); }); break; case Models.ArithmeticType.LRU: count_LRU = 1; GlobalVariable.MemoryQueue_LRU = new Queue <MemoryBlock>(); GlobalVariable.Memory_LRU = new Dictionary <int, int>(); for (int i = 0; i < MemoryBlockNum; i++) { GlobalVariable.Memory_LRU.Add(i + 1, -1); } Thread_LRU = new Thread(() => { MakeLRU(out int pageFaultCount, out int timeSpent); LRU_Result.Invoke(new MethodInvoker(() => { LRU_Result.Text = $"共用时间:{timeSpent}ms 缺页次数:{pageFaultCount} / {InputAddresses.Count} " + $"缺页率:{pageFaultCount / (double)InputAddresses.Count * 100:f2}%"; EnableState(Models.ArithmeticType.LRU); })); }); break; case Models.ArithmeticType.OPT: count_OPT = 1; GlobalVariable.MemoryQueue_OPT = new Queue <MemoryBlock>(); GlobalVariable.Memory_OPT = new Dictionary <int, int>(); for (int i = 0; i < MemoryBlockNum; i++) { GlobalVariable.Memory_OPT.Add(i + 1, -1); } Thread_OPT = new Thread(() => { MakeOPT(out int pageFaultCount, out int timeSpent); OPT_Result.Invoke(new MethodInvoker(() => { OPT_Result.Text = $"共用时间:{timeSpent}ms 缺页次数:{pageFaultCount} / {InputAddresses.Count} " + $"缺页率:{pageFaultCount / (double)InputAddresses.Count * 100:f2}%"; EnableState(Models.ArithmeticType.OPT); })); }); break; case Models.ArithmeticType.All: //所有算法同时进行,依次进行初始化 ArithmeticInit(Models.ArithmeticType.FIFO); ArithmeticInit(Models.ArithmeticType.LRU); ArithmeticInit(Models.ArithmeticType.OPT); break; } }