220 likes | 561 Views
I/O multiplexing , select and poll function. INPUT / OUTPUT MODEL. SYNCHRONOUS I/O. ASYNCHRONOUS I/O. SYNCHRONOUS I/O A synchronous I/O causes the requesting process to be blocked until the operation Completes . ASYNCHRONOUS I/O
E N D
INPUT / OUTPUT MODEL SYNCHRONOUS I/O ASYNCHRONOUS I/O SYNCHRONOUS I/O A synchronous I/O causes the requestingprocess to be blocked until the operation Completes. ASYNCHRONOUS I/O A asynchronous I/O operation does not cause the requesting process to be blocked. SYNCHRONOUS I/O BLOCKING I/O NON-BLOCKING I/O I/O MULTIPLUXING SIGNAL DRIVEN I/O
Blocked I/O kernel Application recvfrom No data gram Data gram ready Copy data gram Process data gram Copy complete
Non – blocking I/O Application kernel recvfrom No data gram recvfrom No data gram No data gram Recvfrom recvfrom Data gram ready Copy datagram Process data gram Return OK Copy complete
I/O MULTIPLEXING Kernel Application System call select Data gram not ready Return readable Data gram ready System call recvfrom Copy data gram Return OK Process datagram Copy complete
Signal driven I/O Application kernel Establish SIGIO signalhandler Data gram ready Signal handler recvfrom Copy data gram Process data gram Return OK Copy complete
Asynchronous I/O kernel Application Aio_read No data gram Data gram ready Copy data gram Deliver signal Process data gram Copy complete
I/O multiplexing This is implemented in the program using the select or the poll function. Now we will see the select function in detail. SYNTAX: #include<sys/select.h> #include<sys/time.h> int select(int maxfdp1,fd_set *readset, fd_set *writeset, fd_se t *exceptset , const struct timeval *timeout); RETURNS: +ve count of ready descripters 0 on time out -1 on error
const struct timeval *timeout This tells the kernel how long to wait for one of the specified descriptor to become ready. Timeval a structure specifies the number of seconds and microseconds. Struct timeval { long tv_sec; long tv_usec; }; *timeout may have three values NULL -----return’s only when one of the specified descriptors is ready 0 0 -----return’s immediately after checking , also called as polling. particular time----returns when one of the specified descriptor is ready or after the specified time. CONST: Indicates that the value of the timeout will not return the number of seconds left if it has return before the stipulated time.
Int maxfdp1: This gives the total number of descriptors the select function has to wait. Its value is maxfdp1+1.The decriptors0,1,2,……maxfd1-1 to be tested by the select command. Fd_set: This with the three cases i.e., Fd_set *readset Fd_set *writeset Fd_set *exceptset Helps us to test the kernel for reading , writing , and exception.
conditions for a socket to be ready for reading and writing condition Exception ? Readable ? Writable ? Data bytes in socket receive buffer> low-water mark yes Read half of the connection closed yes New connection ready for listening yes Data bytes in socket send buffer >= low-water mark yes Write half of the connection closed yes Socket error is pending yes yes TCP out – of – band data yes
For ( ; ; ) { select(maxfd1 , &rset , NULL , NULL , NULL); If(FD_ISSET(sockfd , &rset)) { If(readline(sockfd , recvline , MAXLINE)= = 0) err_quit(“servet terminate”) Fputs (recvline , stdout); } if(DF_ISSET(fileno(fp), &rset)) { if(Fgets(sendline , MAXLINE , fp) = = NULL) return; write(sockfd , sendline ,strlen (sendline)); } }
When select statement is used in the program When connection is released at this point of time the all the data grams at that Time in the network will be lost so we have to introduce a Shout down function which Will terminate the connection in a proper manner
calling shut down to close the connection data data S E R V E R FIN C L I E N T FIN - ACC data data FIN FIN -ACC
Syntax for shutdownfunction Int shutdown( int sockfd , int howto ); Returns 0 if OK Returns -1 on error It is found in the header file of <socket.h> the function of shutdown depends on the howto argument. SHUT_RD The read half of the connection is closed. SHUT_WR The write half the connection is closed. SHUT_RDWR The both half of the connection are closed.
If(FD_ISSET(sockfd , &rset)) { If(readline(sockfd , recvline , MAXLINE)= = 0) { If ( stdinof = = 1) return; else err_quit(“servet terminate”) ; } Fputs (recvline , stdout); } if(DF_ISSET(fileno(fp), &rset)) { if(Fgets(sendline , MAXLINE , fp) = = NULL) { stdinof = 1; shutdown ( sockfd , SHUT_WR); FD_CLR ( fileno(fp) , &rset); Continue; } write(sockfd , sendline ,strlen (sendline)); }
pselect Pselect function: syntax: #include<sys/time.h> #include<signal.h> #include<time.h> int pselect (int maxfdp1, fd_set *readset, fd_set writeset, Fd_set *exceptset, const struct timespec *timeout, const sigset_t *sigmask); Returns: count of ready descriptors if OK. 0 on time out. -1 on error. Timespec gives the time in seconds and nanoseconds. Sigmask allows the program to disable the delivery of certain signals.
poll function Syntax: #include<poll.h> int poll(struct pollfd *fdarray , unsigned long nfds, int timeout); Returns : count of the ready descriptors of OK 0 on time out -1 on error.
Arguments to poll: The first argument is a pointer to the first element of an array of structures. Each element of the array is a pollfd structure.this specifies the conditions to be tested. struct pollfd { int fd; short event; short revent; }; Time out is: INFTIM: wait for ever. 0 return immediately. -1 error.