Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

[SOLVED] Designing an m/m/1 queue using C or C++

Status
Not open for further replies.

natasie

Member level 2
Joined
Jul 11, 2011
Messages
49
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,643
Good day,

Could anyone please give me an idea on how to write a c++ program for an m/m/1 queue or if you have any related links, could you post it? I really need help in this. I am supposed to be able to show packet arrival, transmission time etc.

Please if you have got anything, please help. Thanks!!!!
 

W_Heisenberg

Full Member level 4
Joined
Feb 27, 2011
Messages
217
Helped
6
Reputation
12
Reaction score
6
Trophy points
1,298
Location
Boston
Activity points
2,575
//=========================================================== file = mm1.c =====
//= A simple "straight C" M/M/1 queue simulation =
//==============================================================================
//= Notes: =
//= 1) This program is adapted from Figure 1.6 in Simulating Computer =
//= Systems, Techniqyes and Tools by M. H. MacDougall (1987). =
//= 2) The values of SIM_TIME, ARR_TIME, and SERV_TIME need to be set. =
//=----------------------------------------------------------------------------=
//= Build: gcc mm1.c -lm, bcc32 mm1.c, cl mm1.c =
//=----------------------------------------------------------------------------=
//= Execute: mm1 =
//=----------------------------------------------------------------------------=
//= History: KJC (03/09/99) - Genesis =
//= KJC (05/23/09) - Added RNG function (so not to use compiler RNG) =
//= KJC (05/24/09) - Added updated of busy time at end of main loop =
//==============================================================================
//----- Include files ----------------------------------------------------------
#include <stdio.h> // Needed for printf()
#include <stdlib.h> // Needed for exit() and rand()
#include <math.h> // Needed for log()

//----- Constants --------------------------------------------------------------
#define SIM_TIME 1.0e6 // Simulation time
#define ARR_TIME 1.25 // Mean time between arrivals
#define SERV_TIME 1.00 // Mean service time

//----- Function prototypes ----------------------------------------------------
double rand_val(int seed); // RNG for unif(0,1)
double exponential(double x); // Generate exponential RV with mean x

//===== Main program ===========================================================
int main(void)
{
double end_time = SIM_TIME; // Total time to simulate
double Ta = ARR_TIME; // Mean time between arrivals
double Ts = SERV_TIME; // Mean service time
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)
unsigned int n = 0; // Number of customers in the system
unsigned int c = 0; // Number of service completions
double b = 0.0; // Total busy time
double s = 0.0; // Area of number of customers in system
double tn = time; // Variable for "last event time"
double tb; // Variable for "last start of busy time"
double x; // Throughput
double u; // Utilization
double l; // Mean number in the system
double w; // Mean residence time

// Seed the RNG
rand_val(1);

// Main simulation loop
while (time < end_time)
{
if (t1 < t2) // *** Event #1 (arrival) ***
{
time = t1;
s = s + n * (time - tn); // Update area under "s" curve
n++;
tn = time; // tn = "last event time" for next event
t1 = time + exponential(Ta);
if (n == 1)
{
tb = time; // Set "last start of busy time"
t2 = time + exponential(Ts);
}
}
else // *** Event #2 (departure) ***
{
time = t2;
s = s + n * (time - tn); // Update area under "s" curve
n--;
tn = time; // tn = "last event time" for next event
c++; // Increment number of completions
if (n > 0)
t2 = time + exponential(Ts);
else
{
t2 = SIM_TIME;
b = b + time - tb; // Update busy time sum if empty
}
}
}

// End of simulation so update busy time sum
b = b + time - tb;

// Compute outputs
x = c / time; // Compute throughput rate
u = b / time; // Compute server utilization
l = s / time; // Compute mean number in system
w = l / x; // Compute mean residence or system time

// Output results
printf("=============================================================== \n");
printf("= *** Results from M/M/1 simulation *** = \n");
printf("=============================================================== \n");
printf("= Total simulated time = %3.4f sec \n", end_time);
printf("=============================================================== \n");
printf("= INPUTS: \n");
printf("= Mean time between arrivals = %f sec \n", Ta);
printf("= Mean service time = %f sec \n", Ts);
printf("=============================================================== \n");
printf("= OUTPUTS: \n");
printf("= Number of completions = %ld cust \n", c);
printf("= Throughput rate = %f cust/sec \n", x);
printf("= Server utilization = %f %% \n", 100.0 * u);
printf("= Mean number in system = %f cust \n", l);
printf("= Mean residence time = %f sec \n", w);
printf("=============================================================== \n");

return(0);
}

//=========================================================================
//= Multiplicative LCG for generating uniform(0.0, 1.0) random numbers =
//= - x_n = 7^5*x_(n-1)mod(2^31 - 1) =
//= - With x seeded to 1 the 10000th x value should be 1043618065 =
//= - From R. Jain, "The Art of Computer Systems Performance Analysis," =
//= John Wiley & Sons, 1991. (Page 443, Figure 26.2) =
//= - Seed the RNG if seed > 0, return a unif(0,1) if seed == 0 =
//=========================================================================
double rand_val(int seed)
{
const long a = 16807; // Multiplier
const long m = 2147483647; // Modulus
const long q = 127773; // m div a
const long r = 2836; // m mod a
static long x; // Random int value (seed is set to 1)
long x_div_q; // x divided by q
long x_mod_q; // x modulo q
long x_new; // New x value

// Seed the RNG
if (seed != 0) x = seed;

// RNG using integer arithmetic
x_div_q = x / q;
x_mod_q = x % q;
x_new = (a * x_mod_q) - (r * x_div_q);
if (x_new > 0)
x = x_new;
else
x = x_new + m;

// Return a random value between 0.0 and 1.0
return((double) x / m);
}

//==============================================================================
//= Function to generate exponentially distributed RVs using inverse method =
//= - Input: x (mean value of distribution) =
//= - Output: Returns with exponential RV =
//==============================================================================
double exponential(double x)
{
double z; // Uniform random number from 0 to 1

// Pull a uniform RV (0 < z < 1)
do
{
z = rand_val(0);
}
while ((z == 0) || (z == 1));

return(-x * log(z));
}

- - - Updated - - -

some points for start:
1 random packets: you need to generate a possion arrival
2 set up some time to say your system is in steady state, since the formula we care more is when system is stable.
3 delay time = all the packets in queue and a packet in the server
 
Last edited:

natasie

Member level 2
Joined
Jul 11, 2011
Messages
49
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,643
thanks i will look into it.

I appreciate this very much
 

natasie

Member level 2
Joined
Jul 11, 2011
Messages
49
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,643
Hey W_Heisenberg,

I ran the code and it really helped So thank you, I really appreciate it. I have tweaked it just a bit to fit what I want it to do.
However, I still have a problem, I want to send the data that is being displayed to an excel file and then plot it.
please could you help me with this? this is an example of the code i have so far:

ofstream output("e:\\mm1jobs.csv");
output<<"Time, Packet size, Total Queue Size(bits),Average Queue Length (bits), Average Delay (milli seconds),"<<endl;
output<<current_time<<","<<present_pk_size<<","<<queue_size<<","<<avg_queue_length<<","<<avg_delay_time<<endl;

please could you help me with this. I have created this file but I do not see any data in it.
 
  • Like
Reactions: Vedder

    Vedder

    points: 2
    Helpful Answer Positive Rating

natasie

Member level 2
Joined
Jul 11, 2011
Messages
49
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,643
ok do not worry, I have fixed it, thank you
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top