Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Problem with RR

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Problem with RR

    Hi,
    I write the below code,
    If I'm running code with RR ,all result of SJF algorithm is zero !!
    if i'm running without RR, sjf is ok,
    Why?
    package program;
     
    import java.util.*;
     
     
     
     
     
    public class Algorithm {
     
          int a;
          Random rnd;
          Proccess[] process;
         // Proccess[] ;
          Queue<Proccess> rr;
     
          Queue<Proccess> r2;
         /**
         *
         * @processaram n
         */
        public Algorithm(int n,float Q)
          {
             a = n;
             process = new Proccess[a];
             process = new Proccess[a];
             for (int t = 0 ; t < a ; t++)
             {
                 process[t] = new Proccess();
     
             }
     
             rnd = new Random();
             inputExecutionTime();
              Fcfs();
              RR(Q);
              Sjf();
     
          } 
         private float GenerateRandomExecutionTime()
          {
             int p = 5;
             int b = 15;
             float runTime = rnd.nextFloat() * (b - p) + p;
             float T = Round(runTime,2);
             return T;
          }
            public static float Round(float Rval, int Rpl) {
            float p = (float)Math.pow(10,Rpl);
             Rval = Rval * p;
             float tmp = Math.round(Rval);
             return (float)tmp/p;
      }
             private void inputExecutionTime()
          {
             for (int i = 0; i < a; i++)
             {
     
                process[i].executionTime = GenerateRandomExecutionTime();
                 process[i].name = Integer.toString(i);
     
             }
          }
          public void Sjf()
          {
             System.out.println("Shortest Job First Scheduling");
             float wt = 0; 
                for(int i = 0; i < a; i++){
    	    for(int j = 1; j < (a-i); j++){
    		        if(process[j-1].executionTime > process[j].executionTime){
    		          Proccess t = process[j-1];
    		          process[j-1]=process[j];
    		          process[j]=t;
    		        }
    		      }
                }
                  for (int i = 0; i < a; i++)
                 {
     
                float RTime = 0;
                float waitTimeTemprocess = 0;
                if (i == 0)
                {
                   waitTimeTemprocess = 0;
                   RTime = (waitTimeTemprocess + process[i].executionTime) - process[i].enterTime;
                }
                else
                {
                   for (int j = 0; j < i; j++)
                   {
                      waitTimeTemprocess += (float)(process[j].executionTime);
                   }
     
                   waitTimeTemprocess -= process[i].enterTime;
                   RTime = (waitTimeTemprocess + process[i].executionTime) - process[i].enterTime;
                }
                System.out.println("Enter Time of TH" + process[i].name + ": " + process[i].enterTime);
                System.out.println(" Execution Time of TH" + process[i].name + ": " + process[i].executionTime);
                System.out.println("Waiting of TH" + process[i].name + ": " + waitTimeTemprocess);
                System.out.println("Resprocessonse Time " + process[i].name + ": " + RTime);
                System.out.println("---------------------------");
                wt += waitTimeTemprocess;
     
             }
     
             System.out.println("---------------------------");
             System.out.println("AWT Time ==> SJF: "+ (wt / 5.0F));
             System.out.println();
            }
          public void Fcfs()
          {
             System.out.println("First Come First Served Scheduling");
             float wt = 0; 
     
                  for (int i = 0; i < a; i++)
                 {
     
                float RTime = 0;
                float waitTimeTemp = 0;
                if (i == 0)
                {
                   waitTimeTemp = 0;
                   RTime = (waitTimeTemp + process[i].executionTime) - process[i].enterTime;
                }
                else
                {
                   for (int j = 0; j < i; j++)
                   {
                      waitTimeTemp += (float)(process[j].executionTime);
                   }
     
                   waitTimeTemp -= process[i].enterTime;
                   RTime = (waitTimeTemp + process[i].executionTime) - process[i].enterTime;
                }
                System.out.println("Enter Time of TH" + process[i].name + ": " + process[i].enterTime);
                System.out.println(" Execution Time of TH" + process[i].name + ": " + process[i].executionTime);
                System.out.println("Waiting of TH" + process[i].name + ": " + waitTimeTemp);
                System.out.println("Resprocessonse Time " + process[i].name + ": " + RTime);
                System.out.println("---------------------------");
                wt += waitTimeTemp;
     
             }
     
             System.out.println("---------------------------");
             System.out.println("AWT Time ==> FCFS: "+ (wt / 5.0F));
             System.out.println();
            }
                 public void RR(float Quantum)
          {
              int j = 1;
             float q = Quantum;
             rr = new LinkedList<>();
             r2 = new LinkedList<>();
             System.out.println("Round Robin Scheduling");
             System.out.println();
             float wt = 0;
             float RTime = 0;
             float Total = 0;
             float waitTimeTemp = 0;
             for (int i = 0; i < a; i++)
             {
                 process[i].constET = process[i].executionTime;
                rr.add(process[i]);
     
             }
     
             while (!rr.isEmpty())
             {
                Proccess a = rr.remove();
                if (a.executionTime <= q )
                {
                   Total += a.executionTime;
                   a.exitTime = Total;
                   a.executionTime = 0;
                   r2.add(a);
     
                }
              //  else if (a.executionTime )
               // {
     
               // }
                else 
                {
                   a.executionTime = a.executionTime - q;
                   rr.add(a);
                   Total += q;
                }
             }
     
             while (!r2.isEmpty())
             {
     
                Proccess process = r2.remove();
                if ( process.constET < q)
                {
                    //waitTimeTemprocess = process.enterTime;
                    waitTimeTemp = process.exitTime - process.enterTime - process.constET;
                    RTime = process.exitTime - process.enterTime;
                    System.out.println("Enter Time of TH" + process.name + ": " + process.enterTime);
                    System.out.println("Execution of TH" + process.name + ": " + process.constET);
                    System.out.println("Waiting of TH" + process.name + ": " + waitTimeTemp);
                    System.out.println("Resprocessonse Time " + process.name + ": " + RTime);
                    System.out.println("---------------------------");
                    wt += waitTimeTemp;
     
                }
                else
                {
                    waitTimeTemp = process.exitTime - process.enterTime - q;
                    RTime = process.exitTime - process.enterTime;
                    System.out.println("Enter Time of TH" + process.name + ": " + process.enterTime);
                    System.out.println("Execution of TH" + process.name + ": " + process.constET);
                    System.out.println("Waiting of TH" + process.name + ": " + waitTimeTemp);
                    System.out.println("Resprocessonse Time " + process.name + ": " + RTime);
                    System.out.println("---------------------------");
                    wt += waitTimeTemp;
                }
             }
             System.out.println("---------------------------");
             System.out.println("AWT Time ===> RR: " + (wt / 5.0F));
             System.out.println();
          }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with RR

    Is there a problem with how you have coded the algorithm?

    How are you trying to debug the code to find the problem?
    I use lots of calls to println() statements to show the values of variables as the code executes.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM
  2. [SOLVED] [Problem] imports javax.swing problem
    By Brollie in forum AWT / Java Swing
    Replies: 8
    Last Post: July 5th, 2009, 07:59 AM