160 likes | 453 Views
Project #3 – Event-driven Simulation. CS-2303 System Programming Concepts (Slides include materials from The C Programming Language , 2 nd edition, by Kernighan and Ritchie and from C: How to Program , 5 th and 6 th editions, by Deitel and Deitel). Definitions.
E N D
Project #3 – Event-driven Simulation CS-2303System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) Event-drive Simulation
Definitions • Simulation:–A computer program that mimics the behavior of people and objects in a system • Event-Driven Simulation:–A simulation that is organized around the timing of events representing significant points in the actions of the people or objects Event-drive Simulation
Simulations • Widely used in computing and technology for helping to understand the behavior of systems that are too hard to model in other forms • Processor scheduling • Traffic and highway analysis • Rivers and streams and flooding • Robot movement and control • Network congestion • Satellites and space craft • … Event-drive Simulation
Programming Assignment #3 • Model the behavior of customers queuing in a bank • Specifically, the effects of one queue for all tellers versus separate queues for each teller • Specifically, the effect of providing just enough tellers versus providing extra tellers • Figures of merit • Customer waiting time – i.e., how long before being served • Bank staffing – i.e., how many tellers needed to support a particular level of customer traffic Event-drive Simulation
Key Events for this Simulation • Customer arrivals • I.e., customers walk into bank and get in line • Uniform distribution throughout a period of time • Start of teller service • I.e., teller starts serving person at front of line • End of teller service • I.e., teller finishes serving a customer, looks for next one • Teller goes idle • I.e., no customers in line, teller does something else for a short period of time. Event-drive Simulation
Event Queue • Linked list, ordered by event time • Earliest event time is at front to list, latest at end • New events are added in time order • List elements – struct containing • Time of event • Kind of event • Link to next event • Simulated clock – a numerical value containing the time of the most recent event from event queue Suggest float to represent time Event-drive Simulation
Other Queues • Teller queue • Linked list representing customers lining up in front of a teller (or group of tellers) • New arrivals added to end (i.e., tail) of list • Waiting customers served from beginning (i.e., head) of list Definition:– QueueA linked list in which itemsare added to the tail and fromwhich items are removed fromthe head Strictly speaking, the EventQueue is not a true queue —items are added in time order,not at tail. Event-drive Simulation
Your Program • Initialize:– • Get parameters of simulation from command line • Generate customer arrivals, insert into event queue • Generate idle tellers, insert into event queue • Do the simulation • Get first item from event queue • Update simulated clock to time of event • Perform action of the event • Gather statistics about the event or action • Repeat until no more events • Print out statistics Event-drive Simulation
Actions of the Simulation If multiple shortest queuesselect one at random • Customer arrival:– • Place customer at end of shortest teller queue • Idle teller:– • Gather idle statistics • Check if customer is waiting in teller queue • If yes, generate service event, insert into event queue • If not, generate new idle event in event queue • Service complete:– • Gather customer service statistics • Check if new customer is waiting in teller queue • Insert service event or idle event in event queue as above Event-drive Simulation
Statistics to Gather • How many minutes, on average, does a customer wait to get service? • How many minutes, on average, does a customer spend in the bank? • How many minutes total do tellers spend in idle state? • How many minutes total do tellers spend serving customers? Event-drive Simulation
Notes • Service events may occur after last arrival • E.g., after simulating one hour of arrivals, there may still be customers in the bank at the end of that hour, waiting for service • Simulation stops if there are no more customers waiting in any queue Event-drive Simulation
Repeated calls yielda sequence of numbersthat appear random! Random Number Generators • Random numbers are needed frequently in engineering & scientific computations • Simulations, arrival times, etc. • Exercising other code • Analyzing system performance • Definition:Random Number Generator • A function that returns a seemingly random number each time it is called • (Usually) within a specified range Event-drive Simulation
Random Number Generators (continued) • Algorithmic • Retain information in static variables • Scramble numbers to get something that “looks” random on each call • Entire mathematical theory about them • Evaluating the quality of the randomness • See §5.10 of D&D or pp. 46, 252 of K&R Event-drive Simulation
Problem with Random Number Generators • Don’t give the same answer each time! • Difficult to get reproducible behavior when debugging! • Solution:– the seed • A numeric value for initializing the internal state • So that the generator produces the same sequence each time Event-drive Simulation
Linux Random Number Generators • There are many! • Suggested • int rand(void) • Returns values in range 0 .. RAND_MAX • rand() % r returns values in range 0 .. (r-1) for integer r • Seeding:– • srand(unsigned int seed) • Get value of seed from (optional) command line argument • srand(time(NULL)) • Seeds to current time (measured in seconds since the beginning) Event-drive Simulation
Questions on Programming Assignment? Next Topic – Linked Lists Event-drive Simulation