//==================================================== file = mm1_simple.c ===== //= A simple "straight C" M/M/1 queue simulation = //= - No statistics gathering or reporting = //============================================================================== //= Notes: = //= 1) This program is adapted from Figure 1.4 in Simulating Computer = //= Systems, Techniqyes and Tools by M. H. MacDougall (1987). = //= 2) The values of SIM_TIME, ARR_TIME, and SERV_TIME need to be set. = //=----------------------------------------------------------------------------= //= Build: gcc mm1_simple.c -lm, bcc32 mm1_simple.c, cl mm1_simple.c = //=----------------------------------------------------------------------------= //= Execute: mm1 = //=----------------------------------------------------------------------------= //= History: KJC (01/27/99) - Genesis = //= KJC (06/01/09) - Did some clean-up to align with mm1.c = //============================================================================== //----- Include files ---------------------------------------------------------- #include // Needed for printf() #include // Needed for exit() and rand() #include // Needed for log() //----- Constants -------------------------------------------------------------- #define SIM_TIME 1.0e6 // Simulation time #define ARR_TIME 1.25 // Mean time between arrivals #define SERV_TIME 1.00 // Mean service time //----- Function prototypes ---------------------------------------------------- double rand_val(int seed); // RNG for unif(0,1) double exponential(double x); // Generate exponential RV with mean x //===== Main program =========================================================== int main(void) { double end_time = SIM_TIME; // Total time to simulate double Ta = ARR_TIME; // Mean time between arrivals double Ts = SERV_TIME; // Mean service time double time = 0.0; // Simulation time double t1 = 0.0; // Time for next event #1 (arrival) double t2 = SIM_TIME; // Time for next event #2 (departure) unsigned int n = 0; // Number of customers in the system // Seed the RNG rand_val(1); // Main simulation loop while (time < end_time) { if (t1 < t2) //** Event #1 (arrival) { time = t1; // Set time to that of current event n++; // Increment number of customers in system t1 = time + exponential(Ta); // Assign time for the next arrival event if (n == 1) // If first customer in system then t2 = time + exponential(Ts); // assign its departure time } else // *** Event #2 (departure) { time = t2; // Set time to that of current event n--; // Decrement number of customers in system if (n > 0) // If customers in system then t2 = time + exponential(Ts); // assign next departure time else // If no customers in system then t2 = end_time; // assign next departure to "infinity" } } return(0); } //========================================================================= //= Multiplicative LCG for generating uniform(0.0, 1.0) random numbers = //= - x_n = 7^5*x_(n-1)mod(2^31 - 1) = //= - With x seeded to 1 the 10000th x value should be 1043618065 = //= - From R. Jain, "The Art of Computer Systems Performance Analysis," = //= John Wiley & Sons, 1991. (Page 443, Figure 26.2) = //= - Seed the RNG if seed > 0, return a unif(0,1) if seed == 0 = //========================================================================= double rand_val(int seed) { const long a = 16807; // Multiplier const long m = 2147483647; // Modulus const long q = 127773; // m div a const long r = 2836; // m mod a static long x; // Random int value (seed is set to 1) long x_div_q; // x divided by q long x_mod_q; // x modulo q long x_new; // New x value // Seed the RNG if (seed != 0) x = seed; // RNG using integer arithmetic x_div_q = x / q; x_mod_q = x % q; x_new = (a * x_mod_q) - (r * x_div_q); if (x_new > 0) x = x_new; else x = x_new + m; // Return a random value between 0.0 and 1.0 return((double) x / m); } //============================================================================== //= Function to generate exponentially distributed RVs using inverse method = //= - Input: x (mean value of distribution) = //= - Output: Returns with exponential RV = //============================================================================== double exponential(double x) { double z; // Uniform random number from 0 to 1 // Pull a uniform RV (0 < z < 1) do { z = rand_val(0); } while ((z == 0) || (z == 1)); return(-x * log(z)); }