Example #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            //if (radioButton1.Checked == true)
            //{
            //    this.listBox1.Items.Clear();
            //    JCB j = new JCB(textBox1.Text, int.Parse(textBox2.Text), int.Parse(textBox3.Text));
            //    readyList.Add(j);
            //    MessageBox.Show("进程已经加入到队列");
            //    this.textBox1.Text = "";
            //    this.textBox2.Text = "";
            //    this.textBox3.Text = "";
            //    foreach (JCB c in readyList)
            //        this.listBox1.Items.Add(c.Print_my());
            //}
            //    if (radioButton2.Checked == true||radioButton3.Checked==true)
            //   {
            this.listBox4.Items.Clear();
            JCB j = new JCB(textBox1.Text, int.Parse(textBox2.Text), int.Parse(textBox3.Text));

            waitList.Add(j);
            MessageBox.Show("进程已经加入到队列");
            this.textBox1.Text = "";
            this.textBox2.Text = "";
            this.textBox3.Text = "";
            foreach (JCB c in waitList)
            {
                this.listBox4.Items.Add(c.Print_my());
            }
            //   }
        }
Example #2
0
 private void setlistbox1()
 {
     if (this.InvokeRequired)
     {
         this.Invoke(new setlistBox(setlistbox1), null);
     }
     else
     {
         this.listBox1.Items.Clear();
         foreach (JCB k in readyList)
         {
             this.listBox1.Items.Add(k.Print_my());
         }
         if (readyList.Find((JCB S) => S.get_state() == "exec") != null)
         {
             JCB f = readyList.Find((JCB S) => S.get_state() == "exec");
             this.listBox1.Items.Remove(f.get_state());
         }
     }
 }
Example #3
0
        public void HRNfunction(object source, System.Timers.ElapsedEventArgs e)
        {
            atimer.Interval = 1000;
            if (readyList.Count == 0 && waitList.Count == 0)
            {
                atimer.Stop();
                MessageBox.Show("所有作业执行完毕");
                clickCount = 0;
                return;
            }
            JCB f;

            for (int i = 0; i <= waitList.Count - 1; i++)
            {
                f = waitList.ElementAt <JCB>(i);
                if (f.get_atime() == clock)
                {
                    readyList.Add(f);
                    waitList.Remove(f);
                    setlistbox4();
                    setlistbox1();
                }
            }

            if (readyList.Count != 0)
            {
                if (readyList.FindAll((JCB t) => t.get_state() == "exec").Count == 0)
                {
                    JCB    Q;
                    int    z  = 0;
                    string s1 = readyList.ElementAt <JCB>(0).get_name();
                    double k  = 100;//等待时间+要求服务时间除以要求服务时间 //等待时间clock -atime,要求服务时间 runtime
                    for (int i = 0; i <= readyList.Count - 1; i++)
                    {
                        Q = readyList.ElementAt <JCB>(i);//此处需要修改
                        z = clock - Q.get_atime() + Q.runtime;
                        if (k < ((1.0) * z / Q.runtime))
                        {
                            k = ((1.0) * z / Q.runtime); s1 = Q.get_name();
                        }
                    }
                    readyList.Find((JCB t) => t.get_name() == s1).set_state("exec");
                }
                JCB T = readyList.Find((JCB t) => t.get_state() == "exec");
                if (T.exeT == T.runtime)
                {
                    int ft = clock;
                    T.set_state("finish");
                    T.set_ftime(ft);
                    T.set_total(ft - T.get_atime());
                    T.set_welght(T.get_total() / T.runtime);
                    // T.set_total(T.get_ftime() - T.runtime);
                    finishList.Add(T);
                    readyList.Remove(T);
                    setlistbox2();
                    setlistbox1();
                    if (readyList.Count == 0 && waitList.Count() == 0)
                    {
                        return;
                    }
                    string s2     = readyList.ElementAt <JCB>(0).get_name();
                    int    mg     = 0;
                    double weight = 100;
                    JCB    P;
                    for (int z = 0; z <= readyList.Count - 1; z++)
                    {
                        P  = readyList.ElementAt <JCB>(z);
                        mg = clock - P.get_atime() + P.runtime;
                        if (weight < ((1.0) * mg / P.runtime))
                        {
                            weight = ((1.0) * mg / P.runtime); s2 = P.get_name();
                        }                                              //
                    }
                    T = readyList.Find((JCB t) => t.get_name() == s2); //
                    if (readyList.Count == 0)
                    {
                        return;
                    }
                    T.set_state("exec");
                }


                JCB h;
                h = readyList.Find((JCB t) => t.get_state() == "exec");
                h.exeT++;
                setlistbox3();
                Console.Write(h.Print_my());
                Console.WriteLine();
            }
            //if (readyList.Count == 0) return;
            clock++;
        }
