server

Capsim Block Documentation

Short Description

This star models an M/M/1 queue

Top
Parameters
Num Description Type Name Default Value
0 Type:0=exp,1=gamma int type 0
1 Mean service time float meanServiceTime 0.5
2 Expression for random number generator file expression any expresssion
3 Time to end float timeEnd 480.0
4 Output Request:0=Number in queue,1=Server Status int outputRequest 0
Top
States
Num Type Name Initial Value Description
0 int ibufs
1 int obufs
2 long seed1
3 long seed2
4 int nextEventType
5 int numberCustomersDelayed
6 int numberEvents
7 int numberInQ
8 int serverStatus
9 float areaNumberInQ
10 float areaServerStatus
11 float meanInterArrival
12 float time
13 float timeArrival_A[Q_LIMIT+1]
14 float timeLastEvent
15 float timeNextEvent_A[4]
16 float totalOfDelays
17 int first 1
Top

Declarations


 

	int i,j;
	int samples;
	float sampleOut;
	float	x;
	float	arrivalInterval;
	float	minTimeNextEvent;
	float	timeSinceLastEvent;
	float delay;



Top

Initialization Code



 

	/* 
	 * store as state the number of input/output buffers 
	 */
	if((ibufs = NO_INPUT_BUFFERS()) < 1) {
		fprintf(stderr,"server: no input buffers\n");
		return(2);
	}
	if((obufs = NO_OUTPUT_BUFFERS()) < 1) {
		fprintf(stderr,"server: no output buffers\n");
		return(3);
	}
	/*
	 * Get seeds from expression
	 */
	phrtsd(expression,&seed1,&seed2);
	fprintf(stderr,"server: seed1=%d seed2=%d\n",seed1,seed2);
	setall(seed1,seed2);
	/*
	 * Specify the number of events for the timing function
	 */
	numberEvents=3;
	time=0.0;
	serverStatus=IDLE;
	numberInQ = 0;
	timeLastEvent=0.0;
	/*
	 * Initialize statistical counters
	 */
	numberCustomersDelayed =0;
	totalOfDelays=0.0;
	areaNumberInQ=0.0;
	areaServerStatus=0.0;
	/*
	 * Initialize event list. Since no customers are present
	 * the departure (service completion) event is eliminated
	 * from consideration. The end-simulation event type (type 3) is 
	 * scheduled for time timeEnd.
	 */
	timeNextEvent_A[1] = time;
	timeNextEvent_A[2] = INFINITY;
	timeNextEvent_A[3]= timeEnd;




Top

Main Code



 


/* 
 * read one sample from each input buffer 
 */
for(samples = MIN_AVAIL(); samples >0; --samples) {

	/*
 	 * get a customer and arrival interval
	 * from input buffer 
	 */
	for(i=0; i Q_LIMIT) {

				   /*
				    * the queue has overflowed
				    */
				   fprintf(stderr,"\nOverflow of array time of arrival ar time %f\n",time);
				   return(6);
				}
				/*
			 	 * there is still room in the queue
				 */
				timeArrival_A[numberInQ]= time;
			}
			else {
				/*
				 * Server is idle, so arriving customer has
				 * a delay of zero
				 */
				delay= 0.0;
				totalOfDelays += delay;
				/*
				 * Incrementthe number of customers delayed
				 * and make server busy
				 */

				++numberCustomersDelayed;
				serverStatus= BUSY;

				/*
				 * Schedule a departure (service completion)
				 */
				timeNextEvent_A[2]= time + genexp(meanServiceTime);
			}
			break;
		case 2:
			/*
			 * Depart
			 */

			/*
			 * Check to see whether the queue is empty
			 */
			if(numberInQ == 0) {
				/*
				 * The queue is empty so make the server
				 * idle and eliminate the  departure
				 * ( service completion )  event from 
				 * consideration
				 */
				 serverStatus = IDLE;
				 timeNextEvent_A[2]= INFINITY;
			}
			else {
				/*
				 * the queue is nonempty, so decrease the 
				 * number of customers in queue
				 */
				--numberInQ;
				/*
				 * compute the delay of the customers delayed
				 * who is beginning service and update the
				 * total delay accumulator
				 */
				delay = time - timeArrival_A[1];
				totalOfDelays += delay;
				
				/*
				 * increment the number of customers delayed
				 * and schedule departure
			 	 */
				++numberCustomersDelayed;
				timeNextEvent_A[2]= time+ genexp(meanServiceTime);
				/*
				 * Move each customer in queue (if any) up
				 * one place
				 */
				for(i=1; i<=numberInQ; ++i) 
					timeArrival_A[i] = timeArrival_A[i+1];
			}
			break;
		case 3:
			/*
			 * report
			 */
			return(0);
			break;
	}
	switch(outputRequest) {
		case REQUEST_SERVER_STATUS:
			sampleOut = serverStatus;
			break;
		case REQUEST_NUMBER_IN_QUEUE:
			sampleOut = numberInQ;
			break;
	}
	if(obufs==1 ) {
		if(IT_OUT(0)) {
			fprintf(stderr,"server: Buffer %d is full\n",0);;
			return(99);
		}
		OUTF(0,0) = sampleOut;
	}
	else if( obufs ==2) {
		if(IT_OUT(0)) {
			fprintf(stderr,"server: Buffer %d is full\n",0);;
			return(99);
		}
		OUTF(0,0) = time;
		if(IT_OUT(1)) {
			fprintf(stderr,"server: Buffer %d is full\n",1);;
			return(99);
		}
		OUTF(1,0) = sampleOut;


	}
}

