qwerqwerqwersdf adzxvzxcvzxcv - 1 year ago 54

Java Question

I'm writing a simple cpu scheduler simulator, currently im at Round Robin algorithm. At this little piece of code, my way of implementing it is this:

take the total sum of the burst time from user input for keeping track when

this block should finish. as long as the sum is greater then or equal to 0, keep executing.

now, the problem is that the sum becomes negative and the block stops executing before the processes' burst time become all 0. is there any way i can fix this?

for example, suppose in our case that our input is {9, 12, 1, 1, 4, 4},

the output for phase 1 after executing all processes with quantum 4 is:

(where first column is the original array and the second one the decremented by quantum's value)

`sum: 31`

phase nr: 1

9 7 not zero

12 10 not zero

1 0 check

1 0 check

4 2 not zero

4 2 not zero

phase nr: 2

7 5 not zero

10 8 not zero

0 0 check

0 0 check

2 0 check

2 0 check

at this stage sum becomes negative and stops printing other processes

this is the code ive been working so far. i dont know if this explanation is clear enough, i tried to be as clear and specific as i could.

note: all jobs has an arrival time of 0

`int[] a = {9, 12, 1, 1, 4, 4};`

int sum = 0;

//total sum of the a[]

for (int i = 0; i < a.length; i++)

{

sum += a[i];

}

System.out.printf("\nsum: %d\n\n", sum);

int counter = 1;

while(sum >= 0)

{

System.out.printf("\nphase nr: %d\n", counter);

for (int i = 0; i < a.length; i++)

{

//prints the original array

System.out.printf("%d ",a[i]);

int value = a[i]; //takes a value from a[] at index i

value -= 2; //decrement the value by quantum 2

a[i] = value; //put the value at the same index

if (a[i] < 0) //it checks if any element is less than 0, if yes replace it with 0

{

a[i] = 0;

}

/**

*prints the array after subtracting 2 from each element

*in the original array and checks if there is any value

*equal to 0, if yes print "check" else "not zero"

*/

System.out.printf("%d \t%s",a[i], a[i]==0?"check":"not zero");

System.out.println();

}

/**

* decrements the sum based on the quantum

* multiplied by processes

* sum = sum -(quantum * numberOfProcesses)

*/

sum = sum - (4 * 6);

counter++;

}

Answer Source

The problem is in calculating your sum.

**sum = sum - (4 * 6);**

You subtract 24 in every iteration even if processes finish earlier(like in 1 quantum of time) or are already finished.

What you should subtract from sum in every iteration is the total time spent on actual processing. So if process is done (a[i] == 0) you don't proceed, and if the a[i] < 4 you subtract that value. Only when a[i] >=4 you should subtract 4 quanta.

If you want to spend 4 quanta of time for every process regardless of its real needs and if it's done or not then you calculate your sum wrong and it's not really Round Robin.

while(sum >= 0) { System.out.printf("\nphase nr: %d\n", counter);

```
for (int i = 0; i < a.length; i++)
{
//prints the original array
System.out.printf("%d ",a[i]);
int value = a[i]; //takes a value from a[] at index i
if(a[i] == 0) { continue;} // skip process if it's done
else if(a[i]<=4) { sum -=a[i]; a[i]=0;} // if process is less than one quantum then it's done and sum is decreased by time needed
else { sum -=4; a[i] -=4} // otherwise process needs more than one quantum so it uses up all 4
/**
*prints the array after subtracting 2 from each element
*in the original array and checks if there is any value
*equal to 0, if yes print "check" else "not zero"
*/
System.out.printf("%d \t%s",a[i], a[i]==0?"check":"not zero");
System.out.println();
}
// this part was completely unnecessary so it was removed to for loop above
counter++;
}
```