A sample CSIM program follows. This
program is a model of an M/M/1 queueing
system. The process sim includes a for
loop, which generates, at appropriate
intervals (exponentially distributed
with mean IATM) arriving customers.
These customers contend for the facility
on a first-come-first-served basis.
As each customer gains exclusive use
of the facility, they delay for a service
period (again exponentially distributed,
but with mean SVTM) and then depart.
The individual response times (time
of arrival to time of departure) are
collected in a table. The program also
makes use of the histogram feature to
collect the frequency distribution of
the queue length.
Sample Program
to Simulate Single Server Facility
| // C++/CSIM Model
of M/M/1 queue |
| |
#include "cpp.h"
// class definitions
|
| #include <stdio.h> |
| |
| #define NARS 5000#define
IAR_TM 2.0#define SRV_TM 1.0 |
| |
| event *done; // pointer
to event done |
| facility *f; // pointer
to facility f |
| table *tbl; // pointer
to table of response times |
| qtable *qtbl; // pointer
to qtable of number in system |
| int cnt; // count
of remaining processes |
| FILE *fp; |
| |
| void init(); |
|
| void generateCustomers(); |
|
| void customer(); |
|
|
|
|
extern "C" void sim(int
argc, char *argv[])
{
init();
create("sim");
generateCustomers();
done->wait(); // wait for last
customer to depart
report(); // model report
mdlstat(); // model statistics
} |
|
|
| void generateCustomers() |
|
|
|
{
create("gen");
for(int i = 1; i <= NARS;
i++) {
hold(exponential(IAR_TM));
// interarrival intervalcustomer();//
generate next customer
}
}
|
|
|
| void customer() //
arriving customer
{
double t1;
create("cust");t1
= clock; // record start timeqtbl->note_entry();
// note arrivalf->reserve();
// reserve facilityhold(exponential(SRV_TM));
// service intervalf->release();
// release facilitytbl->record(clock
- t1); // record response timeqtbl->note_exit();
// note departure
if(--cnt == 0)
done->set(); // if last
customer, set event done
}
|
| |
|
void init()
{
fp = fopen("csim.out",
"w");
set_output_file(fp);set_model_name("M/M/1
Queue");
done = new event("done");
// instantiate event done
f = new facility("facility");
// instantiate facility f
tbl = new table("resp tms");
// instantiate table
qtbl = new qtable("num
in sys") ; // instantiate
qtable qtbl->add_histogram(10,
0, 10); // add histogram to
qtable
cnt = NARS; // initialize counter
}
|