Esempio n. 1
0
            public static void SJF_Sched(List <Process> pro, int nprocess, bool Preemtive = false)
            {
                SJFnames.Clear();
                SJFtime.Clear();
                SJFtime.Add(0);
                List <Process> p = new List <Process>();

                foreach (Process x in pro)
                {
                    p.Add(x);
                }
                SJFCompare d = new SJFCompare();

                p.Sort(d);
                int    time = 0;
                double wait = 0;

                if (Preemtive)
                {
                    foreach (Process z in p)
                    {
                        wait -= z.arrival;
                    }

                    int i = 0;
                    while (p.Count() != 0 && p[i].arrival != time && i < p.Count())
                    {
                        i++;
                    }
                    Process currentp = new Process();
                    currentp = p[i];
                    p.RemoveAt(i);
                    bool done = false;
                    while (p.Count() != 0 || !done)
                    {
                        currentp.burst--;
                        wait += p.Count();
                        time++;
                        int j = 0;
                        while (p.Count() != 0 && j < p.Count() - 1 && p[j].arrival > time)
                        {
                            j++;
                        }
                        if (p.Count() != 0)
                        {
                            if (p.Count != 0 && currentp.burst != 0 && currentp.burst > p[j].burst && p[j].arrival <= time)
                            {
                                SJFnames.Add(currentp.name);
                                SJFtime.Add(time);
                                p.Add(currentp);
                                currentp = p[j];
                                p.RemoveAt(j);
                                SJFCompare c = new SJFCompare();
                                p.Sort(c);
                            }
                        }
                        if (currentp.burst == 0)
                        {
                            SJFnames.Add(currentp.name);
                            SJFtime.Add(time);
                            if (p.Count != 0)
                            {
                                currentp = p[j];
                                p.RemoveAt(j);
                            }
                            else
                            {
                                done = true;
                            }
                        }
                    }

                    SJFavgwait = wait / nprocess;
                }

                else
                {
                    while (p.Count() != 0)
                    {
                        int i = 0;
                        while (p.Count() != 0 && i < p.Count() - 1 && p[i].arrival > time)
                        {
                            i++;
                        }
                        if (p[i].arrival <= time)
                        {
                            wait += time - p[i].arrival;
                            time += p[i].burst;
                            SJFnames.Add(p[i].name);
                            SJFtime.Add(time);
                            p.RemoveAt(i);
                        }
                        else
                        {
                            time++;
                            SJFtime.Add(time);
                            SJFnames.Add("No Process");
                        }
                    }
                    SJFavgwait = wait / nprocess;
                }
            }
Esempio n. 2
0
        static void SJF_Sched(List <Process> p, int nprocess, bool Preemtive = false)
        {
            SJFtime.Add(0);
            List <Process> SJF = new List <Process>();

            for (int i = 0; i < nprocess; i++)
            {
                SJF.Add(p[i]);
            }
            SJFCompare d = new SJFCompare();

            SJF.Sort(d);
            int    time = 0;
            double wait = 0;
            bool   done = false;

            if (Preemtive)
            {
                foreach (Process z in SJF)
                {
                    wait -= z.arrival;
                }

                int i = 0;
                while (SJF.Count() != 0 && SJF[i].arrival != time && i < SJF.Count())
                {
                    i++;
                }
                Process currentp = new Process();
                currentp = SJF[i];
                SJF.RemoveAt(i);
                SJFtime.Add(0);

                while (SJF.Count() != 0 || !done)
                {
                    currentp.burst--;
                    wait += SJF.Count();
                    time++;
                    int j = 0;
                    while (SJF.Count() != 0 && j < SJF.Count() - 1 && SJF[j].arrival > time)
                    {
                        j++;
                    }
                    if (SJF.Count() != 0)
                    {
                        if (SJF.Count != 0 && currentp.burst != 0 && currentp.burst > SJF[j].burst && SJF[j].arrival <= time)
                        {
                            SJFnames.Add(currentp.name);
                            SJFtime.Add(time);
                            SJF.Add(currentp);
                            currentp = SJF[j];
                            SJF.RemoveAt(j);
                            SJFCompare c = new SJFCompare();
                            SJF.Sort(c);
                        }
                    }
                    if (currentp.burst == 0)
                    {
                        SJFnames.Add(currentp.name);
                        SJFtime.Add(time);
                        if (SJF.Count != 0)
                        {
                            currentp = SJF[j];
                            SJF.RemoveAt(j);
                        }
                        else
                        {
                            done = true;
                        }
                    }
                }

                SJFavgwait = wait / nprocess;
            }
            else
            {
                while (SJF.Count() != 0)
                {
                    int i = 0;
                    while (SJF.Count() != 0 && SJF[i].arrival > time && i < SJF.Count())
                    {
                        i++;
                    }
                    if (SJF[i].arrival <= time)
                    {
                        wait += time - SJF[i].arrival;
                        time += SJF[i].burst;
                        SJFnames.Add(SJF[i].name);
                        SJFtime.Add(time);
                        SJF.RemoveAt(i);
                    }
                    else
                    {
                        wait += SJF.Count();
                        time++;
                    }
                }
                SJFavgwait = wait / nprocess;
            }
        }