-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_mm1.c
237 lines (221 loc) · 5.37 KB
/
multi_mm1.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include "multi_mm1.h"
// Create a queue
queue *q_new(){
queue *q = malloc(sizeof(queue));
if(q){
q->head = NULL;
q->tail = NULL;
q->size = 0;
q->t = 0;
q->dt = 0;
return q;
}
else
return NULL;
}
// Create a node
node *node_new(double it, double st){
node *tmp = malloc(sizeof(node));
if(tmp){
tmp->it = it;
tmp->st = st;
tmp->at = -1;
tmp->which_server = -1;
return tmp;
}
else
return NULL;
}
// Create a system
_system *system_new(int n, double lambda, double mu){
_system *tmp = malloc(sizeof(_system));
if(tmp){
tmp->time = 0;
tmp->n = n;
tmp->lambda = lambda;
tmp->mu = mu;
tmp->waiting_time = 0;
tmp->system_time = 0;
tmp->total_service_time =0;
tmp->servers = malloc(n * sizeof(queue *));
for(int i=0;i<n;i++){
tmp->servers[i] = q_new();
}
return tmp;
}
else
return NULL;
}
// Add node to queue tail
bool q_insert(queue *q, node *n){
if(!q | !n){
return false;
}
else{
node *tmp = malloc(sizeof(node));
tmp->it = n->it;
tmp->st = n->st;
tmp->dt = n ->dt;
tmp->wt = n->wt;
tmp->which_server = n->which_server;
/* First time entering the system*/
if( n->at == -1 ){
q->t += n->it;
tmp->at = q->t;
}
else{
tmp->at = n->at ;
}
if(q->head && q->tail){
q->tail->next = tmp;
q->tail = tmp;
}
else{
q->head = tmp;
q->tail = tmp;
}
q->size += 1;
return true;
}
}
node *q_pop(queue *q){
node *tmp = q->head;
if(q->size == 1){
q->head = NULL;
q->tail = NULL;
}
else{
q->head = q->head->next;
}
q->size -= 1;
return tmp;
}
double rand_exp(double lambda){
double u;
u = rand()/(RAND_MAX + 1.0);// generate random number between 0~1
return -log(1-u)/lambda;
}
double rand_normal(double std, double mean){
double u = rand() / (double)RAND_MAX;
double v = rand() / (double)RAND_MAX;
double w = sqrt(-2 * log(u)) * cos(2 * M_PI * v);
double x ;
if( w < -3 )
x = mean - 3*std;
else if (w > 3)
x = mean + 3*std;
else
x = mean + w*std;
return x;
}
void scheduling(_system *s, node *n, queue *q_next_system, FILE *fp){
if( n ){
queue *min_q = s->servers[0];
n->which_server = 0;
for(int i=0;i<s->n;i++){
if( min_q->size > s->servers[i]->size ){
min_q = s->servers[i];
n->which_server = i;
}
}
s->time = n->at;
q_insert(min_q, n);
/* printf("%d\n", min_q->head->which_server); */
}
if(!(n->next)){
bool ifrun = true;
while(ifrun){
for(int i=0;i<s->n;i++){
while((s->servers[i]->head) && (s->servers[i]->dt <= s->time)){
double q_dt = s->servers[i]->dt;
double n_at = s->servers[i]->head->at;
double n_st = s->servers[i]->head->st;
/* Don't wait */
if( q_dt <= n_at ){
s->servers[i]->dt = n_at + n_st; // Depart time
s->system_time += n_st;
s->total_service_time += n_st;
s->servers[i]->head->dt = s->servers[i]->dt; // Update arrival time
node *tmp = q_pop(s->servers[i]);
tmp->at -= tmp->wt;
fprintf(fp, "%f %f %f %f %f %d\n", tmp->it, tmp->st, tmp->at, tmp->dt, tmp->wt, tmp->which_server);
tmp->at = tmp->dt, tmp->dt=0, tmp->wt=0;
if(q_next_system)
q_insert(q_next_system, tmp);
}
/* Need to wait */
else{
s->waiting_time += q_dt - n_at;
s->system_time += q_dt - n_at;
s->servers[i]->head->at = s->servers[i]->dt; // Shift arrival time to departure time of last person
s->servers[i]->head->wt = q_dt - n_at;
}
}
double max_dt = s->servers[0]->dt;
for(int i=0;i<s->n;i++){
if(max_dt <= s->servers[i]->dt)
max_dt = s->servers[i]->dt;
}
s->time = max_dt;
}
bool keep_run = false;
for(int i=0;i<s->n;i++){
if(s->servers[i]->head)
keep_run = true;
}
ifrun = keep_run;
}
}
/* Update system state */
else{
/* For loop for updating all queues */
for(int i=0;i<s->n;i++){
while((s->servers[i]->head) && (s->servers[i]->dt <= s->time)){
double q_dt = s->servers[i]->dt;
double n_at = s->servers[i]->head->at;
double n_st = s->servers[i]->head->st;
/* Don't wait */
if( q_dt <= n_at ){
s->servers[i]->dt = n_at + n_st; // Departure time
s->system_time += n_st;
s->total_service_time += n_st;
s->servers[i]->head->dt = s->servers[i]->dt;
node *tmp = q_pop(s->servers[i]);
tmp->at -= tmp->wt; // Real arrival time
fprintf(fp, "%f %f %f %f %f %d\n", tmp->it, tmp->st, tmp->at, tmp->dt, tmp->wt, tmp->which_server);
tmp->at = tmp->dt, tmp->dt=0, tmp->wt=0;
if(q_next_system){
q_insert(q_next_system, tmp);
}
}
/*
Need to wait.
Calculate waiting time and modify arrival time.
*/
else{
s->waiting_time += q_dt - n_at;
s->system_time += q_dt - n_at;
s->servers[i]->head->at = s->servers[i]->dt; // Shift arrival time to departure time of last person
s->servers[i]->head->wt = q_dt - n_at;
}
}
}
}
}
void show_system(_system *s){
double max_dt = s->servers[0]->dt;
for(int i=0;i<s->n;i++){
if( max_dt <= s->servers[i]->dt )
max_dt = s->servers[i]->dt;
}
printf("Service:%f,wt:%f, dt:%f, System:%f\n",s->total_service_time/count, s->waiting_time/count, max_dt, s->system_time/count);
}
double get_dt(_system *s){
double max_dt = s->servers[0]->dt;
for(int i=0;i<s->n;i++){
if( max_dt <= s->servers[i]->dt )
max_dt = s->servers[i]->dt;
}
//printf("Departure time:%f\n", max_dt);
return max_dt;
}