public static List <memoryitem> FirstFit(process newprocess, List <memoryitem> memory) //approved
        {
            process nprocess = newprocess;

            foreach (var item in memory)
            {
                if (item.type == "h")
                {
                    if (item.size >= newprocess.size)
                    {
                        //get el starting address w el size
                        int startaddress = item.getstratingadd();
                        int size         = item.getsize();
                        //add new process in pos (starting address)
                        nprocess.setstarting_add(startaddress);
                        memory.Add(nprocess);
                        //add new hole in pos start address+size of process its size equals ( old hole size - new process size)
                        hole nhole = new hole((startaddress + nprocess.getsize()), (size - nprocess.getsize()));
                        memory.Add(nhole);
                        //delete el hole el adema
                        memory.Remove(item);
                        allprocess.Add(nprocess);
                        break;
                    }
                    //else Console.WriteLine("Process {0} is aded to waiting queue.", nprocess.getname());
                }
            }
            concatenate(memory);
            return(memory);
        }
        public static List <memoryitem> WorstFit(process newprocess, List <memoryitem> memory) //approved
        {
            process           nprocess = newprocess;
            List <memoryitem> temp     = memory;

            temp.OrderByDescending(d => d.getsize());
            foreach (var item in temp)
            {
                if (item.type == "h")
                {
                    if (item.size >= newprocess.size)
                    {
                        //get el starting address w el size
                        int startaddress = item.getstratingadd();
                        int size         = item.getsize();
                        //add new process in pos (starting address)
                        nprocess.setstarting_add(startaddress);
                        memory.Add(nprocess);
                        //add new hole in pos start address+size of process its size equals ( old hole size - new process size)
                        hole nhole = new hole((startaddress + nprocess.getsize()), (size - nprocess.getsize()));
                        memory.Add(nhole);
                        //delete el hole el adema
                        memory.Remove(item);
                        allprocess.Add(nprocess);
                        break;
                    }
                }
            }
            concatenate(memory);
            return(memory);
        }
Exemple #3
0
        private void label_Click(object sender, EventArgs e)
        {
            string            message = "are you sure you want to delete all segments of this process?";
            string            caption = "";
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult      result;

            // Displays the MessageBox.
            result = MessageBox.Show(message, caption, buttons);
            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                Label clickedLabel = sender as Label;
                hole  p            = new hole();
                foreach (hole h in memory)
                {
                    if (h.Owner.Name + h.Name == clickedLabel.Text)
                    {
                        p = h;
                    }
                }


                deallocate(p, clickedLabel);
            }
        }
Exemple #4
0
        void generate_holes()
        {
            double current = 0;

            if (seg_list.Count == 0)
            {
                h_list.Clear();
                hole h = new hole(current, memSize);
                h_list.Add(h);
            }
            else
            {
                seg_list.Sort((x, y) => x.start.CompareTo(y.start));
                h_list.Clear();
                for (int i = 0; i < seg_list.Count; i++)
                {
                    if (current < seg_list[i].start)
                    {
                        hole h = new hole(current, seg_list[i].start - current);
                        h_list.Add(h);
                        current = seg_list[i].start + seg_list[i].size;
                    }
                    else
                    {
                        current += seg_list[i].size;
                    }
                }
                if (current < memSize)
                {
                    hole h = new hole(current, memSize - current);
                    h_list.Add(h);
                    current = memSize;
                }
            }
        }
        public static void concatenate(List <memoryitem> L) // approved
        {
            List <memoryitem> temp  = L;
            List <memoryitem> holes = new List <memoryitem>();
            hole h = new hole();

            foreach (var item in temp)
            {
                if (item.type == "h")
                {
                    holes.Add(item);
                }
            }
            holes.OrderBy(add => add.getstratingadd());
            for (int i = 0; i < holes.Count - 1; i++)
            {
                int size1 = holes[i].getsize();
                int size2 = holes[i + 1].getsize();
                int add1  = holes[i].getstratingadd();
                int add2  = holes[i + 1].getstratingadd();
                if (add1 + size1 == add2)
                {
                    L.Remove(holes[i]);
                    L.Remove(holes[i + 1]);
                    h.setsize(size1 + size2);
                    h.setstarting_add(add1);
                    L.Add(h);
                }
            }
        }
