Event-Driven Programming Vivek Pai Dec 4, 2001 GedankenEvents Enron – bad news all around What Korean are these numbers: $1B, $100B, $80B, $200M facts Largest on-line game has 3M+ subscribers What’s the comparable figure for the US? What are the populations of the two countries? How many albums does the Wu Tang Clan have? What is Ginger? 2 A Random Aside What do these companies have in common VA Linux, Penguin Computing RLX Technologies, Fibercycle HP, Compaq Dell, IBM 3 Mechanics Project 3 grading in progress Project 4 grading in progress All feedback questions answered One mail dropped – extra project proposals Speaking of which We need to agree on a one-page proposal 4 Project 5 Tyranny of the majority Several goals Performance improvement via caching Dynamic adjustment to load Master/slave or symmetric programming Probably due on Dean’s Date Extra credit: 6 points (15 base) 5 Another Random Aside You may want to read Flash: An Efficient and Portable Web Server Available from my home page Caveat: far more complicated than Project 5 6 Official Goals Discuss the difference between standard programming styles and event-driven programming Show the difference between structuring servers using processes and application-level multiplexing Discuss the benefits and drawbacks of each approach 7 What Is An Event? Some kind of notification Interrupts Signals Polling (via poll/select/etc) Callback (via function pointer) Similarities? 8 “Reactive” Environments Windowing systems Network-aware programs Drivers of all sorts 9 Traditional Environments One thing going on at a time Finish that thing, go on to next Any step can block indefinitely Resumption from blocking is simple – OS provided 10 What Goes On In A Web Browser? Drawing the current page Inline images Translating machine names to IP addresses Launching connections Sending requests Getting piecemeal responses, drawing images User clicking to next link 11 Threads Versus Events One stack versus many stacks What happens on blocking operations Parallelism Shared variables State 12 Let’s Think Of States How many possible states are there? Take all pieces of information Decide valid range for all pieces Enumerate Can we reduce states? Some combinations invalid Still, lots of states 13 Really Reducing States Take all major pieces of program Add extra tags to state What do tags look like? Position Count # 14 State Examples If-then-else 3 For 1 states: start, then-clause, else-clause loop state + count Why do we care? Resuming at the right state 15 Remember This Diagram? Accept Conn Read Request Find File Send Header Read File Send Data end 16 Structure of Event-Driven Programs Lots of state machines Maintaining information about each one Some way of moving through states As few restrictions as possible on timing 17 The Real Structure While (1) Get event Dispatch event Or, while loop in library Event handlers in main program 18 Delays Are delays possible? Interrupt handlers – generally not Otherwise? Depends on event rate How to avoid delays? More events – asynchronous operations What happens if no async support? Fake it 19 Blocking Steps Disk Blocking Accept Conn Read Request Find File Send Header Read File Send Data end Network Blocking 20 Overcoming Disk Blocking States Accept Conn Read Request Find File Helper Send Header Read File Send Data end Helper 21 New Architecture - AMPED Asymmetric Multiple Process Event Driven Accept Conn Read Request Find File Send Header Read File Send Data Event Dispatcher Helper 1 Helper 2 Helper N Helpers are threads or processes 22 Caches in Flash Web Server Accept Conn Read Request Find File Send Header Read File Send Data end Pathname Response Mapped Translation Header File Cache Cache Cache Helper Helper 23