need help to convert to round robin

i just need help on converting my void departure(void) into a round robin algortihm, need a selected queue for it, now its on First in first out


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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>


/*variables declaration* yg warna MERAH tu kami ubah…*/
#define TOS 100 //termination of service; number of repetition for each experiment :100
#define MAXNOSRC 6 //number of sources based on article
#define MAXQSIZE 0 //max queue size or buffer based in graph in article
#define MAXNOE 10 //number of experiments


double arrst[MAXNOSRC]; //arrival service time: stores the time each source will generate 'a' packet
double patq[MAXQSIZE]; //packet arrival time in queue
double dpst; //departure service time: store the time a packet will leave the server after being processed
double iat; //inter arrival time (iat=1/?)
double st; //service time (st=1/µ)
double simclock; //simulation clock
double delay; //delay
double tdelay; //total delay
double avgdelay; //average delay= tdelay/npd
double plr; //packet loss ratio=tpl/npa
double tput; //throughput=npd/npa 
double smallest; //smallest simulation time
double tqsize;
double last_check;
double acptdelay;

double load; //?
double npd; //# of packet departured/processed
double npa; //# of packet arrive/generate
double tpl; //total packet lost
int sinkStatus; //data sink status (1: idle; 2: busy)
int ssrc; //selected source
int evtype; //event type (1: arrival; 2: departure)
int cqsize; //current queue size
int noe; //# of experiment
int maxqsize;
int flag;

/*functions prototype*/
double traffic(void);
void init(void);//int. of variable
void updateclock(void);//
void scheduler(void);//
void arrival(void);
void departure(void);
void result(void);
int main(void);

FILE *out_tdelay; //output file pointer for total delay: total_delay.csv
FILE *out_avgdelay; //output file pointer for average delay: average_delay.csv
FILE *out_plr; //output file pointer for packet loss ratio: packet_loss_ratio.csv
FILE *out_tput;// output file pointer for throughput ; thoroughput.csv

double traffic(void)
/*poisson distribution use to generate traffic*/
{
    double x=rand(); //get random #
    double iat=0; //initialize interAT=0
    iat = -log(x/1.0e+30)/load; //formula to inject randomness into Poisson Distribution
    return iat; //return interAT to caller function
}

void init(void)
{
    int i;
    load+=0.1; //increament load, load start at 5 berdasarkan rumus drp little's law : N = lambda x D 

    arrst[MAXNOSRC]=0.0; //arrival service time: stores the time each source will generate 'a' packet
    patq[MAXQSIZE]=0.0; //packet arrival time in queue
    for(i=0; i<MAXNOSRC; ++i)
        arrst[i]=rand(); //assign random start time to each arrival node==> how to get rand # between 0-1??
    //arrst[0]=0.6; arrst[1]=0.5; arrst[2]=0.8;
    dpst=1.0e+30; //departure service time: store the time a packet will leave the server after being processed
    iat=0.5;
    st=5.0; //fix service time (st=1/µ == packet size/bandwidth)
    simclock=0.0; //simulation clock
    smallest=1.0e+30; //smallest simulation time
    avgdelay= 0.0; //average delay=
    plr=0.0; //packet loss ratio
	tput=0.0; // throughput
    tdelay= rand() % 10; //total delay
    npd=0.0; //# of packet departured/sink
    npa=0.0; //# of packet arrived/generate
    tpl=0.0; //# of packet loss
    sinkStatus=0; //data sink status set to IDLE
    ssrc=0; //selected source
    evtype=1; //event type (1: arrival; 2: departure)
    cqsize=0; //current queue size/number in queue
    maxqsize+=50; // berdasarkan paper increment maxqsize adalah 50
   // last_check=0.0;
    tqsize=0.0;
    
    
} //end init

void updateclock(void)
/*Advance the simulation clock*/
{
    simclock=smallest; //assign current smallest time to simclock
}