Exemple #6
0
 void add_holes(List <double> start, List <double> size)
 {
     for (int i = 0; i < start.Count; i++)
     {
         hole h = new hole(start[i], size[i]);
         h_list.Add(h);
     }
 }
        private void process_btn_Click(object sender, EventArgs e)
        {
            hole s = new hole();

            if (name_of_segment_tb.Text != "" || size_of_sigment_tb.Text != "")
            {
                s.Name = name_of_segment_tb.Text;
                if (double.TryParse(size_of_sigment_tb.Text, out double temp1))
                {
                    s.Size = temp1;
                }
                else
                {
                    MessageBox.Show("Invalid segment size!!!!!!!!");
                    return;
                }
            }
            else
            {
                MessageBox.Show("enter all the info!!");
            }


            s.Owner = processes.Last.Value;
            processes.Last.Value.segments.AddLast(s);

            segments_enterd++;

            if (segments_enterd == current_process_no_segments)
            {
                name_of_segment_tb.Enabled = false;
                size_of_sigment_tb.Enabled = false;
                process_btn.Enabled        = false;

                segments_enterd = 0;
                number_of_segments_tb.Enabled  = true;
                number_of_segments_btn.Enabled = true;
                button1.Enabled = true;
            }

            name_of_segment_tb.Text = "";
            size_of_sigment_tb.Text = "";

            label1.Text = "";
            foreach (Process P in processes)
            {
                label1.Text += P.Name + "\n";
                foreach (hole h in P.segments)
                {
                    label1.Text += h.Name + " " + h.Size + "\n";
                }
            }
        }
        private void deallocate(hole s, Label l)
        {
            if (s.Name.Contains("old process"))
            {
                s.Name = "hole" + holes.Count;
                holes.AddLast(s);
                holes_sorted_by_address.Clear();

                IEnumerable <hole> query = holes.OrderBy(h => h.Address);

                foreach (hole h in query)
                {
                    holes_sorted_by_address.AddLast(h);
                }
                //l.Text = s.Name;
                check_adjacent_holes();
                delete(ram1);
                IEnumerable <hole> query1 = memory.OrderBy(h => h.Address);
                foreach (hole h in query1)
                {
                    draw(h, ram1);
                }
            }
            else if (s.Owner.Name != "")
            {
                Process process_to_remove = s.Owner;


                foreach (hole segment in process_to_remove.segments)
                {
                    segment.Name          = "hole" + holes.Count;
                    segment.Hole_Occupied = "";
                    segment.Owner         = new Process();
                    holes.AddLast(segment);
                    holes_sorted_by_address.Clear();

                    IEnumerable <hole> query1 = holes.OrderBy(h => h.Address);

                    foreach (hole h in query1)
                    {
                        holes_sorted_by_address.AddLast(h);
                    }
                    //   l.Text = segment.Name;
                }
                check_adjacent_holes();
                delete(ram1);
                IEnumerable <hole> query = memory.OrderBy(h => h.Address);
                foreach (hole h in query)
                {
                    draw(h, ram1);
                }
            }
        }
