内容简介
#include <poll.h> int poll(struct pollfd *fds, nfds_t%uA0nfds, int%uA0timeout) #define _GNU_SOURCE%uA0#include <poll.h> int ppoll(struct pollfd *fds, nfds_t%uA0nfds,%uA0const struct timespec *timeout, const sigset_t *sigmask) |
描述
poll() performs a similar task to%uA0select(2): it waits for one of a set of file descriptors to become ready to perform I/O.The set of file descriptors to be monitored is specified in the%uA0fds%uA0argument, which is an array of%uA0nfds%uA0structures of the following form:
struct pollfd { int%uA0fd /* file descriptor */ short events /* requested events */ short revents /* returned events */ } |
The field%uA0events%uA0is an input parameter, a bitmask specifying the events the application is interested in.
The field%uA0revents%uA0is an output parameter, filled by the kernel with the events that actually occurred. The bits returned in%uA0reventsꃊn include any of those specified inevents, or one of the values%uA0POLLERR,%uA0POLLHUP, or%uA0POLLNVAL. (These three bits are meaningless in the%uA0events%uA0field, and will be set in the%uA0revents%uA0field whenever the corresponding condition is true.)
If none of the events requested (and no error) has occurred for any of the file descriptors, then%uA0poll() blocks until one of the events occurs.
The%uA0timeout%uA0argument specifies an upper limit on the time for which%uA0poll() will block, in milliseconds. Specifying a negative value in%uA0timeout%uA0means an infinite timeout.
The bits that may be set/returned in%uA0events%uA0and%uA0revents%uA0are defined in <poll.h>:
标签 | 描述 |
---|---|
POLLIN | There is data to read. |
POLLPRI | |
%uA0 | There is urgent data to read (e.g., out-of-band data on TCP socket pseudo-terminal master in packet mode has seen state change in slave). |
POLLOUT | |
%uA0 | Writing now will not block. |
POLLRDHUP%uA0(since Linux 2.6.17) | |
%uA0 | Stream socket peer closed connection, or shut down writing half of connection. The%uA0_GNU_SOURCEꃾature test macro must be defined in order to obtain this definition. |
POLLERR | |
%uA0 | Error condition (output only). |
POLLHUP | |
%uA0 | Hang up (output only). |
POLLNVAL | |
%uA0 | Invalid request:%uA0fd%uA0not open (output only). |
When compiling with%uA0_XOPEN_SOURCEꃞfined, one also has the following, which convey no further information beyond the bits listed above:
标签 | 描述 |
---|---|
POLLRDNORM | |
%uA0 | Equivalent to%uA0POLLIN. |
POLLRDBAND | |
%uA0 | Priority band data can be read (generally unused on Linux). |
POLLWRNORM | |
%uA0 | Equivalent to%uA0POLLOUT. |
POLLWRBAND | |
%uA0 | Priority data may be written. |
Linux also knows about, but does not use%uA0POLLMSG.
ppoll()
The relationship between%uA0poll() and%uA0ppoll() is analogous to the relationship betweenselect() and%uA0pselect(): like%uA0pselect(),%uA0ppoll() allows an application to safely wait until either a file descriptor becomes ready or until a signal is caught.Other than the difference in the%uA0timeout%uA0argument, the following%uA0ppoll() call:
ready = ppoll(&fds, nfds, timeout, &sigmask)
|
sigset_t origmask sigprocmask(SIG_SETMASK, &sigmask, &origmask) ready = ppoll(&fds, nfds, timeout) sigprocmask(SIG_SETMASK, &origmask, NULL) |
See the description of%uA0pselect(2) for an explanation of why%uA0ppoll() is necessary.
The%uA0timeout%uA0argument specifies an upper limit on the amount of time that%uA0ppoll() will block. This argument is a pointer to a structure of the following form:
struct timespec {
long tv_sec /* seconds */
long tv_nsec /* nanoseconds */
}
|
If%uA0timeout%uA0is specified as NULL, then%uA0ppoll() can block indefinitely.
返回值
On success, a positive number is returned this is the number of structures which have non-zero%uA0revents%uA0fields (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%uA0errno%uA0is set appropriately.错误
标签 | 描述 |
---|---|
EBADF | An invalid file descriptor was given in one of the sets. |
EFAULT | The array given as argument was not contained in the calling program’s address space. |
EINTR | A signal occurred before any requested event. |
EINVAL | The%uA0nfds%uA0value exceeds the RLIMIT_NOFILE value. |
ENOMEM | There was no space to allocate file descriptor tables. |
LINUX 注意
The Linux%uA0ppoll() system call modifies its%uA0timeout%uA0argument. However, the glibc wrapper function hides this behaviour by using a local variable for the timeout argument that is passed to the system call. Thus, the glibc%uA0ppoll() function does not modify its%uA0timeoutargument.BUGS
See the discussion of spurious readiness notifications under the BUGS section ofselect(2).遵循于
poll() conforms to POSIX.1-2001.%uA0ppoll() is Linux specific.版本
The%uA0poll() system call was introduced in Linux 2.1.23. The%uA0poll() library call was introduced in libc 5.4.28 (and provides emulation using select() if your kernel does not have a%uA0poll() system call).The%uA0ppoll() system call was added to Linux in kernel 2.6.16. The%uA0ppoll() library call was added in glibc 2.4.