public void check_the_name(ref pro processes)
        {
            int j     = 0;
            int flag  = 0;
            int first = 0;

lo:
            for (int i = 0; i < mylist.Count; i++)
            {
                if (mylist[i].name == processes.name)
                {
                    flag = 1;
                }
            }
            j++;
            if ((first == 0) && (flag == 1))
            {
                first           = 1;
                flag            = 0;
                processes.name += "-copy(";
                processes.name += j.ToString();
                processes.name += ")";
                goto lo;
            }
            else if (flag == 1)
            {
                flag = 0;
                int postion;
                postion         = processes.name.LastIndexOf("(");
                processes.name  = processes.name.Remove(postion, processes.name.Count() - postion);
                processes.name += "(";
                processes.name += j.ToString();
                processes.name += ")";
                goto lo;
            }
        }
        public bool first_fit(pro processes, ref List <block> printlist)
        {
            //at first clear the list to be returned
            printlist.Clear();

            check_the_name(ref processes);
            // no need to sort as it is first fit
            block temp;

            for (int i = 0; i < mylist.Count; i++)
            {
                //traverse untill u find the place and break that block and put in it
                if (mylist[i].type == 0)
                {
                    if (mylist[i].blocksize > processes.size)
                    {
                        temp = mylist[i];
                        int size_remainig = temp.blocksize - processes.size;
                        mylist.RemoveAt(i);
                        temp.name          = processes.name;
                        temp.type          = 1;
                        temp.blocksize     = processes.size;
                        temp.endingaddress = processes.size + temp.startingaddress;
                        mylist.Insert(i, temp);
                        temp.startingaddress = temp.endingaddress;
                        temp.blocksize       = size_remainig;
                        temp.name            = "HOLE";
                        temp.type            = 0;
                        temp.endingaddress   = size_remainig + temp.startingaddress;
                        mylist.Insert(i + 1, temp);
                        // copy from your list to the other


                        printlist = new List <block>(mylist);
                        // print();
                        return(true);
                    }
                    if (mylist[i].blocksize == processes.size)
                    {
                        temp = mylist[i];
                        mylist.RemoveAt(i);
                        temp.name = processes.name;
                        temp.type = 1;
                        mylist.Insert(i, temp);
                        // copy from your list to the other


                        printlist = new List <block>(mylist);
                        //              print();
                        return(true);
                    }
                }
            }

            // copy from your list to the other
            //printlist = mylist.ToList();
            //   copy_list(ref printlist);
            printlist = new List <block>(mylist);
            //      print();
            return(false);
        }
        public bool worst_fit(pro processes, ref List <block> printlist)
        {
            //at first clear the list to be returned
            printlist.Clear();
            check_the_name(ref processes);

            //sort
            mylist.Sort((x, y) => x.blocksize.CompareTo(y.blocksize));
            //reverse
            mylist.Reverse();//to get bigger first
            block temp;

            for (int i = 0; i < mylist.Count; i++)
            {
                //traverse untill u find the place and break that block and put in it
                if (mylist[i].type == 0)
                {
                    if (mylist[i].blocksize > processes.size)
                    {
                        temp = mylist[i];
                        int size_remainig = temp.blocksize - processes.size;
                        mylist.RemoveAt(i);
                        temp.name          = processes.name;
                        temp.type          = 1;
                        temp.blocksize     = processes.size;
                        temp.endingaddress = processes.size + temp.startingaddress;
                        mylist.Insert(i, temp);
                        temp.startingaddress = temp.endingaddress;
                        temp.blocksize       = size_remainig;
                        temp.name            = "HOLE";
                        temp.type            = 0;
                        temp.endingaddress   = size_remainig + temp.startingaddress;
                        mylist.Insert(i + 1, temp);
                        // sort your own list back
                        mylist.Sort((x, y) => x.startingaddress.CompareTo(y.startingaddress));

                        // copy from your list to the other

                        printlist = new List <block>(mylist);
                        return(true);
                    }

                    if (mylist[i].blocksize == processes.size)
                    {
                        temp = mylist[i];
                        mylist.RemoveAt(i);
                        temp.name = processes.name;
                        temp.type = 1;
                        mylist.Insert(i, temp);
                        // sort your own list back
                        mylist.Sort((x, y) => x.startingaddress.CompareTo(y.startingaddress));
                        // copy from your list to the other

                        printlist = new List <block>(mylist);
                        return(true);
                    }
                }
                //traverse untill u find the place and break that block
                // at which return true and update the list

                /*
                 * // sort your own list back
                 * // copy from your list to the other
                 * //return True ;
                 */
            }
            // sort your own list back
            mylist.Sort((x, y) => x.startingaddress.CompareTo(y.startingaddress));
            // copy from your list to the other

            printlist = new List <block>(mylist);
            return(false);
        }
Exemple #4
0
        private void button4_Click(object sender, EventArgs e)
        {
            int processSize;

            try
            {
                processSize = int.Parse(textBox5.Text);
                if (processSize > memorySize)
                {
                    MessageBox.Show("The Process size is greater than the memory size", "Error");
                }
                else
                {
                    pro process = new pro(textBox4.Text, processSize);

                    if (numOfDraws > 0)
                    {
                        for (int i = 0; i < listOfBlocks.Count(); i++)
                        {
                            checkBoxArray[i].Enabled = false;
                        }
                    }
                    //send the process to rizk and algorithm
                    switch (listBox1.SelectedIndex)
                    {
                    case 0:
                        if (memory.first_fit(process, ref listOfBlocks))
                        {
                            drawMemory(listOfBlocks);
                            numOfDraws++;
                        }
                        else
                        {
                            MessageBox.Show("Cannot allocate process try to deallocate memory first");
                            for (int i = 0; i < listOfBlocks.Count(); i++)
                            {
                                checkBoxArray[i].Enabled = true;
                            }
                        }
                        break;

                    case 1:
                        if (memory.best_fit(process, ref listOfBlocks))
                        {
                            drawMemory(listOfBlocks);
                            numOfDraws++;
                        }
                        else
                        {
                            MessageBox.Show("Cannot allocate process try to deallocate memory first");
                            for (int i = 0; i < listOfBlocks.Count(); i++)
                            {
                                checkBoxArray[i].Enabled = true;
                            }
                        }

                        break;

                    case 2:
                        if (memory.worst_fit(process, ref listOfBlocks))
                        {
                            drawMemory(listOfBlocks);
                            numOfDraws++;
                        }
                        else
                        {
                            MessageBox.Show("Cannot allocate process try to deallocate memory first");
                            for (int i = 0; i < listOfBlocks.Count(); i++)
                            {
                                checkBoxArray[i].Enabled = true;
                            }
                        }

                        break;
                    }
                }
            }
            catch
            {
                MessageBox.Show("Enter valid process size", "Error");
            }
        }