954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Dynamic Partitioning Simulation

Hello.

We have been asked to write a program of an event driven simulation in dynamic partition. where we are given a number of jobs with it's given time and job size. the job should remain on the memory until completion (time clocks down to 0).

the program should measure the
*throughput
*storage utilization
*waiting queue length
*waiting time in queue
*external fragmentation

I'm not yet that familiar with c# programming so any thoughts on how to go about this? I've been looking on the net and it seems like pointers was used often but I don't know how to do that on c#. I'm not asking for codes. just a guide to get me started :) thanks in advance.

whodoes21
Newbie Poster
22 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

forgot to mention that the memory will be 50k and we'll use first-fit

whodoes21
Newbie Poster
22 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

knowing dynamic partitions Jobs are given only as much memory as they request when they are loaded just until it finished and so the next jobs are inserted if they fit the free spaces.

so I made a loop that will add up all the contents of an array (I placed the jobs inside an array) until the conditions are met.

int[] job = { 5760, 4190, 3290, 2030, 2550, 6990, 8940, 740, 3930, 6890, 6580, 3820, 9410, 420, 220, 7540, 3210, 1380, 9850, 3610, 7540, 2710, 8390, 5950, 760 };
            int totality = 0, i = 0;
            while (totality <= 50000)
            {
                totality = totality + job[i];
                i++;
            }


The Problem I'm having
* The result of the loop is over the desired condition (which is to not go over 50k)
* I need to keep on checking all the contents if there is still a job that would fit (first fit) and not to stop just because
the next job in line will make the current total go over 50k when there are other jobs that can still fit.
What I need to do after this
*get the address or point-out the contents of the array used. (which is why I think pointers are used but can't do it on c#) and have them handed to variables or something

whodoes21
Newbie Poster
22 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Could try something like:

while (totality <= 50000)
            {
                if (!(totality + job[i]) >= 50000)
                    totality = totality + job[i];
                i++;
            }
MikeyIsMe
Junior Poster
142 posts since Nov 2010
Reputation Points: 32
Solved Threads: 15
 

hmmm well it does the same thing as the top O_O hmmm plus the "!" cannot be applied too kinda modified it for it to work but still does the same thing.. thanks though :)

whodoes21
Newbie Poster
22 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Yeah i had a feeling the ! might error as i wrote it on the fly on here not in visual studio :D would of just had to add an extra set of brackets around the whole expression to apply the ! too :)

Any other issues?

MikeyIsMe
Junior Poster
142 posts since Nov 2010
Reputation Points: 32
Solved Threads: 15
 

Okay working on that... If this one work. Is there a way to mark the elements that you've used so that you can pull them out of the array and store them somewhere else?

whodoes21
Newbie Poster
22 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Okay so I got it to work with this code

while (totality <= 50000) 
            {
                if (totality + job[i] <= 50000)
                {
                    totality = totality + job[i];
                }
                i++;
            }


removed the operator "!" and reversed the comparison from greater than to less than.
but gets the error."Index was outside the bounds of the array." at the end of the run. any thoughts?

whodoes21
Newbie Poster
22 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Okay so the problem still stands the same which is Index was outside the bounds of the array. but I added a little something to help my other concerns. here's my code look like. it should work properly once I have got this done. sorry but I can't really contain the array to not go over it's length :|

List<int> container = new List<int>();
            int[] job = { 5760, 4190, 3290, 2030, 2550, 6990, 8940, 740, 3930, 6890, 6580, 3820, 9410, 420, 220, 7540, 3210, 1380, 9850, 3610, 7540, 2710, 8390, 5950, 760 };
            int totality = 0, i = 0;
            
            while (totality <= 50000)
            {
                if (totality + job[i] <= 50000) //this is where the error is after
                {
                    totality = totality + job[i];
                    container.Add(job[i]);
                }
                
                    i++;
                
            }

            for (int b = 0; b < container.Count; b++)
            {
                Console.WriteLine(container[b]);
            }
                Console.ReadLine();
whodoes21
Newbie Poster
22 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Okay so I got it to work. can someone please teach me on how to print them in different columns like

job name           job size           time
content 1           6545               5
content 2           4544               4
.                    .                 .
.                    .                 .


here's the code I now have

