-
Notifications
You must be signed in to change notification settings - Fork 0
/
roundrobin.c
112 lines (94 loc) · 2.42 KB
/
roundrobin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdio.h>
int main()
{
int t;
float tq, tt = 0, wt = 0;
printf("Enter the number of processes: ");
scanf("%d", &t);
float bt[t], at[t], btr[t], pid[t];
for (int i = 0; i < t; i++)
{
printf("\nEnter the Burst Time of Process %d: ", i+1);
scanf("%f", &bt[i]);
btr[i] = bt [i];
printf("Enter the Arrival Time of Process %d: ", i+1);
scanf("%f", &at[i]);
pid[i] = i+1;
}
int tempat, tempbt, tempbtr, temppid;
for (int i = 0; i < t; i++)
{
for (int j = i+1; j < t; j++)
{
if (at[i]>at[j])
{
tempat = at[i];
at[i] = at[j];
at[j] = tempat;
tempbt = bt[i];
bt[i] = bt[j];
bt[j] = tempbt;
tempbtr = btr[i];
btr[i] = btr[j];
btr[j] = tempbtr;
temppid = pid[i];
pid[i] = pid[j];
pid[j] = temppid;
}
}
}
printf("\nEnter the time quantum: ");
scanf("%f", &tq);
float gct = at[0], totbt = 0, tta[t];
for (int i = 0; i < t; i++)
{
totbt+=bt[i];
}
printf("\nSequence of processes: \n");
for (int i = 0; i < totbt/tq ; i++)
{
for (int j = 0; j < t; j++)
{
if (gct >= at[j])
{
if (btr[j]>=tq)
{
btr[j] -= tq;
gct += tq;
printf("-> P%d ", j+1);
if (btr[j]==0)
{
tta[j] = gct-at[j];
}
}
else if (btr[j]<tq && btr[j]> 0)
{
gct += btr[j];
btr[j] -= btr[j];
printf("-> P%d ", j+1);
if (btr[j]==0)
{
tta[j] = gct-at[j];
}
}
else
continue;
}
}
if (gct - at[0]==totbt)
{
break;
}
}
printf("\n");
for (int i = 0; i < t; i++)
{
wt += tta[i] - bt[i];
}
for (int i = 0; i < t; i++)
{
tt += tta[i];
}
printf("\nAverage turnaround time = %.2f\n", tt/t);
printf("\nAverage waiting time = %.2f\n\n", wt/t);
}