Introduction to poll Functions

Source: Internet
Author: User

Poll () function: This function is provided by some Unix systems to execute functions equivalent to the select () function. The following is the declaration of this function: # include <poll. h> int poll (struct pollfd fds [], nfds_t nfds, int timeout); parameter description: fds: an array of the struct pollfd structure type, it is used to store the Socket descriptor that needs to detect its status. After this function is called, the system does not clear this array, which is convenient to operate. Especially when there are many socket connections, to some extent, the processing efficiency can be improved. This is different from the select () function. After the select () function is called, the select () function clears the socket descriptor set it detects, therefore, the socket descriptor must be added to the collection to be detected before each select () call. Therefore, the select () function is suitable for detecting only one socket descriptor, and poll () letter The number is suitable for a large number of socket descriptors. nfds: nfds_t type parameters are used to mark the total number of struct elements in the array fds; timeout: the time when the poll function calls are blocked, in milliseconds; returned value:> 0: Total number of socket descriptors in the array fds that have the read, write, or error status ready; = 0: no socket descriptor in the array fds is ready for read or write, or an error occurs. At this time, poll times out and the timeout time is timeout millisecond. In other words, if no event occurs on the detected socket descriptor, poll () the function will block timeout returned after the specified length of milliseconds. If timeout = 0, then the poll () function returns immediately without blocking. If timeout = INFTIM, then poll () the function will continue to block until the events of interest on the detected socket descriptor are returned. If the events of interest never occur, then poll () -1: the poll function fails to be called. The global variable errno is automatically set. The poll implementation function poll is similar to the select implementation function, but poll is highly efficient. In the future, pollpoll () will be used to receive a pointer pointing to the 'struct pollfs' list of the structure, this includes the file descriptors and events you want to test. The event is determined by the bit mask of the event domain in the structure. The current structure will be filled in after the call and returned after the event occurs. The "poll. h" file in SVR4 (possibly earlier versions) contains macro definitions used to determine events. The wait time of the event is accurate to milliseconds (but it is confusing that the wait time type is int). When the wait time is 0, the poll () function returns immediately, -1 indicates that poll () is suspended until a specified event occurs. The following is the structure of pollfd. Struct pollfd {int fd;/* file descriptor */short events;/* Waiting event */short revents;/* actually occurred event */}; and select () very similar. When a positive value is returned, it indicates the number of file descriptors meeting the RESPONSE event. If 0 is returned, it indicates that no event has occurred within the specified time. If the returned result is negative, check errno immediately because an error occurs. If no event occurs, the revents will be cleared. [Csharp] # include <fcntl. h> # include <stdio. h> # include <unistd. h> # include <stdlib. h> # include <string. h> # include <time. h> # include <errno. h> # include <poll. h> # define MAX_BUFFER_SIZE 1024 # define IN_FILES 3 # define TIME_DELAY 60*5 # define MAX (a, B) (a> B )? (A) :( B) int main (int argc, char ** argv) {struct pollfd fds [IN_FILES]; char buf [MAX_BUFFER_SIZE]; int I, res, real_read, maxfd; fds [0]. fd = 0; if (fds [1]. fd = open ("data1", O_RDONLY | O_NONBLOCK) <0) {fprintf (stderr, "open data1 error: % s", strerror (errno); return 1 ;} if (fds [2]. fd = open ("data2", O_RDONLY | O_NONBLOCK) <0) {fprintf (stderr, "open data2 error: % s", strerror (errno); return 1 ;} for (I = 0; I <IN_FILES; I ++) {fds [I]. events = POLLIN;} while (fds [0]. events | fds [1]. events | fds [2]. events) {www.2cto. comif (poll (fds, IN_FILES, TIME_DELAY) <= 0) {printf ("Poll error \ n"); return 1 ;}for (I = 0; I <IN_FILES; I ++) {if (fds [I]. revents) {memset (buf, 0, MAX_BUFFER_SIZE); real_read = read (fds [I]. fd, buf, MAX_BUFFER_SIZE); if (real_read <0) {if (errno! = EAGAIN) {return 1 ;}} else if (! Real_read) {close (fds [I]. fd); fds [I]. events = 0;} else {if (I = 0) {if (buf [0] = 'q ') | (buf [0] = 'q') {return 1 ;}} else {buf [real_read] = '\ 0'; printf ("% s ", buf) ;}}}}exit (0 );}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.