Event-Driven ProgrammingGedankenBitsExtra ProjectProject 6Another Random AsideOfficial GoalsWhat Is An Event?“Reactive” EnvironmentsTraditional EnvironmentsWhat Goes On In A Web Browser?Threads Versus EventsLet’s Think Of StatesReally Reducing StatesState ExamplesRemember This Diagram?Structure of Event-Driven ProgramsThe Real StructureDelaysSelect( ) System CallPowerPoint PresentationSelect DescriptionBlocking StepsOvercoming Disk Blocking StatesNew Architecture - AMPEDCaches in Flash Web ServerEvent-Driven ProgrammingVivek PaiDec 5, 20022GedankenBitsGedankenBitsWhat does a raw bit cost?IDE40GB: $100120GB: $18032MB USB Pen: $38FireWire: 60GB: $205120GB: $280USB:40GB: $140120GB: $218What does a managed bit cost?NAS320GB: $3400 (Dell)7.5TB: $300K (Zambeel)What does a person cost?$100K salary + $50K benefits/overhead3Extra ProjectExtra ProjectWe need to agree on itOne-page proposalIdea, implementation, measurementReport (~10 pages) due on Dean’s DateMax boost of 2/3 of a letter grade4Project 6Project 6Several goalsPerformance improvement via cachingDynamic adjustment to loadMaster/slave or symmetric programmingDue on Dean’s DateExtra credit: 4 points (20 base)5Another Random AsideAnother Random AsideYou may want to readFlash: An Efficient and Portable Web ServerAvailable from my home pageCaveat: far more complicated than Project 56Official GoalsOfficial GoalsDiscuss the difference between standard programming styles and event-driven programmingShow the difference between structuring servers using processes and application-level multiplexingDiscuss the benefits and drawbacks of each approach7What Is An Event?What Is An Event?Some kind of notificationInterruptsSignalsPolling (via poll/select/etc)Callback (via function pointer)Similarities?8“Reactive” Environments“Reactive” EnvironmentsWindowing systemsNetwork-aware programsDrivers of all sorts9Traditional EnvironmentsTraditional EnvironmentsOne thing going on at a timeFinish that thing, go on to nextAny step can block indefinitelyResumption from blocking is simple – OS provided10What Goes On In A Web Browser?What Goes On In A Web Browser?Drawing the current pageInline imagesTranslating machine names to IP addressesLaunching connectionsSending requestsGetting piecemeal responses, drawing imagesUser clicking to next link11Threads Versus EventsThreads Versus EventsOne stack versus many stacksWhat happens on blocking operationsParallelismShared variablesState12Let’s Think Of StatesLet’s Think Of StatesHow many possible states are there?Take all pieces of informationDecide valid range for all piecesEnumerateCan we reduce states?Some combinations invalidStill, lots of states13Really Reducing StatesReally Reducing StatesTake all major pieces of programAdd extra tags to stateWhat do tags look like?PositionCount #14State ExamplesState ExamplesIf-then-else3 states: start, then-clause, else-clauseFor loop1 state + countWhy do we care?Resuming at the right state15Remember This Diagram?Remember This Diagram?Read FileSend DataAcceptConnReadRequestFindFileSendHeaderend16Structure of Event-Driven ProgramsStructure of Event-Driven ProgramsLots of state machinesMaintaining information about each oneSome way of moving through statesAs few restrictions as possible on timing17The Real StructureThe Real StructureWhile (1)Get eventDispatch eventOr, while loop in libraryEvent handlers in main program18DelaysDelaysAre delays possible?Interrupt handlers – generally notOtherwise? Depends on event rateHow to avoid delays?More events – asynchronous operationsWhat happens if no async support?Fake it19Select( ) System CallSelect( ) System Callint select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);FD_SET(fd, &fdset);FD_CLR(fd, &fdset);FD_ISSET(fd, &fdset);FD_ZERO(&fdset);2021Select DescriptionSelect DescriptionDESCRIPTION Select() examines the I/O descriptor sets whose addresses are passed in readfds, writefds, and exceptfds to see if some of their descriptors are ready for reading, are ready for writing, or have an exceptional condition pending, respectively. The only exceptional condition detectable is out-of-band data received on a socket. The first nfds descriptors are checked in each set; i.e., the descriptors from 0 through nfds-1 in the descriptor sets are examined. On return, select() replaces the given descriptor sets with subsets consisting of those descriptors that are ready for the requested operation. Select() returns the total number of ready descriptors in all the sets. The descriptor sets are stored as bit fields in arrays of integers. The following macros are provided for manipulating such descriptor sets: FD_ZERO(&fdset) initializes a descriptor set fdset to the null set. FD_SET(fd, &fdset) includes a particular descriptor fd in fdset. FD_CLR(fd, &fdset) removes fd from fdset. FD_ISSET(fd, &fdset) is non- zero if fd is a member of fdset, zero otherwise. The behavior of these macros is undefined if a descriptor value is less than zero or greater than or equal to FD_SETSIZE, which is normally at least equal to the maximum number of descriptors supported by the system.22Blocking StepsBlocking StepsRead FileSend DataAcceptConnReadRequestFindFileSendHeaderendNetwork BlockingDisk Blocking23Overcoming Disk Blocking StatesOvercoming Disk Blocking StatesRead FileSend DataAcceptConnReadRequestFindFileSendHeaderendHelper Helper24New Architecture - AMPEDNew Architecture - AMPEDHelpers are threads or processesRead FileSend DataAcceptConnReadRequestFindFileSendHeaderEvent DispatcherAsymmetric Multiple Process Event DrivenHelper 1 Helper 2 Helper N25Caches in Flash Web ServerCaches in Flash Web ServerRead FileSend DataAcceptConnReadRequestFindFileSendHeaderendPathnameTranslationCacheHelper
View Full Document