-
Notifications
You must be signed in to change notification settings - Fork 10
/
Round Robin Scheduling Algorithm.c
71 lines (71 loc) · 1.61 KB
/
Round Robin Scheduling Algorithm.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
#include<stdio.h>
struct proc
{
int no,at,bt,ct,tat,wt,rt;
};
struct proc read(int i)
{
struct proc p;
printf("\nProcess No: %d\n",i);
p.no=i;
printf("Enter Arrival Time: ");
scanf("%d",&p.at);
printf("Enter Burst Time: ");
scanf("%d",&p.bt);
p.rt=p.bt;
return p;
}
int main()
{
struct proc p[10],tmp;
float avgtat=0,avgwt=0;
int n,tq,ct=0,flag=0,remaining;
printf("<--Round Robin Scheduling Algorithm-->\n");
printf("Enter Number of Processes: ");
scanf("%d",&n);
printf("Enter Time Quantum: ");
scanf("%d",&tq);
for(int i=0;i<n;i++)
p[i]=read(i+1);
for(int i=0;i<n-1;i++)
for(int j=0;j<n-i-1;j++)
if(p[j].at>p[j+1].at)
{
tmp=p[j];
p[j]=p[j+1];
p[j+1]=tmp;
}
remaining=n;
printf("\nProcessNo\tAT\tBT\tCT\tTAT\tWT\n");
for(int i=0;remaining!=0;)
{
if(p[i].rt<=tq&&p[i].rt>0)
{
ct+=p[i].rt;
p[i].rt=0;
flag=1;
}
else if(p[i].rt>0)
{
p[i].rt-=tq;
ct+=tq;
}
if(p[i].rt==0&&flag==1)
{
flag = 0;
remaining--;
p[i].ct=ct;
p[i].tat=p[i].ct-p[i].at;
avgtat+=p[i].tat;
p[i].wt=p[i].tat-p[i].bt;
avgwt+=p[i].wt;
printf("P%d\t\t%d\t%d\t%d\t%d\t%d\n",p[i].no,p[i].at,p[i].bt,p[i].ct,p[i].tat,p[i].wt);
}
if(i!=n-1&&p[i+1].at<=ct)
i++;
else
i=0;
}
avgtat/=n,avgwt/=n;
printf("\nAverage TurnAroundTime=%f\nAverage WaitingTime=%f",avgtat,avgwt);
}