1 / 21

I/O multiplexing , select and poll function

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

courtney
Download Presentation

I/O multiplexing , select and poll function

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 , select and poll function

  2. 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

  3. Blocked I/O kernel Application recvfrom No data gram Data gram ready Copy data gram Process data gram Copy complete

  4. 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

  5. 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

  6. 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

  7. Asynchronous I/O kernel Application Aio_read No data gram Data gram ready Copy data gram Deliver signal Process data gram Copy complete

  8. 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

  9. 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.

  10. 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.

  11. 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

  12. When select function is not used

  13. 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)); } }

  14. 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

  15. 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

  16. 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.

  17. 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)); }

  18. 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.

  19. 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.

  20. 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.

More Related