ROUND ROBIN PROGRAM

PHOTO EMBED

Mon May 27 2024 16:09:59 GMT+0000 (Coordinated Universal Time)

Saved by @user02

#include <stdio.h>

int main() {
    int n, tq, i, total_time = 0, time = 0, flag = 0;
    int bt[10], rem_bt[10], wt[10], tat[10]; // Burst times, remaining burst times, waiting times, turnaround times
    float avg_wt = 0, avg_tat = 0;

    printf("Enter the number of processes: ");
    scanf("%d", &n);

    printf("Enter the burst time for each process:\n");
    for (i = 0; i < n; i++) {
        printf("P[%d]: ", i + 1);
        scanf("%d", &bt[i]);
        rem_bt[i] = bt[i]; // Initialize remaining burst time as burst time
    }

    printf("Enter the time quantum: ");
    scanf("%d", &tq);

    while (1) {
        flag = 0;
        for (i = 0; i < n; i++) {
            if (rem_bt[i] > 0) {
                flag = 1; // There is a pending process

                if (rem_bt[i] > tq) {
                    time += tq;
                    rem_bt[i] -= tq;
                } else {
                    time += rem_bt[i];
                    wt[i] = time - bt[i]; // Waiting time is current time minus burst time
                    rem_bt[i] = 0;
                }
            }
        }
        
        if (flag == 0) // All processes are done
            break;
    }

    printf("\nProcess\tBT\tWT\tTAT\n");
    for (i = 0; i < n; i++) {
        tat[i] = bt[i] + wt[i]; // Turnaround time is burst time plus waiting time
        avg_wt += wt[i];
        avg_tat += tat[i];
        printf("P[%d]\t%d\t%d\t%d\n", i + 1, bt[i], wt[i], tat[i]);
    }

    avg_wt /= n;
    avg_tat /= n;

    printf("\nAverage Waiting Time: %.2f", avg_wt);
    printf("\nAverage Turnaround Time: %.2f", avg_tat);

    return 0;
}
content_copyCOPY