public gant_info start_excution(Method method = Method.FCFS, int q = -1) //execution of all scheduling be arranging the processes { sort(); int index = 0; int time = 0; int next_time = 0; while (true) { //preemtives if (time == next_time && (method == Method.SRJF || method == Method.PRP)) { next_time = get_next_time(time); } //--- //get the index if (method == Method.FCFS || method == Method.RR) { index = get_next(time); } else { index = get_least(time, method); } //--- //to fix the times time_list.Add(time); if (index == -2) { time = all[get_least(-1)].get_arrive(); index_list.Add(-2); continue; } index_list.Add(index); if (index == -1) { break; } if (method == Method.FCFS || method == Method.SJF || method == Method.PRN || (method == Method.SRJF || method == Method.PRP && next_time <= time)) { time = time + all[index].get_remaining(); all[index].excute_all(); } else if ((method == Method.RR && q != -1) || method == Method.SRJF || method == Method.PRP) { if (method == Method.SRJF || method == Method.PRP) { q = next_time - time; } int x = all[index].excute(q); time = x < 0 ? time + q + x : time + q; } //---- } gant_info info = new gant_info(); info.index = index_list; info.time = time_list; return(info); }
//the new gantt chart private void draw(gant_info info, int points_per_timeunit, bool double_lines = false) { int width = 0, offset = width; int row = 1; bool new_row = true; bool first_element_new_row = false; Point location = new Point(); for (int i = 0; i < info.index.Count; i++) { Label process = new Label(); Label time = new Label(); setup_index_time_labels(process, time, location, new_row ? -1 : width, row); time.Text = info.time[i].ToString(); if (info.index[i] != -1) { int next_time = info.time[i + 1]; int current_time = info.time[i]; width = (next_time - current_time) * points_per_timeunit; Point check_validity = new Point(process.Location.X + width, process.Location.Y); if (new_row == true && first_element_new_row == true) { put_process(info, i, process, width); new_row = false; first_element_new_row = false; } else if (check_validity.X < result_width || double_lines == false) { offset = width; put_process(info, i, process, width); new_row = false; first_element_new_row = false; } else { offset = (int)(result_width - process.Location.X); put_process(info, i, process, offset); new_row = true; info.time[i] = (offset / points_per_timeunit) + info.time[i]; Label unnecessary = new Label(); time = new Label(); location = process.Location; setup_index_time_labels(unnecessary, time, location, offset, row); unnecessary.Visible = false; time.Text = info.time[i].ToString(); offset = width; i--; row++; first_element_new_row = true; } width = process.Width; location = process.Location; } else { process.Visible = false; } } }
//calculations of average waiting time private float wait(gant_info info) // to get the whole waiting time { int index = 0, total_time = 0; for (int j = 0; j < processes.all.Count(); j++) { index = info.index.IndexOf(j); total_time += info.time[index] - processes.all[j].get_arrive(); } return(total_time); }
//------------------------------ private void start_Click(object sender, EventArgs e) { State state = get_state(); if (state == State.ProcessAdd) { state = State.ProcessFinish; set_state(state); gantt_chart.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; gant_info info = new gant_info(); if (fcfs.Checked) { info = processes.start_excution(); } else if (sjf.Checked) { if (preem.Checked) { info = processes.start_excution(Method.SRJF); } else { info = processes.start_excution(Method.SJF); } } else if (priority.Checked) { if (preem.Checked) { info = processes.start_excution(Method.PRP); } else { info = processes.start_excution(Method.PRN); } } else if (rr.Checked) { info = processes.start_excution(Method.RR, Int32.Parse(q.Text)); } average_wait(info); draw_chart_alternative(info); } else { state = State.ProcessAdd; set_state(state); processes.reset(); } }
// the used gantt chart// private int get_min_interval(gant_info info) // return the least time a process is executed { int min_interval = 2 ^ 30; //very big interval for (int i = 0; i < info.index.Count - 1; i++) { if ((info.time[i + 1] - info.time[i]) < min_interval) { min_interval = info.time[i + 1] - info.time[i]; } } return(min_interval); }
private void draw_chart_alternative(gant_info info) //to determine how many raws will be drawn(1 or 2) { Label ref_label = new Label(); // for calculation only used once ref_label.Text = "MM.."; result.Controls.Add(ref_label); // to add to the groupbox result ref_label.AutoSize = true; //reference label ref_label.Visible = false; int points_per_timeunit = (result_width) / info.time[info.time.Count - 1]; int min_width = get_min_interval(info) * points_per_timeunit; if (min_width > ref_label.Width) { draw(info, points_per_timeunit); } else { draw(info, points_per_timeunit * 2, true);//*2 to indicate two rows } }
private void put_process(gant_info info, int i, Label process, int offset) //modify the name and the size of a process in the gantt chart { if (info.index[i] == -2) //indicates a gap { process.Text = " "; } else { process.Text = processes.all[info.index[i]].get_name(); process.AutoSize = true; string name = process.Text; if (process.Size.Width > offset)//if the process name is larger than its size { for (int j = name.Length; (j > 2) && (process.Size.Width > offset); j--) { process.Text = name.Substring(0, j) + ".."; } } } process.AutoSize = false; process.Size = new Size(offset, 20); process.BringToFront(); }
private void draw_chart(gant_info info)//old way to draw gantt_chart { int time = 0; for (int i = 0; i < info.index.Count(); i++) { string s = " "; if (info.index[i] == -2) { gantt_chart.AppendText(" "); gantt_chart.AppendText("|"); s += (" "); } else if (info.index[i] != -1) { string ss = processes.get_process(info.index[i]).get_name(); string x = " " + ss + " "; string y = "|"; gantt_chart.AppendText(x); gantt_chart.AppendText(y); for (int j = 0; j < ss.Length; j++) { s = s + " "; } } string m = s; time = info.time[i]; if (time > 9) { m = s.Remove(s.Length - 1); } textBox2.AppendText(Convert.ToString(time)); textBox2.AppendText(m); } }
private void average_wait(gant_info info) // to get the average waiting time { float total_waiting = wait(info); equations.Text = (total_waiting / processes.all.Count()).ToString(); }