290 likes | 753 Views
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.
E N D
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
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
Description of I/O multiplexing Weneed • tocheckbothfd1& fd2 • receivefromtheonewhich has data. • This is called I/O multiplexing. • Achievedbyselectandpollfcns.
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
A note.. • Thesetechniquesare not limitedto network programming. • I/O multiplexing is alsousedby • File I/O • Interactive I/O • Network I/O
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
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
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.
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
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 */ };
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”.
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.
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);
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 */ };
Signal driven I/O • A signal is a notificationto a processthat an event has occured. • Theyareasynchronous. Example: • Pressing^Cwhile program running. • ChildprocessterminatesandkernelsendsSIGCHLDsignaltoparent.
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)
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.
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.
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.
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
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
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.