⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 process.cs

📁 os 课程设计不含文档 123456789
💻 CS
字号:
using System;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
using System.IO;

namespace os
{
    public class process:jincheng
    {
        //public Form1 osform = new Form1();
        public ListView ListView, BlockView, dev_listview;//就绪队列 阻塞队列 设备列表
        public int num;
        public devise devise_;
        public queue readyqueue;                  //就绪队列
        //public ArrayList jiuxuqueue;//动态数组 就绪队列、、、、、、、、、、、
        public queue blockqueue;                  //阻塞队列
        public ram ram_;
        public cpu cpu_;
        public pcb[] pcb_;                  //进程pcb
        public int pcbcount;                  //pcb的数量
        public pcb currentpro;                      //当前进程
        public string ramshow = "";
        public Label lblram;
        public Label lblramnum;
        public int ProCount;                  //进程运行数量 
        public Queue ReleDevPCB;
        //public int sTime = 0;
        public int runedprocount = 0;                 //当前进程运行数量 
        //public Queue reledevPCB;
        public TextBox txtboxrunpro;//正在执行的进程
        public TextBox txtboxzhiling;//正在执行的指令
        public TextBox txtboxzhjjg;//中间结果
        public ListView listboxout;//输出结果列表
        public pcb xg;
        public cpu CPU
        {
            get
            {
                return cpu_;
            }
        }
        public ram RAM
        {
            get
            {
                return ram_;
            }
        }
        public process(ListView listview1, ListView listview2, ListView listview3, ListView listview4,TextBox textbox6,TextBox textbox7,TextBox textbox8,Label label6)
        {
            //lblram = label1;
            lblramnum = label6;
            ListView = listview2;
            BlockView = listview3;
            dev_listview = listview1;
            listboxout = listview4;
            txtboxrunpro = textbox6;
            txtboxzhiling = textbox7;
            txtboxzhjjg = textbox8;
            readyqueue = new queue();
            blockqueue = new queue();
            ram_ = new ram();
            cpu_ = new cpu(ram_, this);
            devise_ = new devise(dev_listview, BlockView);
            ReleDevPCB = new Queue();
            pcbcount = 10;
            ProCount = 0;
            currentpro = NULL;
            pcb_ = new pcb[pcbcount];
            for (int i = 0; i < pcbcount; i++)
            {   //初始化进程控制块
                pcb_[i].id = i;
                pcb_[i].state = tstate.free;
            }
            //xg = pcb_[0];
            //pcb_[0].state = tstate.used;
            //xg.state = tstate.used;
            //xg.register = 0;
            //xg.name = "xg";
            //xg.ip = 0;
            //xg.size = 16;
            //xg.protabaddr = ram_.assignram(16, out xg.usepagecount);
            //ram_.loaddata(xg.protabaddr, xg.usepagecount, "x=5;x--;x--;end.", 16);
            //currentpro = xg;
            //ProCount = 1;
            //cpu_.PT = xg.protabaddr;
        }
        public bool Gauge(string Code)     //判断指令是否正确
        {
            if (Code.Length == 0 || (Code.Length) % 4 != 0 || (Code[Code.Length - 4].ToString() + Code[Code.Length - 3].ToString() + Code[Code.Length - 2].ToString()) != "END")
            {
                MessageBox.Show("指令错误", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }
        public bool CreatPro(string FullPathName, string FileName, string buffer) //创建进程
        {
            string Codes = buffer.ToUpper();
            if (Gauge(Codes) == false)
                return false;
            //寻找空闲PCB
            pcb NewPro;
            int Size = buffer.Length;
            if (Size < 0)
                return false;
            int FreeIndex = -1;
            for (int i = 0; i < pcbcount; i++)  //
                if (pcb_[i].state == tstate.free)
                {
                    FreeIndex = i;
                    break;
                }
            if (FreeIndex == -1)
            {
                MessageBox.Show("无可用PCB", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            //开始创建
            NewPro = pcb_[FreeIndex];
            pcb_[FreeIndex].state = tstate.used;
            NewPro.protabaddr = ram_.assignram(Size, out NewPro.usepagecount);
            //lblram.Text = ram_.ramshow();
            NewPro.size = Size;
            if (NewPro.usepagecount == 0)   //内存分配失败
                return false;
            bool success = ram_.loaddata(NewPro.protabaddr, NewPro.usepagecount, buffer, Size);
            if (success == false)
                return false;
            NewPro.state = tstate.used;
            NewPro.register = 0;
            NewPro.ip = 0;
            Random rnd = new Random();
            NewPro.youxianji = rnd.Next(1, 6);
            NewPro.fullpath = FullPathName;
            NewPro.name = FileName;
            if (ProCount < 1)  //如果无进程运行
            {
                ProCount++;
                NewPro.prostate = tprostate.runing;
                currentpro = NewPro;
                cpu_.PT = NewPro.protabaddr;
                cpu_.yxj = NewPro.youxianji;
            }
            else
            {
                if (cpu_.yxj >= NewPro.youxianji)
                {
                    ProCount++;
                    NewPro.prostate = tprostate.steady;
                    string ProName = NewPro.name.ToString();
                    ListView.Items.Add(ProName);

                    readyqueue.Add(NewPro);    //加入就绪队列		
                }
                else
                {
                    currentpro.youxianji = cpu_.yxj;
                    currentpro.ip = cpu_.IP;
                    currentpro.register = cpu_.EAX;
                    currentpro.prostate = tprostate.steady;
                    ListView.Items.Add(currentpro.name);
                    readyqueue.Add(currentpro);
                    ListView.Items.RemoveAt(0);
                    cpu_.IP = NewPro.ip;
                    cpu_.EAX = NewPro.register;
                    cpu_.PT = NewPro.protabaddr;
                    cpu_.yxj = NewPro.youxianji;
                    NewPro.prostate = tprostate.runing;//如果优先级高于正在运行的进程则直接调换之 
                }
            }
            return true;
        }
        public void BlockProcess(pcb Pro)//阻塞进程
        {
            Pro.ip = cpu_.IP;
            Pro.register = cpu_.EAX;
            Pro.prostate = tprostate.block;
            blockqueue.Add(Pro);
            BlockView.Items.Add(Pro.name+"         使用设备!");
        }
        public string dqjch()
        {
            string jch = currentpro.name;
            return jch;
        }
        public void DelProcess(pcb Pro)//删除进程
        {
            string info = "进程" + Pro.name.ToString() + " 结果=" + Pro.register.ToString();
            //string Information = DateTime.Now + " 路径 [" + Pro.FullPath + "]" + " 进程名 " + Pro.Name.ToString() + " 结束 " + " 运算结果 = " + Pro.Register.ToString();
            ram_.releaseram(Pro.protabaddr, Pro.usepagecount);
            //lblram.Text = ram_.ramshow();
            StreamWriter strm = new StreamWriter("out.txt", true);
            strm.WriteLine(info);
            strm.Close();
            listboxout.Items.Add(info);
            
            switch (Pro.prostate)
            {
                case tprostate.runing:
                    pcb_[Pro.id].state = tstate.free;
                    Pro.state = tstate.free;
                    if (this.ListView.Items.Count != 0)
                        this.ListView.Items.RemoveAt(0);
                    currentpro = NULL;
                    break;
                case tprostate.steady:
                    readyqueue.Remove(Pro);// 从就绪队列中找到该进程删之
                    Pro.state = tstate.free;  // 该进程所占用的pcb值为free
                    break;
                case tprostate.block:
                    blockqueue.Remove(Pro);// 从阻塞队列中找到该进程删之
                    Pro.state = tstate.free; // 该进程所占用的pcb值为free
                    break;
                default:
                    return;               // 删除出错
            }
            ProCount--;
        }
        public bool WakeUpProcess(pcb Pro)////唤醒进程
        {
            if (Pro.Equals(NULL))
            {
                return false;
            }
            else
            {
                Pro.prostate = tprostate.steady;  //把当前进程状态值为就绪态
                ListView.Items.Add(Pro.name);
                readyqueue.Add(Pro);          //加入就绪队列
                this.BlockView.Items.RemoveAt(0);
                return true;
            }
        }
        public void ChangeProcess()////抢占轮换
        {
            if (readyqueue.Count == 0)
            {
                return;
            }
            currentpro.ip = cpu_.IP;
            currentpro.register = cpu_.EAX;
            currentpro.prostate = tprostate.steady;
            ListView.Items.Add(currentpro.name);
            readyqueue.Add(currentpro);
            ListView.Items.RemoveAt(0);
            currentpro = (pcb)readyqueue.dequeue(readyqueue);

            cpu_.IP = currentpro.ip;
            cpu_.EAX = currentpro.register;
            cpu_.PT = currentpro.protabaddr;
            currentpro.prostate = tprostate.runing;

        }
        public void RunProcess(pcb Pro)//运行进程
        {
            if (Pro.name == null)
                return;
            cpu_.IP = Pro.ip;
            cpu_.EAX = Pro.register;
            cpu_.PT = Pro.protabaddr;
            Pro.prostate = tprostate.runing;
            currentpro = Pro;
            //this.Code();
        }
        public void RequestDeive(char DevType, char UseTime)//申请设备函数
        {
            currentpro.wantdev = char.Parse(DevType.ToString().ToUpper());
            currentpro.wanttime = int.Parse(UseTime.ToString());

            if (currentpro.wanttime == 0)
                return;
            devise_.requestdev(currentpro, this.ListView);

            if (ListView.Items.Count != 0)
                ListView.Items.RemoveAt(0);
            BlockProcess(currentpro);
            currentpro = NULL;
        }
        public void GOperation()
        {
            if (currentpro.id != 0)
            {
                num = cpu_.EAX;
                currentpro.register = num;
            }
            if (readyqueue.Count == 0 && blockqueue.Count == 0)
            {
                ListView.Items.Clear();
                BlockView.Items.Clear();
            }
            devise_.devtimer(ReleDevPCB,this);
            

            for (int i = 0; i < ReleDevPCB.Count; i++)
            {
                WakeUpProcess((pcb)ReleDevPCB.Dequeue());
            }
            if (readyqueue.Count != 0 && currentpro.Equals(NULL))
            {
                RunProcess((pcb)readyqueue.dequeue(readyqueue));
            }
            if (readyqueue.Count != 0 && currentpro.id == 0)
            {
                BlockProcess(currentpro);

                RunProcess((pcb)readyqueue.dequeue(readyqueue));
            }

            if (readyqueue.Count == 0 && currentpro.Equals(NULL))
            {
                RunProcess((pcb)blockqueue.dequeue(blockqueue));
            }
            
        }

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -