public void calulate_memory_processes() { List <hole> temp = new List <hole>(); int start = 0; for (int i = 0; i < get_number_holes_after_merge(); i++) { //special case for the process that have start location at zerp if (holes[i].get_start_location() == 0) { start = holes[i].get_end_location(); } if (holes[i].get_start_location() > start) { hole nh = new hole(holes[i].get_start_location() - start, start); temp.Add(nh); start = holes[i].get_end_location(); } } //for adding the last old process after the last hole if (holes[get_number_holes_after_merge() - 1].get_end_location() != size) { hole nh = new hole(size - holes[get_number_holes_after_merge() - 1].get_end_location(), holes[get_number_holes_after_merge() - 1].get_end_location()); temp.Add(nh); } processes = temp; }
private void btn_to_form5_Click(object sender, EventArgs e) { bool success = true; int index = 1; foreach (KeyValuePair <int, List <TextBox> > h in holes) { int location; int size; int test; string s; s = (h.Value[0].Text); if (s == null || int.TryParse(s, out test) == false) { MessageBox.Show("Please enter a valid number\n" + "\t Hole" + index.ToString() + " Location"); success = false; break; } location = int.Parse(s); s = (h.Value[1].Text); if (s == null || int.TryParse(s, out test) == false || int.Parse(s) == 0) { MessageBox.Show("Please enter a valid number\n" + "\t Hole" + index.ToString() + " Size"); success = false; break; } size = int.Parse(s); hole nh = new hole(size, location); if (nh.get_size() + nh.get_start_location() > memory.get_size()) { MessageBox.Show("\t ERROR \n Hole" + index.ToString() + " is Outside than memory space"); return; } memory.add_hole(nh); index++; } memory.merge_holes(); if (memory.get_total_empty_space() > memory.get_size()) { MessageBox.Show("\t ERROR \n Size of Holes is greater than memory size"); return; } if (success) { Form6 frm = new Form6(processes, memory); Visible = false; frm.ShowDialog(); Close(); } }
public int search_process(hole h) { for (int i = 0; i < get_memory_processes().Count; i++) { if (processes[i] == h) { return(i); } } return(-1); }
private void update_holes_list() { holes.Clear(); for (int i = 0; i < holes_number; i++) { hole hol = new hole(); hol.start = Convert.ToInt16(holes_starting[i].Text); hol.size = Convert.ToInt16(holes_size[i].Text); hol.end = hol.start + hol.size; hol.hole_lbl = holes_lbl[i]; holes.Add(hol); } }
public void arrange_memory_descending_size() { for (int i = 0; i < this.get_number_holes_after_merge(); i++) { for (int j = 0; j < this.get_number_holes_after_merge() - i - 1; j++) { if (this.holes[j + 1].get_size() > this.holes[j].get_size()) { hole temp = this.holes[j]; this.holes[j] = this.holes[j + 1]; this.holes[j + 1] = temp; } } } }
public void merge_holes() { List <Tuple <int, int> > start_end = new List <Tuple <int, int> >(); for (int i = 0; i < get_number_holes_after_merge(); i++) { Tuple <int, int> tup = new Tuple <int, int>(holes[i].get_start_location(), holes[i].get_end_location()); start_end.Add(tup); } start_end.Sort(); holes.Clear(); foreach (Tuple <int, int> tup in start_end) { hole nh = new hole(tup.Item2 - tup.Item1, tup.Item1); holes.Add(nh); } List <hole> temp = new List <hole>(); int index = 0; temp.Add(holes[index]); for (int i = 1; i < get_number_holes_after_merge(); i++) { if (holes[i].get_start_location() >= temp[index].get_start_location() && holes[i].get_start_location() <= temp[index].get_end_location()) { if (holes[i].get_end_location() > temp[index].get_end_location()) { hole nh = new hole(holes[i].get_end_location() - temp[index].get_start_location(), temp[index].get_start_location()); temp[index] = nh; } else if (holes[i].get_end_location() < temp[index].get_end_location()) { hole nh = temp[index]; temp[index] = nh; } } else { temp.Add(holes[i]); index++; } } holes = temp; }
public memory copy() { memory other = new memory(get_size(), get_number_holes_after_merge()); List <hole> tholes = new List <hole>(); foreach (hole h in holes) { hole nh = new hole(h.get_size(), h.get_start_location(), h.get_end_location(), h.get_color(), h.get_text()); tholes.Add(nh); } List <hole> tpro = new List <hole>(); foreach (hole h in processes) { hole nh = new hole(h.get_size(), h.get_start_location(), h.get_end_location(), h.get_color(), h.get_text()); tpro.Add(nh); } other.set_holes(tholes); other.set_processes(tpro); return(other); }
private void remove_from_memory(object sender, EventArgs e, Panel p, hole h) { Label lbl = sender as Label; int pos = p.VerticalScroll.Value; //p.clear and then change the holes and then merge adjecent holes and the draw again p.Controls.Clear(); if (h.get_color() == Color.Black) { int index = memory.search_process(h); memory.add_hole(h); memory.remove_process(index); memory.merge_holes(); } else { int count = memory.get_memory_processes().Count; for (int j = 0; j < count; j++) { List <hole> t = memory.get_memory_processes(); hole q = t[j]; if (q.get_color() == h.get_color()) { int index = memory.search_process(q); memory.add_hole(q); memory.remove_process(index); memory.merge_holes(); j--; count--; } } } memory_draw(); p.VerticalScroll.Value = pos; // MessageBox.Show("Process has been removed successfully"); }
public void add_hole(hole nh) { this.holes.Add(nh); }
public void add_process(hole p) { this.processes.Add(p); }
private void add_to_memory(object sender, EventArgs e, Label lbl, process p) { Button b = sender as Button; //sorting descendingly according to size:: try ex p=50,100,200 ;holes sizes=300,100,100;it will not work ////// p.segments_sort_desc();///ES2L FEEHA //renew the holes list and current process in memory Clear the memory panel and then draw it again if (p.get_size() > memory.get_total_empty_space()) { MessageBox.Show("Process" + p.get_index().ToString() + " cannot be added to memory"); return; } //Colning the memory because the defalult assign is by referance in c# ex:memory temp=memory => if we change temp the memory will change due to copying by referrence if (firstfit.Checked == true) { memory temp = memory.copy(); foreach (segment s in p.get_segments()) { int success = 0; foreach (hole h in temp.get_holes()) { if (h.get_size() >= s.get_size()) { hole np = new hole(s.get_size(), h.get_start_location()); np.set_color(s.get_color()); np.set_text(s.get_text()); temp.add_process(np); h.set_size(h.get_size() - s.get_size()); h.set_start_location(h.get_start_location() + s.get_size()); success = 1; break; } } if (success == 0) { MessageBox.Show("Process cannot be added to memory"); return; } } memory = temp; lst_memory.Controls.Clear(); memory_draw(); /*if added successfully remove the process from wait list*/ b.Enabled = false; lbl.Enabled = false; b.Hide(); lbl.Hide(); /**END OF FIRST FIT*/ } else if (bestfit.Checked == true) { memory temp = memory; bool check = false; List <bool> x = new List <bool>(); for (int i = 0; i < temp.get_number_holes_after_merge(); i++) { x.Add(true); } temp.arrange_memory_ascending_size(); foreach (segment s in p.get_segments()) { int i = 0; foreach (hole h in temp.get_holes()) { if (x[i]) { if (s.get_size() > h.get_size()) { check = false; continue; } else { x[i] = false; check = true; break; } } i++; } } if (check) { foreach (segment s in p.get_segments()) { foreach (hole h in temp.get_holes()) { if (s.get_size() > h.get_size()) { continue; } else { hole get_in = new hole(s.get_size(), h.get_start_location()); get_in.set_color(s.get_color()); get_in.set_text(s.get_text()); h.set_size(h.get_size() - s.get_size()); h.set_start_location(h.get_start_location() + s.get_size()); temp.add_process(get_in); break; } } } temp.merge_holes(); } else { MessageBox.Show("Process cannot be added to memory"); return; } memory = temp; lst_memory.Controls.Clear(); memory_draw(); b.Enabled = false; lbl.Enabled = false; b.Hide(); lbl.Hide(); } else if (worstfit.Checked == true) { memory temp = memory; bool check = false; List <bool> x = new List <bool>(); for (int i = 0; i < temp.get_number_holes_after_merge(); i++) { x.Add(true); } temp.arrange_memory_descending_size(); foreach (segment s in p.get_segments()) { int i = 0; foreach (hole h in temp.get_holes()) { if (x[i]) { if (s.get_size() > h.get_size()) { check = false; continue; } else { x[i] = false; check = true; break; } } i++; } } if (check) { foreach (segment s in p.get_segments()) { foreach (hole h in temp.get_holes()) { if (s.get_size() > h.get_size()) { continue; } else { hole get_in = new hole(s.get_size(), h.get_start_location()); get_in.set_color(s.get_color()); get_in.set_text(s.get_text()); h.set_size(h.get_size() - s.get_size()); h.set_start_location(h.get_start_location() + s.get_size()); temp.add_process(get_in); break; } } } temp.merge_holes(); } else { MessageBox.Show("Process cannot be added to memory"); return; } memory = temp; lst_memory.Controls.Clear(); memory_draw(); b.Enabled = false; lbl.Enabled = false; b.Hide(); lbl.Hide(); } }
private void button4_Click(object sender, EventArgs e) { process proc = new process(); Show_processData(proc, false); while (clicked_process.Count != 0) { proc = clicked_process.Pop(); hole h = new hole(); List <process> same_hole_procs; h = holes.Find(x => x.hole_lbl.Text.Contains(proc.allocated_hole_name)); same_hole_procs = new List <process>(procs.FindAll(x => x.allocated_hole_name.Contains(h.hole_lbl.Text.ToString())).OrderByDescending(n => n.start)); if (h.end != proc.start && (same_hole_procs.Count != clicked_process.Count + 1))//there is another processes allocated under than process { //get all processes allocated in that hole and shift them to the bottom of the hole h.end += proc.size; bool flag = false; for (int i = 0; i < same_hole_procs.Count; i++) { if (same_hole_procs[i] == proc) { flag = true; procs.Remove(proc); continue; } if (flag) { same_hole_procs[i].end += proc.size; same_hole_procs[i].start = same_hole_procs[i].end - same_hole_procs[i].size; same_hole_procs[i].rearranged = true; draw_process(); } } h.size += proc.size; } else { h.size += proc.size; h.end += proc.size; procs.Remove(proc); } this.panel1.Controls.Remove(proc.process_lbl); this.panel1.Controls.Remove(proc.start_lbl); this.panel1.Controls.Remove(proc.end_lbl); label16.Text = (Convert.ToInt16(label16.Text) - 1).ToString(); } if (waiting_procs > 0) { // waiting_procs-=clicked_process.Count; // draw_holes(); draw_process(); } if (waiting_procs == 0) { panel2.Controls.Clear(); } }
private void draw_process() { Label start = new Label(); Label end = new Label(); for (int i = 0; i < procs.Count; i++) { process current = procs[i]; bool IsAlocated = false; int scale_width = Scale(current.size); if (current.IsAllocated && current.rearranged == false) { continue; } else if (current.IsAllocated && current.rearranged == true) { panel1.Controls.Remove(current.process_lbl); panel1.Controls.Remove(current.start_lbl); panel1.Controls.Remove(current.end_lbl); Point pt = new Point(45, Scale(current.start)); Point lbl_p = new Point(20, Scale(current.start)); start = creat_lable(current.start, lbl_p.X, lbl_p.Y); end = creat_lable(current.end, lbl_p.X, lbl_p.Y + (Scale(current.size) - 8)); start.BackColor = Color.DimGray; end.BackColor = Color.DimGray; start.ForeColor = Color.GreenYellow; end.ForeColor = Color.GreenYellow; start.Name = current.name; end.Name = current.name; start.Font = new Font("Tahoma", 5, FontStyle.Regular); end.Font = new Font("Tahoma", 5, FontStyle.Regular); if (current.size <= 10) { current.process_lbl.BackColor = Color.Blue; } else { current.process_lbl.BackColor = Color.Green; } current.process_lbl.ForeColor = Color.Black; current.process_lbl.Location = pt; current.process_lbl.Height = scale_width; current.process_lbl.Width = 155; current.process_lbl.TextAlign = ContentAlignment.TopCenter; current.start_lbl = start; current.end_lbl = end; panel1.Controls.Add(current.process_lbl); panel1.Controls.Add(start); panel1.Controls.Add(end); start.BringToFront(); end.BringToFront(); start.Invalidate(); end.Invalidate(); current.process_lbl.Name = current.name; current.process_lbl.BringToFront(); current.process_lbl.Invalidate(); //current.process_lbl.Click += new System.EventHandler(this.label_Click); // current.process_lbl.Leave += new System.EventHandler(this.label_leave); current.rearranged = false; continue; } if (current.method == "First Fit") { holes.Sort(); } else if (current.method == "Best Fit") { holes = new List <hole>(holes.OrderBy(x => x.size).ToList()); } else //worst fit method { holes = new List <hole>(holes.OrderByDescending(x => x.size).ToList()); } for (int j = 0; j < holes.Count; j++) { hole current_hole = holes[j]; if (current_hole.size == 0) { continue; } if (current_hole.size >= current.size) { if (current.waiting != null) { panel2.Controls.Remove(current.waiting); //current.waiting = null; waiting_procs--; inrement_y -= 15; } IsAlocated = true; current.start = current_hole.end - current.size; current.end = current_hole.end; current.IsAllocated = IsAlocated; Point pt = new Point(45, Scale(current.start)); Point lbl_p = new Point(20, Scale(current.start)); start = creat_lable(current.start, lbl_p.X, lbl_p.Y); end = creat_lable(current.end, lbl_p.X, lbl_p.Y + (Scale(current.size) - 8)); start.BackColor = Color.DimGray; end.BackColor = Color.DimGray; start.ForeColor = Color.GreenYellow; end.ForeColor = Color.GreenYellow; start.Name = current.name; end.Name = current.name; start.Font = new Font("Tahoma", 5, FontStyle.Regular); end.Font = new Font("Tahoma", 5, FontStyle.Regular); if (current.size <= 10) { current.process_lbl.BackColor = Color.Blue; } else { current.process_lbl.BackColor = Color.Green; } current.process_lbl.ForeColor = Color.Black; current.process_lbl.Location = pt; current.process_lbl.Height = scale_width; current.process_lbl.Width = 155; current.process_lbl.TextAlign = ContentAlignment.TopCenter; current.start_lbl = start; current.end_lbl = end; current_hole.size -= current.size; //Scale(scale_width); current_hole.end -= current.size; panel1.Controls.Add(current.process_lbl); panel1.Controls.Add(start); panel1.Controls.Add(end); start.BringToFront(); end.BringToFront(); start.Invalidate(); end.Invalidate(); current.allocated_hole_name = current_hole.hole_lbl.Text; current.process_lbl.BringToFront(); current.process_lbl.Invalidate(); current.process_lbl.Name = current.name; current.process_lbl.Click += new System.EventHandler(this.label_Click); current.process_lbl.Leave += new System.EventHandler(this.label_leave); break; } } if (!current.IsAllocated) { Label waiting_lbl = new Label(); waiting_lbl = creat_lable(current.name + " with size = " + current.size + "->" + current.method, 6, 27 + inrement_y); waiting_lbl.Name = current.name; if (current.waiting == null) { current.waiting = waiting_lbl; } if (!panel2.Controls.Contains(current.waiting)) { panel2.Controls.Add(current.waiting); waiting_procs++; inrement_y += 15; } } } }