1 / 22

I/O Multiplexing

I/O Multiplexing. Road Map: Motivation Description of I/O multiplexing Scenarios to use I/O multiplexing I/O Models Blocking I/O Non-blocking I/O I/O multiplexing ( select (..) and poll (..) function s ) Signal driven I/O Asynchronous I/O Comparison of models. Motivation.

idra
Download Presentation

I/O Multiplexing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. I/O Multiplexing Road Map: Motivation Description of I/O multiplexing Scenarios to use I/O multiplexing I/O Models Blocking I/O Non-blocking I/O I/O multiplexing (select(..)andpoll(..) functions) Signal driven I/O Asynchronous I/O Comparison of models

  2. Motivation • Read/write from multiple descriptors? • Whatif data arrivefrombothfd1& fd2? • Ifyou do recv(fd1,..), thenyoublockeduntilsomethingarrives. • Ifsomethingarrives at fd2, youhave no idea! • Ex: Web client, downloadaccelerator, proxies. • Solution : I/O multiplexing

  3. Description of I/O multiplexing Weneed • tocheckbothfd1& fd2 • receivefromtheonewhich has data. • This is called I/O multiplexing. • Achievedbyselectandpollfcns.

  4. Scenarios to use I/O multiplexing • Clienthandlemultipledescriptors • Ex: interactiveinput & network socket • Clienthandlemultiplesockets • Web client, downloadaccelerator, proxies • Server handlesboth TCP & UDP • DNS listensboth TCP53 & UDP53 • Server handlesmultipleservicesorprotocols

  5. A note.. • Thesetechniquesare not limitedto network programming. • I/O multiplexing is alsousedby • File I/O • Interactive I/O • Network I/O

  6. Blocking I/O ApplicationKernel systemcall no readydatagram recvfrom waitfor data here • Nowlet’s talk aboutthismethod. • Do youthink it is a goodapproach? • Whatareprosandcons? Applicationblockshere datagramready copydatagram copy data fromkerneltouser processdatagram copycomplete return OK

  7. Non-blocking I/O ApplicationKernel Processrepeatedlycallsrecvfromwaitingfor an OK (polling) systemcall no readydatagram recvfrom waitfor data here EWOULDBLOCK systemcall no readydatagram recvfrom EWOULDBLOCK . . . systemcall datagramready copydatagram recvfrom copy data fromkerneltouser processdatagram copycomplete return OK

  8. Non-blocking I/Ocont. • Requiressocketto be set non-blockingmode. • fcntl (standsfor file control) sets a sockettonon-blockingmode • Example: UdpNonblockingIO.c • #include<fcntl.h> • intfcntl(intfd, intcmd,...) • returns: depends on cmdif OK, -1 on error.

  9. I/O multiplexing model ApplicationKernel Processblocks in selectwaitingforone of possiblymanysocketstobecomereadable systemcall no readydatagram select waitfor data here returnreadable datagramready copydatagram recvfrom Processblockswhile data is copiedfromkernel systemcall copy data fromkerneltouser processdatagram copycomplete return OK

  10. selectSystemCall select allows the process to instruct the kernel to wait for any of multiple events to occur and to wake up the process only when one or more of these events occurs or when a specified amount of time has passed. • #include<sys/select.h> • #include <sys/time.h> • intselect(intmaxfd, fd_set *readset, fd_set *writeset, • fd_set *exceptset, conststructtimeval *timeout) • returns: positivecount of descriptors, 0 on timeout, -1 on error. structtimeval{ long tv_sec; /* seconds */ long tv_usec; /* milliseconds */ };

  11. selectcont. 3 possibilities of timeout • Waitforever: NULLtimeoutargument. • Waitupto a fixedamount of time. • No wait at all: Returnimmediatelyaftercheckingthedescriptors. This is called “polling”. bothtv_sec& tv_usecare set to “0”.

  12. An example We can callselectandtellthekerneltoreturnonlywhenany of thedescriptors in the set • {1,4,5} arereadyforreading. • {2,7} arereadyforwriting. • {2,4,5} have an exception. • 10.2 secondshaveelapsed. Tellthekernelwhatdescriptorsweareinterested in (forreading, writing, or an exceptioncondition) andhowlongtowait.

  13. Howtospecifydescriptor set? Therearefuctionsto set, clear, andquery on a descriptor set. Example: selectEx.c void FD_ZERO(fd_set *fdset); /* clearallbits in fdset */ void FD_SET(intfd, fd_set *fdset); /* turn on bits */ void FD_CLR(intfd, fd_set *fdset); /* turnoffbits */ int FD_ISSET(intfd, fd_set *fdset); /* is the bit forfd on in fdset? */ fd_set rset; FD_ZERO(&rset); FD_SET(1, &rset); FD_SET(4, &rset); FD_CLR(5, &rset);

  14. pollSystemCall Verysimilartoselect. fdarrayis a pointertothefirst element of an array of structures. Each element in thearray is a pollfdstructurethatspecifiestheconditionsto be testedfor a givendescriptor, fd. Mostimportantevents: POLLIN, POLLOUT. • #include<poll.h> • intpoll(structpollfdfdarray[], unsignedlongnfds, • inttimeout); • returns: count of readydescriptors, 0 on timeout, -1 on error. structpollfd{ intfd; /* descriptortocheck */ shortevents; /* events of interests on fd */ shortrevents; /* eventsoccured on fd */ };

  15. Signal driven I/O • A signal is a notificationto a processthat an event has occured. • Theyareasynchronous. Example: • Pressing^Cwhile program running. • ChildprocessterminatesandkernelsendsSIGCHLDsignaltoparent.

  16. Signal driven I/Ocont. • Everysignal has a disposition. • We set dispositionbycalling “sigaction” with 3 options. • Provide a function (signalhandler) forsignaloccurence. SIGKILL& SIGSTOPsignalscannot be caught. • Ignore a signalbysettingdispositiontoSIG_IGN. SIGKILL& SIGSTOPcannot be ignored. • Set defaultdispositionby SIG_DFL. Example: • signalEx1.c(handling^C) • signalEx2.c(usingSIGALRMtoperform I/O)

  17. Signal driven I/Ocont. • The idea is to set the I/O descriptorforsignaldriven I/O andthen set SIGIOhandlercallingsigaction. • Wheneverthedescriptor is readyforreading, thekernelwillgenerateSIGIOsignal. • The I/O can then be performedwithinsignalhandler.

  18. Signal driven I/Odiagram ApplicationKernel Processcontinues sigactionsystemcall initialize EstablishSIGIOsignalhandler waitfor data here return deliver SIGIO datagramready signalhandler Processwaits recvfrom copydatagram systemcall copy data fromkerneltouser processdatagram copycomplete return OK This is an advanced I/O model & wewill not coverthis in thisclass.

  19. Asynchronous I/O • The idea is to start the I/O operationandletthekernelnotify us whentheentireprocess is complete (includingthecopy of the data fromthekerneltoourbuffer). • Maindifferencebetweensignaldriven I/O is thatwithsignaldriven I/O thekerneltellswhen an I/O operation can be initiated. • But herekerneltellswhen I/O operation is complete.

  20. Asynchronous I/Odiagram ApplicationKernel systemcall no readydatagram aio_read return waitfor data here Processcontinuesexecuting datagramready copydatagram copy data fromkerneltouser Deliver signalspecified in aio_read Signalhandlerprocessdatagram copycomplete • Few POSIX systemssupportasynchronous I/O

  21. Comparison of I/O Models b l o c k e d b l o c k e d b l o c k e d b l o c k e d

  22. Lastword • Theeasiesttouseamongthem • Blocked I/O • I/O Multiplexing • Ifyoursystemsupportsthreads, you can emulateother I/O modelsusingmultiplethreads: • Onethreadtowaitfor I/O forfirstandthirdmodels & theotherthreaddoingprocessingifsomethingneedsto be processed. Thusweoverlap I/O & computationwithin a singleapplication.

More Related