//==================================================== file = as3_3sol.c ===== //= Program to simulate an M/D/1/K queue = //============================================================================ //= Notes: This is assignment #3 for CIS 4930 (Simulation) for Spring 1999 = //============================================================================ //= Notes: This program is adapted from "Figure 1.6" in Simulating = //= Computer Systems, Techniques and Tools by M. H. MacDougall (1987) = //=--------------------------------------------------------------------------= //= Build: gcc as3_3sol.c -lm, bcc32 as3_3sol.c, cl as3_3sol.c = //=--------------------------------------------------------------------------= //= Execute: as3_3sol = //=--------------------------------------------------------------------------= //= History: KJC (01/31/99) - Genesis (from lec8a.c) = //============================================================================ //----- Include files -------------------------------------------------------- #include // Needed for printf() #include // Needed for exit(), rand(), and RAND_MAX #include // Needed for log() //----- Constants ------------------------------------------------------------ #define SIM_TIME 1.0e8 // Simulation time (in "time units") //----- Function prototypes -------------------------------------------------- double expntl(double x); // Generate exponential RV with mean x //===== Main program ========================================================= void main(int argc, char *argv[]) { double end_time = SIM_TIME; // Total time to simulate double Ta; // Mean time between arrivals double Ts = 1.0; // Mean service time double load; // Offered load long int size; // Buffer size 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) long int n = 0; // Number of packets in the system long int loss = 0; // Counter for lost packets double loss_ratio; // Ratio of lost packets to total completions // Instrumentation variables double b = 0.0; // Total busy time double c = 0.0; // Number of service completions double s = 0.0; // Area of number of packets in system double tn = time; // Variable for "last event time" double tb; // Variable for "last start of busy time" double u; // Utilization double l; // Mean number in the system // Check for sufficient command line parameters if (argc < 3) { printf("Usage is 'as3_3sol load size' where load is from 0.0 to 1.0 \n"); printf("and size is buffer size in integer number of packets \n"); exit(1); } // Assign load and size load = atof(argv[1]); size = atoi(argv[2]); // Initialize Ta based on load Ta = Ts / load; // Main simulation loop while (time < end_time) { if (t1 < t2) // *** Event #1 (arrival) { time = t1; s = s + n * (time - tn); if (n <= size) // Implement finite buffer here noting that n++; // ... one pkt can be in server (hence the else // ... use of "<=" instead of "<" loss++; tn = time; t1 = time + expntl(Ta); if (n == 1) { tb = time; t2 = time + Ts; } } else // *** Event #2 (departure) { time = t2; s = s + n * (time - tn); n--; tn = time; c++; if (n > 0) t2 = time + Ts; else { t2 = end_time; b = b + time - tb; } } } u = b / time; // Compute server utilization l = s / time; // Compute mean number in system loss_ratio = (loss / (loss + c)); // Compute loss_ratio // Output results printf("================================================================\n"); printf("= *** Results from M/D/1/K simulation *** \n"); printf("================================================================\n"); printf("= Total simulated time = %f time units \n", end_time); printf("===============================================================\n"); printf("= INPUTS: \n"); printf("= Offered load = %f %% \n", 100.0 * load); printf("= Buffer size = %d pkts \n", size); printf("============================================================== \n"); printf("= OUTPUTS: \n"); printf("= Mean number in system = %f pkts \n", l); printf("= Number of completions = %f pkts \n", c); printf("= Percent of packet loss = %f %% \n", 100.0 * loss_ratio); printf("= Server utilization = %f %% \n", 100.0 * u); printf("============================================================== \n"); } //============================================================================ //= Function to generate exponentially distributed RVs = //= - Input: x (mean value of distribution) = //= - Output: Returns with exponential RV = //============================================================================ double expntl(double x) { double z; // Uniform random number from 0 to 1 // Pull a uniform RV (0 < z < 1) do { z = ((double) rand() / RAND_MAX); } while ((z == 0) || (z == 1)); return(-x * log(z)); }