C# and Shortest Remaining Time Algorithm

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 mattmandery  Icon User is offline

  • New D.I.C Head

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

C# and Shortest Remaining Time Algorithm

Posted 16 July 2013 - 11:14 AM

Hello Community. I have a school assignment to implement a SRTF (Shortest Remaining Time) algorithm in C#. I am not good at c# at all and am pretty lost at this point. I know this is a school assignment and I am not looking for someone to hand me the answer but i would really appreciate some suggestions or a point in the right direction. Just spent 3 hrs on google with no real results to show for it. The assignmemt states "Create processes at random intervals (1 to 3 seconds). Each process has a random processing time (1 to 10 seconds). Start the simulation at time = 0 and create processes until time <= 60. Use time quantum (q) = 1. After time = 60, continue to simulate execution of processes until all processes have completed execution. "

As far as code that I have right now its not very good.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    } 

    public class Process
   {
        Attribute runtime;
        Attribute ID;
        Attribute startTime;

        static void timer()
        {
            System.Threading.Thread.Sleep(1000);
            
        }
    }

}



Any help or references that you could refer me to would really be helpful. Thanks again guys.

Is This A Good Question/Topic? 0
  • +

Replies To: C# and Shortest Remaining Time Algorithm

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9573
  • View blog
  • Posts: 36,255
  • Joined: 12-June 08

Re: C# and Shortest Remaining Time Algorithm

Posted 16 July 2013 - 11:36 AM

Quote

Start the simulation at time = 0

Sounds like you need a 'time' variable.

Quote

Use time quantum (q) = 1

... that increments by one..

Quote

create processes until time <= 60.

For each tick it creates a new 'process'

Quote

After time = 60, continue to simulate execution of processes until all processes have completed execution. "

Stop creating new 'process' after 60.. end when there's nothing more running.



--

Does this specifically indicate you should be using threading, or is this just a simulation with an array?
Was This Post Helpful? 2
  • +
  • -

#3 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 16 July 2013 - 12:16 PM

View Postmodi123_1, on 16 July 2013 - 11:36 AM, said:

Does this specifically indicate you should be using threading, or is this just a simulation with an array?

I do not believe I have to do threading. I believe It is just a simulation with an array. The array values being random of course.

