-
Notifications
You must be signed in to change notification settings - Fork 0
/
roundrobin.cpp
94 lines (92 loc) · 2.93 KB
/
roundrobin.cpp
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
#include<stdio.h>
struct times
{
int p,art,but,wtt,tat,rnt;
};
void sortart(struct times a[],int pro)
{
int i,j;
struct times temp;
for(i=0;i<pro;i++)
{
for(j=i+1;j<pro;j++)
{
if(a[i].art > a[j].art)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return;
}
int main()
{
int i,j,pro,time,remain,flag=0,ts;
struct times a[100];
float avgwt=0,avgtt=0;
printf("Round Robin Scheduling Algorithm\n");
printf("Note -\n1. Arrival Time of at least on process should be 0\n2. CPU should never be idle\n");
printf("Enter Number Of Processes : ");
scanf("%d",&pro);
remain=pro;
for(i=0;i<pro;i++)
{
printf("Enter arrival time and Burst time for Process P%d : ",i);
scanf("%d%d",&a[i].art,&a[i].but);
a[i].p = i;
a[i].rnt = a[i].but;
}
sortart(a,pro);
printf("Enter Time Slice OR Quantum Number : ");
scanf("%d",&ts);
printf("\n***************************************\n");
printf("Gantt Chart\n");
printf("0");
for(time=0,i=0;remain!=0;)
{
if(a[i].rnt<=ts && a[i].rnt>0)
{
time = time + a[i].rnt;
printf(" -> [P%d] <- %d",a[i].p,time);
a[i].rnt=0;
flag=1;
}
else if(a[i].rnt > 0)
{
a[i].rnt = a[i].rnt - ts;
time = time + ts;
printf(" -> [P%d] <- %d",a[i].p,time);
}
if(a[i].rnt==0 && flag==1)
{
remain--;
a[i].tat = time-a[i].art;
a[i].wtt = time-a[i].art-a[i].but;
avgwt = avgwt + time-a[i].art-a[i].but;
avgtt = avgtt + time-a[i].art;
flag=0;
}
if(i==pro-1)
i=0;
else if(a[i+1].art <= time)
i++;
else
i=0;
}
printf("\n\n");
printf("***************************************\n");
printf("Pro\tArTi\tBuTi\tTaTi\tWtTi\n");
printf("***************************************\n");
for(i=0;i<pro;i++)
{
printf("P%d\t%d\t%d\t%d\t%d\n",a[i].p,a[i].art,a[i].but,a[i].tat,a[i].wtt);
}
printf("***************************************\n");
avgwt = avgwt/pro;
avgtt = avgtt/pro;
printf("Average Waiting Time : %.2f\n",avgwt);
printf("Average Turnaround Time : %.2f\n",avgtt);
return 0;
}