void scheduler(void)
/*Determine the event type of the next event to occur*/
{
    int i;
    smallest=1.0e+30; //assign very big number(infinity) to smallest time
    for(i=0; i<MAXNOSRC; ++i) //find the smallest among sources
    {
        if(arrst[i]<smallest) //if arrival start time[a] is less than smallest time
        {
            smallest=arrst[i]; //smallest become arrival start time for source [a]
            ssrc=i; //pick selected source
            evtype=1; //event type is ARRIVAL
        }
        if(dpst<smallest) //if departure start time is less than smallest time
        {
            smallest=dpst; //assign departure start time to smallest time
            evtype=2; //event type is DEPARTURE
        }
    } //end for
} //end scheduler

void arrival(void)
{
    ++npa; //increament of packet generation/arrival
    iat=traffic(); //inter arrival time set to [-log(x/1.0+30)]/load; x is random #
    arrst[ssrc]=simclock+iat;
    if(sinkStatus==0) //if sink status is IDLE 1
    {
        sinkStatus=1; //change sink status to BUSY 2
        dpst=simclock+st;  //schedule the time a task completely processed
    }
    else if(sinkStatus==1) //if sink status is BUSY 2
    {
        if(cqsize==MAXQSIZE) //if queue is full
            ++tpl; //increment total packet lost
        else if(cqsize<MAXQSIZE) //if queue space is still available
        {
            patq[cqsize]=simclock; //assign simclock to the time of current packet in queue
            ++cqsize; //increment current buffer in queue
        } //end else
    } //end elseif
} //end arrival

void departure(void)
{
    int i;

    ++npd; //increment the number of task processed/packet departed
    while (flag==1)
          if(cqsize==0) //if the buffer or queue is empty
           { 
             sinkStatus=0; //no processsing
              dpst=1.0e+30; //assign very large value (infinity) to dpst
               }
           else if(cqsize!=0) //if there are packets in the queue
           {
               delay=simclock-patq[0]; //compute delay of packet who is beginning service
               tdelay+=delay; //update of total delay
               if (delay > acptdelay) {
               --cqsize;
               ++tpl;
                for(i=0; i<MAXQSIZE; ++i) 
                patq[i]=patq[i+1]; //move packet in queue up 1 place
                dpst=simclock+st; //departure start time
                --cqsize;
           }
          else if (delay < acptdelay)
              {
                  dpst=simclock+st;
                  flag==2;
                  --cqsize;
                  }
                  }
} //end departure

void result(void)
/*Compute and write estimates of desired measures of performance*/
{
    avgdelay=tdelay/npd; //to compute average delay
    plr=tpl/npa; //to compute packet loss ratio
    tput=npd/npa; // to compute throughput
  //  printf("%f,%f", load, tdelay);
    fprintf(out_tdelay,"%f,%f\n",load, tdelay);
    fprintf(out_avgdelay,"%f,%f\n",load, avgdelay);
    fprintf(out_plr,"%f,%f\n",load, plr);
    fprintf(out_tput,"%f,%f\n",load, tput);
    
   
} //end result

int main(void)
{
    out_tdelay=fopen("total_delay.csv","w");
    out_avgdelay=fopen("average_delay.csv","w");
    for(noe=0; noe<MAXNOE; ++noe) //represent the repeatition of experiments
    {
        init();
        //repeatition for one individually experiment
        while(npd<TOS) //TOS based on npd
        {
            scheduler(); //invoke scheduler function
            updateclock(); //invoke updateclock function
            if(evtype==1)
                arrival(); //invoke arrival function
            else if(evtype==2)
                departure(); //invoke departure function
        } //end while
        result(); //invoke result function
    } //end for
    fclose(out_tdelay);
    fclose(out_avgdelay);
    system("wgnuplot simplot_tdelay.plt");
    system("wgnuplot simplot_avgdelay.plt");
    return 0;
} //end main 
Topic archived. No new replies allowed.