Example #4
0
        public void SJFfunction(object source, System.Timers.ElapsedEventArgs e)
        {
            /*
             * 如何实现SJF算法:
             * 首先先选取时间最短来执行的,当执个更短的作业到达时将,将原来的作业调出并把状态为置为"wait",将其移动到waitList,如果此
             * 时就绪队列中作业数小于4,则把waitList中的runtime-exeT最小的调入一个进到就绪队列。
             * 如果waitList.count+readyList.count<2,则把所有在等待队列的作业调入到就绪队列,如果runtime==exeT则把其状态位置为"finish"
             * 并从readyList移出,放入finishList。如果就绪队列和等待队列都空了,则算法结束。算法结束需要弹出messageBox。
             */
            atimer.Interval = 1000;
            if (readyList.Count == 0 && waitList.Count == 0)
            {
                atimer.Stop();
                MessageBox.Show("所有作业执行完毕");
                clickCount = 0;
                return;
            }
            JCB f;

            for (int i = 0; i <= waitList.Count - 1; i++)
            {
                f = waitList.ElementAt <JCB>(i);
                if (f.get_atime() == clock)
                {
                    readyList.Add(f);
                    waitList.Remove(f);
                    setlistbox4();
                    setlistbox1();
                }
            }

            if (readyList.Count != 0)
            {
                if (readyList.FindAll((JCB t) => t.get_state() == "exec").Count == 0)
                {
                    JCB Q;
                    int k = 100;
                    for (int i = 0; i <= readyList.Count - 1; i++)
                    {
                        Q = readyList.ElementAt <JCB>(i);
                        if (Q.runtime < k)
                        {
                            k = Q.runtime;
                        }
                    }
                    readyList.Find((JCB t) => t.runtime == k).set_state("exec");
                }
                JCB T = readyList.Find((JCB t) => t.get_state() == "exec");
                if (T.exeT == T.runtime)
                {
                    int ft = clock;
                    T.set_state("finish");
                    T.set_ftime(ft);
                    T.set_total(T.get_ftime() - T.runtime);
                    finishList.Add(T);
                    readyList.Remove(T);
                    setlistbox2();
                    setlistbox1();
                    int time = 100;
                    JCB P;
                    for (int z = 0; z <= readyList.Count - 1; z++)
                    {
                        P = readyList.ElementAt <JCB>(z);
                        if (P.runtime < time)
                        {
                            time = P.runtime;
                        }
                    }
                    T = readyList.Find((JCB t) => t.runtime == time);
                    if (readyList.Count == 0)
                    {
                        return;
                    }
                    T.set_state("exec");
                }


                JCB h;
                h = readyList.Find((JCB t) => t.get_state() == "exec");
                h.exeT++;
                setlistbox3();
                Console.Write(h.Print_my());
                Console.WriteLine();
            }
            //if (readyList.Count == 0) return;
            clock++;
        }
Example #5
0
        public void FCFSfunction(object source, System.Timers.ElapsedEventArgs e)
        {
            /*
             * 如何实现FCFS方法:
             * 首先从就绪列表readyList里每次选取到达时间最短的,然后将执行时间通过调用 System.Timers.ElapsedEventHandler(METHODS);
             * 其中METHODS()满足参数要求(object sender, EventArgs e)。对于这个方法只需修改exeT,判断是否与runtime相等,如果相等
             * 把其状态位置为"finish"并从readyList移出,放入finishList,如果就绪队列空了则结束,算法结束需要弹出messageBox
             */
            //atimer.Interval = 1000;
            //if (readyList.Count == 0) {
            //    atimer.Stop();
            //    MessageBox.Show("所有作业执行完毕");
            //    clickCount = 0;
            //    return;
            //}
            //clock++;
            //int i = 100;
            //string name = "";
            //JCB F;
            //foreach (JCB j in readyList)
            //{
            //    if (j.get_atime() < i)
            //    {
            //        i = j.get_atime();
            //        name = j.get_name();
            //    }
            //}
            //F = readyList.Find((JCB s) => s.get_name() == name);
            //F.set_state("exec");
            //if (F.exeT == F.runtime)
            //{
            //    int f = clock-circle;
            //    F.set_state("finish");
            //    F.set_ftime(f);
            //    F.set_total(f-F.get_atime());
            //    F.set_welght(F.get_ftime()/F.runtime);
            //    finishList.Add(F);
            //    readyList.Remove(F);

            //    atimer.Stop();
            //    if (MessageBox.Show("作业" + F.get_name() + "已完成!", "是否继续", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { atimer.Start(); }
            //    circle++;
            //    setTB("进程名:"+F.get_name()+"作业状态:"+F.get_state()+"作业周转时间:"+F.get_total()+"带权周转系数:"+F.get_welght().ToString());
            //    //atimer.Stop();
            //}
            //else
            //{
            //    F.exeT++;
            //    SetTB(F.get_nameANDexeT());
            //}
            //// this.listBox3.Items.Add(F.get_nameANDexeT());
            //Console.Write(F.get_nameANDexeT());
            //Console.WriteLine();
            atimer.Interval = 1000;
            if (readyList.Count == 0 && waitList.Count == 0)
            {
                atimer.Stop();
                MessageBox.Show("所有作业执行完毕");
                clickCount = 0;
                return;
            }
            JCB f;

            for (int i = 0; i <= waitList.Count - 1; i++)
            {
                f = waitList.ElementAt <JCB>(i);
                if (f.get_atime() == clock)
                {
                    readyList.Add(f);
                    waitList.Remove(f);
                    setlistbox4();
                    setlistbox1();
                }
            }

            if (readyList.Count != 0)
            {
                if (readyList.FindAll((JCB t) => t.get_state() == "exec").Count == 0)
                {
                    JCB Q;
                    int k = 100;
                    Q = readyList.ElementAt <JCB>(0);
                    Q.set_state("exec");
                }
                JCB T = readyList.Find((JCB t) => t.get_state() == "exec");
                if (T.exeT == T.runtime)
                {
                    int ft = clock;
                    T.set_state("finish");
                    T.set_ftime(ft);
                    T.set_total(T.get_ftime() - T.runtime);
                    finishList.Add(T);
                    readyList.Remove(T);
                    setlistbox2();
                    setlistbox1();
                    if (readyList.Count == 0)
                    {
                        return;
                    }
                    T = readyList.ElementAt <JCB>(0);
                    T.set_state("exec");
                }


                JCB h;
                h = readyList.Find((JCB t) => t.get_state() == "exec");
                h.exeT++;
                setlistbox3();
                Console.Write(h.Print_my());
                Console.WriteLine();
            }
            //if (readyList.Count == 0) return;
            clock++;
        }