1
votes

I was asked to do a multithreaded simulator of a specific algorithm. One of the tasks was to compare the regular scheduling results with round robin results. When I was looking for information about the round robin scheduling method I found vary general explanations and some code examples that I couldn’t find any relation between them and scheduling the threads. For example this code (found here on stack overflow):

public static void RR3(int numProcess, int[] cpuBurst, int[] arrivalTime){
    int quantum = 3,time = 0, temp;
    int completionTime = 0;
    LinkedList <Integer>process = new LinkedList();
    for (int i = 0; i < numProcess; i++) {
        process.add(i, cpuBurst[i]);
    }

    while (process.isEmpty() != true){

        for (int j  = 0; j < quantum; j++) {
            System.out.println(process.getFirst());
            if(process.peek() == 0 ){
                completionTime = completionTime + time;
                process.remove();
            }
            else{
                temp = process.pop();
                process.push(temp - 1);                   
                time++;                  
            }
        }
        process.addLast(process.getFirst()); 
        process.removeFirst();
    }

    double act = (double) completionTime/numProcess;
    System.out.println("-----------------RR3-----------------");
    System.out.println("             Act = " + act + "ms");  
}

I don't see anything but integers that represent the amount of process, time for each etc., but how do I actually manage their behavior? I dont see any call for a process to run or stop.

1
What is this specific algorithm?Leo

1 Answers

3
votes

You already noticed that this is an abstraction. Namely that there is no real work performed. Instead, the work is just "imitated" by a set of Integers that represent the amount of work.

The question about how to run or stop the processes is somewhat hidden in the algorithm itself: The LinkedList stores the "active" processes. They are all started at the beginning. In each turn, they receive a short time slot in which they can do some of their work. When all their work is done, they are removed from the list.

In the simplest form, when the Integer values are replaced by real tasks, you could replace the line

if(process.peek() == 0 ){ ... }

with something like

Task task = process.peek();
if (task.isFinished()) { ... }

Otherwise (in the else case), when there is work to be done, you could replace the lines

temp = process.pop();
process.push(temp - 1);  

with something like

Task task = process.peek();
task.doALittleBitOfWork();

The code that you posted was originally part of a question, so one has to assume that there's still something wrong with it, but maybe it is sufficient to get the basic idea.