Presentation is loading. Please wait.

Presentation is loading. Please wait.

Event-drive SimulationCS-2303, C-Term 20101 Project #3 – Event-driven Simulation CS-2303 System Programming Concepts (Slides include materials from The.

Similar presentations


Presentation on theme: "Event-drive SimulationCS-2303, C-Term 20101 Project #3 – Event-driven Simulation CS-2303 System Programming Concepts (Slides include materials from The."— Presentation transcript:

1 Event-drive SimulationCS-2303, C-Term 20101 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)

2 Event-drive SimulationCS-2303, C-Term 20102 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

3 Event-drive SimulationCS-2303, C-Term 20103 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 –…

4 Event-drive SimulationCS-2303, C-Term 20104 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

5 Event-drive SimulationCS-2303, C-Term 20105 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.

6 Event-drive SimulationCS-2303, C-Term 20106 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

7 Event-drive SimulationCS-2303, C-Term 20107 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 which items are removed from the head Strictly speaking, the Event Queue is not a true queue — items are added in time order, not at tail.

8 Event-drive SimulationCS-2303, C-Term 20108 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

9 Event-drive SimulationCS-2303, C-Term 20109 Actions of the Simulation 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 If multiple shortest queues select one at random

10 Event-drive SimulationCS-2303, C-Term 201010 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?

11 Event-drive SimulationCS-2303, C-Term 201011 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

12 Event-drive SimulationCS-2303, C-Term 201012 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 Repeated calls yield a sequence of numbers that appear random!

13 Event-drive SimulationCS-2303, C-Term 201013 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

14 Event-drive SimulationCS-2303, C-Term 201014 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

15 Event-drive SimulationCS-2303, C-Term 201015 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)

16 Event-drive SimulationCS-2303, C-Term 201016 Questions on Programming Assignment? Next Topic – Linked Lists


Download ppt "Event-drive SimulationCS-2303, C-Term 20101 Project #3 – Event-driven Simulation CS-2303 System Programming Concepts (Slides include materials from The."

Similar presentations


Ads by Google