Beispiel #1
0
        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();
            }
        }
Beispiel #2
0
        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);
            }
        }