The poll function provides similar functionality to the select function, but it can provide additional information when processing stream devices. Poll (pollfd * fdarray, unsigned nfds, -- timeout, --- ErrorFd; events; revents;
The condition to be tested is specified by the events member. The function returns the status of the descriptive word in the corresponding revents member. (Each descriptive word has two variables: one is the call value and the other is the return result, thus avoiding using the value-result parameter .) Each of these two members is composed of one or more digits that specify a specific condition. Lists some common values used to specify the events flag and test the revents flag.
We divide the graph into three parts: The first part is to process the four constant values of the input, and the second part is to process the three common values of the output, the third part is the three common values for error handling. The three common values in the third part cannot be set in events, but when the corresponding condition exists, it is returned in revents.
Poll identifies three types of data: normal, priority band, and high priority ). These terms all come from stream implementation.
For TCP and UDP interfaces, the following conditions cause poll to return a specific revent. Unfortunately, POSIX leaves many holes in its poll definition (that is, there are multiple methods that can return the same condition ).
(1) All formal TCP data and all UDP data are considered as common data.
(2) out-of-band data of TCP is considered as priority data.
(3) When the read half of the TCP connection is closed (for example, a FIN from the peer side is received), it is also considered as normal data, and then the read operation will return 0.
(4) errors in the TCP connection can be considered both common data and POLLERR. In either case,-1 will be returned for subsequent read operations, set errno to an appropriate value. This can be used to process conditions such as receiving RST or timeout.
(5) There is a new connection available on the listener interface, which can be considered as common data or priority data. Most implementations are regarded as common data.
(6) the completion of non-blocking connect is considered to make the corresponding interface writable.
The number of elements in the structure array is specified by the nfds parameter..
The timeout parameter specifies the waiting time before the poll function returns. It is a positive value that specifies the number of milliseconds to wait. The possible values are given.
The INFTIM constant value is defined as a negative value. If the system does not provide a millisecond-level precision timer, this value is rounded up to the nearest supported value.
Poll function return value
When an error occurs, the return value of the poll function is-1. If no descriptive word is ready before the timer time ends, 0 is returned; otherwise, the number of ready descriptive words is returned, that is, the number of descriptive words whose revents member value is not 0.
If we no longer care about a specific descriptive word, we can set the fd Member of the pollfd structure corresponding to it to a negative value. The poll function ignores the events members of such a poll structure and sets the value of its revents member to 0 when returning the result.