0
for (int i = 0; i < 4; i++)
{
 if (bur[i] > 0) {
    if (bur[i] > qtm) {
      execOrder.add(i + 1);
      bur[i] = bur[i] -qtm;
      flagClounter++;
    } else {
      execOrder.add(i + 1);
      bur[i] = 0;
      flagClounter++;
   }    
 }    
}

Output:

    the time quantum in milliseconds:-
    40000
    the burst_time of the respective machines:-
    00:01:00
    00:02:00
    00:01:00
    00:02:00
    burst_time in milliseconds:-
    60000
    120000
    60000
    120000

    jobs scheduled:-

    1   2   3   4   1   2   3   4   2   4

the above code performs round robin algorithm.. the bur[i] is the burst time array which is retrieved from database and stored it in the form of milliseconds qtm is the time quantum stored in the form of milliseconds. the scheduling is working fine... I just want this to work taking the real time units..i.e, to say if I execute the code at the current time then its should start scheduling and wait for that much burst time to complete the first job then the second and so on.... but at present it just runs in a single shot. for this should I use any Timer class?? the user requests for the schedule of job at a particular time so it should start from there... how can I perform it??please help me with this...

bgth
  • 450
  • 9
  • 26
sb15
  • 69
  • 10

1 Answers1

0

Here is an example to perhaps get things started. As I note, the rounrobin algorithm is similar to the first come first serve scheduling algorithm but the preemption is the added functionality to switch between the processes. Here is a simple example showing roundrobin in Java with output after selecting 3 processes and the relevant fixed time for each process:

import java.util.*;

class RR {
    Scanner sc = new Scanner(System.in);
    int[] bur, rem, wai, ta;
    int size, q, b = 0, t = 0, flag = 0;

    RR(int size) {
        this.size = size;
        bur = new int[size];
        wai = new int[size];
        ta = new int[size];
        rem = new int[size];
    }

    void get() {
        for (int i = 0; i < size; i++) {
            System.out.print("Enter burst time of P" + (i + 1) + ":");
            bur[i] = rem[i] = sc.nextInt();
        }
        System.out.print("Enter quantum time:");
        q = sc.nextInt();
    }

    void round() {
        do {
            flag = 0;
            for (int i = 0; i < size; i++) {
                if (rem[i] >= q) {
                    System.out.print("P" + (i + 1) + "\t");
                    for (int j = 0; j < size; j++) {
                        if (j == i)
                            rem[i] = rem[i] - q;
                        else if (rem[j] > 0)
                            wai[j] += q;
                    }
                } else if (rem[i] > 0) {
                    System.out.print("P" + (i + 1) + "\t");
                    for (int j = 0; j < size; j++) {
                        if (j == i)
                            rem[i] = 0;
                        else if (rem[j] > 0)
                            wai[j] += rem[i];
                    }
                }
            }
            for (int i = 0; i < size; i++)
                if (rem[i] > 0)
                    flag = 1;
        } while (flag == 1);
        for (int i = 0; i < size; i++)
            ta[i] = wai[i] + bur[i];
    }

    void display() {
        System.out.println("\nProcess\tBurst\tWaiting\tTurnaround");
        for (int i = 0; i < size; i++) {
            System.out.println("P" + (i + 1) + "\t" + bur[i] + "\t" + wai[i]
                    + "\t" + ta[i]);
            b += wai[i];
            t += ta[i];
        }
        System.out.println("Average waiting time:" + (b / size));
        System.out.println("Average Turnaround time:" + (t / size));
    }
}

class KRR {
    public static void main(String arg[]) {
        Scanner s = new Scanner(System.in);
        System.out.print("Enter the no of process:");
        int n = s.nextInt();
        RR obj = new RR(n);
        obj.get();
        obj.round();
        obj.display();
    }
}

Once the code is compiled and run, on the console you get the following. Let's say that we enter the various burst times manually (which you could of course put into an array and loop through it). Here is the output, assuming that we decided to have 3 processes running.

Enter the no of processes: 3
Enter burst time of P1: 13
Enter burst time of P2: 8
Enter burst time of P3: 3
Enter quantum time: 4

Process Burst   Waiting Turnaround
P1      13      11      24
P2      8       11      19
P3      3       8       11

Average waiting time:10
Average Turnaround time:18                  
bgth
  • 450
  • 9
  • 26
Nathaniel Payne
  • 2,749
  • 1
  • 28
  • 32
  • I don't have problem in executing the round robin algo it gives correct output..... but I want to take actual time units and perform it.. that is,, when I execute it has to wait for that much burst time to complete in real time say, 1st job I gave 1 min bursttime and 40 sec as qtm, so I need the program to wait for so much time while executing and then the second one and so on... should I use any timer function??? – sb15 Apr 06 '14 at 06:13
  • 1
    Have you tried Thread.sleep..? http://stackoverflow.com/questions/14005549/how-to-properly-use-thread-sleep – bgth Apr 06 '14 at 06:19
  • I was going through the thread.sleep but here in the program the bursttime varies and the time quantum is fixed. how to give it in the thread.sleep as a parameter..since it varies.. please help me with that – sb15 Apr 06 '14 at 07:15