roundrobin.c
68 linesc
DOWNLOAD
1// Aim: Program to perform Round Robin operations (Round Robin).
2#include<stdio.h>
3
4void findwt(int processes[], int n, int bt[], int wt[], int quantum) {
5    int rembt[n];
6    for (int i = 0; i < n; i++) {
7        rembt[i] = bt[i];
8    }
9    int t = 0;
10    while (1) {
11        int done = 1;
12        for (int i = 0; i < n; i++) {
13            if (rembt[i] > 0) {
14                done = 0;
15                if (rembt[i] > quantum) {
16                    t += quantum;
17                    rembt[i] -= quantum;
18                } else {
19                    t = t + rembt[i];
20                    wt[i] = t - bt[i];
21                    rembt[i] = 0;
22                }
23            }
24        }
25        if (done == 1) {
26            break;
27        }
28    }   
29}
30
31void findtat(int processes[], int n, int bt[], int wt[], int tat[]) {
32    for (int i = 0; i < n; i++) {
33        tat[i] = bt[i] + wt[i];
34    }
35}
36
37void findavgtime(int processes[], int n, int bt[], int quantum) {
38    int wt[n], tat[n];
39    findwt(processes, n, bt, wt, quantum);
40    findtat(processes, n, bt, wt, tat);
41    
42    printf("Processes\tBurst Time\tWaiting Time\tTurnaround Time\n");
43    int total_wt = 0, total_tat = 0;
44    for (int i = 0; i < n; i++) {
45        total_wt += wt[i];
46        total_tat += tat[i];
47        printf("\n%d\t\t%d\t\t%d\t\t%d", processes[i], bt[i], wt[i], tat[i]);
48    }
49    printf("\nAverage waiting time = %f", (float)total_wt / (float)n);
50    printf("\nAverage turnaround time = %f", (float)total_tat / (float)n);
51}
52
53int main() {
54    printf("Enter the number of processes: ");
55    int n;
56    scanf("%d", &n);
57    int processes[n], bt[n];
58    printf("Enter the burst time of each process: ");
59    for (int i = 0; i < n; i++) {
60        scanf("%d", &bt[i]);
61        processes[i] = i + 1;
62    }
63    int quantum;
64    printf("Enter the time quantum: ");
65    scanf("%d", &quantum);
66    findavgtime(processes, n, bt, quantum);
67    return 0;
68}