240 likes | 405 Views
Event-Driven Programming. Vivek Pai Dec 4, 2001. GedankenEvents. Enron – bad news all around What are these numbers: $1B, $100B, $80B, $200M Korean facts Largest on-line game has 3M+ subscribers What’s the comparable figure for the US? What are the populations of the two countries?
E N D
Event-Driven Programming Vivek Pai Dec 4, 2001
GedankenEvents • Enron – bad news all around • What are these numbers: $1B, $100B, $80B, $200M • Korean 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?
A Random Aside • What do these companies have in common • VA Linux, Penguin Computing • RLX Technologies, Fibercycle • HP, Compaq • Dell, IBM
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
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)
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
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
What Is An Event? • Some kind of notification • Interrupts • Signals • Polling (via poll/select/etc) • Callback (via function pointer) • Similarities?
“Reactive” Environments • Windowing systems • Network-aware programs • Drivers of all sorts
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
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
Threads Versus Events • One stack versus many stacks • What happens on blocking operations • Parallelism • Shared variables • State
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
Really Reducing States • Take all major pieces of program • Add extra tags to state • What do tags look like? • Position • Count #
State Examples • If-then-else • 3 states: start, then-clause, else-clause • For loop • 1 state + count • Why do we care? • Resuming at the right state
Remember This Diagram? Accept Conn Read Request Find File Send Header Read File Send Data end
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
The Real Structure While (1) Get event Dispatch event • Or, while loop in library • Event handlers in main program
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
Blocking Steps Disk Blocking Accept Conn Read Request Find File Send Header Read File Send Data end Network Blocking
Overcoming Disk Blocking States Accept Conn Read Request Find File Send Header Read File Send Data end Helper Helper
Helper 1 Helper 2 Helper N New Architecture - AMPED Asymmetric Multiple Process Event Driven Helpers are threads or processes Accept Conn Read Request Find File Send Header Read File Send Data Event Dispatcher
Caches in Flash Web Server Accept Conn Read Request Find File Send Header Read File Send Data end Pathname Translation Cache Response Header Cache Mapped File Cache Helper Helper