Monday 14 December 2015

Round Robin Algorithm Implementation

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
#include<iostream.h>
#include<conio.h>
struct process
{
    int no;
    int at,et,wt,tt;
    int tet;
    int t;
};
 
void main()
{
    process p[99];
    int i,j,k;
    cout<<"\n Enter No of Processes:";
    int np;
    cin>>np;
 
    for (i=0;i<np;i++)
    {
        cout<<"\n Enter Execution time of process"<<i+1<<":";
        cin>>p[i].et;
        p[i].tet=p[i].et;
        p[i].at=p[i].t=p[i].tt=p[i].wt=0;
        p[i].no=i+1;
    }
 
    cout<<"\n Enter Time Quantum:";
    int q;
    cin>>q;
 
    cout<<"\n Entered Data";
    cout<<"\n Process\tET";
    for(i=0;i<np;i++)
    {
        cout<<"\n "<<p[i].no<<"\t"<<p[i].et;
    }
 
    int totaltime=0;
    for(i=0;i<np;i++)
    {
        totaltime+=p[i].et;
    }
 
    i=0;
    k=0;
 
    int rrg[99];
    for(j=0;j<totaltime;j++)
    {
        if((k==0)&&(p[i].et!=0))
        {
            p[i].wt=j;
            if((p[i].t!=0))
            {
                p[i].wt-=q*p[i].t;
            }
        }
        if((p[i].et!=0)&&(k!=q))
        {
            rrg[j]=p[i].no;
            p[i].et-=1;
            k++;
        }
        else
        {
            if((k==q)&&(p[i].et!=0))
            {
                p[i].t+=1;
            }
            i=i+1;
            if(i==np)
            {
                i=0;
            }
 
                k=0;
            j=j-1;
        }
    }
 
    /*
    for(j=0;j<totaltime;j++)
    {
        cout<<"\n"<<rrg[j];
    }
    */
 
    int twt=0;
    int ttt=0;
    cout<<"\n Result Of Round Robin";
    cout<<"\n PNo\tET\tWT\tTT";
    for(i=0;i<np;i++)
    {
        p[i].tt=p[i].wt+p[i].tet;
        ttt+=p[i].tt;
        twt+=p[i].wt;
        cout<<"\n "<<p[i].no<<"\t"<<"\t"<<p[i].tet<<"\t"<<p[i].wt<<"\t"<<p[i].tt;
    }
 
    cout<<"\n Average Waiting Time:"<<(float)twt/np;
    cout<<"\n Average Turn Around Time:"<<(float)ttt/np;
 
    getch();
}
 
/* Output
 
 
 Enter No of Processes:5
 
 Enter Execution time of process1:10
 
 Enter Execution time of process2:29
 
 Enter Execution time of process3:3
 
 Enter Execution time of process4:7
 
 Enter Execution time of process5:12
 
 Enter Time Quantum:10
 
 Entered Data
 Process        ET
 1      10
 2      29
 3      3
 4      7
 5      12
 Result Of Round Robin
 PNo    ET      WT      TT
 1              10      0       10
 2              29      32      61
 3              3       20      23
 4              7       23      30
 5              12      40      52
 Average Waiting Time:23
 Average Turn Around Time:35.2
 
*/



Brought to you By B2B Info Solutions

No comments:

Post a Comment