Exemple #9
0
 void fix_holes()
 {
     for (int i = 0; i < h_list.Count - 1; i++)
     {
         if (h_list[i].start + h_list[i].size == h_list[i + 1].start)
         {
             hole h = new hole(h_list[i].start, h_list[i + 1].size + h_list[i].size);
             h_list.RemoveAt(i);
             h_list.RemoveAt(i);
             h_list.Insert(i, h);
         }
     }
 }
        /* private void GenerateTable(int columnCount, int rowCount)
         * {
         *   //Clear out the existing controls, we are generating a new table layout
         *   tableLayoutPanel1.Controls.Clear();
         *
         *   //Clear out the existing row and column styles
         *   tableLayoutPanel1.ColumnStyles.Clear();
         *   tableLayoutPanel1.RowStyles.Clear();
         *
         *   //Now we will generate the table, setting up the row and column counts first
         *   tableLayoutPanel1.ColumnCount = columnCount;
         *   tableLayoutPanel1.RowCount = rowCount;
         *
         *   for (int x = 0; x < columnCount; x++)
         *   {
         *       //First add a column
         *       tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
         *
         *       for (int y = 0; y < rowCount; y++)
         *       {
         *           //Next, add a row.  Only do this when once, when creating the first column
         *           if (x == 0)
         *           {
         *               tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
         *           }
         *
         *           //Create the control, in this case we will add a button
         *           Label cmd = new Label();
         *           cmd.Text = "ahmed";         //Finally, add the control to the correct location in the table
         *           tableLayoutPanel1.Controls.Add(cmd, x, y);
         *       }
         *   }
         * }
         */

        private void check_adjacent_holes()
        {
            int counter = 0;

            hole[] holes_sorted = new hole[holes_sorted_by_address.Count];
            int    i            = 0;

            foreach (hole h in holes_sorted_by_address)
            {
                holes_sorted[i] = h;


                i++;
            }
            while (true)
            {
                counter = 0;



                for (int j = 0; j < holes_sorted_by_address.Count - 1; j++)
                {
                    for (int k = j + 1; k < holes_sorted_by_address.Count; k++)
                    {
                        if ((holes_sorted[j].Address > -1 && holes_sorted[k].Address > -1) && (holes_sorted[j].Size > 0 && holes_sorted[k].Size > 0) && holes_sorted[j].Address + holes_sorted[j].Size == holes_sorted[k].Address)
                        {
                            holes_sorted[j].Size += holes_sorted[k].Size;

                            holes_sorted[k].Size    = 0;
                            holes_sorted[k].Address = -1;


                            counter++;
                        }
                    }
                }

                if (counter == 0)
                {
                    holes_sorted_by_address.Clear();
                    foreach (hole h in holes_sorted)
                    {
                        holes_sorted_by_address.AddLast(h);
                    }

                    break;
                }
            }
        }
        static void Main(string[] args)
        {
            hole    h1 = new hole(50, 220);
            hole    h2 = new hole(300, 100);
            hole    h3 = new hole(560, 140);
            hole    h4 = new hole(700, 80);
            process p1 = new process("p1", 150);
            process p2 = new process("p2", 130);
            process p3 = new process("p3", 270);
            process p4 = new process("p4", 50);
            process p5 = new process("p5", 180);
            process p6 = new process("p6", 90);

            List <memoryitem> x = new List <memoryitem>();

            x.Add(h1); x.Add(h2); x.Add(h3); x.Add(h4);

            //MemoryAllocation.methodology.FirstFit(p1, x);
            //MemoryAllocation.methodology.FirstFit(p2, x);
            //MemoryAllocation.methodology.FirstFit(p3, x);
            //MemoryAllocation.methodology.FirstFit(p4, x);
            //MemoryAllocation.methodology.FirstFit(p5, x);
            //MemoryAllocation.methodology.FirstFit(p6, x);
            //MemoryAllocation.methodology.deallocate("p4", x);

            MemoryAllocation.methodology.BestFit(p1, x);
            MemoryAllocation.methodology.BestFit(p2, x);
            MemoryAllocation.methodology.BestFit(p3, x);
            MemoryAllocation.methodology.BestFit(p4, x);
            MemoryAllocation.methodology.BestFit(p5, x);
            MemoryAllocation.methodology.BestFit(p6, x);

            //MemoryAllocation.methodology.WorstFit(p1, x);
            //MemoryAllocation.methodology.WorstFit(p2, x);
            //MemoryAllocation.methodology.WorstFit(p3, x);
            //MemoryAllocation.methodology.WorstFit(p4, x);
            //MemoryAllocation.methodology.WorstFit(p5, x);
            //MemoryAllocation.methodology.WorstFit(p6, x);

            foreach (var item in x)
            {
                Console.Write(item.getstratingadd());
                Console.WriteLine();
                Console.Write(item.getsize());
                Console.WriteLine();
            }
        }
        public static List <memoryitem> deallocate(string processname, List <memoryitem> memory)
        {
            string name = processname;

            foreach (var item in memory)
            {
                if (item.type == "p" && item.getname() == name)
                {
                    int size    = item.getsize();
                    int address = item.getstratingadd();
                    memory.Remove(item);
                    allprocess.Remove(item);
                    hole nhole = new hole((address), (size));
                    memory.Add(nhole);
                    break;
                }
            }
            concatenate(memory);
            return(memory);
        }
        private void draw(hole h, TableLayoutPanel t)
        {
            if (h.Size > 0 && h.Address > -1)
            {
                Label    l    = new Label();
                RowStyle temp = t.RowStyles[t.RowCount - 1];
                t.RowCount++;
                t.RowStyles.Add(new RowStyle(temp.SizeType, 50));
                t.Controls.Add(l, 1, t.RowCount - 1);
                l.Text = h.Owner.Name + h.Name;
                if (h.Name.Contains("hole"))
                {
                    l.Text = "hole";
                }
                if (h.Owner.Name != "" || h.Name.Contains("old process"))
                {
                    l.Click += new EventHandler(this.label_Click);
                }
                if (h.Address == 0)
                {
                    t.Controls.Add(new Label()
                    {
                        Text = h.Address + "\n" + (h.Address + h.Size)
                    }, 0, t.RowCount - 1);
                }
                else
                {
                    t.Controls.Add(new Label()
                    {
                        Text = "\n" + (h.Address + h.Size)
                    }, 0, t.RowCount - 1);
                }

                /* tableLayoutPanel1.RowCount++;
                 * tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                 * tableLayoutPanel1.Controls.Add(new Label() { Text = h.Name }, 1, tableLayoutPanel1.RowCount - 1);
                 * tableLayoutPanel1.Controls.Add(new Label() { Text = h.Address + "\n" + (h.Address + h.Size) }, 0, tableLayoutPanel1.RowCount - 1);
                 */
            }
        }
 public void inserthole(hole hole, List <memoryitem> memory)
 {
     // check holes
     memory.Add(hole);
 }
