public void RunningProcess(PCB QueueName) { int icount = QueueName.CurrentInstruction; double timsplice = double.Parse(Box_Time_Break.Text) / 1000.0; double rmtime = QueueName.PInstructions[icount].IRemainTime; Box_Processing.Text = QueueName.PName; Box_needtime.Text = Convert.ToString(rmtime); Box_timecount.Text = Convert.ToString(tcountsum); #region 处理就绪队列 AddLog(QueueName.PName + "\tC\t\t" + QueueName.CurrentInstruction + "\t\t" + QueueName.PInstructions[icount].IRemainTime + "\t\t"); if (rmtime <= timsplice)//一次完成 { QueueName.PInstructions[icount].IRemainTime = 0; } else { QueueName.PInstructions[icount].IRemainTime = rmtime - timsplice; QueueName.CurrentInstruction = icount; } #endregion #region 处理其他队列 for (int i = 0; i < InputQueue.Count(); i++)//每个都要处理 { AddLog(InputQueue[i].PName + "\tI\t\t" + InputQueue[i].CurrentInstruction + "\t\t" + InputQueue[i].PInstructions[icount].IRemainTime + "\t\t"); if (InputQueue[i].PInstructions[InputQueue[i].CurrentInstruction].IRemainTime <= timsplice)//一次完成 { InputQueue[i].PInstructions[InputQueue[i].CurrentInstruction].IRemainTime = 0; } else//慢慢来 { InputQueue[i].PInstructions[InputQueue[i].CurrentInstruction].IRemainTime -= timsplice; } } for (int i = 0; i < OutputQueue.Count(); i++)//每个都要处理 { AddLog(OutputQueue[i].PName + "\tO\t\t" + OutputQueue[i].CurrentInstruction + "\t\t" + OutputQueue[i].PInstructions[icount].IRemainTime + "\t\t"); if (OutputQueue[i].PInstructions[OutputQueue[i].CurrentInstruction].IRemainTime <= timsplice)//一次完成 { OutputQueue[i].PInstructions[OutputQueue[i].CurrentInstruction].IRemainTime = 0; } else//慢慢来 { OutputQueue[i].PInstructions[OutputQueue[i].CurrentInstruction].IRemainTime -= timsplice; } } for (int i = 0; i < OtherQueue.Count(); i++)//每个都要处理 { AddLog(OtherQueue[i].PName + "\tW\t\t" + OtherQueue[i].CurrentInstruction + "\t\t" + OtherQueue[i].PInstructions[icount].IRemainTime + "\t\t"); if (OtherQueue[i].PInstructions[OtherQueue[i].CurrentInstruction].IRemainTime <= timsplice)//一次完成 { OtherQueue[i].PInstructions[OtherQueue[i].CurrentInstruction].IRemainTime = 0; } else//慢慢来 { OtherQueue[i].PInstructions[OtherQueue[i].CurrentInstruction].IRemainTime -= timsplice; } } //这次循环结束的,统一这里重新发配,当场发配会导致重复消耗时间,比如一个C结束变成了I,结果这一时间片就直接减了 if (QueueName.PInstructions[icount].IRemainTime == 0) { SortOrder(QueueName); } else { SortFirst(QueueName); } int tempt1 = InputQueue.Count(); for (int i = 0; i < tempt1; i++) //每个都要处理 { if (InputQueue[i].PInstructions[InputQueue[i].CurrentInstruction].IRemainTime == 0) //一次完成 { SortOrder(InputQueue[i]); //重新分配 tempt1 = InputQueue.Count(); //重新分配后上限改变 i = 0; //并且我们要重头开始,直到这里没有待分配 } } tempt1 = OutputQueue.Count(); for (int i = 0; i < tempt1; i++) //每个都要处理 { if (OutputQueue[i].PInstructions[OutputQueue[i].CurrentInstruction].IRemainTime == 0) //一次完成 { SortOrder(OutputQueue[i]); //重新分配 tempt1 = OutputQueue.Count(); i = 0; } } tempt1 = OtherQueue.Count(); for (int i = 0; i < OtherQueue.Count(); i++) //每个都要处理 { if (OtherQueue[i].PInstructions[OtherQueue[i].CurrentInstruction].IRemainTime == 0) //一次完成 { SortOrder(OtherQueue[i]); //重新分配 tempt1 = OtherQueue.Count(); i = 0; } } #endregion }
public void AddList(List <PCB> QueueList, string Name, List <Instructions> Instruct, int current) { PCB TmpQueue = new PCB(Name, Instruct, current); QueueList.Add(TmpQueue); }
private void Button_OpenFile_Click(object sender, EventArgs e) { ListBox_ReadyQueue.Items.Clear(); // 清除列表 AllQueue = new List <PCB>(); // 初始化全部进程队列 ReadyQueue = new List <PCB>(); ReserveQueue = new List <PCB>(); InputQueue = new List <PCB>(); OutputQueue = new List <PCB>(); OtherQueue = new List <PCB>(); OpenFileDialog OFD = new OpenFileDialog();//获取文件路径并读取 OFD.ShowDialog(); string Path = OFD.FileName; Console.WriteLine(Path);//输出屏幕 if (Path != String.Empty) { StreamReader SR = new StreamReader(Path, Encoding.Default); string Content = @""; string ProcessName = @""; while ((Content = SR.ReadLine()) != null) { if (Content.StartsWith("P") || Content.StartsWith("p")) { ProcessName = Content; ListBox_ReadyQueue.Items.Add(ProcessName); InsList = new List <Instructions>(); } else if (Content.StartsWith("H")) { /* * 这一部分处理最后获取的结束指令,代表进程结束,将指令列表添加进PCB列表 * 从文件中读取下列指令: * IName: 指令类型 * IRuntime:指令运行时间 * IRemainTime:指令剩余运行时间 */ char IName = char.Parse(Content.Substring(0, 1)); double IRunTime = double.Parse(Content.Substring(1, 2)); double IRemainTime = double.Parse(Content.Substring(1, 2)); // 指令生成 Instructions Ins = new Instructions(IName, IRunTime, IRemainTime); // 往指令列表中添加指令 InsList.Add(Ins); /* * 进程生成 * 进程初始化:进程名称,进程指令列表,指令索引初始化 */ PCB PCB_P = new PCB(ProcessName, InsList, 0); // 往进程表中添加进程 AllQueue.Add(PCB_P); } else { /* * 这一部分主要处理非 P & H 开头的指令 * 处理过程同上... */ char IName = char.Parse(Content.Substring(0, 1)); double IRunTime = double.Parse(Content.Substring(1, 2)); double IRemainTime = double.Parse(Content.Substring(1, 2)); Instructions Ins = new Instructions(IName, IRunTime, IRemainTime); InsList.Add(Ins); } Button_Start.Enabled = true; Button_OpenFile.Enabled = false; } } }
public void DeleteQueue(ListBox ListBoxName, PCB QueueName) { ListBoxName.Items.Remove(QueueName.PName); }
public void SortOrder(PCB QueueName) { QueueName.CurrentInstruction++; //先转到下一条指令 //先来个清理门户 int temp1 = ReserveQueue.Count(); //防止删除后越界 for (int i = 0; i < temp1; i++) { if (QueueName.PName == ReserveQueue[i].PName) { ReserveQueue.Remove(ReserveQueue[i]); temp1 = ReserveQueue.Count(); } } temp1 = ReadyQueue.Count(); for (int i = 0; i < temp1; i++) { if (QueueName.PName == ReadyQueue[i].PName) { ReadyQueue.Remove(ReadyQueue[i]); temp1 = ReadyQueue.Count(); } } temp1 = InputQueue.Count(); for (int i = 0; i < temp1; i++) { if (QueueName.PName == InputQueue[i].PName) { InputQueue.Remove(InputQueue[i]); temp1 = InputQueue.Count(); } } temp1 = OutputQueue.Count(); for (int i = 0; i < temp1; i++) { if (QueueName.PName == OutputQueue[i].PName) { OutputQueue.Remove(OutputQueue[i]); temp1 = OutputQueue.Count(); } } temp1 = OtherQueue.Count(); for (int i = 0; i < temp1; i++) { if (QueueName.PName == OtherQueue[i].PName) { OtherQueue.Remove(OtherQueue[i]); temp1 = OtherQueue.Count(); } } int icount = QueueName.CurrentInstruction; if (icount < QueueName.PInstructions.Count()) { switch (QueueName.PInstructions[icount].IName.ToString())//分辨指令类型 { #region CPU计算 case "C": AddList(ReadyQueue, QueueName.PName, QueueName.PInstructions, QueueName.CurrentInstruction); //放入就绪队列 break; #endregion #region 输入 case "I": AddList(InputQueue, QueueName.PName, QueueName.PInstructions, QueueName.CurrentInstruction); //放入输入队列 break; #endregion #region 输出 case "O": AddList(OutputQueue, QueueName.PName, QueueName.PInstructions, QueueName.CurrentInstruction); //放入输入队列 break; #endregion #region 等 case "W": AddList(OtherQueue, QueueName.PName, QueueName.PInstructions, QueueName.CurrentInstruction); //放入输入队列 break; #endregion #region 结束 case "H": //下次再说 if (ReadyQueue.Count() + InputQueue.Count() + OutputQueue.Count() + OtherQueue.Count() == 0) { Box_Processing.Text = ""; timer1.Enabled = false; tcountsum = 1; Button_Start.Enabled = false; Button_OpenFile.Enabled = true; Box_needtime.Text = ""; Box_timecount.Text = ""; MessageBox.Show("所有进程完成"); } break; #endregion } F5(); } }