C# and Shortest Remaining Time Algorithm

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 2344 Views - Last Post: 20 July 2013 - 10:02 AM Rate Topic: -----

#16 mattmandery  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 17-May 13

Re: C# and Shortest Remaining Time Algorithm

Posted 18 July 2013 - 09:43 AM

View PostWitchking, on 18 July 2013 - 08:56 AM, said:

Quote

/*I want to get the current List<Process> index. that is how many objects are created + 1 */

To get the number of elements in a list, use the List<T>.Count property.

You have a couple problems here. First of all, don't create a new Random instance every time. Declare one on the class level and use that. Same thing with the list, declare it on the class level. Like i explained in my previous post about scope, if you declare the list here, you will lose it's contents as soon as the method execution ends. Lastly, the list only accepts objects of type Process, so you'll add to it like so process.Add(new Process());, however you'll most likely want to write a constructor method for the Process class, like i showed previously.


Edit: As a side note processleft()==true can be shortened to just processleft() and processleft()==false to !processleft(), though your way works just as well.


So when I try to move my List<Process> procees = new List<Process>() to right when my class is defined (right above the Main) i have an error showing where I try to access it saying a object reference is required for a non static field method or porperty.

Also I understand what you are saying just passing the timeLeft which is a good idea but I need to Console.WriteLine both the processing time left and also which process it is. Any ideas on how to keep track which process has which time left? Sorry for such newbie problems. I am very familiar in Java but not at all in C# so i'm struggling with the syntax . Thanks for all the help man!
Was This Post Helpful? 0
  • +
  • -

#17 mattmandery  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 17-May 13

Re: C# and Shortest Remaining Time Algorithm

Posted 18 July 2013 - 10:00 AM

