epoll_wait(2) - Linux 手册页
名称
epoll_wait, epoll_pwait - 等待 epoll 文件描述符上的 I/O 事件
概要
#include <sys/epoll.h> int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout); int epoll_pwait(int epfd, struct epoll_event *events, int maxevents, int timeout, const sigset_t *sigmask);
描述
epoll_wait() 系统调用等待由文件描述符 epfd 引用的 epoll(7) 实例上的事件。由 events 指向的内存区域将包含对调用者可用的事件。epoll_wait() 返回最多 maxevents 个事件。maxevents 参数必须大于零。
timeout 参数指定 epoll_wait() 将阻塞的最小毫秒数。(此间隔将被向上舍入到系统时钟粒度,并且内核调度延迟意味着阻塞间隔可能会超出少量时间。)指定 timeout 为 -1 将导致 epoll_wait() 永久阻塞,而指定 timeout 等于零将导致 epoll_wait() 立即返回,即使没有事件可用。
struct epoll_event 的定义如下:
-
typedef union epoll_data { void *ptr; int fd; uint32_t u32; uint64_t u64; } epoll_data_t; struct epoll_event { uint32_t events; /* Epoll events */ epoll_data_t data; /* User data variable */ }; - 每个返回结构的 data 将包含用户在使用 epoll_ctl(2) (EPOLL_CTL_ADD,EPOLL_CTL_MOD) 时设置的相同数据,而 events 成员将包含返回的事件位域。
epoll_pwait()
- epoll_wait() 和 epoll_pwait() 之间的关系类似于 select(2) 和 pselect(2) 之间的关系:与 pselect(2) 类似,epoll_pwait() 允许应用程序安全地等待,直到文件描述符准备就绪或捕获到信号为止。
以下 epoll_pwait() 调用
ready = epoll_pwait(epfd, &events, maxevents, timeout, &sigmask);
等同于原子性地执行以下调用sigset_t origmask; sigprocmask(SIG_SETMASK, &sigmask, &origmask); ready = epoll_wait(epfd, &events, maxevents, timeout); sigprocmask(SIG_SETMASK, &origmask, NULL);
sigmask 参数可以指定为 NULL,在这种情况下,epoll_pwait() 等效于 epoll_wait()。
返回值
成功时,epoll_wait() 返回准备好请求 I/O 的文件描述符的数量,或者如果在请求的 timeout 毫秒内没有文件描述符准备就绪则返回零。如果发生错误,epoll_wait() 返回 -1 并且 errno 被适当地设置。
错误
- EBADF
epfd 不是有效的文件描述符。
EFAULT
由 events 指向的内存区域没有写入权限。
EINTR
在任何请求的事件发生或 timeout 到期之前,调用被信号处理程序中断;请参阅 signal(7)。
EINVAL
epfd 不是 epoll 文件描述符,或者 maxevents 小于或等于零。
版本
epoll_wait() 被添加到内核的版本 2.6 中。glibc 从版本 2.3.2 开始提供库支持。
epoll_pwait() 被添加到 Linux 内核 2.6.19 中。glibc 从版本 2.6 开始提供库支持。
符合
epoll_wait() 是 Linux 特有的。
说明
当一个线程在 epoll_pwait() 调用中阻塞时,另一个线程可以将文件描述符添加到等待的 epoll 实例中。如果新的文件描述符准备就绪,将导致 epoll_wait() 调用取消阻塞。
对于如果另一个线程关闭由 epoll_wait() 监视的 epoll 实例中的文件描述符可能发生的情况的讨论,请参阅 select(2)。
错误
在内核 2.6.37 之前,大于大约 LONG_MAX / HZ 毫秒的 timeout 值被视为 -1(即无穷大)。因此,例如,在 sizeof(long) 为 4 且内核 HZ 值为 1000 的系统上,这意味着大于 35.79 分钟的超时被视为无穷大。
参见
epoll_create(2), epoll_ctl(2), epoll(7)