return(0);	/* at least one input buffer empty */





Top

Wrapup Code



 

/*
 * Report results
 */
fprintf(stderr,"Single-server queueing system \n");
fprintf(stderr,"Mean service time %f \n", meanServiceTime);
fprintf(stderr,"\n\nAverage delay in queue %11.3f minutes\n\n",
	totalOfDelays/numberCustomersDelayed);
fprintf(stderr,"Average number in queue %10.3f\n\n",
	areaNumberInQ/time);
fprintf(stderr,"Server utilization %15.3f\n\n",
	areaServerStatus/time);
fprintf(stderr,"Number of delays completed %7d\n",
	numberCustomersDelayed);




Top

License



/*  Capsim (r) Text Mode Kernel (TMK) Star Library (Blocks)
    Copyright (C) 1989-2017  Silicon DSP Corporation

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    http://www.silicondsp.com
    Silicon DSP  Corporation
    Las Vegas, Nevada
*/


Top

Description



 

/* server.s */
/**********************************************************************
			server()
***********************************************************************
This star models an M/M/1 queue
The input buffer is the customer inter arrival times. Thus,
each sample on the input buffer represents the arrival of a customer.
The value of the sample is the customer inter arrival time.
The server places the customer in a queue if necessary and processes the
next available customer.
The server does not need to output anything but we have chosen to output
the current delay in the queue. Thus by connecting the output of
the server star to the plot star, you can observe the delay in the queue
over time. 
Many other output combinations are possible but this star is used to serve
as an example.
The server star implements the C code in "Simulation Modeling and Analysis"
by Averill M. Law and W. David Kelton, Second Edition 1991.
We have included original comments.
The parameters are:
(1) The type of distribution for the server 
(2) The mean service time
(3) An expression for the random number generator seeds.
(4) The time to end the simulation
Notes:
   (1) The simulation will end when there are no more customers.
   However, the simulation can end using parameter 4 as a condition.
   (2) Presently, parameter 1 is ignored and exponential distribution is used.
The input buffers are arbitrary so that in the future multiple customer 
sources may be modeled with a single server.
The number of input buffers is arbitrary and determined at run time.
The number of output buffers is also arbitrary (auto-fanout).

server


This star models an M/M/1 queue
The input buffer is the customer inter arrival times. Thus,
each sample on the input buffer represents the arrival of a customer.
The value of the sample is the customer inter arrival time.
The server places the customer in a queue if necessary and processes the
next available customer.
The server does not need to output anything but we have chosen to output
the current delay in the queue. Thus by connecting the output of
the server star to the plot star, you can observe the delay in the queue
over time. 
Many other output combinations are possible but this star is used to serve
as an example.
The server star implements the C code in "Simulation Modeling and Analysis"
by Averill M. Law and W. David Kelton, Second Edition 1991.
We have included original comments.
The parameters are:
(1) The type of distribution for the server 
(2) The mean service time
(3) An expression for the random number generator seeds.
(4) The time to end the simulation
Notes:
   (1) The simulation will end when there are no more customers.
   However, the simulation can end using parameter 4 as a condition.
   (2) Presently, parameter 1 is ignored and exponential distribution is used.
The input buffers are arbitrary so that in the future multiple customer 
sources may be modeled with a single server.
The number of input buffers is arbitrary and determined at run time.
The number of output buffers is also arbitrary (auto-fanout).


Programmer: Sasan Ardalan 
Date:	November 6, 1993

*/