public memory(List <hole> all_holes)
        {
            segments = new List <hole>();
            hole.sort(all_holes);
            bool ok1 = false, ok2 = false;

            hole first = all_holes[0];
            hole last  = all_holes[all_holes.Count - 1];

            if (first.start == 0 && (last.start + last.size) == Form1.total_memory_size)
            {
                number_of_segments = all_holes.Count + all_holes.Count - 1;
                ok1 = true; ok2 = true;
            }
            else if (first.start == 0 && (last.start + last.size) != Form1.total_memory_size)
            {
                number_of_segments = all_holes.Count + all_holes.Count;
                ok1 = true; ok2 = false;
            }
            else if (first.start != 0 && (last.start + last.size) == Form1.total_memory_size)
            {
                number_of_segments = all_holes.Count + all_holes.Count + 1;
                ok1 = false; ok2 = true;
            }
            else
            {
                number_of_segments = all_holes.Count + all_holes.Count;
                ok1 = false; ok2 = false;
            }

            if (!ok1)
            {
                hole h = new hole(0, all_holes[0].start);
                h.name = "out";
                segments.Add(h);
            }
            for (int i = 0; i < all_holes.Count; i++)
            {
                segments.Add(all_holes[i]);
                if (i != all_holes.Count - 1)
                {
                    int  s  = all_holes[i].start + all_holes[i].size;
                    int  ss = all_holes[i + 1].start - all_holes[i].start - all_holes[i].size;
                    hole h  = new hole(s, ss);
                    h.name = "out";
                    segments.Add(h);
                }
                else
                {
                    if (!ok2)
                    {
                        int  s  = all_holes[i].start + all_holes[i].size;
                        int  ss = Form1.total_memory_size - s;
                        hole h  = new hole(s, ss);
                        h.name = "out";
                        segments.Add(h);
                    }
                }
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            hole.sort(segments);
            int         start = 0;
            List <hole> temp  = new List <hole>();

            for (int i = 0; i < segments.Count; i++)
            {
                if (segments[i].start > start)
                {
                    hole t = new hole(start, segments[i].start - start);
                    temp.Add(t);
                }
                start = segments[i].start + segments[i].size;
            }
            if (start < Form1.total_memory_size)
            {
                hole t = new hole(start, Form1.total_memory_size - start);
                temp.Add(t);
            }
            for (int i = 0; i < temp.Count; i++)
            {
                segments.Add(temp[i]);
            }

            schedular2 = new worstfit(segments, processes);
            schedular2.fix();
            segments = schedular2.holes;
            Form7 form = new Form7(holes, segments, processes, num_of_process, 2);

            form.Show();
            Hide();
        }
Exemple #3
0
 public static void sort_by_size(List <hole> l)
 {
     for (int i = 0; i < l.Count - 1; i++)
     {
         for (int j = i + 1; j < l.Count; j++)
         {
             if (l[i].size > l[j].size)
             {
                 hole temp = l[i];
                 l[i] = l[j];
                 l[j] = temp;
             }
         }
     }
 }
Exemple #4
0
 public static void sort2(List <hole> l)
 {
     for (int i = 0; i < l.Count; i++)
     {
         for (int j = 0; j < l.Count; j++)
         {
             if (l[i].size < l[j].size)
             {
                 hole temp = l[i];
                 l[i] = l[j];
                 l[j] = temp;
             }
         }
     }
 }
        public void fix()
        {
            hole.sort(holes);
            List <hole> tempHoles = new List <hole>(0);
            hole        tempBig   = new MemoryManagmentVisualization.hole(0, 0);

            while (holes.Count > 0)
            {
                hole temp = holes[0];
                holes.RemoveAt(0);
                if (temp.alocated)
                {
                    tempHoles.Add(tempBig);
                    tempBig = new MemoryManagmentVisualization.hole(0, 0);
                    tempHoles.Add(temp);
                }
                else
                {
                    if (tempBig.size == 0)
                    {
                        tempBig = temp;
                    }
                    else if (temp.start == tempBig.start + tempBig.size)
                    {
                        tempBig.size = tempBig.size + temp.size;
                    }
                    else
                    {
                        tempHoles.Add(tempBig);
                        tempBig = temp;
                    }
                }
            }
            if (tempBig.size > 0)
            {
                tempHoles.Add(tempBig);
            }
            holes = tempHoles;
            hole.sort(holes);
            for (int i = 0; i < holes.Count; i++)
            {
                if (holes[i].size == 0)
                {
                    holes.RemoveAt(i); i--;
                }
            }
        }
        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();
        }
Exemple #7
0
        public List <hole> func(List <process> p, List <hole> h)
        {
            hole.sort_by_start(h);
            List <hole> final_memory = new List <hole>();
            List <hole> empty_holes  = new List <hole>();

            empty_holes = copy(h);
            int  no_of_holes = h.Count;
            hole m;
            int  k    = 0;
            int  size = 0;
            int  j;
            int  x;

            int[] counter = new int[no_of_holes];

            int total_sgements = 0;

            for (int i = 0; i < p.Count; i++)
            {
                total_sgements = p[i].no_of_segments + total_sgements;
            }

            //  Compaction_Before(empty_holes, ref no_of_holes);
            while (p.Count > 0)
            {
                List <hole> temperory_memory = new List <hole>(); // for segments of each process
                for (x = 0; x < p[0].no_of_segments; x++)
                {
                    List <hole> best_holes = new List <hole>();
                    for (j = 0; j < no_of_holes; j++)
                    {
                        if (!empty_holes[j].alocated)
                        {
                            if (p[0].segmenst_sizes[x] <= empty_holes[j].size)
                            {
                                size = h[j].size - p[0].segmenst_sizes[x];
                                k++;
                                m         = new hole(h[j].start, size);
                                m.hole_id = h[j].hole_id;
                                best_holes.Add(m);

                                if (k > 1)
                                {
                                    hole.sort_by_size(best_holes);
                                }
                            }
                        }
                    }

                    if (k != 0)
                    {
                        if (best_holes[0].size == 0)
                        {
                            empty_holes[best_holes[0].hole_id].alocated      = true;
                            empty_holes[best_holes[0].hole_id].name          = p[0].name_of_segment[x];
                            empty_holes[best_holes[0].hole_id].process_index = p[0].process_id;
                            temperory_memory.Add(h[best_holes[0].hole_id]);
                        }

                        else
                        {
                            int  a = best_holes[0].start;
                            int  b = p[0].segmenst_sizes[x];
                            hole t = new hole(a, b);
                            t.alocated      = true;
                            t.name          = p[0].name_of_segment[x];
                            t.process_index = p[0].process_id;
                            temperory_memory.Add(t);

                            // second hole after splitting
                            int  z = t.start + t.size;
                            int  y = best_holes[0].size;
                            hole s = new hole(z, y);
                            s.hole_id = best_holes[0].hole_id;
                            temperory_memory.Add(s);
                            // Compaction_After(empty_holes, best_holes, ref no_of_holes);
                            hole.sort_by_start(empty_holes);
                        }
                        k = 0;
                    }
                }
                int count = 0;
                if (temperory_memory.Count == 0)
                {
                    p.Remove(p[0]);
                }
                for (int y = 0; y < temperory_memory.Count; y++)
                {
                    for (int u = 0; p.Count > 0 && u < p[0].no_of_segments; u++)
                    {
                        if (temperory_memory[y].name == p[0].name_of_segment[u])
                        {
                            count++;
                            if (count == p[0].no_of_segments)
                            {
                                for (int q = 0; q < temperory_memory.Count; q++)
                                {
                                    if (temperory_memory[q].name == "hole")
                                    {
                                        empty_holes[temperory_memory[q].hole_id].start = temperory_memory[q].start;
                                        empty_holes[temperory_memory[q].hole_id].size  = temperory_memory[q].size;
                                        hole.sort_by_start(empty_holes);
                                    }
                                    else
                                    {
                                        final_memory.Add(temperory_memory[q]);
                                        //temperory_memory.Remove(temperory_memory[q]);
                                        p.Remove(p[0]);
                                        for (int w = 0; w < empty_holes.Count; w++)
                                        {
                                            if (temperory_memory[q].start == empty_holes[w].start && temperory_memory[q].size == empty_holes[w].size)
                                            {
                                                empty_holes[w].process_index = p[0].process_id;
                                            }
                                        }
                                    }
                                }
                            }
                            if (y == temperory_memory.Count - 1)
                            {
                                remaining_processes.Add(p[0]);
                                p.Remove(p[0]);
                                for (int q = 0; q < temperory_memory.Count; q++)
                                {
                                    for (int w = 0; w < empty_holes.Count; w++)
                                    {
                                        if (temperory_memory[q].start == empty_holes[w].start && temperory_memory[q].size == empty_holes[w].size)
                                        {
                                            empty_holes[w].alocated = false;
                                            empty_holes[w].name     = "hole";
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                hole.sort_by_start(final_memory);
            }

            for (int i = 0; i < no_of_holes; i++)
            {
                if (!empty_holes[i].alocated)
                {
                    final_memory.Add(empty_holes[i]);
                    hole.sort_by_start(final_memory);
                }
            }

            return(final_memory);
        }