Repository navigation
Expand file tree
/
Copy pathex446.c
More file actions
executable file
·150 lines (134 loc) · 5.42 KB
/
Copy pathex446.c
File metadata and controls
executable file
·150 lines (134 loc) · 5.42 KB
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
/* -------------------------------------------------------------------------
* This program is a next-event simulation of a single-server FIFO service
* node with immediate feedback using Exponentially distributed interarrival
* times and Uniformly distributed service times (i.e., a M/U/1 queue).
* There are 2 events:
* arrival of a job;
* completion of service.
* The simulation clock is advanced by comparing the next scheduled time
* for each event and then choosing that event which is most imminent. The
* service node is assumed to be initially idle, no arrivals are permitted
* after the terminal time 'stop', and the node is then purged by processing
* any remaining jobs in the node (including fed back jobs).
*
* Name : ssq3.c (Single Server Queue, version 3)
* Author : Steve Park & Dave Geyer
* Language : ANSI C
* Latest Revision : 10-27-96
* -------------------------------------------------------------------------
*/
#include <stdio.h>
#include <math.h>
#include "rngs.h" /* the multi-stream generator */
#define STOP 20000.0 /* terminal (close the door) time */
#define JOBS 10000 /* number of jobs to be processed */
#define NEVER (100.0 * STOP) /* must be much larger than STOP */
#define BETA 0.2 /* feedback probability */
/* ============================== */
static double Min(double a, double c)
/* ============================== */
{
if (a < c)
return (a);
else
return (c);
}
/* ============================ */
static double Exponential(double m)
/* ============================ */
{
return (-m * log(1.0 - Random()));
}
/* ================================== */
[[maybe_unused]] static double Uniform(double a, double b)
/* ================================== */
{
return (a + (b - a) * Random());
}
/* ======================== */
static double GetInterarrival() /* returns an Exponential random */
/* ======================== */ /* variate with mean 1.0 */
{
SelectStream(0);
return (Exponential(1.0));
}
/* =================== */
static double GetService() /* returns an Exponential random */
/* =================== */ /* variate with mean 0.8 */
{
SelectStream(1);
return (Exponential(0.8));
}
/* ================= */
static int GetFeedback()
/* ================= */
{
SelectStream(2);
if (Random() < BETA)
return (1);
else
return (0);
}
/* ============== */
int main(void)
/* ============== */
{
struct {
double arrival; /* next arrival time */
double completion; /* next completion time */
double current; /* current time */
double next; /* next (most imminent) event time */
double last; /* last arrival time */
} t = {}; /* C23 empty initializer: all times zero */
struct {
double node; /* time integrated number in the node */
double queue; /* time integrated number in the queue */
double service; /* time integrated number in service */
} area = {0.0, 0.0, 0.0};
long number = 0; /* number in the node */
long index = 0; /* used to count departed jobs */
PlantSeeds(123456789);
t.current = 0.0; /* set the clock */
t.arrival = t.current + GetInterarrival(); /* schedule first arrival */
t.completion = NEVER; /* first event != completion */
while ((t.arrival < STOP) || (number != 0)) {
t.next = Min(t.arrival, t.completion); /* next event time */
if (number > 0) { /* update integrals */
area.node += (t.next - t.current) * number;
area.queue += (t.next - t.current) * (number - 1);
area.service += (t.next - t.current);
}
t.current = t.next; /* advance the clock */
if (t.current == t.arrival) { /* process an arrival */
number++;
t.arrival = t.current + GetInterarrival();
if (t.arrival > STOP) {
t.last = t.current;
t.arrival = NEVER;
}
if (number == 1)
t.completion = t.current + GetService();
}
else { /* process a completion */
if (GetFeedback())
t.completion = t.current + GetService();
else {
index++;
number--;
if (number > 0)
t.completion = t.current + GetService();
else
t.completion = NEVER;
}
}
}
printf("\nfor %ld jobs, with feedback probability %4.2f\n", index, BETA);
printf(" average interarrival time = %6.2f\n", t.last / index);
printf(" average wait ............ = %6.2f\n", area.node / index);
printf(" average delay ........... = %6.2f\n", area.queue / index);
printf(" average service time .... = %6.2f\n", area.service / index);
printf(" average # in the node ... = %6.2f\n", area.node / t.current);
printf(" average # in the queue .. = %6.2f\n", area.queue / t.current);
printf(" utilization ............. = %6.2f\n", area.service / t.current);
return (0);
}