Figured I would include my code to help you see what im talking about. in my processleft() method im trying to check if the list (declared above the main) is empty or not. However It gives me the error message I wrote above.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication7
{
    class Program
    {
        
        List<Process> list = new List<Process>();
        static void Main(string[] args)
        {
             while (processleft()==true)
                { 
                   while (time<=60)
                    { 
                      if (CreateNewProces()==true)
                      {
                          StartProcess();
                      }
                      if (processleft()==false)
                      {
                          idle();
                      }
                   }
                } 
        }
        //static int Count { get; }
        static Boolean processleft()
        {
            
            if (list == null)
            {
                return false;
            }
            else
                return true;
        }
        static void CreateNewProcess()
        {
           // List<Process> process = new List<Process>();
            Random rand = new Random();

            list.Add(new Process(rand.Next(1, 11));
        }
        static void StartProcess()
        {

        }
        
        static void idle()
        {

        } 
        
    }
    class Process
    {
        int timeLeft;

        public Process(int time)
        {
            this.timeLeft = time;
        }

        public void DoProcessing()
        {
            timeLeft--;
    }
     
        
       


    }



Was This Post Helpful? 0
  • +
  • -

#18 Witchking  Icon User is offline

  • D.I.C Head

Reputation: 68
  • View blog
  • Posts: 188
  • Joined: 17-February 13

Re: C# and Shortest Remaining Time Algorithm

Posted 18 July 2013 - 10:07 AM

To access the list from a static method, it must also be declared static like i explained in one of my previous posts.
void Main()
{
	
}

static List<Process> ProcessList = new List<Process>();
If you want each process to have an ID, then by all means, do so. As for which process has how much time left, each process has its own object instance with its own separate TimeLeft property.

I'm surprised that you would struggle with C# sharp syntax after Java, as they are very similar, and both use the same OOP concepts.

EDIT:

View Postmattmandery, on 18 July 2013 - 07:00 PM, said:

in my processleft() method im trying to check if the list (declared above the main) is empty or not. However It gives me the error message I wrote above.

static bool ProcessLeft()
{
	if (list == null) //...
}
Ah but you see the list variable, even if the list is empty, still points to the List<Process> object. What you should do is
static bool ProcessLeft()
{
	return List.Count > 0;
}
or
static bool ProcessLeft()
{
	return List.Any();
}
both have the same result; if the list has elements in it, return true.

This post has been edited by Witchking: 18 July 2013 - 10:10 AM

Was This Post Helpful? 0
  • +
  • -

#19 mattmandery  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 17-May 13

Re: C# and Shortest Remaining Time Algorithm

Posted 18 July 2013 - 10:32 AM

Yes your right they are very similar I guess i am pretty off my game at the moment. So I have it running now however I have 1 more problem.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication7
{
    class Program
    {
        static int delay;
        
        static int id;
        static int time;
        static List<Process> list = new List<Process>();
        static Random rand = new Random();
       
        static void Main(string[] args)
        { 
            delay = rand.Next(1, 4);
            while (processleft() == true)
            {
                while (time <= 60)
                {
                    if (CreateNewProcess() == true)
                    {
                        StartProcess();
                    }
                    if (processleft() == false)
                    {
                        idle();
                    }
                    time++;
                }
            }
        }
        static Boolean processleft()
        {

            if (list == null)
            {
                return false;
            }
            else
                return true;
        }
        static Boolean CreateNewProcess()
        {
            Console.WriteLine(delay);
            if (delay == 0)
            {
                int burstTime;
                burstTime = rand.Next(1, 11);
                list.Add(new Process(burstTime, id));
                delay = rand.Next(1, 4);
                Console.WriteLine("Process " + id + " created -- processing time " + burstTime + ".");
                return true;
            }
            else
            {
                delay--;
                return false;
            }

        }
        static void StartProcess()
        {
            //I want to search for the lowest burstTime and send it to the Process class DoProcessing() method
            id++;
        }

        static void idle()
        {
            Console.WriteLine("Console is idle");
            time++;
        }

    }
    class Process
    {
        int timeLeft;
        int id;

        public Process(int time, int ID)
        {
            this.timeLeft = time;
            this.id = ID;
        }

        public void DoProcessing()
        {
            Console.WriteLine("Process " + id + " is in the Running State.");
            timeLeft--;
        }





    }
}



I put in comments where the code is in StartProcess(). I want to compare all of my Process objects and select the one with the lowest burstTime and send it to the DoProcessing() method in the Process class. Any suggestions?
Was This Post Helpful? 0
  • +
  • -

#20 Witchking  Icon User is offline

  • D.I.C Head

Reputation: 68
  • View blog
  • Posts: 188
  • Joined: 17-February 13

Re: C# and Shortest Remaining Time Algorithm

Posted 18 July 2013 - 10:44 AM

For this you'll need a public BurstTime property, like so
class Process
{
	private int burstTime;
	public int BurstTime { get { return burstTime; } }
}

But i fail to see the point of this. I think you're perhaps over thinking the problem. I would simply call the DoProcessing method on all Process objects for every simulated second, and once it hits zero, remove the object from the list. Once the list has no objects left the simulation is completed.
Was This Post Helpful? 0
  • +
  • -

#21 mattmandery  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 17-May 13

Re: C# and Shortest Remaining Time Algorithm

Posted 18 July 2013 - 10:48 AM

View PostWitchking, on 18 July 2013 - 10:44 AM, said:

For this you'll need a public BurstTime property, like so
class Process
{
	private int burstTime;
	public int BurstTime { get { return burstTime; } }
}

But i fail to see the point of this. I think you're perhaps over thinking the problem. I would simply call the DoProcessing method on all Process objects for every simulated second, and once it hits zero, remove the object from the list. Once the list has no objects left the simulation is completed.


That's exactly what I want to do but not quite sure how to implement it.
Was This Post Helpful? 0
  • +
  • -

#22 mattmandery  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 17-May 13

Re: C# and Shortest Remaining Time Algorithm

Posted 18 July 2013 - 11:11 AM

any ideas?
Was This Post Helpful? 0
  • +
  • -

#23 mattmandery  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 17-May 13

Re: C# and Shortest Remaining Time Algorithm

Posted 18 July 2013 - 11:29 AM

I want to do something like this but it isnt liking it

 static void StartProcess()
        {
            //I want to search for the lowest burstTime and send it to the Process class DoProcessing() method
            while (list != null)
            {
                foreach (Process element in list)
                {
                    DoSomething(list);
                }
            }

        }

Was This Post Helpful? 0
  • +
  • -

#24 Witchking  Icon User is offline

  • D.I.C Head

Reputation: 68
  • View blog
  • Posts: 188
  • Joined: 17-February 13

Re: C# and Shortest Remaining Time Algorithm

Posted 18 July 2013 - 11:58 AM

I've helped you quite a bit already, but i can't just give you working code to hand over to your teacher. I think you should take thorough read through my previous posts.

This post has been edited by Witchking: 18 July 2013 - 12:10 PM

Was This Post Helpful? 0
  • +
  • -

#25 mattmandery  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 17-May 13

Re: C# and Shortest Remaining Time Algorithm

Posted 18 July 2013 - 12:22 PM

View PostWitchking, on 18 July 2013 - 11:58 AM, said:

I've helped you quite a bit already, but i can't just give you working code to hand over to your teacher. I think you should take thorough read through my previous posts.

I guess maybe I misworded that. I appreciate all your help really I do, I would be lost without it. I was simply looking for the syntax to use the method from another class. I simply do not see how to begin decreasing the timeLeft down from the List<Process>. I understand if you dont want to assist anymore haha. Its due tonight so i'm just a little brain dead trying to logic this out. Thanks again for your help mate!
Was This Post Helpful? 0
  • +
  • -

#26 Witchking  Icon User is offline

  • D.I.C Head

Reputation: 68
  • View blog
  • Posts: 188
  • Joined: 17-February 13

Re: C# and Shortest Remaining Time Algorithm

Posted 18 July 2013 - 12:38 PM

It's not that i don't want to help, but i can't simply give you the working code.

Now, the Process class already has the DoProcessing method which subtracts one from the TimeLeft. As long as it's public and you have reference to the object you can call it easily, for example
foreach (Process p in list)
{
    p.DoProcessing();
}
or using LINQ
list.ForEach(n => n.DoProcessing());
You can then remove all Process instances with no time left from the list using again a loop or LINQ.
Was This Post Helpful? 0
  • +
  • -

#27 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,161
  • Joined: 05-May 12

Re: C# and Shortest Remaining Time Algorithm

Posted 18 July 2013 - 12:41 PM

Since you claim to be so familiar with Java, please post the code how you would solve this problem using Java. Then we can help you do the translation.
Was This Post Helpful? 0
  • +
  • -

#28 mattmandery  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 17-May 13

Re: C# and Shortest Remaining Time Algorithm

Posted 18 July 2013 - 12:53 PM

That was exactly what I was looking for. Thank you. However, the programming assignment states that I need to use the SRT, which means I need to locate the process with the smallest burst time and decrement it one at a time for each second gone. So for example P(1) = 10 p(2) = 6 and lets say p(3)=2 was added. The program should select p(3) and do that process first before continuing to 2 then 1. So for the foreach you suggested do you think it would be a good idea to compare the burst times for each? If so how do I specify the id's to compare then store that as the lowest burst time. I guess im asking how can i access individual values in the Process List?

Screen shot of what I have ( keep in mind that I am not decreasing the burst time in the code yet so none of the running states would finish yet)

What it should look like
Time Event(s)
0 Processor is idle
1 Process 0 created -- processing time 5
1 Process 0 is in the Running State
2 Process 0 is in the Running State
3 Process 1 created -- processing time 7
3 Process 0 is in the Running State
...
...
...

Sorry here is the screenshot

View PostSkydiver, on 18 July 2013 - 12:41 PM, said:

Since you claim to be so familiar with Java, please post the code how you would solve this problem using Java. Then we can help you do the translation.

if i wasnt on a deadline I would be more than happy to do that. However, Time is short and I really need to try and finish this first. After if you are still interested I will be more than willing to do a java version.

Attached image(s)

  • Attached Image

This post has been edited by mattmandery: 18 July 2013 - 12:49 PM

Was This Post Helpful? 0
  • +
  • -

#29 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,161
  • Joined: 05-May 12

Re: C# and Shortest Remaining Time Algorithm

Posted 20 July 2013 - 10:02 AM

The point I was trying to make was reinforce what Witchking had previously said: C# and Java have very similar syntax. If the approach you take to solve the problem works in Java, it will also work in C#. Since you claimed that you are very familiar with Java (as you said in post #16) then it should be a no brainer to write the code in Java, and then do a translation. C# make have some syntactic sugar to make somethings easier, but if you can solve your problem using Java, you should be able to solve it in C#.

I personally suspect that you don't quite know how to approach the problem in Java itself based on the flavor of questions you have beet asking. That is why I was pushing you to post the Java code because it would at least let you tackle the problem in more familiar surroundings first, instead of trying to deal with a language and an algorithm at the same time.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2