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

📄 scheduling.cs

📁 java csharp program that ask users a input(process arrival time, burst time and priority)
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace CPUScheduling
{
    public partial class Scheduling : Form
    {
        List<CPU> ps = new List<CPU>();
        List<CPU> sysS = new List<CPU>();
        List<CPU> intS = new List<CPU>();
        List<CPU> batS = new List<CPU>();
        List<CPU> allS = new List<CPU>();
        Input input;
        int timer;
        CPU cpu = new CPU();
        CPU temp = new CPU();
        public Scheduling()
        {
            InitializeComponent();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            input = new Input();
            input.ShowDialog();
            bOutput.Enabled = false;
            button1.Enabled = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Output output = new Output(allS, timer);
            output.ShowDialog();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        void sorter(List<CPU> c)
        {
            CPU temp = new CPU();
            for (int x = 0; x < c.Count; x++)
            {
                for (int y = 0; y < c.Count; y++)
                {
                    if (c[x].bt < c[y].bt)
                    {
                        temp = c[x];
                        c[x] = c[y];
                        c[y] = temp;
                    }

                    if (c[x].bt == c[y].bt && c[x].at < c[y].at)
                    {
                        temp = c[x];
                        c[x] = c[y];
                        c[y] = temp;
                    }
                }
            }
        }

        void layouter(TableLayoutPanel t, List<CPU> c)
        {
            t.Controls.Clear();
            foreach (CPU cpu in c)
            {
                TextBox tb = new TextBox();
                tb.Multiline = true;
                tb.Margin = new Padding(0);
                tb.Text = cpu.id.Substring(0, 1) + System.Environment.NewLine + cpu.id.Substring(1);
                tb.BackColor = cpu.color;
                tb.ForeColor = Color.White;
                tb.WordWrap = true;
                tb.Dock = DockStyle.Fill;
                t.Controls.Add(tb);
            }
            t.Refresh();
        }


        private void button1_Click(object sender, EventArgs e) {
            ps = input.list;
            cpu = null;
            timer = 0;
            int tq = int.Parse(nud.Value.ToString());
            lv.Items.Clear();

            while (ps.Count != 0 || sysS.Count != 0 || intS.Count != 0 || batS.Count != 0 || cpu != null) {
                for (int x = 0; x < ps.Count;x++ ) {
                    CPU p = ps[x];
                    if (p.at == timer) {
                        switch (p.type) {
                            case Processtype.System:
                                sysS.Add(p);
                                rtb.Text += p.id + " entered System" + System.Environment.NewLine;
                                break;
                            case Processtype.Interactive:
                                intS.Add(p);
                                rtb.Text += p.id + " entered Interactive" + System.Environment.NewLine;
                                sorter(intS);
                                break;
                            case Processtype.Batch:
                                batS.Add(p);
                                rtb.Text += p.id + " entered Batch" + System.Environment.NewLine;
                                sorter(batS);
                                break;
                        }
                        x--;
                        ps.Remove(p);
                    }
                }

                layouter(tSystem, sysS);
                layouter(tInteractive, intS);
                layouter(tBatch, batS);
                Thread.Sleep(250);

                if (cpu != null) {
                    cpu.bt--;
                    if (cpu.bt == 0) {
                        cpu.tt = timer - cpu.at;
                        cpu.wt = cpu.tt - cpu.bbt;
                        allS.Add(cpu);
                        rtb.Text += cpu.id + " has finished executing" + System.Environment.NewLine;
                        ListViewItem li = new ListViewItem(cpu.id);
                        li.BackColor = cpu.color;
                        lv.Items.Add(li);
                        lv.Refresh();
                        cpu = null;

                        label9.Text = "CPU";
                        label9.ForeColor = Color.White;
                        label9.Refresh();

                    } else if (cpu.type == Processtype.System) {
                        cpu.rq++;
                        if (cpu.rq == tq) {
                            cpu.rq = 0;
                            batS.Add(cpu);
                            rtb.Text += cpu.id + " time quantum finished" + System.Environment.NewLine;
                            cpu = null;

                            label9.Text = "CPU";
                            label9.ForeColor = Color.White;
                            label9.Refresh();
                        }
                    }
                }

                textBox1.Text = timer.ToString();
                textBox1.Refresh();
                timer++;

                if (sysS.Count != 0) {
                    if (cpu == null) {
                        cpu = sysS[0];
                        label9.Text = cpu.id;
                        label9.ForeColor = cpu.color;
                        label9.Refresh();
                        rtb.Text += cpu.id + " occupied the CPU" + System.Environment.NewLine;
                        sysS.RemoveAt(0);
                        layouter(tSystem, sysS);
                    }

                } else if (intS.Count != 0) {
                    if (cpu == null) {
                        cpu = intS[0];
                        label9.Text = cpu.id;
                        label9.ForeColor = cpu.color;
                        label9.Refresh();
                        rtb.Text += cpu.id + " occupied the CPU" + System.Environment.NewLine;
                        intS.RemoveAt(0);
                        layouter(tInteractive, intS);
                    } else {
                        if (cpu.type == Processtype.Interactive && cpu.bt > intS[0].bt) {
                            temp = cpu;

                            cpu = intS[0];
                            label9.Text = cpu.id;
                            label9.ForeColor = cpu.color;
                            rtb.Text += temp.id + " was replaced by " +cpu.id + System.Environment.NewLine;
                            intS[0] = temp;
                            layouter(tInteractive, intS);
                        }
                    }
                } else if (batS.Count != 0) {
                    if (cpu == null) {
                        cpu = batS[0];
                        label9.Text = cpu.id;
                        label9.ForeColor = cpu.color;
                        label9.Refresh();
                        rtb.Text += cpu.id + " occupied the CPU" + System.Environment.NewLine;
                        batS.RemoveAt(0);
                        layouter(tBatch, batS);
                    }
                }
                rtb.Focus();
                rtb.SelectionStart = rtb.Text.Length;
                rtb.ScrollToCaret();
                rtb.Refresh();
                Thread.Sleep(500);
            }
            rtb.Text += "Simulation Completed";
            bOutput.Enabled = true;
        }

      

        private void rtb_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }
    }
}

⌨️ 快捷键说明

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