//====================================================== file = md1_csim.c ===== //= A CSIM simulation of an M/D/1 queueing system = //============================================================================== //= Notes: 1) Values for lambda and mu are set in the main program = //= 2) Value for SIM_TIME is set in #define = //= 3) Computes M/M/1 theoretical results for comparison = //=----------------------------------------------------------------------------= //= Example execution: = //= = //= *** BEGIN SIMULATION *** = //= ============================================================= = //= == *** CSIM M/D/1 queueing system simulation *** == = //= ============================================================= = //= = Lambda = 1.000000 cust/sec = //= = Mu = 3.000000 cust/sec = //= ============================================================= = //= = Total CPU time = 2.230000 sec = //= = Total sim time = 1000000.000000 sec = //= = Total completions = 1001211 cust = //= =------------------------------------------------------------ = //= = >>> Simulation results - = //= =------------------------------------------------------------ = //= = Utilization = 33.373700 % = //= = Mean num in system = 0.417360 cust = //= = Mean response time = 0.416855 sec = //= = Mean service time = 0.333333 sec = //= = Mean throughput = 1.001211 cust/sec = //= =------------------------------------------------------------ = //= = >>> Theoretical results from M/D/1 formulas - = //= =------------------------------------------------------------ = //= = Utilization = 33.333333 % = //= = Mean num in system = 0.416667 cust = //= = Mean response time = 0.416667 sec = //= = Mean service time = 0.333333 sec = //= = Mean throughput = 1.000000 cust/sec = //= ============================================================= = //= *** END SIMULATION *** = //=----------------------------------------------------------------------------= //= Build: standard CSIM build = //=----------------------------------------------------------------------------= //= Execute: md1_csim = //=----------------------------------------------------------------------------= //= Author: Ken Christensen = //= University of South Florida = //= WWW: http://www.csee.usf.edu/~christen = //= Email: christen@csee.usf.edu = //=----------------------------------------------------------------------------= //= History: KJC (10/24/14) - Genesis (from mm1_csim.c) = //============================================================================== //----- Includes --------------------------------------------------------------- #include // Needed for printf() #include "csim.h" // Needed for CSIM stuff //----- Defines ---------------------------------------------------------------- #define SIM_TIME 1.0e6 // Total simulation time in seconds //----- Globals ---------------------------------------------------------------- FACILITY Server; // Declaration of CSIM Server facility //----- Prototypes ------------------------------------------------------------- void generate(double lambda, double mu); // Customer generator void queue1(double service_time); // Single server queue //============================================================================== //== Main program == //============================================================================== void sim(void) { double lambda; // Mean arrival rate (cust/sec) double mu; // Mean service rate (cust/sec) double rho; // Theoretical utilization // Create the simulation create("sim"); // CSIM initializations Server = facility("Server"); // Parameter initializations lambda = 1.0; mu = 3.0; // Output begin-of-simulation banner printf("*** BEGIN SIMULATION *** \n"); // Initiate generate function and hold for SIM_TIME generate(lambda, mu); hold(SIM_TIME); // Compute theoretical utilization rho = lambda / mu; // Output results printf("============================================================= \n"); printf("== *** CSIM M/D/1 queueing system simulation *** == \n"); printf("============================================================= \n"); printf("= Lambda = %f cust/sec \n", lambda); printf("= Mu = %f cust/sec \n", mu); printf("============================================================= \n"); printf("= Total CPU time = %f sec \n", cputime()); printf("= Total sim time = %f sec \n", clock); printf("= Total completions = %d cust \n", completions(Server)); printf("=------------------------------------------------------------ \n"); printf("= >>> Simulation results - \n"); printf("=------------------------------------------------------------ \n"); printf("= Utilization = %f %% \n", 100.0 * util(Server)); printf("= Mean num in system = %f cust \n", qlen(Server)); printf("= Mean response time = %f sec \n", resp(Server)); printf("= Mean service time = %f sec \n", serv(Server)); printf("= Mean throughput = %f cust/sec \n", tput(Server)); printf("=------------------------------------------------------------ \n"); printf("= >>> Theoretical results from M/D/1 formulas - \n"); printf("=------------------------------------------------------------ \n"); printf("= Utilization = %f %% \n", 100.0 * rho); printf("= Mean num in system = %f cust \n", (lambda / (2.0 * mu)) + (lambda / (2.0 * (mu - lambda)))); printf("= Mean response time = %f sec \n", (mu - (lambda / 2.0)) / (mu * (mu - lambda))); printf("= Mean service time = %f sec \n", (1.0 / mu)); printf("= Mean throughput = %f cust/sec \n", lambda); printf("============================================================= \n"); // Output end-of-simulation banner printf("*** END SIMULATION *** \n"); } //============================================================================== //== Process to generate Poisson customers == //============================================================================== void generate(double lambda, double mu) { double interarrival_time; // Interarrival time to next send double service_time; // Service time for this customer create("generate"); // Loop forever to create customers while(1) { // Pull an exponentially distributed interarrival time and hold for it interarrival_time = exponential(1.0 / lambda); hold(interarrival_time); // Pull a deterministic service time and send the customer to the queue service_time = 1.0 / mu; queue1(service_time); } } //============================================================================== //== Process for single server queue == //============================================================================== void queue1(double service_time) { create("queue1"); // Reserve, hold, and release server reserve(Server); hold(service_time); release(Server); }