p.at));
Queueimport java.util.*;
class Process {
int pid, at, bt, rt, ct, tat, wt; // rt = remaining time
Process(int pid, int at, int bt) {
this.pid = pid;
this.at = at;
this.bt = bt;
this.rt = bt;
}
}
public class RoundRobin {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of processes: ");
int n = sc.nextInt();
Process[] processes = new Process[n];
for (int i = 0; i < n; i++) {
System.out.print("Enter Arrival Time and Burst Time for Process P" + (i + 1) + ": ");
int at = sc.nextInt();
int bt = sc.nextInt();
processes[i] = new Process(i + 1, at, bt);
}
System.out.print("Enter Time Quantum: ");
int tq = sc.nextInt();
// Sort by arrival time
Arrays.sort(processes, Comparator.comparingInt(p -> p.at));
Queue<Process> queue = new LinkedList<>();
int time = 0, completed = 0;
boolean[] visited = new boolean[n];
queue.add(processes[0]);
visited[0] = true;
time = processes[0].at;
while (!queue.isEmpty()) {
Process p = queue.poll();
if (p.rt > tq) {
p.rt -= tq;
time += tq;
} else {
time += p.rt;
p.rt = 0;
p.ct = time;
p.tat = p.ct - p.at;
p.wt = p.tat - p.bt;
completed++;
}
// Add processes that have arrived by current time
for (int i = 0; i < n; i++) {
if (!visited[i] && processes[i].at <= time) {
queue.add(processes[i]);
visited[i] = true;
}
}
// If the current process still has remaining time, re-add it
if (p.rt > 0) queue.add(p);
// If queue is empty but some processes remain, jump to next arrival
if (queue.isEmpty()) {
for (int i = 0; i < n; i++) {
if (!visited[i]) {
queue.add(processes[i]);
visited[i] = true;
time = processes[i].at;
break;
}
}
}
}
// Print results
System.out.println("\\nPID\\tAT\\tBT\\tCT\\tTAT\\tWT");
int totalTAT = 0, totalWT = 0;
for (Process p : processes) {
System.out.println("P" + p.pid + "\\t" + p.at + "\\t" + p.bt + "\\t" +
p.ct + "\\t" + p.tat + "\\t" + p.wt);
totalTAT += p.tat;
totalWT += p.wt;
}
System.out.printf("\\nAverage TAT = %.2f\\n", (float) totalTAT / n);
System.out.printf("Average WT = %.2f\\n", (float) totalWT / n);
sc.close();
}
}
