1. Basic knowledge
The poll mechanism is similar to select in that there is no significant difference in nature with Select, that managing multiple descriptors is also polling and processing according to the state of the descriptor, but poll has no limit on the maximum number of file descriptors. The disadvantage of poll and select is that an array containing a large number of file descriptors is copied in between the user state and the kernel's address space, regardless of whether the file descriptor is ready, and its overhead increases linearly as the number of file descriptors increases.
2. Poll function
The function format is as follows:
# include <poll.h>
int poll (struct POLLFD * FDS, unsigned int nfds, int timeout);
The POLLFD structure is defined as follows:
struct POLLFD {
int fd; /* File descriptor * /
Short events; /* Events Waiting for * /
Short revents; /* Events that have actually occurred * /
} ;
Each pollfd struct specifies a monitored file descriptor that can pass multiple structures, indicating that poll () monitors multiple file descriptors. The events field for each struct is the event mask that monitors the file descriptor, which is set by the user. The Revents field is the action result event mask of the file descriptor, which is set by the kernel when the call returns. Any events requested in the events domain may be returned in the revents domain. The legitimate events are as follows:
The Pollin has data to read.
Pollrdnorm have normal data to read.
Pollrdband has priority data to read.
POLLPRI has urgent data to read.
Pollout write data does not cause blocking.
Pollwrnorm writing normal data does not cause blocking.
Pollwrband Write priority data does not cause blocking.
Pollmsgsigpoll messages are available.
Additionally, the following events may be returned in the revents domain:
Poller An error occurred with the specified file descriptor.
Pollhup the specified file descriptor pending event.
Pollnval The specified file descriptor is illegal.
These events do not make sense in the events field, because they are always returned from revents at the appropriate time. the events to be monitored are specified by the events member, and the function returns the state of the descriptor in the corresponding Revents member (each file descriptor has two events, one is an incoming event, and one is an outgoing revents, thus avoiding the use of the passed-in parameters. Note the difference from select, which tells the application what events actually occurred on the FD. Events and revents can be bitwise OR of multiple events.
The timeout parameter specifies the number of milliseconds to wait, and poll returns regardless of whether I/O is ready. Timeout is specified as a negative value to indicate an infinite timeout, so that poll () hangs until a specified event occurs, and a timeout of 0 indicates that the poll call immediately returns and lists the file descriptor ready for I/O, but does not wait for other events. In this case, poll (), like its name, returns as soon as it is elected.
Return values and error codes
On success, poll () returns the number of file descriptors that are not 0 in the Revents field in the struct, and if no events occur before the timeout, poll () returns 0; when failed, poll () returns-1, and sets errno to one of the following values:
EBADF the file descriptor specified in one or more structs is invalid.
The address that the Efaultfds pointer points to exceeds the address space of the process.
Eintr the requested event before generating a signal that the call can be re-initiated.
The Einvalnfds parameter exceeds the Plimit_nofile value.
Enomem There is not enough memory available to complete the request.
3, enter data and echo through poll to stdin, timeout set to 5000ms
Code:
#include <stdio.h> #include <poll.h>int main () {struct POLLFD fd_ev[1];fd_ev[0].fd = 0;fd_ev[0].events = Pollin;fd_ev[0].revents = 0;int done = 0;while (!done) {int Timeout =5000;switch (poll (Fd_ev, 1, timeout)) {Case-1:perror (" Poll "); Break;case 0:printf (" timeout...\n "); Break;default:{if (Fd_ev[0].revents & Pollin) {char buf[1024]; ssize_t _s = Read (FD_EV[0].FD, buf, sizeof (BUF)-1), if (_s > 0) {buf[_s-1] = ' ';p rintf ("Echo:%s\n", buf);}} Break;}}}
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/85/46/wKioL1ee8M3D9RrSAAA_vPkN_Y0554.png "title=" screen Shot 2016-08-01 at 14.46.08.png "alt=" Wkiol1ee8m3d9rrsaaa_vpkn_y0554.png "/>
4,the characteristics of poll
a) poll the file descriptor and event binding, and the event can be specified individually, and can be a bitwise OR of multiple events, thus finer the registration of the event, and poll a single element to hold the result when the return is ready, so that the next time you call poll, There is no need to reset previously registered events;
b) poll uses a set of polling for all registered file descriptors, which returns the entire user-registered collection of events, so the time complexity of the application index-ready file is O (n).
c) poll uses the Nfds parameter to specify the maximum number of file descriptors and events to listen to, which can reach the maximum file descriptor that the system allows to open, which is 65535.
d) can only work in the LT mode.
5, poll
1) Poll, unlike Select, passes a POLLFD array to the kernel to pass events that need attention, so there is no limit to the number of descriptors, and the events field and revents in POLLFD are used to indicate the event of concern and the event that occurs. Therefore, the POLLFD array needs to be initialized only once.
2) The implementation mechanism of poll is similar to select, which corresponds to the sys_poll in the kernel, except that poll passes the POLLFD array to the kernel, then POLLFD each descriptor in poll, which is more efficient than fdset.
6, poll
1) Although FD has no limitations,
2) Every call to poll, there is a user-to-kernel copy, a large overhead
3) Every call to poll is polled to detect if the revents of FD in the events array is the same as its events, with a large overhead
This article from "Momo is spicy moe" blog, please be sure to keep this source http://momo462.blog.51cto.com/10138434/1832998
Multiplexing--poll