Example #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            int total_burst = 0;

            njobs = Int32.Parse(text_njobs.Text);

            algorithm = ddl_algorithm.Text;
            if (algorithm == "Round Robin")
            {
                if (text_quantum.Text != "")
                {
                    quantum = Int32.Parse(text_quantum.Text);
                }
                else
                {
                    string message = "Missing Data";
                    string caption = "Error Detected in Input";
                    MessageBox.Show(message, caption);
                    algorithm = "none";
                }
            }
            // we created a list where we'll store the incoming processes data

            List <Process> queue = new List <Process> ();

            for (int i = 1; i < njobs + 1; i++)
            {
                Process p = new Process();

                Control c = (TextBox)tableLayoutPanel1.GetControlFromPosition(0, i);

                if (c.Text != "")
                {
                    p.setID(Int32.Parse(c.Text));
                }
                else
                {
                    string message = "Missing Data";
                    string caption = "Error Detected in Input";
                    MessageBox.Show(message, caption);
                    algorithm = "none";
                    break;
                }

                c = (TextBox)tableLayoutPanel1.GetControlFromPosition(1, i);
                if (c.Text != "")
                {
                    p.setArrival(Int32.Parse(c.Text));
                }
                else
                {
                    string message = "Missing Data";
                    string caption = "Error Detected in Input";
                    MessageBox.Show(message, caption);
                    algorithm = "none";
                    break;
                }

                c = (TextBox)tableLayoutPanel1.GetControlFromPosition(2, i);
                if (c.Text != "")
                {
                    p.setBurst(Int32.Parse(c.Text));
                    p.setRT(Int32.Parse(c.Text));
                    total_burst += Int32.Parse(c.Text);
                }
                else
                {
                    string message = "Missing Data";
                    string caption = "Error Detected in Input";
                    MessageBox.Show(message, caption);
                    algorithm = "none";
                    break;
                }
                if (algorithm == "Priority - Preemptive" || algorithm == "Priority - Nonpreemptive")
                {
                    c = (TextBox)tableLayoutPanel1.GetControlFromPosition(3, i);

                    if (c.Text != "")
                    {
                        p.setPriority(Int32.Parse(c.Text));
                    }
                    else
                    {
                        string message = "Missing Data";
                        string caption = "Error Detected in Input";
                        MessageBox.Show(message, caption);
                        algorithm = "none";
                        break;
                    }
                }
                queue.Add(p);
            }

            //Here, all processes have been added to the queue..
            //FCFS
            if (algorithm == "FCFS")
            {
                //Sort according to arrival Time
                list_Sort(queue, "arrival");
                //We have to calculate the start and end of each process
                for (int i = 0; i < queue.Count; i++)
                {
                    if (i == 0)
                    {
                        int start = queue[i].getArrival();
                        int end   = start + queue[i].getBurst();
                        queue[i].setStart(start);
                        queue[i].setEnd(end);
                    }
                    else
                    {
                        int start = queue[i - 1].getEnd();
                        int end   = start + queue[i].getBurst();
                        queue[i].setStart(start);
                        queue[i].setEnd(end);
                    }
                }
                float sum = 0;
                //Calculate average WT
                for (int i = 0; i < queue.Count; i++)
                {
                    sum += queue[i].getWT();
                }
                float avgWT = sum / queue.Count;

                //Start drawing Gantt Chart!!
                DrawGantt(queue);
                label_wait.Text = avgWT.ToString();
            }
            else if (algorithm == "SJF - Nonpreemtive")
            {
                list_Sort(queue, "sjf");
                for (int i = 0; i < queue.Count; i++)
                {
                    if (i == 0)
                    {
                        int start = queue[i].getArrival();
                        int end   = start + queue[i].getBurst();
                        queue[i].setStart(start);
                        queue[i].setEnd(end);
                    }
                    else
                    {
                        int start = queue[i - 1].getEnd();
                        int end   = start + queue[i].getBurst();
                        queue[i].setStart(start);
                        queue[i].setEnd(end);
                    }
                }
                float sum = 0;
                //Calculate average WT
                for (int i = 0; i < queue.Count; i++)
                {
                    sum += queue[i].getWT();
                }
                float avgWT = sum / queue.Count;

                //Start drawing Gantt Chart!!
                DrawGantt(queue);
                label_wait.Text = avgWT.ToString();
            }
            else if (algorithm == "Priority - Nonpreemptive")
            {
                list_Sort(queue, "priority");
                for (int i = 0; i < queue.Count; i++)
                {
                    if (i == 0)
                    {
                        int start = queue[i].getArrival();
                        int end   = start + queue[i].getBurst();
                        queue[i].setStart(start);
                        queue[i].setEnd(end);
                    }
                    else
                    {
                        int start = queue[i - 1].getEnd();
                        int end   = start + queue[i].getBurst();
                        queue[i].setStart(start);
                        queue[i].setEnd(end);
                    }
                }
                float sum = 0;
                //Calculate average WT
                for (int i = 0; i < queue.Count; i++)
                {
                    sum += queue[i].getWT();
                }
                float avgWT = sum / queue.Count;

                //Start drawing Gantt Chart!!
                DrawGantt(queue);
                label_wait.Text = avgWT.ToString();
            }
            else if (algorithm == "Round Robin")
            {
                quantum = Int32.Parse(text_quantum.Text);

                //Sort according to arrival Time
                list_Sort(queue, "arrival");

                int            t      = 0;
                List <Process> readyQ = new List <Process>();

                while (t < total_burst)
                {
                    for (int i = 0; i < queue.Count; i++)
                    {
                        if (queue[i].getRT() > 0 && queue[i].getArrival() <= t)   //there is still remaining time
                        {
                            if (queue[i].getRT() < quantum)
                            { //remaining time is less than quantum
                                Process p = new Process();
                                p.setID(queue[i].getID());
                                p.setStart(t);
                                p.setEnd(t + queue[i].getRT());

                                readyQ.Add(p);

                                if (queue[i].getRT() == queue[i].getBurst())
                                {
                                    queue[i].setStart(t);
                                }
                                queue[i].setEnd(t + queue[i].getRT()); //updates end of original process

                                t += queue[i].getRT();
                                queue[i].setRT(0);
                            }
                            else
                            { //remaining time is greater than quantum
                                Process p = new Process();
                                p.setID(queue[i].getID());
                                p.setStart(t);
                                p.setEnd(t + quantum);

                                readyQ.Add(p);

                                if (queue[i].getRT() == queue[i].getBurst())
                                {
                                    queue[i].setStart(t);
                                }
                                queue[i].setEnd(t + queue[i].getRT()); //updates end of original process

                                t += quantum;
                                int r = queue[i].getRT() - quantum;
                                queue[i].setRT(r);
                            }
                        }
                    }
                }

                float sum = 0;
                for (int i = 0; i < queue.Count; i++)
                {
                    sum += (queue[i].getEnd() - queue[i].getArrival() - queue[i].getBurst()); //wt of each process = tat - bt // tat = end - arrival
                }
                float avgWT = sum / (queue.Count);

                //Draw Gantt chart and update Waiting Time
                DrawGantt(readyQ);
                label_wait.Text = avgWT.ToString();
            }
            else if (algorithm == "SJF - Preemptive")
            {
                list_Sort(queue, "sjf");
                int            t      = 0;
                List <Process> readyQ = new List <Process>();

                int running_index = 0;
                queue[running_index].setStart(0);

                while (t < total_burst)
                {
                    t++;

                    int currRT = queue[running_index].getRT();
                    queue[running_index].setRT(currRT - 1);

                    if (queue[running_index].getRT() == 0)
                    {
                        queue[running_index].setEnd(t);

                        Process p = new Process();
                        p.setID(queue[running_index].getID());
                        p.setStart(queue[running_index].getStart());
                        p.setEnd(queue[running_index].getEnd());

                        readyQ.Add(p);

                        for (int i = 0; i < queue.Count; i++)
                        {
                            if (queue[i].getRT() != 0)
                            {
                                running_index = i;
                                queue[running_index].setStart(t);
                                break;
                            }
                        }
                    }
                    for (int i = 0; i < queue.Count; i++)
                    {
                        if (queue[i].getArrival() <= t && queue[i].getRT() < queue[running_index].getRT() && queue[i].getRT() != 0)
                        {
                            queue[running_index].setEnd(t);

                            if (queue[running_index].getEnd() != queue[running_index].getStart())
                            {
                                Process p = new Process();
                                p.setID(queue[running_index].getID());
                                p.setStart(queue[running_index].getStart());
                                p.setEnd(queue[running_index].getEnd());

                                readyQ.Add(p);
                            }
                            running_index = i;
                            queue[running_index].setStart(t);
                        }
                    }
                }
                float sum = 0;
                for (int i = 0; i < queue.Count; i++)
                {
                    sum += (queue[i].getEnd() - queue[i].getArrival() - queue[i].getBurst()); //wt of each process = tat - bt // tat = end - arrival
                }
                float avgWT = sum / (queue.Count);
                DrawGantt(readyQ);
                label_wait.Text = avgWT.ToString();
            }
            else if (algorithm == "Priority - Preemptive")
            {
                list_Sort(queue, "priority");
                int            t             = 0;
                List <Process> readyQ        = new List <Process>();
                int            running_index = 0;
                queue[running_index].setStart(0);

                while (t < total_burst)
                {
                    t++;
                    int currRT = queue[running_index].getRT();
                    queue[running_index].setRT(currRT - 1);
                    if (queue[running_index].getRT() == 0)
                    {
                        queue[running_index].setEnd(t);
                        Process p = new Process();
                        p.setID(queue[running_index].getID());
                        p.setStart(queue[running_index].getStart());
                        p.setEnd(queue[running_index].getEnd());
                        readyQ.Add(p);
                        for (int i = 0; i < queue.Count; i++)
                        {
                            if (queue[i].getRT() != 0)
                            {
                                running_index = i;
                                queue[running_index].setStart(t);
                                break;
                            }
                        }
                    }
                    for (int i = 0; i < queue.Count; i++)
                    {
                        if (queue[i].getArrival() <= t && queue[i].getPriority() < queue[running_index].getPriority() && queue[i].getRT() != 0)
                        {
                            queue[running_index].setEnd(t);

                            if (queue[running_index].getEnd() != queue[running_index].getStart())
                            {
                                Process p = new Process();
                                p.setID(queue[running_index].getID());
                                p.setStart(queue[running_index].getStart());
                                p.setEnd(queue[running_index].getEnd());

                                readyQ.Add(p);
                            }
                            running_index = i;
                            queue[running_index].setStart(t);
                        }
                    }
                }
                float sum = 0;
                for (int i = 0; i < queue.Count; i++)
                {
                    sum += (queue[i].getEnd() - queue[i].getArrival() - queue[i].getBurst()); //wt of each process = tat - bt // tat = end - arrival
                }
                float avgWT = sum / (queue.Count);
                DrawGantt(readyQ);
                label_wait.Text = avgWT.ToString();
            }
        }