简体   繁体   中英

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?

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.

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

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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