-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnd
More file actions
458 lines (383 loc) · 9.47 KB
/
nd
File metadata and controls
458 lines (383 loc) · 9.47 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// Java program to implement Round Robin
// Scheduling with different arrival time
class roundrobin {
public static void roundRobin(String p[], int a[],
int b[], int n)
{
// result of average times
int res = 0;
int resc = 0;
// for sequence storage
String seq = new String();
// copy the burst array and arrival array
// for not effecting the actual array
int res_b[] = new int[b.length];
int res_a[] = new int[a.length];
for (int i = 0; i < res_b.length; i++) {
res_b[i] = b[i];
res_a[i] = a[i];
}
// critical time of system
int t = 0;
// for store the waiting time
int w[] = new int[p.length];
// for store the Completion time
int comp[] = new int[p.length];
while (true) {
boolean flag = true;
for (int i = 0; i < p.length; i++) {
// these condition for if
// arrival is not on zero
// check that if there come before qtime
if (res_a[i] <= t) {
if (res_a[i] <= n) {
if (res_b[i] > 0) {
flag = false;
if (res_b[i] > n) {
// make decrease the b time
t = t + n;
res_b[i] = res_b[i] - n;
res_a[i] = res_a[i] + n;
seq += "->" + p[i];
}
else {
// for last time
t = t + res_b[i];
// store comp time
comp[i] = t - a[i];
// store wait time
w[i] = t - b[i] - a[i];
res_b[i] = 0;
// add sequence
seq += "->" + p[i];
}
}
}
else if (res_a[i] > n) {
// is any have less arrival time
// the coming process then execute them
for (int j = 0; j < p.length; j++) {
// compare
if (res_a[j] < res_a[i]) {
if (res_b[j] > 0) {
flag = false;
if (res_b[j] > n) {
t = t + n;
res_b[j] = res_b[j] - n;
res_a[j] = res_a[j] + n;
seq += "->" + p[j];
}
else {
t = t + res_b[j];
comp[j] = t - a[j];
w[j] = t - b[j] - a[j];
res_b[j] = 0;
seq += "->" + p[j];
}
}
}
}
// now the previous porcess according to
// ith is process
if (res_b[i] > 0) {
flag = false;
// Check for greaters
if (res_b[i] > n) {
t = t + n;
res_b[i] = res_b[i] - n;
res_a[i] = res_a[i] + n;
seq += "->" + p[i];
}
else {
t = t + res_b[i];
comp[i] = t - a[i];
w[i] = t - b[i] - a[i];
res_b[i] = 0;
seq += "->" + p[i];
}
}
}
}
// if no process is come on thse critical
else if (res_a[i] > t) {
t++;
i--;
}
}
// for exit the while loop
if (flag) {
break;
}
}
System.out.println("name ctime wtime");
for (int i = 0; i < p.length; i++) {
System.out.println(" " + p[i] + " " + comp[i]
+ " " + w[i]);
res = res + w[i];
resc = resc + comp[i];
}
System.out.println("Average waiting time is "
+ (float)res / p.length);
System.out.println("Average compilation time is "
+ (float)resc / p.length);
System.out.println("Sequence is like that " + seq);
}
// Driver Code
public static void main(String args[])
{
// name of the process
String name[] = { "p1", "p2", "p3", "p4" };
// arrival for every process
int arrivaltime[] = { 0, 1, 2, 3 };
// burst time for every process
int bursttime[] = { 10, 4, 5, 3 };
// quantum time of each process
int q = 3;
// cal the function for output
roundRobin(name, arrivaltime, bursttime, q);
}
}
#include<bits/stdc++.h>
using namespace std;
#define n 5
void arrangeacctoarrival(int pid[n],int at[n],int bt[n])
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(at[j]>at[j+1])
{
swap(pid[j],pid[j+1]);
swap(at[j],at[j+1]);
swap(bt[j],bt[j+1]);
}
}
}
}
void roundrobin(int pid[n],int at[n],int bt[n],int wt[n],int tat[n],int ct[n],int tq)
{
int bt1[n];
for (int i = 0 ; i < n ; i++)
bt1[i] = bt[i];
int t = 0;
while (1)
{
bool done = true;
for (int i = 0 ; i < n; i++)
{
if (bt1[i] > 0)
{
done = false;
if (bt1[i] > tq)
{
t += tq;
bt1[i] -= tq;
}
else
{
t = t + bt1[i];
wt[i] = t - bt[i];
bt1[i] = 0;
}
}
}
if (done == true)
break;
}
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
// for (int i=0; i<n; i++)
// {
// total_wt = total_wt + wt[i];
// total_tat = total_tat + tat[i];
// cout << " " << i+1 << "\t\t" << bt[i] <<"\t "
// << wt[i] <<"\t\t " << tat[i] <<endl;
// }
// cout << "Average waiting time = "
// << (float)total_wt / (float)n;
// cout << "\nAverage turn around time = "
// << (float)total_tat / (float)n;
}
int main()
{
int pid[n],at[n],bt[n],wt[n],tat[n],ct[n];
int tq;
cout<<"...Enter the process ID...\n";
for(int i=0; i<n; i++)
{
cout<<"...Process "<<i+1<<"...\n";
cout<<"Enter Process Id: ";
cin>>pid[i];
cout<<"Enter Arrival Time: ";
cin>>at[i];
cout<<"Enter Burst Time: ";
cin>>bt[i];
wt[i]=0;
tat[i]=0;
ct[i]=0;
}
cout<<"Enter time quanta:\n";
cin>>tq;
arrangeacctoarrival(pid,at,bt);
cout<<"According to arrival ...\n";
cout<<"Process ID\tArrival Time\tBurst Time\n";
for(int i=0; i<n; i++)
{
cout<<pid[i]<<"\t\t"<<at[i]<<"\t\t"<<bt[i]<<"\n";
}
roundrobin(pid,at,bt,wt,tat,ct,tq);
cout<<"Final Result...\n";
cout<<"Process ID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n";
for(int i=0; i<n; i++)
{
cout<<pid[i]<<"\t\t"<<at[i]<<"\t\t"<<bt[i]<<"\t\t"<<wt[i]<<"\t\t"<<tat[i]<<"\n";
}
}
#include<bits/stdc++.h>
using namespace std;
#define n 6
void arrangeacctoarrival(int pid[n],int at[n],int bt[n])
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(at[j]>at[j+1])
{
swap(pid[j],pid[j+1]);
swap(at[j],at[j+1]);
swap(bt[j],bt[j+1]);
}
}
}
}
void roundrobin(int p[n],int at[n],int bt[n],int w[n],int tat[n],int comp[n],int tq)
{
string seq;
// copy the burst array and arrival array
// for not effecting the actual array
int bt1[n];
int at1[n];
for (int i = 0; i < n; i++) {
bt1[i] = bt[i];
at1[i] = at[i];
}
int t = 0;
while (true) {
bool flag = true;
for (int i = 0; i < n; i++) {
if (at1[i] <= t) {
if (at1[i] <= tq) {
if (bt1[i] > 0) {
flag = false;
if (bt1[i] > tq) {
// make decrease the b time
t = t + tq;
bt1[i] = bt1[i] - tq;
at1[i] = at1[i] + tq;
seq += "->" + to_string(p[i]);
}
else {
// for last time
t = t + bt1[i];
// store comp time
comp[i] = t - at[i];
// store wait time
w[i] = t - bt[i] - at[i];
bt1[i] = 0;
// add sequence
seq += "->" + to_string(p[i]);
}
}
}
else if (at1[i] > n) {
// is any have less arrival time
// the coming process then execute them
for (int j = 0; j < n; j++) {
// compare
if (at1[j] < at1[i]) {
if (bt1[j] > 0) {
flag = false;
if (bt1[j] > tq) {
t = t + tq;
bt1[j] = bt1[j] - tq;
at1[j] = at1[j] + tq;
seq += "->" + to_string(p[j]);
}
else {
t = t + bt1[j];
comp[j] = t - at[j];
w[j] = t - bt[j] - at[j];
bt1[j] = 0;
seq += "->" + to_string(p[j]);
}
}
}
}
if (bt1[i] > 0) {
flag = false;
if (bt1[i] > tq) {
t = t + tq;
bt1[i] = bt1[i] - tq;
at1[i] = at1[i] + tq;
seq += "->" + p[i];
}
else {
t = t + bt1[i];
comp[i] = t - at[i];
w[i] = t - bt[i] - at[i];
bt1[i] = 0;
seq += "->" + to_string(p[i]);
}
}
}
}
// if no process is come on thse critical
else if (at1[i] > t) {
t++;
i--;
}
}
// for exit the while loop
if (flag) {
break;
}
}
cout<<seq;
}
int main()
{
int pid[n],at[n],bt[n],wt[n],tat[n],ct[n];
int tq;
cout<<"...Enter the process ID...\n";
for(int i=0; i<n; i++)
{
cout<<"...Process "<<i+1<<"...\n";
cout<<"Enter Process Id: ";
cin>>pid[i];
cout<<"Enter Arrival Time: ";
cin>>at[i];
cout<<"Enter Burst Time: ";
cin>>bt[i];
wt[i]=0;
tat[i]=0;
ct[i]=0;
}
cout<<"Enter time quanta:\n";
cin>>tq;
arrangeacctoarrival(pid,at,bt);
cout<<"According to arrival ...\n";
cout<<"Process ID\tArrival Time\tBurst Time\n";
for(int i=0; i<n; i++)
{
cout<<pid[i]<<"\t\t"<<at[i]<<"\t\t"<<bt[i]<<"\n";
}
roundrobin(pid,at,bt,wt,tat,ct,tq);
cout<<"Final Result...\n";
cout<<"Process ID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n";
for(int i=0; i<n; i++)
{
cout<<pid[i]<<"\t\t"<<at[i]<<"\t\t"<<bt[i]<<"\t\t"<<wt[i]<<"\t\t"<<tat[i]<<"\n";
}
}