Пример #1
0
 public static void sort2(List <process> l)
 {
     for (int i = 0; i < l.Count; i++)
     {
         for (int j = 0; j < l.Count; j++)
         {
             if (l[i].priority < l[j].priority)
             {
                 process temp = l[i];
                 l[i] = l[j];
                 l[j] = temp;
             }
         }
     }
 }
Пример #2
0
 public static void sort3(List <process> l)
 {
     for (int i = 0; i < l.Count; i++)
     {
         for (int j = 0; j < l.Count; j++)
         {
             if (l[i].burst_time < l[j].burst_time)
             {
                 process temp = l[i];
                 l[i] = l[j];
                 l[j] = temp;
             }
         }
     }
 }
Пример #3
0
 public static void sort1(List <process> l)
 {
     for (int i = 0; i < l.Count; i++)
     {
         for (int j = 0; j < l.Count; j++)
         {
             if (l[i].arrival < l[j].arrival)
             {
                 process temp = l[i];
                 l[i] = l[j];
                 l[j] = temp;
             }
         }
     }
 }
Пример #4
0
        public void computeRR()
        {
            List <process> p = new List <process>(num_process);

            for (int i = 0; i < num_process; i++)
            {
                p.Add(new process(i, arrivalTime[i], cpu_brustTime[i]));
            }
            process.sort1(p);

            int cnt = 0, idx = 0;

            while (p.Count != 0)
            {
                List <process> ready = new List <process>(num_process);
                if (p.First().arrival > cnt)
                {
                    start[idx] = cnt;
                    end[idx]   = p.First().arrival;
                    proc[idx]  = -1;
                    idx++;
                    cnt = p.First().arrival;
                }
                for (int i = 0, j = 0; j < p.Count; i++)
                {
                    if (p[j].arrival <= cnt)
                    {
                        ready.Add(p[j]);
                        p.RemoveAt(j);
                    }
                    else
                    {
                        j++;
                    }
                }
                while (ready.Count != 0)
                {
                    process readyProcess = ready[0];
                    ready.RemoveAt(0);
                    if (quantum < readyProcess.burst_time)
                    {
                        start[idx] = cnt;
                        end[idx]   = cnt + quantum;
                        proc[idx]  = readyProcess.index;
                        cnt       += quantum;
                        readyProcess.burst_time -= quantum;
                        for (int i = 0, j = 0; j < p.Count; i++)
                        {
                            if (p[j].arrival <= cnt)
                            {
                                ready.Add(p[j]);
                                p.RemoveAt(j);
                            }
                            else
                            {
                                j++;
                            }
                        }
                        ready.Add(readyProcess);
                        idx++;
                    }
                    else
                    {
                        start[idx] = cnt;
                        end[idx]   = cnt + readyProcess.burst_time;
                        proc[idx]  = readyProcess.index;
                        cnt       += readyProcess.burst_time;
                        for (int i = 0, j = 0; j < p.Count; i++)
                        {
                            if (p[j].arrival <= cnt)
                            {
                                ready.Add(p[j]);
                                p.RemoveAt(j);
                            }
                            else
                            {
                                j++;
                            }
                        }
                        idx++;
                    }
                }
            }
            for (int j = 0; j < num_process; j++)
            {
                bool k = false; int wait = 0;
                for (int i = 0; i <= idx; i++)
                {
                    if (proc[i] == j)
                    {
                        if (!k)
                        {
                            waitingTime[j] = start[i] - arrivalTime[j]; k = true;
                            wait           = end[i];
                        }
                        else if (k)
                        {
                            waitingTime[j] += start[i] - wait;
                            wait            = end[i];
                        }
                        turnaroundTime[j] = wait - arrivalTime[j];
                    }
                }
            }
            for (int i = 0; i < num_process; i++)
            {
                if (waitingTime[i] == 0)
                {
                    continue;
                }
                else
                {
                    avg_waiting += waitingTime[i];
                }
            }
            avg_waiting /= num_process;
            for (int i = 0; i < num_process; i++)
            {
                if (turnaroundTime[i] == 0)
                {
                    continue;
                }
                else
                {
                    avg_turnaround += turnaroundTime[i];
                }
            }
            avg_turnaround /= num_process;
            lastValid       = idx;
        }
Пример #5
0
        public void computeSJF_N()
        {
            List <process> p = new List <process>(num_process);


            for (int i = 0; i < num_process; i++)
            {
                p.Add(new process(i, arrivalTime[i], cpu_brustTime[i]));
            }
            process.sort1(p);
            int cnt = 0;
            int idx = 0;

            while (p.Count != 0)
            {
                List <process> ready = new List <process>(num_process);
                if (p.First().arrival > cnt)
                {
                    start[idx] = cnt;
                    end[idx]   = p.First().arrival;
                    proc[idx]  = -1;
                    idx++;
                    cnt = p.First().arrival;
                }
                for (int i = 0, j = 0; j < p.Count; i++)
                {
                    if (p[j].arrival <= cnt)
                    {
                        ready.Add(p[j]);
                        p.RemoveAt(j);
                    }
                    else
                    {
                        j++;
                    }
                }
                while (ready.Count != 0)
                {
                    process.sort3(ready);
                    process readyProcess = ready[0];
                    ready.RemoveAt(0);
                    start[idx]          = cnt;
                    end[idx]            = cnt + readyProcess.burst_time;
                    proc[idx]           = readyProcess.index;
                    waitingTime[idx]    = start[idx] - readyProcess.arrival;
                    turnaroundTime[idx] = end[idx] - readyProcess.arrival;
                    idx++;
                    cnt += readyProcess.burst_time;
                    for (int i = 0, j = 0; i < p.Count; i++)
                    {
                        if (p[j].arrival <= cnt)
                        {
                            ready.Add(p[j]);
                            p.RemoveAt(j);
                        }
                        else
                        {
                            j++;
                        }
                    }
                }
            }
            for (int i = 0; i < num_process; i++)
            {
                if (waitingTime[i] == 0)
                {
                    continue;
                }
                else
                {
                    avg_waiting += waitingTime[i];
                }
            }
            avg_waiting /= num_process;
            for (int i = 0; i < num_process; i++)
            {
                if (turnaroundTime[i] == 0)
                {
                    continue;
                }
                else
                {
                    avg_turnaround += turnaroundTime[i];
                }
            }
            avg_turnaround /= num_process;
            lastValid       = idx;
        }