i have implemented Round Robin Algorithm with fixed time quantum in Scala. Code is working fine with desired output but in a specific case code is not giving desirable results.
Let I have five processes p0,p1,p2,p3,p4 with arrival time 3,0,1,9,6 and Burst time 1,2,6,7,9 respectively and set (quantum time) q=3. In first iteration of do-while loop,my code will execute p1,p2. In second iteration of loop it will now execute all processes. but execution is this way is not desired one.
I want execution in this way. In first iteration of do-while loop, When code start execution it will first check arrival time of p0 that is not 0 (not less than or equal to variable tracker as initial value of tracker = 0). Code will now execute p1 as its arrival time is 0. When p1 completes its execution,p2 will be arrived, so p2 will be executed. after execution of p2 ,p0 is arrived (as tracker = 5 at this point and arrival time of p0 = 4) , so now code should execute p0 and than should execute other processes. Mean execution should be in this sequence p1,p2,p0,p4,p3 in first iteration of do-while loop and than go for second iteration based on given condition.
Note: Sorting process based on arrival time is not solution according to my need. Processes are sorted based on their Burst time. My code is
def RoundRobin(n: Int, data: Array[Array[Double]]): Unit = {
var arr = data.map(_.map(identity)) // first column of arr holds arrival time and second column holds Burst time.
val q = 3.0
val wt = new Array[Double](n) // wt is waiting time
val a = new Array[Double](n)
var sum = 0.0
for (i <- 0 to n - 1) {
a(i) = arr(i)(1)
}
for (i <- 0 to n - 1) {
wt(i) = 0
}
var tracker = 0.0 // Used for arrival time comparision
do {
arr = arr.sortBy(x => x(1)) //sort based on Burst time
for (i <- 0 to arr.size - 1) {
if (tracker >= arr(i)(0)) // check if tracker is greater is greater than or equal to Arrival Time of process i
{
if (arr(i)(1) > q) { //if Burst time > quantum time
arr(i)(1) -= q // decrement burst time of process i by q (quantum time) unit of time
tracker = tracker + q // increment value of tracker by q unit of time
for (j <- 0 to arr.size - 1) {
if ((j != i) && (arr(j)(1) != 0))
wt(j) += q // increment waiting time of process j by q (quantum time)
}
}
else {
for (j <- 0 to arr.size - 1) {
if ((j != i) && (arr(j)(1) != 0)) {
wt(j) += arr(i)(1) // increment waiting time of process j by rem aining burst time of i
}
}
tracker = tracker + arr(i)(1) // increment value of tracker by burst time of process
arr(i)(1) = 0 // set burst time of process i = 0 as process completed execution
}
}
else {
if (i == arr.length - 1) { // if all processes have been traversed that is value of i == length of list than increment tracker by 1
tracker = tracker + 1
}
}
}
sum = 0.0
for (i <- 0 to arr.length - 1)
sum = sum + arr(i)(1)
} while (sum != 0)
var avg_wt = 0.0 //wt is waiting time
var avg_tat = 0.0 // tat is Turn around time
for (j <- 0 to n - 1) {
wt(j) = wt(j) - arr(j)(0)
if(wt(j) < 0){
wt(j)=0.0
}
}
for (j <- 0 to n - 1) {
avg_wt = avg_wt + wt(j);
avg_tat = avg_tat + wt(j) + a(j) // TaT = WT + BT
}
println(" total waiting time= " + avg_wt + " Total turn around time= " + avg_tat )
println(" average waiting time= " + (avg_wt / n) + " Average turn around time= " + (avg_tat / n))
}
def main(args: Array[String]): Unit = {
println("Enter number of process:");
val n = scala.io.StdIn.readInt(); // n is number of processes
var array: Array[Array[Double]] = Array.ofDim(n, 2)
//arrival time
array(0)(0) = 3
array(1)(0) = 0
array(2)(0) = 1
array(3)(0) = 9
array(4)(0) = 6
//Burst time
array(0)(1) = 1
array(1)(1) = 2
array(2)(1) = 6
array(3)(1) = 7
array(4)(1) = 9
RoundRobin(n,array)
}