Exemple #15
0
        private void best_fit()
        {
            hole[]             temp_holes  = new hole[holes_sorted_by_address.Count];
            LinkedList <hole>  temp_memory = new LinkedList <hole>();
            IEnumerable <hole> query1      = holes_sorted_by_address.OrderBy(h => h.Size);

            int i = 0;

            foreach (hole h in query1)
            {
                temp_holes[i] = h;
                i++;
            }

            foreach (Process p in processes)
            {
                if (p.Allocated == false)
                {
                    foreach (hole s in p.segments)
                    {
                        foreach (hole h in temp_holes)
                        {
                            if (h.Size >= s.Size)
                            {
                                s.Address       = h.Address;
                                h.Address      += s.Size;
                                h.Size         -= s.Size;
                                s.Hole_Occupied = h.Name;


                                break;
                            }
                        }

                        if (s.Address == -1)
                        {
                            p.Fit = false;
                            foreach (hole segment in p.segments)
                            {
                                if (segment.Hole_Occupied == "")
                                {
                                    break;
                                }
                                foreach (hole h in temp_holes)
                                {
                                    if (segment.Hole_Occupied == h.Name)
                                    {
                                        h.Address      -= segment.Size;
                                        h.Size         += segment.Size;
                                        s.Address       = -1;
                                        s.Hole_Occupied = "";


                                        break;
                                    }
                                }
                            }


                            break;
                        }
                    }
                }
            }



            foreach (hole h in temp_holes)
            {
                temp_memory.AddLast(h);
            }
            foreach (Process p in processes)
            {
                if (p.Fit == true && p.Allocated == false)
                {
                    foreach (hole s in p.segments)
                    {
                        memory.AddLast(s);

                        p.Allocated = true;
                    }
                }
            }

            IEnumerable <hole> query = memory.OrderBy(h => h.Address);

            foreach (hole h in query)
            {
                draw(h, ram1);
            }
        }
