/* * This program simulates a queueing system. There could be a number * of input job streams and a number of servers. The input process * follows Poison process, i.e. the interarrival time of the jobs * follows exponential distribution. The service requirement of the * jobs are also exponentially distributed, making it M/M/m queues. * * The purpose of the program is mainly to exercise basic discrete * event-driven simulation techniques in high level programming * language. It also serves to compare with the analytical calculations. * * Author: Xiannong Meng * CS/Bucknell University, Lewisburg, PA 17837 * meng@bucknell.edu * Date: Jan. 1994 * Modification: * Xiannong Meng * Modified for CS6354 Fall 1999 for g++ 2.81 under Solaris 2.7 * Date: oct-24-1999 */ #include "simul.h" main() { /* * variable declarations */ eventQClass eventQue; // main event queue int totalTime; // total simulatoin time int seed; // random number seed double meanServTime; // mean service time double meanArvTime; // mean interarrival time eventType newType = ARRIVE; // these two are used to double newTime = 0; // initialize the first event double currentTime = 0; // these two are used to double cpuTime = 0; // initialize the first job jobType job(currentTime,cpuTime); eventClass event; // current event Server server; // the server /* * input parameters */ cout << " input total simulation time "; cin >> totalTime; cout << totalTime << endl; cout << " input a random number seed (0-15) "; cin >> seed; cout << seed << endl; stream(seed); cout << " input batch size "; cin >> server.BATCHSIZE; cout << server.BATCHSIZE << endl; cout << " input mean service time "; cin >> meanServTime; cout << meanServTime << endl; cout << " input mean interarrival time "; cin >> meanArvTime; cout << meanArvTime << endl; /* * initialize the event and event queue */ event.create(newTime,newType,job); eventQue.Enqueue(event); while (currentTime < totalTime) // do the simulation { event = eventQue.Front(); eventQue.Dequeue(); currentTime = event.eventtime; switch (event.eventtype) { /* * upon arrival, generate service time request * if the server is free * go to service * else * put into the job queue at the server * schedule a new arrival */ case ARRIVE : cpuTime = expntl(meanServTime); job.set(currentTime,cpuTime); server.increment(); job.id = server.arrivals; if (server.isBusy() == FALSE) { server.setBusy(); newType = COMP; newTime = currentTime + job.cpuReq; event.create(newTime,newType,job); eventQue.Enqueue(event); } else server.jobQ.Enqueue(job); newType = ARRIVE; newTime = expntl(meanArvTime) + currentTime; event.create(newTime,newType,job); eventQue.Enqueue(event); break; /* * when a job completes, * if there are jobs waiting * put the job in service, schedule its * completion * else * set the server free */ case COMP : server.collectStats(currentTime, event.job.arrivalTime); if (server.jobQ.Empty() == FALSE) { job = server.jobQ.Front(); server.jobQ.Dequeue(); newType = COMP; newTime = currentTime + job.cpuReq; event.create(newTime,newType,job); eventQue.Enqueue(event); } else server.setFree(); break; } // end of switch } // end of while server.printStats(); } void error(int number, char * msg) { cout << number << msg << endl; }