예제 #1
0
        public bool Process_IS_Fit(process p)
        {
            bool fitted             = false;
            int  sum_segments_sizes = 0;

            if (holes.Count == 1)
            {
                for (int j = 0; j < p.no_of_segments; j++)
                {
                    sum_segments_sizes = sum_segments_sizes + p.segmenst_sizes[j];
                }

                //1.1 The only case to allocate the process in the hole
                if (holes[0].size >= sum_segments_sizes)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }

            int[] holes_size = new int[holes.Count];
            for (int j = 0; j < holes.Count; j++)
            {
                holes_size[j] = holes[j].size;
            }


            for (int j = 0; j < p.no_of_segments; j++)
            {
                int segment_size = p.segmenst_sizes[j];   //code = 170

                for (int i = 0; i < holes.Count; i++)
                {
                    if (holes_size[i] >= segment_size)
                    {
                        holes_size[i] = holes_size[i] - segment_size;
                        fitted        = true;
                        break;
                    }

                    else
                    {
                        fitted = false;
                    }
                }

                if (!fitted)
                {
                    return(fitted);
                }
            }
            return(fitted);
        }
        private void btn_ok_number_of_process_Click(object sender, EventArgs e)
        {
            num_of_process++;
            int     promptValue = Form7.ShowDialog("Number Of Segments", "enter no of segments");
            process p           = Form7.ShowDialog2("enter process data", "new process", promptValue, num_of_process);

            //num_of_process++;
            processes.Add(p);
            Form7 form = new Form7(holes, segments, processes, num_of_process, 1);

            form.Show();
            Hide();
        }
 public void computeWorstFit()
 {
     while (processes.Count > 0)
     {
         process temp = processes[0];
         processes.RemoveAt(0);
         List <hole> tempHoles = new List <hole>();
         tempHoles = copy(holes);
         int allocated = 0;
         for (int i = 0; i < temp.no_of_segments; i++)
         {
             hole.sort2(tempHoles);
             tempHoles.Reverse();
             for (int j = 0; j < tempHoles.Count; j++)
             {
                 if (tempHoles[j].alocated == false && tempHoles[j].size >= temp.segmenst_sizes[i])
                 {
                     hole tempHole = new MemoryManagmentVisualization.hole(tempHoles[j].start, tempHoles[j].size);
                     tempHole.alocated = false;
                     tempHoles.RemoveAt(j);
                     int remainSize = tempHole.size - temp.segmenst_sizes[i];
                     tempHole.size = temp.segmenst_sizes[i];
                     allocated++;
                     tempHole.alocated      = true;
                     tempHole.name          = temp.name_of_segment[i];
                     tempHole.process_index = temp.process_id;
                     tempHoles.Add(tempHole);
                     if (remainSize > 0)
                     {
                         tempHoles.Add(new hole(tempHole.start + tempHole.size, remainSize));
                     }
                     break;
                 }
             }
         }
         if (allocated == temp.no_of_segments)
         {
             holes = copy(tempHoles);
         }
         else
         {
             holdProcesses.Add(temp);
         }
     }
 }
        public Form7()
        {
            InitializeComponent();


            num_of_process = Form1.no_of_processes;

            for (int i = 0; i < Form1.no_of_holes; i++)
            {
                hole h = new hole(Form1.holes_start_adress[i], Form1.holes_sizes[i]);
                holes.Add(h);
            }

            int idx = 0;

            for (int i = 0; i < Form1.no_of_processes; i++)
            {
                process p = new process(i + 1, Form1.no_of_segments[i]);
                for (int j = 0; j < Form1.no_of_segments[i]; j++)
                {
                    p.segmenst_sizes[j]  = Form1.size_of_segments[idx];
                    p.name_of_segment[j] = Form1.name_of_segments[idx];
                    idx++;
                }
                processes.Add(p);
            }

            colors     = new Color[15];
            colors[0]  = Color.Maroon;
            colors[1]  = Color.Yellow;
            colors[2]  = Color.Violet;
            colors[3]  = Color.Blue;
            colors[4]  = Color.Lime;
            colors[5]  = Color.Chocolate;
            colors[6]  = Color.Aqua;
            colors[7]  = Color.DarkGreen;
            colors[8]  = Color.Maroon;
            colors[9]  = Color.Pink;
            colors[10] = Color.Silver;
            colors[11] = Color.BlanchedAlmond;
            colors[12] = Color.Cyan;
            colors[13] = Color.Fuchsia;
            colors[14] = Color.Gold;
            if (Form1.type_of_algorithm == 1)
            {
                schedular1 = new firstfit(holes, processes);
                schedular1.First_Fit_Algorithm();
                segments = schedular1.holes;
                ShowDialog4(schedular1.segments_not_allocated);
            }
            else if (Form1.type_of_algorithm == 2)
            {
                schedular2 = new worstfit(holes, processes);
                schedular2.computebestFit();
                segments = schedular2.holes;
                ShowDialog4(schedular2.holdProcesses);
            }
            else if (Form1.type_of_algorithm == 3)
            {
                schedular2 = new worstfit(holes, processes);
                schedular2.computeWorstFit();
                segments = schedular2.holes;
                ShowDialog4(schedular2.holdProcesses);
            }
            Draw();
        }
        private static process ShowDialog2(string text, string caption, int x, int idx)
        {
            process p      = new process(idx, x);
            Form    prompt = new Form();

            prompt.Width      = 600;
            prompt.Height     = 600;
            prompt.AutoScroll = true;
            prompt.Text       = caption;
            NumericUpDown[] values = new NumericUpDown[x];
            TextBox[]       names  = new TextBox[x];
            prompt.BackColor = Color.MistyRose;
            Label textLabel = new Label()
            {
                Left = 50, Top = 64, Width = 200, Height = 41, Text = text, BackColor = Color.Gray, Font = new Font("Segoe UI", 14F)
            };
            //===============================
            NumericUpDown num_oldd = new NumericUpDown();

            num_oldd = new NumericUpDown()
            {
                Left = 100, Top = 123, Maximum = 10000000000, Width = 89, Font = new Font("Segoe UI", 14F), Height = 50, Minimum = 1, BackColor = Color.Bisque, Value = 1, Visible = true, ForeColor = Color.Black
            };
            num_oldd.Enabled = true;
            int y = 60;
            int m = num_oldd.Location.Y;

            for (int j = 0; j < x; j++)
            {
                NumericUpDown numd = new NumericUpDown();
                numd.Size      = num_oldd.Size;
                numd.Minimum   = num_oldd.Minimum;
                numd.Maximum   = num_oldd.Maximum;
                numd.TextAlign = num_oldd.TextAlign;
                numd.Location  = new Point(num_oldd.Location.X, num_oldd.Location.Y + y - 60);
                numd.Enabled   = num_oldd.Enabled;
                numd.BackColor = num_oldd.BackColor;
                numd.Font      = num_oldd.Font;
                numd.TextAlign = num_oldd.TextAlign;
                numd.ReadOnly  = num_oldd.ReadOnly;
                values[j]      = numd;
                prompt.Controls.Add(numd);
                y = y + 60;
            }
            //===============================
            TextBox num_ol = new TextBox();

            num_ol.Enabled = true;
            int yy = 60;

            for (int j = 0; j < x; j++)
            {
                TextBox n = new TextBox();
                n.Size      = num_ol.Size;
                n.TextAlign = num_ol.TextAlign;
                n.Location  = new Point(num_oldd.Location.X + 100, m + yy - 60);
                n.Enabled   = num_ol.Enabled;
                n.BackColor = num_ol.BackColor;
                n.Font      = num_ol.Font;
                n.TextAlign = num_ol.TextAlign;
                n.ReadOnly  = num_ol.ReadOnly;
                names[j]    = n;
                prompt.Controls.Add(n);
                yy = yy + 60;
            }
            Button confirmation = new Button()
            {
                Text = "Ok", Left = 100, Top = yy + 50, ForeColor = Color.DodgerBlue, Width = 88, Height = 46, BackColor = Color.Aquamarine
            };

            confirmation.Click += (sender, e) => {
                bool ok = false;


                for (int i = 0; i < x; i++)
                {
                    p.segmenst_sizes[i] = (int)values[i].Value;
                    String s = names[i].Text.ToString();
                    if (s == "" || s == " " || s == "  " || s == "   " || s == "    " || s == "     " || s == "      " || s == "       " || s == "         " || s == "          " || s == "        ")
                    {
                        ok = true;
                    }
                    p.name_of_segment[i] = s;
                }
                if (ok)
                {
                    MessageBox.Show("Please enter Valid names", "Error",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    prompt.Close();
                }
            };
            prompt.Controls.Add(confirmation);
            prompt.Controls.Add(textLabel);
            prompt.ShowDialog();
            return(p);
        }
예제 #6
0
        public void First_Fit_Algorithm()
        {
            int    process_segments_number;
            int    segment_Size;
            string segment_name;
            int    remained_hole_size;

            //  int allocated_not_done = 0;
            //  int place = 0;

            while (processes.Count > 0)
            {
                //store each process info then remove it:
                process allocation_process = processes[0];
                process p = allocation_process;
                processes.RemoveAt(0);

                if (!Process_IS_Fit(allocation_process))
                {
                    segments_not_allocated.Add(allocation_process);
                    continue;
                }

                process_segments_number = allocation_process.no_of_segments;
                bool[] segment_allocation_done = new bool[process_segments_number];

                for (int i = 0; i < process_segments_number; i++)
                {
                    segment_Size = allocation_process.segmenst_sizes[i];
                    segment_name = allocation_process.name_of_segment[i];
                    segment_allocation_done[i] = false;

                    for (int j = 0; j < holes.Count; j++)
                    {
                        //There is a hole and its size equal or more than the required size for the segment:
                        if ((holes[j].alocated == false) && (holes[j].size >= segment_Size))
                        {
                            holes[j].alocated = true;
                            // segment_allocation_done[i] = true;
                            remained_hole_size     = holes[j].size - segment_Size;
                            holes[j].size          = segment_Size;
                            holes[j].name          = segment_name;
                            holes[j].process_index = allocation_process.process_id;

                            if (remained_hole_size != 0)
                            {
                                holes.Insert(j + 1, new hole(holes[j].start + holes[j].size, remained_hole_size));
                            }

                            break;
                        }
                    }
                    //if (!segment_allocation_done[i])
                    //  allocated_not_done++;
                }

                /*
                 * if (allocated_not_done != 0)
                 * {
                 *  //No one of process segment is allocated:
                 *  if (allocated_not_done == process_segments_number)
                 *  {
                 *      segments_not_allocated.Add(allocation_process);
                 *  }
                 *  else
                 *  {
                 *      p.no_of_segments = allocated_not_done;
                 *      for (int k = 0; k < process_segments_number; k++)
                 *      {
                 *          if (segment_allocation_done[k] == false)
                 *          {
                 *              p.name_of_segment[place] = allocation_process.name_of_segment[k];
                 *              p.segment_sizes[place] = allocation_process.segment_sizes[k];
                 *              place++;
                 *          }
                 *      }
                 *      segments_not_allocated.Add(p);
                 *  }
                 * }
                 */
            }
        }