-
Notifications
You must be signed in to change notification settings - Fork 0
/
rr2.c
83 lines (72 loc) · 1.89 KB
/
rr2.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
#include<stdio.h>
#include<stdlib.h>
typedef struct {
int pid;
int at;
int bt;
int ct;
int wt;
int tat;
int rt;
} process;
int main() {
int n, i, j,completed=0, curtime=0,avtat=0,avwt=0,front=0,rear=-1;
printf("Enter the number of processes: ");
scanf("%d", &n);
process p[30],q[30];
int quantum;
printf("Enter the time quantum: ");
scanf("%d", &quantum);
printf("Enter arrival time and burst time for each process:\n");
for (int i = 0; i < n; i++)
{
p[i].pid = i + 1;
scanf("%d %d", &p[i].at, &p[i].bt);
p[i].rt = p[i].bt;
}
for (int i = 0; i < n; i++)
{
for (int j = i; j <n; j++)
{
if (p[j].at > p[j ].at)
{
process temp = p[j];
p[j] = p[j ];
p[j] = temp;
}
}
}
for (int i = 0; i < n; i++)
q[++rear] = p[i];
while(completed < n)
{
i=front%n;
if(q[i].rt>quantum)
{
q[i].rt-=quantum;
curtime+=quantum;
}
else if(q[i].rt<=quantum && q[i].rt!=-1){ // p[i].rt<=quantum; Process completed
curtime+=q[i].rt;
q[i].ct=curtime;
q[i].tat=q[i].ct-q[i].at;
q[i].wt=q[i].tat-q[i].bt;
avtat+=q[i].tat;
avwt=q[i].wt;
++completed;
q[i].rt=-1;
}
front++;
}
for (int i = 0; i < n; i++)
{
p[i] = q[i];
}
printf("\nProcess id\tArrival time\tBurst time\tCompletion time\tWaiting time\tTurnaround time\n");
for (i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", p[i].pid, p[i].at, p[i].bt, p[i].ct, p[i].wt, p[i].tat);
}
printf("Average waiting time:%d",avwt/n);
printf("Average turnaround time:%d",avtat/n);
return 0;
}