-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscrete_event_sim.c
398 lines (337 loc) · 9.19 KB
/
discrete_event_sim.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* by: Leomar Duran <https://github.com/lduran2>
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
/* these deal with actions */
typedef enum Action {
ARRIVES,
ENTERS_CPU, ENTERS_DISK1, ENTERS_DISK2, ENTERS_NETWORK,
FINISH_CPU, FINISH_DISK1, FINISH_DISK2, FINISH_NETWORK,
TERMINATES
} Action;
char *action_names[] = {
"arrives",
"enters the CPU", "enters Disk #1", "enters Disk #2",
"enters the Network",
"finishes in the CPU", "finishes in Disk #1",
"finishes in Disk #2", "finishes in the Network",
"terminates"
};
/* the process alias */
typedef int Process;
/* the event structure */
typedef struct Event {
long int time;
Process process;
Action action;
} Event;
typedef struct PQNode {
Event *data;
struct PQNode *next;
struct PQNode *prev;
} PQNode;
typedef struct PrtyQu {
PQNode* pre_head;
} PrtyQu;
Event *evnew(int time, Process process, Action action) {
Event* event = (Event*)malloc(sizeof(Event));
event->time = time;
event->process = process;
event->action = action;
return event;
}
PQNode *pqnnew(Event* data)
{
PQNode *node = (PQNode*)malloc(sizeof(PQNode));
node->data = data;
node->next = NULL;
node->prev = NULL;
return node;
}
PrtyQu *prqnew() {
PQNode* pqnnew(Event*);
PrtyQu *queue = (PrtyQu*)malloc(sizeof(PrtyQu));
PQNode *pre_head = pqnnew(NULL);
pre_head->next = pre_head;
pre_head->prev = pre_head;
queue->pre_head = pre_head;
return queue;
}
void prqins(PrtyQu* queue, PQNode* new_element)
{
PQNode *position = queue->pre_head;
for (;
(position->next != queue->pre_head)
/* be sure to compare to *next* position */
&& (position->next->data->time <= new_element->data->time);
position = position->next)
{
// nop
}
new_element->prev = position;
new_element->next = position->next;
new_element->next->prev = new_element;
position->next = new_element;
}
int pq2str(char **pstr, PrtyQu queue)
{
int ev2str(char**, Event);
int size = 0;
int sum = 0;
char *line = NULL;
char *new_str;
PQNode* position;
for (position = queue.pre_head->next;
/* the pre-head is always empty, so skip it */
(position != queue.pre_head);
(position = position->next)
) {
ev2str(&line, *(position->data));
size = snprintf(NULL, 0, "%s%s\n", *pstr, line);
new_str = (char*)malloc((size + 1)*sizeof(char));
sprintf(new_str, "%s%s\n", *pstr, line);
*pstr = new_str;
sum += size;
}
return sum;
}
/* these handle the FIFO queues */
typedef struct FFQNod {
Event *data;
struct FFQNod *next;
} FFQNod;
typedef struct FFOQue {
FFQNod *pre_head;
FFQNod *tail;
} FFOQue;
FFQNod *ffnnew(Event* data)
{
FFQNod *node = (FFQNod*)malloc(sizeof(FFQNod));
node->data = data;
node->next = NULL;
return node;
}
FFOQue* ffqnew() {
FFQNod* ffnnew(Event*);
FFOQue *queue = (FFOQue*)malloc(sizeof(FFOQue));
FFQNod *pre_head = ffnnew(NULL);
queue->pre_head = pre_head;
queue->tail = pre_head;
return queue;
}
void ffqenq(FFOQue *queue, FFQNod *new_element) {
queue->tail->next = new_element;
queue->tail = new_element;
}
FFQNod *ffqdeq(FFOQue *queue) {
FFQNod *dequeued = queue->pre_head->next;
queue->pre_head->next = queue->pre_head->next->next;
return dequeued;
}
/* these deal with constant configuration */
char *const_keys[] = {
"SEED", "INIT_TIME", "FIN_TIME", "ARRIVE_MIN", "ARRIVE_MAX", /* 0:4 */
"CPU_MIN", "CPU_MAX", "DISK1_MIN", "DISK1_MAX", /* 5:8 */
"DISK2_MIN", "DISK2_MAX", "NETWORK_MIN", "NETWORK_MAX", /* 9:12 */
"QUIT_PROB", "NETWORK_PROB" /* 13:14 */
};
typedef enum IntConstantIndex {
I_SEED, I_INIT_TIME, I_FIN_TIME, I_ARRIVE_MIN, I_ARRIVE_MAX,
I_CPU_MIN, I_CPU_MAX, I_DISK1_MIN, I_DISK1_MAX,
I_DISK2_MIN, I_DISK2_MAX, I_NETWORK_MIN, I_NETWORK_MAX
} IntConstantIndex;
typedef enum DblConstIndex {
I_QUIT_PROB, I_NETWORK_PROB
} DblConstIndex;
// NETWORK_MAX is the last int constant,
// I_NETWORK_PROB is the last double constant.
// +2 because there are 2 arrays.
const size_t N_CONST_KEYS = (I_NETWORK_MAX + I_NETWORK_PROB + 2);
long int
irndom(long int min, long int max) {
return (long int)((((double)random())/RAND_MAX)*(max - min) + max);
}
int
main(int argc, char **argv)
{
int pq2str(char**, PrtyQu);
int ev2str(char**, Event);
bool rdcfgk(FILE*, int[], double[]);
const Process SIMULATION = -1;
long int SEED;
/* the stating time of the simulator */
long int INIT_TIME = 0;
/* the ending time of the simulator */
long int FIN_TIME = 0;
/* the minimum interarrival time */
long int ARRIVE_MIN = 0;
/* the maximum interarrival time */
long int ARRIVE_MAX = 0;
/* the minimum time the CPU will service a process */
long int CPU_MIN = 0;
/* the maximum time the CPU will service a process */
long int CPU_MAX = 0;
/* the minimum time disk 1 will service a process */
long int DISK1_MIN = 0;
/* the maximum time disk 1 will service a process */
long int DISK1_MAX = 0;
/* the minimum time disk 2 will service a process */
long int DISK2_MIN = 0;
/* the maximum time disk 2 will service a process */
long int DISK2_MAX = 0;
/* the minimum time the network will service a process */
long int NETWORK_MIN = 0;
/* the maximum time the network will service a process */
long int NETWORK_MAX = 0;
/* the probability of the current job quitting the system */
double QUIT_PROB = 0;
/* the probability that the current job will access the network */
double NETWORK_PROB = 0;
int int_const_values[] = {
0, 0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
double dbl_const_values[] = {
0.0, 0.0
};
/* logical time of simulation */
long int t;
FILE *config_file = fopen("sim.config", "r");
rdcfgk(config_file, int_const_values, dbl_const_values);
fclose(config_file);
SEED = int_const_values[I_SEED];
INIT_TIME = int_const_values[I_INIT_TIME];
FIN_TIME = int_const_values[I_FIN_TIME];
ARRIVE_MIN = int_const_values[I_ARRIVE_MIN];
ARRIVE_MAX = int_const_values[I_ARRIVE_MAX];
CPU_MIN = int_const_values[I_CPU_MIN];
CPU_MAX = int_const_values[I_CPU_MAX];
DISK1_MIN = int_const_values[I_DISK1_MIN];
DISK1_MAX = int_const_values[I_DISK1_MAX];
DISK2_MIN = int_const_values[I_DISK2_MIN];
DISK2_MAX = int_const_values[I_DISK2_MAX];
NETWORK_MIN = int_const_values[I_NETWORK_MIN];
NETWORK_MAX = int_const_values[I_NETWORK_MAX];
QUIT_PROB = dbl_const_values[I_QUIT_PROB];
NETWORK_PROB = dbl_const_values[I_NETWORK_PROB];
FFOQue* queue = ffqnew();
ffqenq(queue, ffnnew(evnew(50, 1, ARRIVES)));
ffqenq(queue, ffnnew(evnew(200, 2, ARRIVES)));
ffqenq(queue, ffnnew(evnew(25, 3, ARRIVES)));
ffqenq(queue, ffnnew(evnew(250, 4, ARRIVES)));
ffqenq(queue, ffnnew(evnew(50, 5, ARRIVES)));
ffqenq(queue, ffnnew(evnew(10, 6, ARRIVES)));
ffqenq(queue, ffnnew(evnew(275, -1, ARRIVES)));
char *data = NULL;
ev2str(&data, *evnew(50, 1, ARRIVES));
printf("%s\n", data);
for (int k = 8; k > 0; --k) {
ev2str(&data, *ffqdeq(queue)->data);
printf("%s\n", data);
}
return;
long int t_next_arrival = INIT_TIME;
int i_job = SIMULATION;
srandom(SEED);
PrtyQu *priorities = prqnew();
for (t = INIT_TIME; t < FIN_TIME; ++t) {
if (t >= t_next_arrival) {
++i_job;
prqins(priorities, pqnnew(evnew(t, i_job, ARRIVES)));
// printf("arrival at t = %20ld!\n", t);
t_next_arrival
= t + irndom(ARRIVE_MIN, ARRIVE_MAX);
}
}
char *queue_string = "";
pq2str(&queue_string, *priorities);
printf("%s", queue_string);
return 0;
}
bool
rdcfgk(FILE *config_file, int int_const_values[], double dbl_const_values[])
{
int strfci(char, const size_t, char*);
int arrstr(const size_t, char*, const size_t, char**);
char *line = NULL;
size_t line_len = 0;
int i_space;
int i_const_key;
char *str_const_value;
char *string_remainder;
if (NULL == config_file) {
exit(1);
}
while (-1 != getline(&line, &line_len, config_file)) {
i_space = strfci(' ', line_len, line);
if (i_space < 0) {
continue;
}
i_const_key = arrstr(i_space, line, N_CONST_KEYS, const_keys);
str_const_value = (line + i_space + 1);
if (i_const_key < 0) {
continue;
}
else if (i_const_key <= I_NETWORK_MAX) {
int_const_values[i_const_key] =
strtol(str_const_value, &string_remainder, 10);
}
else if (i_const_key < N_CONST_KEYS) {
dbl_const_values[i_const_key - (I_NETWORK_MAX + 1)] =
strtod(str_const_value, &string_remainder);
}
else {
/* nop */
}
}
}
int
strfci(char needle,
const size_t HAYSTACK_LEN, char *haystack)
{
int k;
for (k = 0; (HAYSTACK_LEN != k); ++k) {
if (haystack[k] == needle) {
return k;
}
}
return -1;
}
int
arrstr(const size_t NEEDLE_LEN, char *needle,
const size_t HAYSTACK_LEN, char **haystack)
{
int k;
for (k = HAYSTACK_LEN; (0 != (k--)); ) {
if (0 == strncmp(haystack[k], needle, NEEDLE_LEN)) {
return k;
}
}
return -1;
}
int
ev2str(char **pstr, Event event)
{
char* JOB_FORMAT = "Job #%3d";
char* EVENT_FORMAT = "t = %10ld: %3s %s.";
char* JOB_NAME = NULL;
Process process_id = event.process;
size_t size;
switch (process_id) {
case -1: JOB_NAME = "simulation"; break;
default:
size = snprintf(NULL, 0, JOB_FORMAT, process_id);
JOB_NAME = (char*)malloc((size + 1)*sizeof(char));
sprintf(JOB_NAME, JOB_FORMAT, process_id);
break;
}
size = snprintf(NULL, 0, EVENT_FORMAT,
event.time, JOB_NAME, action_names[event.action]);
*pstr = (char*)malloc((size + 1)*sizeof(char));
sprintf(*pstr, EVENT_FORMAT, event.time, JOB_NAME, action_names[event.action]);
return size;
}