nfds should be set to the highest-numbered file descriptor in any of the three sets, plus 1. The indicated file descriptors in each set are checked, up to this limit (but see BUGS).
Three independent sets of file descriptors are watched.
The file descriptors listed in readfds will be watched to see if characters become available for reading (more precisely, to see if a read will not block; in particular, a file descriptor is also ready on end-of-file).
The file descriptors in writefds will be watched to see if space is available for write (though a large write may still block).
The file descriptors in exceptfds will be watched for exceptional conditions. (For examples of some exceptional conditions, see the discussion of POLLPRI in poll(2).)
The time structures involved are defined in <sys/time.h> and look like
1 2 3 4
structtimeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ };
and
1 2 3 4
structtimespec { long tv_sec; /* seconds */ long tv_nsec; /* nanoseconds */ };
intpoll(struct pollfd *fds, nfds_t nfds, int timeout);
structpollfd { int fd; /* file descriptor */ short events; /* requested events */ short revents; /* returned events */ };
采用数组指针+长度的参数形式
返回值
1 2 3 4 5
On success, a positive number is returned; this is the number of structures which have nonzero revents fields (in other words, those descriptors with events or errors reported). A value of 0 indicates that the call timed out and no file descriptors were ready. On error, -1 is returned, and errno is set appropriately.
水平触发
其和select不同的地方:采用数组的方式替换原有fd_set数据结构,而使其没有连接数的限制。
虽然也是轮询,但是假设是单个fd,但fd的值很大的情况下,poll就会比select效率好
上面看到了只需要一次初始化,和恢复已经就绪的fd,不需要每次初始化
可移植性:select( ) is more portable, as some Unix systems do not support poll( )
epoll
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
intepoll_create(int size); // Since Linux 2.6.8, the size argument is ignored, but must be greater than zero; intepoll_ctl(int epfd, int op, int fd, struct epoll_event *event); intepoll_wait(int epfd, struct epoll_event *events,int maxevents, int timeout);