This post has been edited by tlhIn`toq: 16 July 2013 - 02:07 PM

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9573
  • View blog
  • Posts: 36,255
  • Joined: 12-June 08

Re: C# and Shortest Remaining Time Algorithm

Posted 16 July 2013 - 12:19 PM

Stellar. Then there's your direction - now scoot!
Was This Post Helpful? 1
  • +
  • -

#5 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 16 July 2013 - 04:27 PM

Ok guys i have made some progress but I have a few questions regarding the Syntax. I have embedded the questions in the code attached. Any help would be really great.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication6
{
    class Program
    {
 //      public int id;
  //     public int runtime;
   //    public int starttime;
       public int[][][] Process(int id, int runtime, int starttime)
        {
           
            int ID = id;
            int runTime = runtime;
            int startTime = starttime;
           return Process(ID, runTime, startTime);
          

        }

        static void Main(string[] args)
        { 
            int time;
            while(processleft()==true){
                while(time<= 60){
                    if((CreateNewProcess()==true)){
                        StartProcess();
                    }
                    if((processleft()==false)){
                        idol();
                    }
                }
                time++;
            }
    

        

        Boolean processleft(){
            if ( Process().Equals(null){
                return true;

                //what is the correct call to check if Process() is null?
            }
        }

        void idol(){
             System.Threading.Thread.Sleep(1000); // idols timer 1 second  *Increment timer?*
        }

        void CreateNewProcess(){
            Random rand = new Random(); 
            int id = rand.Next(int 2);   //error here. Why?  Trying to get any int between 1-3
        }
         
        void StartProcess(){
        }
  
            //not implemented 
	    }   

    }


Was This Post Helpful? 0
  • +
  • -

#6 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 16 July 2013 - 04:53 PM

Quote

what is the correct call to check if Process() is null?

if (MyObject == null) //do stuff


Quote

int id = rand.Next(int 2);   //error here. Why?  Trying to get any int between 1-3

You can't create a new variable there. Just use rand.Next(2) instead. Though if you want values from 1 to 3 you'll need to use rand.Next(1, 4). The Random.Next(int) has an inclusive low limit of zero, and the specified high limit is exclusive, so calling rand.Next(2) would get you only 0 or 1.

This post has been edited by Witchking: 16 July 2013 - 04:53 PM

Was This Post Helpful? 0
  • +
  • -

#7 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 17 July 2013 - 04:19 PM

Hey guys so i'm running into a syntax error. I am wanting to compare my Process(int id, int runtime, int starttime) to the next Process in the array so (Process(id +1, runtime +1, starttime +1). Basicly im trying to compare the runtimes and then begin to modify the lower valued runTime. Code attached.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication6
{
    class Program
    {
 //      public int id;
  //     public int runtime;
   //    public int starttime;
       public int[][][] Process(int id, int runtime, int starttime)
        {
           
            int ID = id;
            int runTime = runtime;
            int startTime = starttime;
           return Process(ID, runTime, startTime);
          

        }

       static void Main(string[] args)
       {
           int time;
           while (processleft() == true)
           {
               while (time <= 60)
               {
                   if ((CreateNewProcess() == true))
                   {
                       StartProcess();
                   }
                   if ((processleft() == false))
                   {
                       idol();
                   }
               }
               time++;
           }
       }
    

        

        Boolean processleft(){
            if ( Process() == null){
                return false;
            }
            else
                return true;

                
        }
        

        void idol(){
             System.Threading.Thread.Sleep(1000); // idols timer 1 second  *Increment timer?*
        }

        void CreateNewProcess(){
            Random rand = new Random(); 
            int id = rand.Next(int 2);   //error here. Why?  Trying to get any int between 1-3
        }
         
        void StartProcess(){
            while (Process().hasNext)
            {
                if(Process.   // how do i compare Process(int id, INT RUNTIME, int starttime) to Process(id +1, runtime +1, starttime +1)
            }
        }
  
            //not implemented 
	    }   

    }


thanks in advance
Was This Post Helpful? 0
  • +
  • -

#8 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 17 July 2013 - 04:31 PM

int id = rand.Next(int 2)
You get the error here because of the int keyword. You use this to declare variables. When calling the method you simply need a value, which can come from a variable, a method, a property, or a literal. In this case you should use rand.Next(2), like i already pointed out in my previous post.

Your Process method takes three arguments, so you cannot call it as Process(). In addition the Process method calls itself recursively over and over causing an infinite loop. Furthermore i don't know what you're trying to do with that method, but it certainly doesn't seem like it'll work the way you're hoping it will. I suggest you write a Process class, and for each process started instantiate an object into a List<Process>.

This post has been edited by Witchking: 17 July 2013 - 04:31 PM

Was This Post Helpful? 0
  • +
  • -

#9 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 17 July 2013 - 04:41 PM

View PostWitchking, on 17 July 2013 - 04:31 PM, said:

int id = rand.Next(int 2)
You get the error here because of the int keyword. You use this to declare variables. When calling the method you simply need a value, which can come from a variable, a method, a property, or a literal. In this case you should use rand.Next(2), like i already pointed out in my previous post.

Your Process method takes three arguments, so you cannot call it as Process(). In addition the Process method calls itself recursively over and over causing an infinite loop. Furthermore i don't know what you're trying to do with that method, but it certainly doesn't seem like it'll work the way you're hoping it will. I suggest you write a Process class, and for each process started instantiate an object into a List<Process>.

Ya sorry I simply forgot to change the rand.next(2); I did take your advice on that. I am hoping you can give me an example of the List<Process>. I am trying to pass the 3 variable I specified earlier. I simply want to compare all the Process(es) and select the lowest value for runtime and subtract 1 for each second (time++). Thanks in advance
Was This Post Helpful? 0
  • +
  • -

#10 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 17 July 2013 - 05:15 PM

You previously had a Process class, but it seems you abandoned that prematurely.
class Process
{
	private int timeLeft;
	
	public Process(int time)
	{
		this.timeLeft = time;
	}
	
	public void DoProcessing()
	{
		timeLeft--;
	}
}
Here is a simple example of a Process class you might use. It has an int variable showing the time left on the process, and a method simulating processing, which subtracts from the timeLeft variable. You could instantiate the class at random intervals, with random processing times, into a List<Process> like so:
List<Process> list = new List<Process>();
list.Add(new Process(/*random*/));
The List<T> class is a generic collection class, which can store objects of the specified type, and is in many cases easier to work with than an array.

This post has been edited by Witchking: 17 July 2013 - 05:17 PM

Was This Post Helpful? 1
  • +
  • -

#11 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 17 July 2013 - 06:26 PM

Ok i Feel like i made some good strides but I have a new and ever so great problem. I want to start testing my program but I cam getting an error where this is coded
 int time = 0;
            while (processleft() == true)
            {
                while (time <= 60)
                {
                    if ((CreateNewProcess() == true))
                    {
                        StartProcess();
                    }
                    if ((processleft() == false))
                    {
                        idol();
                    }
                }
                time++;
            }
        }


Every method is underlined red and says "Error: An object reference is required for the non-static field, method, or property."

Full Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication6
{
    class Program
    {
        int startTime = 0;
        int p = 0;
        List<Process> list = new List<Process>();
        Random rand = new Random();
        static void Main(string[] args)
        {
            

            int time = 0;
            while (processleft() == true)
            {
                while (time <= 60)
                {
                    if ((CreateNewProcess() == true))
                    {
                        StartProcess();
                    }
                    if ((processleft() == false))
                    {
                        idol();
                    }
                }
                time++;
            }
        }




        Boolean processleft()
        {
            if (list == null)
            {
                return false;
            }
            else
                return true;


        }


        void idol()
        {
            System.Threading.Thread.Sleep(1000); 
        }

        Boolean CreateNewProcess()
        {
            
           
            if (startTime == 0)
            { 
                startTime = rand.Next(1, 4);
                return true;
            }
            else
            {
                startTime--;
                return false;
            }
            
        }

        void StartProcess()
        {
            int time;
            time = rand.Next(1,11);
            list.Add(new Process(p, time));
            
            Console.WriteLine("Process " + p + " created -- processing time " + time + ".");
            p++;
        }

        //not implemented 
    }
        class Process
        {

            Attribute ID { get; set; }
            Attribute runTime { get; set; }
            Attribute startTime { get; set; }

            private int timeLeft;

            public Process(int index, int time)
            {
                this.timeLeft = time;
                Console.WriteLine("Process " + index + " is in the running State");
                DoProcessing(timeLeft);
            }

            public void DoProcessing(int timeLeft)
            {
                timeLeft--;
            }



        }
    
}
       


Thanks in advance
Was This Post Helpful? 0
  • +
  • -

#12 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 17 July 2013 - 09:01 PM

So I decided to make all my first Class static. This is causing headaches. I was trying to cheat with global variables (I know its not a good idea) but now i'm stuck re configuring how to access all these variables. Code is attached. Any ideas about how to make this easier would be really appreciated. Maybe some get / set methods? not sure how I will be able to do the startTime and pass it to my object. Just a mess right now. Cheers for any advice.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication6
{
    class Program
    {
        int startTime = 0;
        int p = 0;
        List<Process> list = new List<Process>();
        Random rand = new Random();
       static void Main(string[] args)
        {
            List<Process> list = new List<Process>();
            Random rand = new Random();
           
           int time = 0;
            while (processleft() == true)
            {
                while (time <= 60)
                {
                    if ((CreateNewProcess() == true))
                    {
                        StartProcess();
                    }
                    if ((processleft() == false))
                    {
                        idol();
                    }
                }
                time++;
            }
        }


      
 

     static Boolean processleft()
        {
         
            if (list == null)
            {
                return false;
            }
            else
                return true;


        }


      static void idol()
        {
            System.Threading.Thread.Sleep(1000);
        }

       static Boolean CreateNewProcess()
        {


            if (startTime == 0)
            {
                startTime = rand.Next(1, 4);
                return true;
            }
            else
            {
                startTime--;
                return false;
            }

        }

       static void StartProcess()
        {
            int time;
            time = rand.Next(1, 11);
            list.Add(new Process(p, time));

            Console.WriteLine("Process " + p + " created -- processing time " + time + ".");
            
            p++;
        }

        //not implemented 
    }
        class Process
        {

            Attribute ID { get; set; }
            Attribute runTime { get; set; }
            Attribute startTime { get; set; }

            private int timeLeft;

            public Process(int index, int time)
            {
                this.timeLeft = time;
                Console.WriteLine("Process " + index + " is in the running State");
                DoProcessing(timeLeft);
            }

            public void DoProcessing(int timeLeft)
            {
                timeLeft--;
            }

        

        }
    
}
       

Was This Post Helpful? 0
  • +
  • -

#13 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 - 04:07 AM

All variables have a scope, determined by where you declare them. Classes, and all their members also have a protection level. When you declare a variable in a class like so
class MyClass
{
	int myInteger;
	
	void MyMethod()
	{
		myInteger = 1;
	}
}
it is accessible to all instance members of the same class, including methods. Inside the class static members can only access other static members.
class MyClass
{
	static void Main(string[] args)
	{
		myInteger = 1;
	}
	
	int myInteger;
}
This won't work because the void Main is static.


If you declare a variable inside a method, its scope is that method, meaning it can only be accessed inside the method.
void MyMethod()
{
	int i = 7;
}

void MyOtherMethod()
{
	i = 2;
}
This won't work because i's scope is to the MyMethod method. Method parameters, unless they use the ref keyword, are passed as copies and their scope is to the method. Furthermore once the method execution is over, the variable is disposed of, and if the method is called again, the variable won't necessarily have the same value as before. All of this also applies to loops, if clauses and basically everything with curly brackets.


The protection level of a class member determines how it is accessible outside the class.
class MyClass
{
	public int MyInteger { get; set; }
	private int otherInteger;
}
The MyInteger property is public, and therefore accessible from outside the class. The otherInteger variable is private (default for class members) and is accessible only inside the class.




Quote

Attribute ID { get; set; }
Attribute runTime { get; set; }
Attribute startTime { get; set; }

The System.Attribute class is an abstract base class for creating attribute types and should not be used in this way. Instead declare the properties as int.

Quote

List<Process> list = new List<Process>();
       	static void Main(string[] args)
        {
            List<Process> list = new List<Process>();

You needlessly declare and instantiate the list variable twice. The second list declared in the Main method is only accessible to the main method, and makes the previous list accessible there only by using the this keyword like so this.list.
Was This Post Helpful? 2
  • +
  • -

#14 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 - 08:45 AM

I decided to scrap that and start fresh because I was having too many errors and was getting things confused. I have attached 2 different comments and would love feedback to achieve them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
             while (processleft()==true)
                { 
                   while (time<=60)
                    { 
                      if (CreateNewProces()==true)
                      {
                          StartProcess();
                      }
                      if (processleft()==false)
                      {
                          idle();
                      }
                   }
                } 
        }

        static void processleft()
        {

        }
        static void CreateNewProcess()
        {
            List<Process> process = new List<Process>();
            Random rand = new Random();

            process.Add(rand.Next(1, 11), /*I want to get the current List<Process> index.  that is how many objects are created + 1 */, rand.Next(1, 4));
        }
        static void StartProcess()
        {

        }
        static void processleft()
        {

        }
        static void idle()
        {

        } 
        void timer()
        {
            system.threading.thread.sleep(1000);
        }
    }
    class Process
    {
        int runtime { get; set; }
        int ID {get; set;}
        int startTime {get; set;}
        /*Do I do anything with this class other than define Process?*/
    }
     
        
       


    }



Was This Post Helpful? 0
  • +
  • -

#15 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 - 08:56 AM

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.

Quote

/*Do I do anything with this class other than define Process?*/

You can do with it as you like. I'm not sure why you would have the ID, runtime and startTime properties when you could just use a single TimeLeft propery.

static void CreateNewProcess()
{
    List<Process> process = new List<Process>();
    Random rand = new Random();

    process.Add(rand.Next(1, 11), rand.Next(1, 4));
}
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.

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

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2