Beispiel #1
0
        // 进程唤醒
        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;
            }
        }
Beispiel #2
0
        //闲逛进程
        private void Idle()
        {
            string path = @"idle.bin";
            PCB    pcb  = new PCB(path, 0);

            pcb.pos = 0;
            ReadyQueue.Add(pcb);
            ReadyQueueReset();
        }
Beispiel #3
0
 public static void Add(PCB pcb)
 {
     pcb.State = emState.block;
     if (pcbEnd == null && pcbStart == null)
     {
         pcbStart = pcb;
         pcbEnd   = pcb;
     }
     else
     {
         pcbEnd.next = pcb;
         pcbEnd      = pcb;
     }
     pcbEnd.next = null;
 }
Beispiel #4
0
 public static void Add(PCB pcb)    //将进程中就绪的文件添加到就绪队列
 {
     pcb.State = emState.ready;
     if (pcbEnd == null && pcbStart == null)    //若就绪队列为空就添加到队首
     {
         pcbStart = pcb;
         pcbEnd   = pcb;
     }
     else    //否则添加到就绪队列文件末尾的下一个
     {
         pcbEnd.next = pcb;
         pcbEnd      = pcb;
     }
     pcbEnd.next = null;     //将就绪队列的对位置空
 }
Beispiel #5
0
 // 更新就绪队列
 private void ReadyQueueReset()
 {
     lock (this)
     {
         listView_readyQueue.Items.Clear();
         ListViewItem item = null;
         PCB          pcb  = ReadyQueue.pcbStart;
         while (pcb != null)
         {
             item      = new ListViewItem();
             item.Text = pcb.Name;
             item.SubItems.Add(pcb.Number.ToString());
             listView_readyQueue.Items.Add(item);
             pcb = pcb.next;
         }
     }
 }
Beispiel #6
0
        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("请先开机。");
            }
        }
Beispiel #7
0
 public static PCB Get()
 {
     if (pcbEnd == null && pcbStart == null)
     {
         return(null);
     }
     else   //就绪队列不为空时就将队首取出进程
     {
         PCB pcbTemp = pcbStart;
         pcbStart = pcbStart.next;
         if (pcbStart == null)
         {
             pcbEnd = null;                     //若取出队首后为空就将队列置空
         }
         return(pcbTemp);
     }
 }
Beispiel #8
0
        private void blockTime()
        {
            PCB pcb = BlockQueue.pcbStart;

            while (pcb != null)
            {
                pcb.Timer--;
                if (pcb.Timer == 0)
                {
                    //防止陷入死循环
                    if (this.PSW == 1 || this.PSW == 0 || this.PSW == 4)
                    {
                        this.PSW += 2;
                    }
                }
                pcb = pcb.next;
            }
        }
Beispiel #9
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);
            }
        }
Beispiel #10
0
 public static void End()
 {
     pcbStart = null;
     pcbEnd   = null;
 }
Beispiel #11
0
 static BlockQueue()
 {
     pcbStart = null;
     pcbEnd   = null;
 }
Beispiel #12
0
 static ReadyQueue()   //就绪队列初始化
 {
     pcbStart = null;
     pcbEnd   = null;
 }