1. Some disadvantages of select
Recall the Select interface
int Select (int Nfds, fd_set *readfds, fd_ Set *writefds, fd_set *exceptfds, struct Timeval *timeout);
Select requires us to specify the maximum value of the file descriptor, and then take [0,nfds] values in this range to view it in the collection Readfds,writefds or Execptfds, which means there are some file descriptors that are not of interest to us, The CPU did a little work, poll improved on her, and see how poll did it.
2. Poll Interface
#include <poll.h>
int poll (struct POLLFD *fds, nfds_t nfds, int timeout);
Unlike Select, poll no longer informs the kernel of a range, but rather tells the kernel user exactly what file descriptors (streams) are being cared for by the struct POLLFD array. The parameter Nfds indicates the size of the struct array. Timeout indicates the programmer's endurance, there are three kinds of values:
- 0,poll function does not block
- Integer, blocking timeout time
- Negative, infinite blocking
Here's a look at the struct POLLFD struct, and what's the value of the event, and what it means
struct POLLFD {
int FD; /* An Open File file descriptor */
Short events; /* Requested events */
Short revents; /* returned events */
};
- The FD attribute represents an open file descriptor
- The Events property is an input parameter that describes the event (read, write) that the program is interested in by a bit mask
- The Revents property is an outgoing parameter that describes the event that occurs in the same way as a bit mask, and the value of this property is set by the kernel. The value of revents may be the value of the events property, or it may be one or more of the pollerr,pollhup,pollnval, Pollerr,pollhup,pollnval is meaningless in the events property.
The values that events and revents can set are defined in the <poll.h> header, and there are several possible
- Pollin, read events
- POLLPRI, read events, but represent emergency data, such as out-of-band data for TCP sockets
- Pollrdnorm, read event, indicates that there is plain data readable
- Pollrdband, reading events, indicating that there are priority data readable
- Pollout, write events
- Pollwrnorm, write events, indicate that there is plain data to write
- Pollwrband, write events, indicate that there are priority data to write
- Pollrdhup (since Linux 2.6.17), one end of thestream socket closes the connection (note that the stream socket, we know there is a raw socket,dgram socket), Or the write end closes the connection, and if you want to use this event, you must define the _gnu_source macro. This event can be used to determine if the link has an exception (the more general approach, of course, is to use the heartbeat mechanism). To use this event, you have to include the header file:
#define _gnu_source
#include <poll.h>
- Pollerr, only for kernel settings outgoing parameter revents, indicates device error
- Pollhup, only for the kernel settings outgoing parameter revents, indicating that the device is suspended, if the poll listening FD is a socket, indicating that the socket does not establish a connection on the network, for example, only call the socket () function, but does not connect.
- Pollnval, only for kernel settings outgoing parameter revents, indicates illegal request file descriptor FD not open
Poll function return value, there are three kinds of possible
- Positive number, which indicates how many revents in a struct POLLFD struct array are not 0, in other words, which events are called poll this time.
- 0, which means timeout is up, and no file descriptor is ready.
- -1, internal error has occurred, errno will be set
When the return value of poll is-1, it means poll error, errno will be set, errno's value is 4 possible
- Efault, the parameter struct POLLFD struct array is not in the user address space, such as the incoming parameter Nfds larger than the actual array.
- Eintr, interrupted by signal.
- EINVAL, Nfds exceeds the Rlimit_nofile value.
- Enomem, the kernel does not have enough memory to load the struct POLLFD struct array
3. Comparison of poll and select parameters
Using poll () and select () are different, you do not need to explicitly request an exception condition report.
Pollin | Pollpri equivalent to the Read event of select (), Pollout | Pollwrband is equivalent to the Write event of select (). Pollin equivalent to Pollrdnorm | Pollrdband, while pollout is equivalent to Pollwrnorm.
4, poll principle
The function of the poll is the same as the function of select, except that the parameters are slightly different, the underlying principle of poll is similar to select, not much, I/O multiplexing Select
Poll for I/O multiplexing