// 进程唤醒 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; } }
private List <Label> mmm = new List <Label>();//显示内存 private void Button_on_Click(object sender, EventArgs e) { if (button_on.Text.ToString().Equals("开机")) //开机 { button_on.Text = "关机"; thread = new Thread(CPU); thread.IsBackground = true; thread.Start(); mm = new MemoryManage(); mmm[0].BackColor = Color.FromArgb(255, 0, 0); mmm[1].BackColor = Color.FromArgb(255, 0, 0); } else //关机 { button_on.Text = "开机"; if (thread != null) { thread.Abort(); } for (int i = 0; i < 128; i++) { mmm[i].BackColor = Color.FromArgb(0, 255, 255); } listView_readyQueue.Items.Clear(); listView_blockQueue.Items.Clear(); ReadyQueue.End(); BlockQueue.End(); } }
//闲逛进程 private void Idle() { string path = @"idle.bin"; PCB pcb = new PCB(path, 0); pcb.pos = 0; ReadyQueue.Add(pcb); ReadyQueueReset(); }
private void Button_process_Click(object sender, EventArgs e) { if (thread != null) { string text = sender.ToString(); text = text.Substring(text.Length - 1, 1); string path = @"../data/" + text + ".in"; int need = 0; //需要的内存 //计算所需要的内存 using (FileStream stream = new FileStream(path, FileMode.Open)) { byte[] buffer = new byte[1024 * 10]; stream.Read(buffer, 0, buffer.Length); String str = Encoding.Default.GetString(buffer).Replace("\n", "").Replace("\r", "").Trim(); for (int i = 0; str[i] != '\0'; i++) { need++; } } //分配内存 int can = mm.Apply(need); //can为已分分区下标 if (can == -1) { MessageBox.Show("内存不足!"); return; } PCB pcb = new PCB(path, pcbNumber++); for (int i = mm.used[can].begin / 4; i < (mm.used[can].size + mm.used[can].begin) / 4; i++) { mmm[i].BackColor = Color.FromArgb(255, 0, 0); } pcb.pos = can; ReadyQueue.Add(pcb); //当前进程是闲逛进程时,中断 if (this.pcbNow.Number == 0) { this.PSW = 4; } ReadyQueueReset(); } else { MessageBox.Show("请先开机。"); } }
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.labelInstruction.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.labelInstruction.Text = "唤醒进程"; wake(); if (this.PSW != 4) { this.PSW -= 2; } } #endregion #region 序执行软中断,撤销进程,进程调度 PSW>=4 else if (this.PSW >= 4) { this.pcbNow = null; this.labelInstruction.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.labelLastResult.Text = "x = " + DR.ToString(); this.PSW = 4; for (int i = mm.used[this.pcbNow.pos].begin / 4; i < (mm.used[this.pcbNow.pos].size + mm.used[this.pcbNow.pos].begin) / 4; i++) { mmm[i].BackColor = Color.FromArgb(0, 255, 255); } mm.recycle(this.pcbNow.pos); } 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.labelNum.Text = pcbNow.Number.ToString(); this.labelRunning.Text = pcbNow.Name.ToString(); this.labelTimer.Text = this.Timer.ToString(); this.labelInstruction.Text = new string(IR); this.labelResult.Text = "x = " + this.DR.ToString(); #endregion } ReadyQueueReset(); BlockQueueReset(); //延时; Thread.Sleep(SLEEP); } }