//===================================================== file = as3_6sol.c ===== //= CSIM model of dual queues with single source = //============================================================================= //= Notes: This is a CSIM version of as3_4sol.c = //=---------------------------------------------------------------------------= //= Build: csim.gcc as3_4sol.c (on Solaris) = //=---------------------------------------------------------------------------= //= History: KJC (03/29/99) - Genesis (from lec19.c and as3_4sol.c) = //============================================================================= //----- Include files --------------------------------------------------------- #include // Needed for printf() #include "csim.h" // Needed for CSIM stuff //----- Constants ------------------------------------------------------------- #define BALANCE_MODE 2 // 0 = random balance, 1 = round robin, // 2 = shortest queue #define SIM_TIME 1.0e5 // Simulation time //----- External variables ---------------------------------------------------- FACILITY Server1, Server2; // Declare the facilities for two Web servers double Lambda; // Arrival rate double Mu; // Common service rate for each server //----- Function protoypes ---------------------------------------------------- void generate(void); // Traffic source void system1(double service_time); // Single server queue for Web server #1 void system2(double service_time); // Single server queue for Web server #2 //============================================================================= //= First process for a CSIM simulation = //============================================================================= void sim(void) { int count1, count2; // Completions for Server1 and Server2 double mean_wait; // Mean wait time // Create a CSIM process create("sim"); // Intialize the facility server Server1 = facility("Web server #1"); Server2 = facility("Web server #2"); // Initialize Lambda and Mu Lambda = 18.0; Mu = 10.0; // Kick-off traffic source and let simulation run for SIM_TIME generate(); hold((double) SIM_TIME); // Compute the mean system wait count1 = completions(Server1); count2 = completions(Server2); mean_wait = ((double) count1 / (count1 + count2)) * resp(Server1) + ((double) count2 / (count1 + count2)) * resp(Server2); // 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", clock); printf("= Mean interarrival time = %4.3f sec \n", 1.0 / Lambda); printf("= Facility #1 mean serv time = %4.3f sec \n", 1.0 / Mu); printf("= Facility #2 mean serv time = %4.3f sec \n", 1.0 / Mu); printf("= \n"); printf("= Facility #1 utilization = %4.3f %% \n", 100 * util(Server1)); printf("= Facility #1 customer count = %ld \n", count1); printf("= Facility #1 mean sys wait = %4.3f sec \n", resp(Server1)); printf("= \n"); printf("= Facility #2 utilization = %4.3f %% \n", 100 * util(Server2)); printf("= Facility #2 customer count = %ld \n", count2); printf("= Facility #2 mean sys wait = %4.3f sec \n", resp(Server2)); printf("= \n"); printf("= Mean wait for a customer = %4.3f sec \n", mean_wait); printf("= \n"); printf("============================================================== \n"); } //============================================================================= //= Process for a traffic source = //============================================================================= void generate(void) { double arrival_time; // Interarrival time double service_time; // Service time int queue_len_1; // Queue length #1 (including in-service) int queue_len_2; // Queue length #2 (including in-service) static int index = 0; // Server index for round robin balancing create("generate"); // Create a CSIM process // Generate traffic forever while(1) { // Generate and hold for an interarrival time arrival_time = expntl(1.0 / Lambda); hold(arrival_time); // Generate a service time service_time = 1.0 / Mu; // Do load balancing if (BALANCE_MODE == 0) { if (prob() < 0.50) system1(service_time); else system2(service_time); } if (BALANCE_MODE == 1) { index++; if (index == 2) index = 0; if (index == 0) system1(service_time); else system2(service_time); } if (BALANCE_MODE == 2) { queue_len_1 = qlength(Server1) + num_busy(Server1); queue_len_2 = qlength(Server2) + num_busy(Server2); if (queue_len_1 < queue_len_2) system1(service_time); if (queue_len_1 > queue_len_2) system2(service_time); if (queue_len_1 == queue_len_2) { if (prob() < 0.50) system1(service_time); else system2(service_time); } } } } //============================================================================= //= Process for a single server queue for server #1 = //============================================================================= void system1(double service_time) { // Create a CSIM process create("system1"); // Reserve, hold, and release the server reserve(Server1); hold(service_time); release(Server1); } //============================================================================= //= Process for a single server queue for server #2 = //============================================================================= void system2(double service_time) { // Create a CSIM process create("system2"); // Reserve, hold, and release the server reserve(Server2); hold(service_time); release(Server2); }