private void button4_Click(object sender, EventArgs e) { string procrssName = textBox4.Text; int DeAllocate; for (DeAllocate = 0; DeAllocate <= ProcessesList.Count() - 1; DeAllocate++) { if (ProcessesList[DeAllocate].process_name == procrssName) { groupBoxMEM.Controls.Clear(); hole backhole = new hole(); backhole.start = (ProcessesList[DeAllocate]).p_start_address; backhole.size = (ProcessesList[DeAllocate]).Process_size; backhole.end = (ProcessesList[DeAllocate]).End_p_address; ListOfHoles.Add(backhole); ListOfHoles.Sort((x, y) => x.start.CompareTo(y.start)); ProcessesList.RemoveAt(DeAllocate); // Drawing1(ListOfHoles); Drawing(ListOfHoles); DrawingProcesses(ProcessesList); } } }
public void Allocating(string Technique, Process p) { ////Fist Fit Function hole myhole = new hole(); int n = 0; int what = 0; if (Technique == "First Fit") { for (n = 0; n < ListOfHoles.Count; n++) { if (ListOfHoles[n].size >= p.Process_size) { myhole = ListOfHoles[n]; what = 0; break; } what = 1; } if (what == 0) { /// process take the hole information : p.p_start_address = myhole.start; p.End_p_address = p.p_start_address + p.Process_size; ///update the hole information : ListOfHoles[n].start += p.Process_size; ListOfHoles[n].size -= p.Process_size; //Drawing the holes Drawing(ListOfHoles); //drawing the new process : Label allocateLabel = new Label(); allocateLabel.Location = new Point(0, 2 * p.p_start_address); allocateLabel.Height = 2 * p.Process_size; allocateLabel.BackColor = System.Drawing.Color.Red; allocateLabel.BorderStyle = BorderStyle.Fixed3D; allocateLabel.Width = groupBoxMEM.Width; allocateLabel.Text = p.process_name; groupBoxMEM.Controls.Add(allocateLabel); //// draw Label :starting address of process int firstlabel = 100; Label firstlabelL = new Label(); firstlabelL.Location = new Point(620, firstlabel + 2 * (p.p_start_address + p.Process_size)); firstlabelL.Text = p.End_p_address.ToString(); this.Controls.Add(firstlabelL); } if (what == 1) { Drawing(ListOfHoles); MessageBox.Show("Sorry.There is not enough space. Please remove a process from Memory and try again . "); } } //////Best FIT else if (Technique == "Best Fit") { List <hole> copy = new List <hole>(ListOfHoles); copy = ListOfHoles.ToList(); copy.Sort((x, y) => x.size.CompareTo(y.size)); for (n = 0; n < copy.Count; n++) { if (copy[n].size >= p.Process_size) { myhole = copy[n]; what = 0; break; } what = 1; } if (what == 0) { p.p_start_address = copy[n].start; p.End_p_address = p.p_start_address + p.Process_size; ///update the hole information : copy[n].start += p.Process_size; copy[n].size -= p.Process_size; //Drawing the holes Drawing(copy); //drawing the new process : Label allocateLabel = new Label(); allocateLabel.Location = new Point(0, 2 * p.p_start_address); allocateLabel.Height = 2 * p.Process_size; allocateLabel.BackColor = System.Drawing.Color.Red; allocateLabel.BorderStyle = BorderStyle.Fixed3D; allocateLabel.Width = groupBoxMEM.Width; allocateLabel.Text = p.process_name; groupBoxMEM.Controls.Add(allocateLabel); ///update original hole info int v = 0; int k = 0; for (v = 0; v < ListOfHoles.Count; v++) { for (k = v; k < ListOfHoles.Count; k++) { if (ListOfHoles[v].size == copy[k].size) { break; } // break; } break; } ListOfHoles[v].size = copy[k].size; ListOfHoles[v].start = copy[k].start; //// draw Label :starting address of process int firstlabel = 100; Label firstlabelL = new Label(); firstlabelL.Location = new Point(620, firstlabel + 2 * (p.p_start_address + p.Process_size)); firstlabelL.Text = p.End_p_address.ToString(); this.Controls.Add(firstlabelL); } if (what == 1) { Drawing(ListOfHoles); MessageBox.Show("Sorry.There is not enough space. Please remove a process from Memory and try again . "); } } ///Worst Fit : /// else if (Technique == "Worst Fit") { List <hole> copy = new List <hole>(ListOfHoles); copy = ListOfHoles.ToList(); copy.Sort((x, y) => x.size.CompareTo(y.size)); n = copy.Count() - 1; if (copy[n].size >= p.Process_size) { myhole = copy[n]; p.p_start_address = copy[n].start; p.End_p_address = p.p_start_address + p.Process_size; ///update the hole information : copy[n].start += p.Process_size; copy[n].size -= p.Process_size; //Drawing the holes Drawing(copy); //drawing the new process : Label allocateLabel = new Label(); allocateLabel.Location = new Point(0, 2 * p.p_start_address); allocateLabel.Height = 2 * p.Process_size; allocateLabel.BackColor = System.Drawing.Color.Red; allocateLabel.BorderStyle = BorderStyle.Fixed3D; allocateLabel.Width = groupBoxMEM.Width; allocateLabel.Text = p.process_name; groupBoxMEM.Controls.Add(allocateLabel); ///update original hole info int v = 0; int k = 0; for (v = 0; v < ListOfHoles.Count; v++) { for (k = v; k < ListOfHoles.Count; k++) { if (ListOfHoles[v].size == copy[k].size) { break; } // break; } break; } ListOfHoles[v].size = copy[k].size; ListOfHoles[v].start = copy[k].start; //// draw Label :starting address of process int firstlabel = 100; Label firstlabelL = new Label(); firstlabelL.Location = new Point(620, firstlabel + 2 * (p.p_start_address + p.Process_size)); firstlabelL.Text = p.End_p_address.ToString(); this.Controls.Add(firstlabelL); } else { Drawing(ListOfHoles); MessageBox.Show("Sorry.There is not enough space. Please remove a process from Memory and try again . "); } } }