【发布时间】:2012-12-24 03:55:16
【问题描述】:
我有两个文件描述符,一个在管道上读取文件描述符,另一个是套接字连接描述符。它们都不是非阻塞的。它们都通过单个 EPOLLIN 事件添加到 epoll 上下文中。最后,我用 timeout = -1 调用 epoll_wait。下面是示例代码。我有 两个问题:-
管道和连接描述符是否需要非阻塞。这不是边沿触发的。如果是,这是良好做法还是强制性的,如果是强制性的,为什么?
-
我将 timeout 设置为 -1,但 epoll_wait 立即返回,返回值为 0。为什么会这样?超时时间为 -1,epoll_wait 应该仅在有事件时返回。
int total_fd_ready = -1; struct epoll_event pipe_event, connection_event, total_events[64]; pipe_event.data.fd = pipe_fd[0]; piple_event.events = EPOLLIN; connection_event.data.fd = conn_fd; connection_event.events = EPOLLIN; total_fd_ready = Epoll_wait(epoll_fd, total_events, 64, -1); printf("%d\n", total_fd_ready);epoll_wait 定义为
int Epoll_wait(int e_fd, struct epoll_event *events, int max_events, int timeout) { #ifdef DBG printf("Epoll_wait called on epoll_fd: %d with max_events: %d and timeout: %d\n", e_fd, max_events, timeout); #endif int result = -1; if(result = (epoll_wait(e_fd, events, max_events, timeout)) < 0) if(errno != EINTR) err_sys("epoll_wait error with epoll fd: %d and timeout : %d\n", e_fd, timeout); #ifdef DBG else printf("epoll_wait was interrupted\n"); #endif return result; }
更新: 发现了问题,虽然我无法解释为什么结果设置为 0。我需要在下面的 if 语句中使用括号
if((result = (epoll_wait(e_fd, events, max_events, timeout))) < 0)
【问题讨论】:
-
如何声明和初始化
total_events? -
用它更新了代码
-
如果编译时启用了警告,例如使用
gcc -Wall,编译器会适当地警告你