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(); } }