epoll返回0事件

时间:2012-12-24 03:55:16

标签: c linux

我有两个文件描述符,一个读取管道上的文件描述符和另一个套接字连接描述符。它们都没有阻挡。它们都通过单个EPOLLIN事件添加到epoll上下文中。最后,我使用timeout = -1调用epoll_wait。下面是示例代码。我有 两个问题: -

  1. 管道和连接描述符是否需要非阻塞。这不是边缘触发的。如果是,是好的做法还是强制性的,如果是强制性的,为什么?

  2. 我将超时设置为-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;
    }
    
  3. 更新: 发现了这个问题,虽然我无法解释为什么结果设置为0.我需要在下面的if语句

    中使用
     if((result = (epoll_wait(e_fd, events, max_events, timeout))) < 0)
    

1 个答案:

答案 0 :(得分:4)

答案是比较运算符<的优先级高于赋值,这意味着result被赋予表达式(epoll_wait(e_fd, events, max_events, timeout)) < 0的结果。