//===================================================== file = as3_5sol.c ===== //= A simulation of dual queues with a single source = //= > Adds convergence to confidence interval to as3_4sol.c = //============================================================================= //= Notes: = //= This program is the solution for assignment #5 for Computer = //= Simulation, Spring 1999. See the handout for the specific = //= requirements for this program. = //=---------------------------------------------------------------------------= //= Build: bcc32 as3_5sol.c smpl.c bmeans.c rand.c = //=---------------------------------------------------------------------------= //= History: KJC (03/07/99) - Genesis = //============================================================================= //----- Include files --------------------------------------------------------- #include // Needed for printf() #include "smpl.h" // Needed for SMPL //----- Defines --------------------------------------------------------------- #define BALANCE_MODE 2 // 0 = random balance, 1 = round robin, // 2 = shortest queue #define ARRIVAL_TIME 1.0 / 18 // Mean time between customer arrivals (sec) #define SERV_TIME_1 1.0 / 10 // Mean service time (sec) for server #1 #define SERV_TIME_2 1.0 / 10 // Mean service time (sec) for server #2 //===== Main program ========================================================== void main(void) { real Ta = ARRIVAL_TIME; // Mean interarrival time (sec) real Ts_1 = SERV_TIME_1; // Mean service time (sec) for server #1 real Ts_2 = SERV_TIME_2; // Mean service time (sec) for server #2 int balance_mode; // Balance mode (0, 1, or 2) int index = 0; // Server index for round robin balancing int customer = 1; // Customer id (always '1' for this simulation) int event; // Event int server_1; // Handle for server facility #1 int server_2; // Handle for server facility #2 long count_1; // Number of customers served by facility #1 long count_2; // Number of customers served by facility #2 int queue_len_1; // Queue length #1 (including in-service customer) int queue_len_2; // Queue length #2 (including in-service customer) real len_1; // Mean system length for facility #1 real wait_1; // Mean system wait for facility #1 real len_2; // Mean system length for facility #2 real wait_2; // Mean system wait for facility #2 real mean_wait; // Mean wait for an arbitrary customer // Batch means stuff int num_deletions; // Number of deletions for batch means int batch_size; // Batch size (in number of observations) int done; // Simulation done flag (0 = false, 1 = true) real mean; // Mean values returned by civals(); real half_width; // Half width returned by civals(); int num_batches; // Number of batches returned by civals(); // Initialize balancing mode balance_mode = BALANCE_MODE; // Initialize SMPL subsystem smpl(0, "Dual Queue Simulation"); // Initialize server facilities (two single servers) server_1 = facility("server #1", 1); server_2 = facility("server #2", 1); // Initialize batch means with deletion count and batch size num_deletions = 0; batch_size = 10000; init_bm(num_deletions, batch_size); // Schedule arrival event at time 0 to kick-off simulation schedule(1, 0.0, customer); // Initialize customer counters count_1 = count_2 = 0; // Loop while simulation time is less than te done = 0; while (done == 0) { // "Cause" the next event on the event list cause(&event,&customer); // Process the event switch(event) { case 1: // ***** Arrival if (balance_mode == 0) { if (ranf() < 0.50) schedule(2, 0.0, customer); else schedule(4, 0.0, customer); } if (balance_mode == 1) { index++; if (index == 2) index = 0; if (index == 0) schedule(2, 0.0, customer); else schedule(4, 0.0, customer); } if (balance_mode == 2) { queue_len_1 = inq(server_1) + status(server_1); queue_len_2 = inq(server_2) + status(server_2); if (queue_len_1 < queue_len_2) schedule(2, 0.0, customer); if (queue_len_1 > queue_len_2) schedule(4, 0.0, customer); if (queue_len_1 == queue_len_2) { if (ranf() < 0.50) schedule(2, 0.0, customer); else schedule(4, 0.0, customer); } } schedule(1, expntl(Ta), customer); // Batch mean stuff for a observation done = obs((real) (inq(server_1) + status(server_1))); break; case 2: // ***** Request server #1 if (request(server_1, customer, 0) == 0) schedule(3, Ts_1, customer); break; case 3: // ***** Release server #1 release(server_1, customer); count_1++; break; case 4: // ***** Request server #2 if (request(server_2, customer, 0) == 0) schedule(5, Ts_2, customer); break; case 5: // ***** Release server #2 release(server_2, customer); count_2++; break; } } // Compute (using Little's Law) mean system wait len_1 = ((double) Lq(server_1) + U(server_1)); len_2 = ((double) Lq(server_2) + U(server_2)); wait_1 = len_1 * (time() / count_1); wait_2 = len_2 * (time() / count_2); mean_wait = ((double) count_1 / (count_1 + count_2)) * wait_1 + ((double) count_2 / (count_1 + count_2)) * wait_2; // Output simulation results printf("============================================================== \n"); printf("= ***** Results from dual queue simulation ***** = \n"); printf("============================================================== \n"); if (balance_mode == 0) printf("= >>> Using random server selection \n"); if (balance_mode == 1) printf("= >>> Using round robin server selection \n"); if (balance_mode == 2) printf("= >>> Using shortest queue server selection \n"); printf("= \n"); printf("= Total simulation time = %4.3f sec \n", time()); printf("= Mean interarrival time = %4.3f sec \n", Ta); printf("= Facility #1 mean service time = %4.3f sec \n", Ts_1); printf("= Facility #2 mean service time = %4.3f sec \n", Ts_2); printf("= \n"); printf("= Facility #1 utilization = %4.3f %% \n", 100 * U(server_1)); printf("= Facility #1 customer count = %ld \n", count_1); printf("= Facility #1 mean system len = %4.3f sec \n", len_1); printf("= Facility #1 mean system wait = %4.3f sec \n", wait_1); printf("= \n"); printf("= Facility #2 utilization = %4.3f %% \n", 100 * U(server_2)); printf("= Facility #2 customer count = %ld \n", count_2); printf("= Facility #2 mean system len = %4.3f sec \n", len_2); printf("= Facility #2 mean system wait = %4.3f sec \n", wait_2); printf("= \n"); printf("= Mean wait for a customer = %4.3f sec \n", mean_wait); printf("= \n"); printf("============================================================== \n"); }