List<int> container = new List<int>();
            List<int> usabletime = new List<int>();
            List<String> jobtitle = new List<string>();
            string[] jobname = { "JB1", "JB2", "JB3", "JB4", "JB5", "JB6", "JB7", "JB8", "JB9", "JB10", 
                                 "JB11",  "JB12",  "JB13",  "JB14",  "JB15",  "JB16",  "JB17",  "JB18",  
                                 "JB19", "JB21",  "JB21",  "JB22",  "JB23",  "JB24",  "JB25",  }; 
            int[] job = { 5760, 4190, 3290, 2030, 2550, 6990, 8940, 740, 3930, 6890, 6580, 3820, 
                          9410, 420, 220, 7540, 3210, 1380, 9850, 3610, 7540, 2710, 8390, 5950, 
                          760 };
            int[] time = {5, 4, 8, 2, 2, 6, 8, 10, 7, 6, 5, 8, 9, 10, 10, 7, 3, 1, 9, 3, 7, 2, 8, 5, 10 };
            int totality = 0, i = 0;
            
            while (totality <= 50000 && i <= job.Length-1)
            {
                if (totality + job[i] <= 50000)
                {
                    totality = totality + job[i];
                    container.Add(job[i]);
                    usabletime.Add(time[i]);
                    jobtitle.Add(jobname[i]);
                }
                
                    i++;
                
            }

                Console.WriteLine("Job Sizes");
            for (int b = 0; b < container.Count; b++)
            {
                Console.WriteLine(container[b]);
            }
                
            Console.WriteLine("Time");
            for (int a = 0; a < container.Count; a++)
            {
                Console.WriteLine(usabletime[a]);
            }
            Console.WriteLine("Job Name");
            for (int c = 0; c < container.Count; c++)
            {
                Console.WriteLine(jobtitle[c]);
            }
            Console.ReadLine();
whodoes21
Newbie Poster
22 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Again not being written in an IDE but still.

Could you not do something like the following:

Console.Writeline("\t Job Name \t Job Size \t Time")
For (int i = 0; i < container.Count; i++)
{
    String.Format("{0}. \t {1} \t {2} \t {3}", (i+1), jobname[i], job[i], time[i]);
}


This code uses a string formatter to put the values into each line of the string, they are spaced [hopefully] evenly using the [TAB] char "\t" however you may need to fine tune the tabbing.

Hope that helps.

MikeyIsMe
Junior Poster
142 posts since Nov 2010
Reputation Points: 32
Solved Threads: 15
 

thanks that worked fine. had to tweak the spacing but just a bit. :)
now have to deal with the other things.

whodoes21
Newbie Poster
22 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

I have a question...
let's say I have this list

List<int> sample = new List<int>();
            sample.Add(5);
            sample.Add(10);
            sample.Add(15);

            for (int i = 0; i < sample.Count; i++)
            {
                Console.WriteLine(sample[i]);
            }


how do I read all the values one by one then decrement it by one.
after doing that store it at the same list?

so that I will have a value of this:

4
9
14

instead of this

5
10
15
whodoes21
Newbie Poster
22 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 
for (int i = 0; i < sample.Count; i++)
            {
                Console.WriteLine(sample[i]);
                sample[i] -= 1; //This line is shorthand for sample[i] = sample[i] - 1
            }


That should do the trick :)

MikeyIsMe
Junior Poster
142 posts since Nov 2010
Reputation Points: 32
Solved Threads: 15
 

It does decrement the items on the list but it doesn't replace the old ones. what I wanted to do is decrement the items on the list by one and then save it on that same list replacing the old values.

what I have so far is this

int decrement = 0;

            foreach (int usedtime in usabletime)
            {

                decrement = usedtime - 1;
                newusabletime.Add(decrement);

            }


it works but it requires a new list and I can't loop it to do the same thing over and over again.

I've read something like this on a site

//Orignal List
List list = new List();
for (int i = 1; i < 10; i++)
{
list.Add(i);
}

//Filtered List
List newList = new List();
foreach (int i in list)
{
if (! MustRemove(i))
newList.Add(i);
}

//Replacing the original list
list = newList;


I've been trying to implement it but I don't know what goes in the if condition. the author is saying something about negative logic and to quote "observe the ! preceding the condition that indicates which items must be removed, it is negative logic!"

btw thanks for your continous help :)

whodoes21
Newbie Poster
22 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Right so let me understand,

You have one list containing for example 15, 10, 5.

You wish to decrement those values by 1.

You finally wish to overwrtie the old list so it now contains 14, 9, 4?

Just to clarify before I try code it :)

MikeyIsMe
Junior Poster
142 posts since Nov 2010
Reputation Points: 32
Solved Threads: 15
 

Assuming my clarification was correct the following console app will show you that the code above I gave does indeed work :)

class Program
    {
        static void Main(string[] args)
        {
            List<int> Numbers = new List<int>() { 15, 10, 5 };

            Console.WriteLine("Original numbers:");
            Console.WriteLine();

            //The code to print numbers and then decrement/update list.
            for (int x = 0; x < Numbers.Count; x++)
            {
                Console.WriteLine(Numbers[x]);
                Numbers[x] -= 1;
            }

            //Spacing because i like formatting on console apps
            Console.WriteLine();
            Console.WriteLine("New decremented list values");
            Console.WriteLine();

            //Just used to check the decrement worked.
            for (int x = 0; x < Numbers.Count; x++)
            {
                Console.WriteLine(Numbers[x]);
            }

            Console.ReadLine();
        }
    }
MikeyIsMe
Junior Poster
142 posts since Nov 2010
Reputation Points: 32
Solved Threads: 15
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: