Exemplo n.º 1
0
        void next_segment_info_Click(object sender, System.EventArgs e)
        {
            segment_info_label.Hide();
            segment_name_label.Hide();
            segment_size_label.Hide();
            string search_name, search_size, search_name_label, search_size_label;

            for (int j = 0; j < current_process.number_of_segments; j++)
            {
                search_name       = "textBoxName" + current_process_id;
                search_name      += j;
                search_size       = "textBoxSize" + current_process_id;
                search_size      += j;
                search_name_label = "segment_name_label" + j;
                search_size_label = "segment_size_label" + j;
                TextBox name_value = (TextBox)this.Controls.Find(search_name, false).FirstOrDefault();
                TextBox size_value = (TextBox)this.Controls.Find(search_size, false).FirstOrDefault();
                //Input error checking
                if (string.IsNullOrWhiteSpace(name_value.Text) || string.IsNullOrWhiteSpace(size_value.Text))
                {
                    System.Windows.Forms.MessageBox.Show("You can't leave any field empty, please fill all fields.");
                    name_value.Hide();
                    size_value.Hide();
                    name_value = (TextBox)this.Controls.Find(search_name.Substring(0, search_name.Length - 1), false).FirstOrDefault();
                    size_value = (TextBox)this.Controls.Find(search_size.Substring(0, search_size.Length - 1), false).FirstOrDefault();
                    while (name_value != null && size_value != null)
                    {
                        name_value = (TextBox)this.Controls.Find(search_name.Substring(0, search_name.Length - 1), false).FirstOrDefault();
                        size_value = (TextBox)this.Controls.Find(search_size.Substring(0, search_size.Length - 1), false).FirstOrDefault();
                        name_value.Hide();
                        size_value.Hide();
                    }
                    next_segment_info.Hide();
                    return;
                }
                if (int.Parse(size_value.Text) <= 0)
                {
                    System.Windows.Forms.MessageBox.Show("Segments size must be greater than 0.");
                    name_value.Hide();
                    size_value.Hide();
                    name_value = (TextBox)this.Controls.Find(search_name.Substring(0, search_name.Length - 1), false).FirstOrDefault();
                    size_value = (TextBox)this.Controls.Find(search_size.Substring(0, search_size.Length - 1), false).FirstOrDefault();
                    while (name_value != null && size_value != null)
                    {
                        name_value = (TextBox)this.Controls.Find(search_name.Substring(0, search_name.Length - 1), false).FirstOrDefault();
                        size_value = (TextBox)this.Controls.Find(search_size.Substring(0, search_size.Length - 1), false).FirstOrDefault();
                        name_value.Hide();
                        size_value.Hide();
                    }
                    next_segment_info.Hide();
                    return;
                }
                ///////////////////////
                current_process.name_of_segments[j] = name_value.Text;
                current_process.size_of_segments[j] = int.Parse(size_value.Text);
                name_value.Hide();
                size_value.Hide();
            }

            next_segment_info.Hide();
            Add.Show();
            if (method == "First Fit")
            {
                for (int i = 0; i < current_process.number_of_segments; i++)
                {
                    for (int j = 0; j < no_of_holes; j++)
                    {
                        if (current_process.size_of_segments[i] < holesSize[j])
                        {
                            drawrectangle(holesStart[j], (current_process.name + ":" + current_process.name_of_segments[i]), current_process.size_of_segments[i]);
                            current_process.start_address_segment[i] = holesStart[j];
                            holesStart[j] = holesStart[j] + current_process.size_of_segments[i];
                            holesSize[j]  = holesSize[j] - current_process.size_of_segments[i];
                            merge_holes();
                            i++;
                            break;
                        }
                        else if (current_process.size_of_segments[i] == holesSize[j])
                        {
                            drawrectangle(holesStart[j], (current_process.name + ":" + current_process.name_of_segments[i]), current_process.size_of_segments[i]);
                            current_process.start_address_segment[i] = holesStart[j];
                            holesStart.Remove(holesStart[j]);
                            holesSize.Remove(holesSize[j]);
                            no_of_holes--;
                            i++;
                            break;
                        }
                        else if (no_of_holes <= 0 || (current_process.size_of_segments[i] > holesSize[j] && j == (no_of_holes - 1)))
                        {
                            for (int k = 0; k < i; k++)
                            {
                                holesStart.Add(current_process.start_address_segment[k]);
                                holesSize.Add(current_process.size_of_segments[k]);
                                no_of_holes++;
                            }
                            sort_start(ref holesStart, ref holesSize);
                            merge_holes();
                            System.Windows.Forms.MessageBox.Show("No hole fits the " + current_process.name_of_segments[i] + " of process " + current_process.name);
                            return;
                        }
                    }

                    if (no_of_holes <= 0 && i < current_process.number_of_segments)
                    {
                        for (int k = 0; k < i; k++)
                        {
                            holesStart.Add(current_process.start_address_segment[k]);
                            holesSize.Add(current_process.size_of_segments[k]);
                            no_of_holes++;
                        }
                        sort_start(ref holesStart, ref holesSize);
                        merge_holes();
                        System.Windows.Forms.MessageBox.Show("No hole fits the " + current_process.name_of_segments[i] + " of process " + current_process.name);
                        return;
                    }
                    i--;
                }
                running_processes.Items.Add(current_process.name);
                user_processes.Add(current_process);
            }
            else if (method == "Best Fit")
            {
                int fittest_hole = 0;
                int found_hole   = 0;
                for (int i = 0; i < current_process.number_of_segments; i++)
                {
                    found_hole   = 0;
                    fittest_hole = 0;
                    sort_size(ref holesStart, ref holesSize);
                    for (int j = 0; j < no_of_holes; j++)
                    {
                        if (current_process.size_of_segments[i] < holesSize[j])
                        {
                            found_hole = 1;
                            if (holesSize[j] < holesSize[fittest_hole] || current_process.size_of_segments[i] > holesSize[fittest_hole])
                            {
                                fittest_hole = j;
                            }
                        }
                        else if (current_process.size_of_segments[i] == holesSize[j])
                        {
                            found_hole   = 2;
                            fittest_hole = j;
                            break;
                        }
                    }

                    if (found_hole == 1)
                    {
                        drawrectangle(holesStart[fittest_hole], (current_process.name + ":" + current_process.name_of_segments[i]), current_process.size_of_segments[i]);
                        current_process.start_address_segment[i] = holesStart[fittest_hole];
                        holesStart[fittest_hole] = holesStart[fittest_hole] + current_process.size_of_segments[i];
                        holesSize[fittest_hole]  = holesSize[fittest_hole] - current_process.size_of_segments[i];
                        drawrectangle(holesStart[fittest_hole], "Hole", holesSize[fittest_hole]);
                        sort_start(ref holesStart, ref holesSize);
                    }
                    else if (found_hole == 2)
                    {
                        drawrectangle(holesStart[fittest_hole], (current_process.name + ":" + current_process.name_of_segments[i]), current_process.size_of_segments[i]);
                        current_process.start_address_segment[i] = holesStart[fittest_hole];
                        holesStart.Remove(holesStart[fittest_hole]);
                        holesSize.Remove(holesSize[fittest_hole]);
                        no_of_holes--;
                    }
                    else
                    {
                        for (int k = 0; k < i; k++)
                        {
                            holesStart.Add(current_process.start_address_segment[k]);
                            holesSize.Add(current_process.size_of_segments[k]);
                            no_of_holes++;
                        }
                        sort_start(ref holesStart, ref holesSize);
                        merge_holes();
                        System.Windows.Forms.MessageBox.Show("No hole fits the " + current_process.name_of_segments[i] + " of process " + current_process.name);
                        return;
                    }
                }
                running_processes.Items.Add(current_process.name);
                user_processes.Add(current_process);
            }
            else if (method == "Worst Fit")
            {
                int unfittest_hole;
                int found_hole;
                for (int i = 0; i < current_process.number_of_segments; i++)
                {
                    found_hole     = 0;
                    unfittest_hole = 0;
                    sort_size_desc(ref holesStart, ref holesSize);
                    for (int j = 0; j < no_of_holes; j++)
                    {
                        if (current_process.size_of_segments[i] < holesSize[j])
                        {
                            if (holesSize[j] > holesSize[unfittest_hole] || found_hole == 0)
                            {
                                found_hole     = 1;
                                unfittest_hole = j;
                            }
                        }
                        else if (current_process.size_of_segments[i] == holesSize[j] && found_hole != 1)
                        {
                            found_hole     = 2;
                            unfittest_hole = j;
                            break;
                        }
                    }
                    if (found_hole == 1)
                    {
                        drawrectangle(holesStart[unfittest_hole], (current_process.name + ":" + current_process.name_of_segments[i]), current_process.size_of_segments[i]);
                        current_process.start_address_segment[i] = holesStart[unfittest_hole];
                        holesStart[unfittest_hole] = holesStart[unfittest_hole] + current_process.size_of_segments[i];
                        holesSize[unfittest_hole]  = holesSize[unfittest_hole] - current_process.size_of_segments[i];
                        drawrectangle(holesStart[unfittest_hole], "Hole", holesSize[unfittest_hole]);
                    }
                    else if (found_hole == 2)
                    {
                        drawrectangle(holesStart[unfittest_hole], (current_process.name + ":" + current_process.name_of_segments[i]), current_process.size_of_segments[i]);
                        current_process.start_address_segment[i] = holesStart[unfittest_hole];
                        holesStart.Remove(holesStart[unfittest_hole]);
                        holesSize.Remove(holesSize[unfittest_hole]);
                        no_of_holes--;
                    }
                    else
                    {
                        for (int k = 0; k < i; k++)
                        {
                            holesStart.Add(current_process.start_address_segment[k]);
                            holesSize.Add(current_process.size_of_segments[k]);
                            no_of_holes++;
                        }
                        sort_start(ref holesStart, ref holesSize);
                        merge_holes();
                        System.Windows.Forms.MessageBox.Show("No hole fits the " + current_process.name_of_segments[i] + " of process " + current_process.name);
                        return;
                    }
                }
                running_processes.Items.Add(current_process.name);
                user_processes.Add(current_process);
            }
        }