Project 3 Eventdriven Simulation CS2303 System Programming Concepts

  • Slides: 16
Download presentation
Project #3 – Event-driven Simulation CS-2303 System Programming Concepts (Slides include materials from The

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) CS-2303, C-Term 2010 Event-drive Simulation 1

Definitions • Simulation: – A computer program that mimics the behavior of people and

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 CS-2303, C-Term 2010 Event-drive Simulation 2

Simulations • Widely used in computing and technology for helping to understand the behavior

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 … CS-2303, C-Term 2010 Event-drive Simulation 3

Programming Assignment #3 • Model the behavior of customers queuing in a bank –

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 CS-2303, C-Term 2010 Event-drive Simulation 4

Key Events for this Simulation • Customer arrivals • I. e. , customers walk

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. CS-2303, C-Term 2010 Event-drive Simulation 5

Event Queue • Linked list, ordered by event time • Earliest event time is

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 Suggest float to represent time • Simulated clock – a numerical value containing the time of the most recent event from event queue CS-2303, C-Term 2010 Event-drive Simulation 6

Other Queues • Teller queue • Linked list representing customers lining up in front

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: – Queue A linked list in which items are added to the tail and from Strictly speaking, the Event which items are removed from Queue is not a true queue — the head items are added in time order, CS-2303, C-Term 2010 Event-drive Simulation 7 not at tail.

Your Program • Initialize: – • Get parameters of simulation from command line •

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 CS-2303, C-Term 2010 Event-drive Simulation 8

Actions of the Simulation • If multiple shortest queues Customer arrival: – select one

Actions of the Simulation • If multiple shortest queues Customer arrival: – select one at random • 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 CS-2303, C-Term 2010 Event-drive Simulation 9

Statistics to Gather • How many minutes, on average, does a customer wait to

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? CS-2303, C-Term 2010 Event-drive Simulation 10

Notes • Service events may occur after last arrival • E. g. , after

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 CS-2303, C-Term 2010 Event-drive Simulation 11

Random Number Generators • Random numbers are needed frequently in engineering & scientific computations

Random Number Generators • Random numbers are needed frequently in engineering & scientific computations • Simulations, arrival times, etc. • Exercising other code • Analyzing system performance ield ers y alls umb c f n m! ed t o a do pe ence number n seemingly a Re random r equ pear s a ap t a th • Definition: Random Number Generator • A function that returns a each time it is called • (Usually) within a specified range CS-2303, C-Term 2010 Event-drive Simulation 12

Random Number Generators (continued) • Algorithmic – Retain information in static variables – Scramble

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 CS-2303, C-Term 2010 Event-drive Simulation 13

Problem with Random Number Generators • Don’t give the same answer each time! •

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 CS-2303, C-Term 2010 Event-drive Simulation 14

Linux Random Number Generators • There are many! • Suggested • int rand(void) •

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) CS-2303, C-Term 2010 Event-drive Simulation 15

Questions on Programming Assignment? Next Topic – Linked Lists CS-2303, C-Term 2010 Event-drive Simulation

Questions on Programming Assignment? Next Topic – Linked Lists CS-2303, C-Term 2010 Event-drive Simulation 16