/* Simulation model for a slotted-time switch with finite buffer. */ /* The switch has N input links, and only one output link. */ /* There is a finite buffer available at the output link. */ /* Configuration parameters are specified as interactive input. */ /* The simulation models the packet arrival, queueing, and loss. */ /* Written by Carey Williamson for CPSC 441 March 24, 2018 */ #include #define MAX_INPUTS 4 #define MAXINT 2147483647.0 /* #define DEBUG 1 */ /* #define RAND_DEBUG 1*/ int random(); int main() { int inpkt[MAX_INPUTS], pktssent[MAX_INPUTS], pktslost[MAX_INPUTS]; int totpktssent, totpktslost; int outputq, maxqsize; int i, j, n, steps; int pktsthistime; float lambda; float prob; /* prompt user for configuration parameters */ printf("Number of input links? "); scanf("%d", &n); if( n > MAX_INPUTS ) { printf("Too many input links!!! Resetting to %d...\n", MAX_INPUTS); n = MAX_INPUTS; } printf("Maximum buffer size at output link? "); scanf("%d", &maxqsize); printf("Traffic load level? (lambda) "); scanf("%f", &lambda); printf("Simulation run length (time slots)? "); scanf("%d", &steps); /* Initialization */ for( j = 0; j < n; j++ ) { pktssent[j] = 0; pktslost[j] = 0; inpkt[j] = 0; } totpktssent = 0; totpktslost = 0; outputq = 0; pktsthistime=0; prob = 0; /* seed the pseudo-random number generator */ srandom(time(NULL)); /* Main loop: repeat once for each time slot being simulated */ for( i = 0; i < steps; i++ ) { #ifdef DEBUG printf("Top of loop: output queue size at time step %d is %d\n", i, outputq); #endif /* Generate the new packet arrivals in time step i */ pktsthistime = 0; for( j = 0; j < n; j++ ) { /* Generate a packet (at random) from each input link */ prob = random() / MAXINT; #ifdef RAND_DEBUG printf("random value for pkt generation is %f\n", prob); #endif if( prob <= lambda ) inpkt[j] = 1; else inpkt[j] = 0; if( inpkt[j] > 0 ) { pktsthistime++; pktssent[j]++; totpktssent++; } } #ifdef DEBUG printf("There are %d new packets arriving in slot %d\n", pktsthistime, i); #endif /* Move each new packet onto the output queue, if possible */ for( j = 0; j < n; j++ ) { if( inpkt[j] == 0 ) continue; /* Check to see if it fits in queue or not */ if( outputq < maxqsize ) outputq++; else { pktslost[j]++; totpktslost++; } } #ifdef DEBUG printf("Output queue size for step %d: %d\n", i, outputq); printf("Cumulative losses so far, by input link:\n"); for( j = 0; j < n; j++ ) printf("%2d ", pktslost[j]); printf("\n"); #endif /* Transmit a packet on the output link (if queue is non-empty) */ if( outputq > 0 ) outputq--; #ifdef DEBUG printf("Bottom of loop: output queue size at time step %d is %d\n", i, outputq); #endif } /* Print simulation results */ printf("\n\nSimulation run length: %d time steps\n", steps); printf("Total packets generated: %d\n", totpktssent); printf(" By input link: "); for( j = 0; j < n; j++ ) printf("%2d ", pktssent[j]); printf("\n"); printf("Total packets lost: %d (%10.8f)\n", totpktslost, (float) 1.0 * totpktslost/totpktssent); printf(" By input link: "); for( j = 0; j < n; j++ ) printf("%2d ", pktslost[j]); printf("\n"); }