简体   繁体   English

在信号和epoll_wait()上中断accept()

[英]accept() interrupted on a signal and epoll_wait()

If I am epoll_wait()ing on a listening socket and, when epoll_wait() returns indicating it has activity (in this case, a connection waiting to be accept()ed), then if the accept() call fails with errno=EINTR, will epoll_wait() indicate that the same connection is waiting on the listening socket the next time it returns? 如果我正在侦听套接字上使用epoll_wait(),并且当epoll_wait()返回指示它具有活动性(在这种情况下,连接等待被接受)时,如果accept()调用失败并出现errno = EINTR ,epoll_wait()会指示下一次返回时,同一连接正在侦听套接字上等待吗?

ie, will I need to do something along the lines of: 即,我是否需要做以下事情:

while(1){
    epoll_wait(epfd, &events, maxevents, timeout);
    if (events.data.fd == listener){
        connsock = accept(listener, &addr, &addrlen);
        while (connsock != -1){
            if (errno == EINTR){
                accept(listener, &addr, &addrlen);
                }
            }
        }
    }

in order to make sure that the connection gets accepted, or will this work and still ensure that the connection for which accept() was interrupted by a signal gets accepted: 为了确保该连接被接受,或者将确保该连接正常工作,并且仍然确保接受被信号中断的连接:

while(1){
    epoll_wait(epfd, &events, maxevents, timeout);
    if (events.data.fd == listener){
        connsock = accept(listener, &addr, &addrlen);
        }
    }

where in this case if accept() is interrupted by a signal it'll just pick up the same connection the next time through the loop after epoll_wait returns again. 在这种情况下,如果accept()被信号中断,它将在epoll_wait再次返回之后,在下一次通过循环时拾取相同的连接。

Obviously, in both these examples I'm making some assumptions (that only one event on one socket is returned in a given call to epoll_wait, for instance) and leaving out error checking (except for EINTR on accept(), since it's the whole point here) to simplify things 显然,在这两个示例中,我都作了一些假设(例如,在给定调用epoll_wait的情况下,仅在一个套接字上返回了一个事件),并省略了错误检查(除了accept()上的EINTR,因为它是整个过程)指向此处)以简化操作

This is the difference between edge triggering and level triggering. 这是边沿触发和电平触发之间的区别。 Use level triggering, the default, and you don't have to worry about it. 使用级别触发(默认设置),您不必担心。

The tradeoff with level triggering is that you can't have one thread handle the detected event while another thread goes back to call epoll_wait -- it would just detect the same event again. 使用级别触发的权衡是,您无法让一个线程处理检测到的事件,而另一个线程又返回调用epoll_wait -它只会再次检测到同一事件。 But in most cases, you don't need to do this anyway, and the tradeoff of it being impossible to lose an event is worth it. 但是在大多数情况下,无论如何您都不需要这样做,并且在不可能丢失事件的情况下进行权衡是值得的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM