/// <summary> /// 写入闲逛进程 /// </summary> private void Idle() { string path = @"idle.bin"; PCB pcb = new PCB(path, 0); ReadyQueue.Add(pcb); ReadyQueueReset(); }
private void btnProcess_Click(object sender, EventArgs e) { if (thread != null) { string text = sender.ToString(); text = text.Substring(text.Length - 1, 1); string path = @"../data/" + text + ".in"; PCB pcb = new PCB(path, pcbNumber++); ReadyQueue.Add(pcb); //当前进程是闲逛进程时,中断 if (this.pcbNow.Number == 0) { this.PSW = 4; } ReadyQueueReset(); } else { MessageBox.Show("请先开机。"); } }
/// <summary> /// 进程唤醒 /// </summary> private void wake() { PCB pcb = new PCB(@"idle.bin", 0); pcb.next = BlockQueue.pcbStart; while (pcb.next != null) { if (pcb.next.Timer == 0) { if (pcb.next == BlockQueue.pcbStart) { BlockQueue.pcbStart = BlockQueue.pcbStart.next; if (BlockQueue.pcbStart == null) { BlockQueue.pcbEnd = null; } } PCB temp = pcb.next; pcb.next = temp.next; ReadyQueue.Add(temp); } else { pcb = pcb.next; } } if (BlockQueue.pcbEnd != null) { BlockQueue.pcbEnd = pcb; } //当前进程是闲逛进程时,中断 if (this.pcbNow.Number == 0) { this.PSW = 4; } }
//249528401 /// <summary> /// CPU /// </summary> private void CPU() { if (ReadyQueue.pcbStart == null) { this.PSW = 4; Idle(); } while (true) { if (this.PSW != 0) { #region 时间片到,进程调度 PSW=1 if (this.PSW == 1) { this.labCommand.Text = "中断,时间片到"; this.pcbNow.DR = DR; this.pcbNow.PC = PC; ReadyQueue.Add(this.pcbNow); this.PSW = 4; } #endregion #region 唤醒阻塞进程 PSW=2 PSW=3 else if (this.PSW == 2 || this.PSW == 3) { this.labCommand.Text = "中断,唤醒进程"; wake(); if (this.PSW != 4) { this.PSW -= 2; } } #endregion #region 序执行软中断,撤销进程,进程调度 PSW>=4 else if (this.PSW >= 4) { this.pcbNow = null; this.labCommand.Text = "中断,进程调度"; if (ReadyQueue.pcbStart == null) { Idle(); } this.pcbNow = ReadyQueue.Get(); this.PC = this.pcbNow.PC; this.Timer = TIME; this.DR = this.pcbNow.DR; this.pcbNow.State = emState.operation; this.PSW -= 4; } #endregion } else { #region 取PC指令,放入IR寄存器 this.IR = new char[4]; this.IR[0] = this.pcbNow.Program[this.PC]; this.IR[1] = this.pcbNow.Program[this.PC + 1]; this.IR[2] = this.pcbNow.Program[this.PC + 2]; this.IR[3] = this.pcbNow.Program[this.PC + 3]; #endregion #region pc++ this.PC += 4; #endregion #region 执行IR指令 if (Regex.IsMatch(new string(this.IR), @"x=\d;") == true) { this.DR = Convert.ToInt32(IR[2]) - 48; } else if (Regex.IsMatch(new string(this.IR), @"x\+\+;") == true) { this.DR++; } else if (Regex.IsMatch(new string(this.IR), @"x--;") == true) { this.DR--; } else if (Regex.IsMatch(new string(this.IR), @"![AB]\d;") == true) { this.pcbNow.Event = this.IR[1]; this.pcbNow.Timer = Convert.ToInt32(IR[2]) - 48; this.pcbNow.DR = DR; this.pcbNow.PC = PC; BlockQueue.Add(pcbNow); this.PSW = 4; } else if (Regex.IsMatch(new string(this.IR), @"end.") == true) { string path = Path.ChangeExtension(this.pcbNow.path, "out"); using (FileStream stream = new FileStream(path, FileMode.Create)) { string str = Path.GetFullPath(path) + "\r\n" + "x=" + this.DR.ToString(); byte[] buffer = Encoding.UTF8.GetBytes(str); stream.Write(buffer, 0, buffer.Length); } this.labEndResult.Text = DR.ToString(); this.PSW = 4; } else if (Regex.IsMatch(new string(this.IR), @"gob;") == true) { this.PC = 0; } #endregion #region 时间片--,阻塞进程的时间-- this.Timer--; if (this.Timer == 0 && this.PSW == 0) { this.PSW = 1; } blockTime(); #endregion #region 页面更新 this.labNumber.Text = pcbNow.Number.ToString(); this.labRunning.Text = pcbNow.Name.ToString(); this.labTimer.Text = this.Timer.ToString(); this.labCommand.Text = new string(IR); this.labResult.Text = this.DR.ToString(); #endregion } ReadyQueueReset(); BlockQueueReset(); //延时; Thread.Sleep(SLEEP); } }