#include <stdio.
h>
struct Process {
int id;
int at, bt, rt;
int ct, tat, wt;
int completed;
};
int main() {
int n, completed = 0, time = 0, min_rt, idx;
float totalTAT = 0, totalWT = 0;
printf("Enter number of processes: ");
scanf("%d", &n);
struct Process p[100];
for (int i = 0; i < n; i++) {
p[i].id = i + 1;
printf("Enter Arrival Time and Burst Time for P%d: ", p[i].id);
scanf("%d%d", &p[i].at, &p[i].bt);
p[i].rt = p[i].bt;
p[i].completed = 0;
}
// scheduling
while (completed < n) {
min_rt = 1e9;
idx = -1;
for (int i = 0; i < n; i++) {
if (p[i].at <= time && !p[i].completed && p[i].rt < min_rt && p[i].rt >
0) {
min_rt = p[i].rt;
idx = i;
}
}
if (idx != -1) {
p[idx].rt--;
time++;
// If process is completed
if (p[idx].rt == 0) {
p[idx].ct = time;
p[idx].tat = p[idx].ct - p[idx].at;
p[idx].wt = p[idx].tat - p[idx].bt;
p[idx].completed = 1;
totalTAT += p[idx].tat;
totalWT += p[idx].wt;
completed++;
}
} else {
time++; // No process available, advance time
}
}
// Output
printf("\nID\tAT\tBT\tCT\tTAT\tWT\n");
for (int i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\t%d\t%d\n", p[i].id, p[i].at, p[i].bt, p[i].ct,
p[i].tat, p[i].wt);
}
printf("\nAverage Turnaround Time: %.2f\n", totalTAT / n);
printf("Average Waiting Time: %.2f\n", totalWT / n);
return 0;
}