Search code examples
javalinked-listround-robin

Round Robin by using Linked List. The input cannot use more than one process


The image shows there are logic errors. I can only input one process. If I add more, there will be an error. The system says that I have an array out of boundary by where. I really need help for this problem. The reason why I converted linked list to array is because I don't have any expertise using linked list.

Here the output that I get

import java.util.*;

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        LinkedList<Integer> cpuburst = new LinkedList<>();
        LinkedList<Integer> priority = new LinkedList<>();
        LinkedList<String> process = new LinkedList<>();
        LinkedList<Integer> nextTime = new LinkedList<>();

        int clockTime = 0;
        double totalWaitTime = 0;
        int quit, quantum = 2;
        int processesComplete = 0;

        do {
            System.out.print("input process");
            process.add(sc.next());
            System.out.print("input cpu_burst");
            cpuburst.add(sc.nextInt());
            if (cpuburst.add(0)) {
                processesComplete++;
            }
            System.out.print("input priority");
            priority.add(sc.nextInt());
            nextTime.add(0);
            System.out.print("more?");
            quit = sc.nextInt();
        } while (quit != 0);

        String[] Process = process.toArray(new String[process.size()]);
        Integer[] cpu_burst = cpuburst.toArray(new Integer[cpuburst.size()]);
        Integer[] Priority = priority.toArray(new Integer[priority.size()]);
        Integer[] next = nextTime.toArray(new Integer[nextTime.size()]);

        for (int i = 0; i < next.length; i++) {
            System.out.println(Process[i] + "\t\t" + cpu_burst[i] + "\t\t" + Priority[i]);
        }

        int roundRobinIndex = 0;
        System.out.println(" | Process | CPU Burst | Priority | Time  |  Clock        Time |  Wait Time |");

        while (processesComplete < cpu_burst.length) {
            if (cpu_burst[roundRobinIndex] > 0) {
                int time = Math.min(quantum, cpu_burst[roundRobinIndex]);// compare value
                cpu_burst[roundRobinIndex] -= time;

                if (cpu_burst[roundRobinIndex] == 0)
                    processesComplete++;

                int waitTime = clockTime - next[roundRobinIndex];

                totalWaitTime += waitTime;

                System.out.println(" |    " + Process[roundRobinIndex] + "    |    " + cpu_burst[roundRobinIndex]
                        + "      |    " + Priority[roundRobinIndex] + "     |    " + time + "  |     " + clockTime
                        + "        | " + waitTime + "   |");

                //clockTime += quantum;
                clockTime += time;
                next[roundRobinIndex] = clockTime;
            }
            roundRobinIndex = (roundRobinIndex + 1) % cpu_burst.length;
        }
        System.out.println("Average wait time" + totalWaitTime / cpu_burst.length);
    }
}

Solution

  • Well, I think there are several problems with this code, but to answer the question: You get the IndexOutOfBounds error because every time you add a process, when you call

    if (cpuburst.add(0)) {
        processesComplete++;
    }
    

    you're adding an additional item (value 0) to the cpuburst-list, so that list is double the length of the others, alternating between the input values and zeros. Later on, you wrote

    if (cpu_burst[roundRobinIndex] > 0) {
        //...some code here...
    }
    roundRobinIndex = (roundRobinIndex + 1) % cpu_burst.length;
    

    which means, the code in there will be executed the first time around when there's a cpuburst value the user put in, then the next time it won't because there's a zero and the third time (when you tested with two processes), it will again enter the if-clause but you get indexOutOfBounds errors at

    int waitTime = clockTime - next[roundRobinIndex];
    

    because next only has two entries.