-
Notifications
You must be signed in to change notification settings - Fork 4
/
mm1.cc
81 lines (65 loc) · 1.45 KB
/
mm1.cc
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
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include "edsim_naive.h"
//return exponentially distributed random numbers
double exponential(double r);
//settings:
//average inter-arrival time
double g_avg_arrv_intv = 5;
//average serve time
double g_avg_serv_time = 500;
//statistics
int g_online_user_cnt = 0;
FILE *g_log_file = NULL;
void userArrival(void);
void userDeparture(void);
void logOnlineUserCount(void);
int main(int argc, const char *argv[])
{
g_log_file = fopen("online_user_cnt.txt", "w");
if (g_log_file==NULL){
fprintf(stderr, "Open log file failed\n");
return 1;
}
double end_time = 100000;
//first arrival event
Event first_arrv(0.0, &userArrival);
enqueue(first_arrv);
run_sim(end_time);
fclose(g_log_file);
return 0;
}
void logOnlineUserCount(void)
{
fprintf(g_log_file, "%f %d\n", now(), g_online_user_cnt);
}
void userDeparture(void)
{
--g_online_user_cnt;
logOnlineUserCount();
}
void userArrival(void)
{
++g_online_user_cnt;
logOnlineUserCount();
double interval = exponential(g_avg_arrv_intv);
double serv_time = exponential(g_avg_serv_time);
Event next_arrv(now()+interval, &userArrival);
Event departure(now()+serv_time, &userDeparture);
enqueue(next_arrv);
enqueue(departure);
}
//random number generation
double uniform()
{
return ((double)rand())/( ((double)(RAND_MAX)) + 1);
}
double exponential()
{
return (-log(uniform()));
}
double exponential(double r)
{
return (r * exponential());
}