-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrr_final.c
196 lines (178 loc) · 6 KB
/
rr_final.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
struct Process
{
int pno;
int arrival;
int burst;
float burst_complete;
float burst_remain;
float last_time;
int turn;
int wait;
int done;
}P[20];
void swap(struct Process *p1, struct Process *p2)
{
struct Process tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
void sorting(int n)
{
for(int i=0;i<n-1;i++)
for (int j=0;j<n-i-1;j++)
if(P[j].arrival > P[j+1].arrival)
swap(&P[j], &P[j+1]);
}
void waiting(int n, float quantum, char opt)
{
float wavg=0.0, tavg=0.0, time = P[0].arrival, total_burst;
for(int i=0;i<n;i++)
{
total_burst += P[i].burst;
P[i].burst_remain = P[i].burst;
P[i].burst_complete = 0;
P[i].wait = 0;
P[i].last_time = 0;
P[i].done = 0;
}
if(opt=='y')
printf("\n\nOutput:\nProcess\t Burst Time Completed\t Burst Time Remaining\t Time Elapsed\n");
while(1)
{
int flag = 0, flag2=0;
for(int i=0;i<n;i++)
{
if(P[i].done==0)
{
if(flag2!=0)
time += 1;
if(P[i].arrival<=time)
{
if(P[i].burst_remain<=quantum)
{
if(P[i].last_time==0)
{
P[i].wait = time - P[i].arrival;
}
else
P[i].wait += (time - P[i].last_time);
time += P[i].burst_remain;
P[i].burst_complete += P[i].burst_remain;
P[i].burst_remain = 0;
P[i].turn = time - P[i].arrival;
P[i].last_time = time;
P[i].done = 1;
}
else
{
if(P[i].last_time==0)
{
P[i].wait = time - P[i].arrival;
}
else
P[i].wait += (time - P[i].last_time);
time += quantum;
P[i].burst_complete += quantum;
P[i].burst_remain -= quantum;
P[i].last_time = time;
}
if(opt=='y')
printf("\nP%d\t\t%f\t\t\t%f\t %f\n",P[i].pno,P[i].burst_complete,P[i].burst_remain,time);
}
else
{
flag2++;
continue;
}
}
else
{
flag++;
continue;
}
}
if(flag==n)
break;
}
int err = 0;
for(int i=0;i<n;i++)
{
wavg += P[i].wait;
tavg += P[i].turn;
if(P[i].burst!=P[i].burst_complete) //
{ // Some Error checks
printf("\nBurst integrity failed for P%d",P[i].pno); // Not Necessary
err++; //
} //
}
if(err==0) //
printf("\nIndividual burst time of each process integral"); //
else //
printf("\nIndividual burst time integrity failed for %d processes",err); // Some Error checks
if(total_burst=time-P[0].arrival) // Not Necessary
printf("\nTotal burst time integrity check passed\n"); //
else //
printf("\nTotal burst time integrity check failed\n"); //
printf("\nProcess\t Burst Time\t Arrival Time\t Waiting Time\t Turnaround Time\n");
for(int i=0;i<n;i++)
{
printf("\nP%d\t\t%d\t\t%d\t\t%d\t\t%d\n",P[i].pno,P[i].burst,P[i].arrival,P[i].wait,P[i].turn);
}
printf("\nAverage waiting time: %f", wavg/n);
printf("\nAverage turnaround time: %f", tavg/n);
}
int main()
{
int n;
char ch, fl, opt;
float quantum = 5;
printf("Continue with random input? (y/n): ");
scanf("%c",&ch);
scanf("%c",&fl);
printf("Do you want to print the sequence? (y/n): ");
scanf("%c",&opt);
scanf("%c",&fl);
if(ch=='y')
{
printf("continuing with random inputs");
srand(time(0));
n = rand()%10 + 1;
for (int i=0; i<n; i++)
{
P[i].pno=i+1;
P[i].burst = rand()%10 + 1;
P[i].arrival = rand()%10 + 1;
}
printf("\n\nInput: Quantum = %f",quantum);
printf("\nProcess\t Burst Time\t Arrival Time\n");
for(int i=0;i<n;i++)
printf("\nP%d\t\t%d\t\t%d\n",P[i].pno,P[i].burst,P[i].arrival);
}
else if(ch=='n')
{
printf("Enter no of processes: ");
scanf("%d", &n);
printf("Enter values: ");
for (int i=0; i<n; i++)
{
P[i].pno=i+1;
printf("P[%d]: ", i);
printf("Burst time: ");
scanf("%d", &P[i].burst);
printf("Arrival time: ");
scanf("%d", &P[i].arrival);
}
printf("\n\nInput: Quantum = %f",quantum);
printf("\nProcess\t Burst Time\t Arrival Time\n");
for(int i=0;i<n;i++)
printf("\nP%d\t\t%d\t\t%d\n",P[i].pno,P[i].burst,P[i].arrival);
}
else {printf("Invalid Input\n"); return 0;}
sorting(n);
waiting(n,quantum,opt);
printf("\n");
return 0;
}