-
Notifications
You must be signed in to change notification settings - Fork 1
/
Round Robin(RR).c
109 lines (96 loc) · 2.16 KB
/
Round Robin(RR).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
107
108
109
#include<stdio.h>
#include<limits.h>
#include<stdbool.h>
struct P
{
int AT,BT,ST[20],WT,FT,TAT,pos;
};
int quant;
int main()
{
int n, i, j;
printf("Enter the no. of processes: ");
scanf("%d",&n);
struct P p[n];
printf("Enter the quantum: ");
scanf("%d",&quant);
printf("Enter the process numbers: \n");
for(i=0;i<n;i++)
scanf("%d",&(p[i].pos));
printf("Enter the Arrival time of processes: \n");
for(i=0;i<n;i++)
scanf("%d",&(p[i].AT));
printf("Enter the Burst time of processes: \n");
for(i=0;i<n;i++)
scanf("%d",&(p[i].BT));
int c=n, s[n][20];
float time=0, mini=INT_MAX, b[n], a[n];
int index=-1;
for(i=0;i<n;i++)
{
b[i]=p[i].BT;
a[i]=p[i].AT;
for(j=0;j<20;j++)
s[i][j]=-1;
}
int tot_wt=0, tot_tat=0;
bool flag=false;
while(c!=0)
{
mini=INT_MAX; flag=false;
for(i=0;i<n;i++)
{
float p=time+0.1;
if(a[i]<=p && mini>a[i] && b[i]>0)
{
index=i;
mini=a[i];
flag=true;
}
}
if(!flag)
{
time++;
continue;
}
j=0;
while(s[index][j]!=-1)
j++;
if(s[index][j]==-1)
{
s[index][j]=time;
p[index].ST[j]=time;
}
if(b[index]<=quant)
{
time+=b[index];
b[index]=0;
}
else
{
time+=quant;
b[index]-=quant;
}
if(b[index]>0)
a[index]=time+0.1;
if(b[index]==0)
{
c--;
p[index].FT=time;
p[index].WT=p[index].FT-p[index].AT-p[index].BT;
tot_wt+=p[index].WT;
p[index].TAT=p[index].BT+p[index].WT;
tot_tat+=p[index].TAT;
}
}
printf("P_id\tAT\tBT\tCT\tTAT\tWT\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t%d\t", p[i].pos, p[i].AT, p[i].BT);
printf("%d\t%d\t%d\n", p[i].FT, p[i].TAT, p[i].WT);
}
double avg_wt=tot_wt/(float)n, avg_tat=tot_tat/(float)n;
printf("The average TurnAround time is: %lf\n", avg_tat);
printf("The average wait time is: %lf\n", avg_wt);
return 0;
}