//====================================================== file = balance.c ===== //= CSIM model of a dual queue system with load balancing = //= - Includes delayed status feedback from servers to load balancer = //============================================================================= //= Notes: This model is the starting point for assignment #6 and #7 = //=---------------------------------------------------------------------------= //= Build: csim.gcc balance on Solaris = //=---------------------------------------------------------------------------= //= History: KJC (03/23/05) - Genesis = //============================================================================= //----- Include files --------------------------------------------------------- #include // Needed for printf() #include // Needed for atof() #include "csim.h" // Needed for CSIM stuff //----- Constants ------------------------------------------------------------- #define RANDOM 0 // Random load balancing #define RR 1 // Round Robin load balancing #define SHORTQ 2 // Shortest queue load balancing #define BALANCE SHORTQ // Define the type of load balancing //----- External variables ---------------------------------------------------- FACILITY Server1; // Declare the facility Server1 FACILITY Server2; // Declare the facility Server2 int Len1, Len2; // Queue length of each queue for SHORTQ //----- Function protoypes ---------------------------------------------------- void generate(double lambda, double mu1, double mu2); // Traffic source void status_post(double post_time); // Periodic status poster void system1(double mu); // Single server queue #1 void system2(double mu); // Single server queue #2 //============================================================================= //= First process for a CSIM simulation = //============================================================================= void sim(void) { double sim_time; // Simlation time double lambda, mu1, mu2; // Arrival and service rates double post_time; // Post time period // Create a CSIM process create("sim"); // Increase the number of processes max_processes(100000); // Intialize the facility servers Server1 = facility("Queue #1"); Server2 = facility("Queue #2"); // Initialize sim_time, post_time, lambda, mu1, and mu2 sim_time = 1.0e7; post_time = 30.0; lambda = 1.8; mu1 = 1.0; mu2 = 1.0; // Kick-off traffic source and let simulation run for sim_time status_post(post_time); generate(lambda, mu1, mu2); hold(sim_time); // Output results report(); } //============================================================================= //= Process for a traffic source and load balancing = //============================================================================= void generate(double lambda, double mu1, double mu2) { int index; // RR poller index create("generate"); // Do forever, system() queues a customer (with random load balancing) index = 0; while(1) { // Hold for exponential interarrival time hold(expntl(1.0 / lambda)); // Send customer to the right queue if (BALANCE == RANDOM) // Random load balancing { if (prob() < 0.50) system1(mu1); else system2(mu2); } if (BALANCE == RR) // Round robin load balancing { index = (index + 1) % 2; if (index == 0) system1(mu1); else system2(mu2); } if (BALANCE == SHORTQ) // Shortest queue load balancing { if (Len1 < Len2) system1(mu1); else if (Len1 > Len2) system2(mu2); else if (Len1 == Len2) { if (prob() < 0.50) system1(mu1); else system2(mu2); } } } } //============================================================================= //= Process for periodic posting of queue length status for SHORTQ = //============================================================================= void status_post(double post_time) { create("status_post"); // Post status forever every post_time seconds while(1) { Len1 = qlength(Server1) + num_busy(Server1); Len2 = qlength(Server2) + num_busy(Server2); hold(post_time); } } //============================================================================= //= Process for a single server queue (queue #1) = //============================================================================= void system1(double mu1) { create("system1"); // Reserve, hold, and release the server reserve(Server1); hold(expntl(1.0 / mu1)); release(Server1); } //============================================================================= //= Process for a single server queue (queue #2) = //============================================================================= void system2(double mu2) { create("system2"); // Reserve, hold, and release the server reserve(Server2); hold(expntl(1.0 / mu2)); release(Server2); }