Esempio n. 1
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++;
        }
Esempio n. 2
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++;
        }