Exemple #16
0
        private void memory_init()
        {
            int i = 0;
            IEnumerable <hole> query = holes.OrderBy(h => h.Address);

            hole[] holes_sorted = new hole[holes.Count];
            foreach (hole h in query)
            {
                holes_sorted[i] = h;
                holes_sorted_by_address.AddLast(h);

                i++;
            }



            for (int j = 0; j < holes.Count; j++)
            {
                if (j == 0)
                {
                    if (holes_sorted[j].Address > 0)
                    {
                        hole temp = new hole();
                        temp.Address = 0;
                        temp.Size    = holes_sorted[j].Address;
                        temp.Name    = "old process" + old_processes.Count;
                        old_processes.AddLast(temp);
                    }
                    if (holes.Count == 1)
                    {
                        if (holes_sorted[j].Address + holes_sorted[j].Size < globals.total_memory_size)
                        {
                            hole temp1 = new hole();
                            temp1.Address = holes_sorted[j].Address + holes_sorted[j].Size;
                            temp1.Size    = globals.total_memory_size - temp1.Address;
                            temp1.Name    = "old process" + old_processes.Count;
                            old_processes.AddLast(temp1);
                        }
                    }
                }
                else if (holes_sorted[j].Address > holes_sorted[j - 1].Address + holes_sorted[j - 1].Size)
                {
                    hole temp = new hole();
                    temp.Address = holes_sorted[j - 1].Address + holes_sorted[j - 1].Size;
                    temp.Size    = holes_sorted[j].Address - temp.Address;
                    temp.Name    = "old process" + old_processes.Count;
                    old_processes.AddLast(temp);
                    if (j == holes.Count - 1)
                    {
                        if (holes_sorted[j].Address + holes_sorted[j].Size < globals.total_memory_size)
                        {
                            hole temp1 = new hole();
                            temp1.Address = holes_sorted[j].Address + holes_sorted[j].Size;
                            temp1.Size    = globals.total_memory_size - temp1.Address;
                            temp1.Name    = "old process" + old_processes.Count;
                            old_processes.AddLast(temp1);
                        }
                    }
                }
                else if (j == holes.Count - 1)
                {
                    if (holes_sorted[j].Address + holes_sorted[j].Size < globals.total_memory_size)
                    {
                        hole temp1 = new hole();
                        temp1.Address = holes_sorted[j].Address + holes_sorted[j].Size;
                        temp1.Size    = globals.total_memory_size - temp1.Address;
                        temp1.Name    = "old process" + old_processes.Count;
                        old_processes.AddLast(temp1);
                    }
                }
            }

            //*******************************************************************************

            IEnumerable <hole> query2 = holes_sorted_by_address.Concat(old_processes);
            IEnumerable <hole> query1 = query2.OrderBy(h => h.Address);

            //********************************************************************************
            foreach (hole h in query1)
            {
                memory.AddLast(h);
            }

            foreach (hole h in memory)
            {
                //GenerateTable(2, 1);

                draw(h, ram);
            }
        }
Exemple #17
0
        private void hole_btn_Click(object sender, EventArgs e)
        {
            int  i = 0;
            hole h = new hole();

            label3.Text = "";
            if (Int64.TryParse(hole_address_tb.Text, out Int64 temp))
            {
                if (holes.Count > 0)
                {
                    foreach (hole x in holes)
                    {
                        if (temp >= x.Address && temp < x.Address + x.Size)
                        {
                            hole_address_tb.Text = "";
                            hole_size_tb.Text    = "";
                            MessageBox.Show("Invalid hole 1!!!!!!!!");
                            return;
                        }
                    }
                }

                h.Address            = temp;
                hole_address_tb.Text = "";
            }
            else
            {
                hole_address_tb.Text = "";
                hole_size_tb.Text    = "";
                MessageBox.Show("Invalid hole 2!!!!!!!!");
                return;
            }

            if (double.TryParse(hole_size_tb.Text, out double temp1))
            {
                if (holes.Count > 0)
                {
                    foreach (hole x in holes)
                    {
                        if (temp + temp1 > x.Address && temp + temp1 < x.Address + x.Size)
                        {
                            hole_address_tb.Text = "";
                            hole_size_tb.Text    = "";
                            MessageBox.Show("Invalid hole 3!!!!!!!!");
                            return;
                        }
                    }
                }

                h.Size            = temp1;
                hole_size_tb.Text = "";
            }
            else
            {
                hole_address_tb.Text = "";
                hole_size_tb.Text    = "";
                MessageBox.Show("Invalid hole 4!!!!!!!!");
            }

            if (h.Address == -1 || h.Size == -1)
            {
                hole_address_tb.Text = "";
                hole_size_tb.Text    = "";
                MessageBox.Show("Invalid hole 5!!!!!!!!");
            }
            else
            {
                holes.AddLast(h);
            }


            foreach (hole x in holes)
            {
                x.Name       = "hole" + i;
                label3.Text += x.Name + "  " + x.Address + "  " + x.Size + "\n";
                i++;
            }
        }
Exemple #18
0
        public void newHole(List <hole> hole_list, double st, double sz)
        {
            hole h = new hole(st, sz);

            hole_list.Add(h);
        }