我有两个文件描述符,一个在管道上读取文件描述符,另一个是套接字连接描述符。它们都不是非阻塞的。它们都通过单个 EPOLLIN 事件添加到 epoll 上下文中。最后,我用 timeout = -1 调用 epoll_wait。下面是示例代码。我有两个问题:-
管道和连接描述符是否需要是非阻塞的。这不是边沿触发的。如果是,这是良好做法还是强制性的,如果是强制性的,为什么?
我将超时设置为 -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)