epoll with Edge Triggered event

The man page of epoll :

http://linux.die.net/man/7/epoll

have a sample code for Edge Triggered like the follwoing :

for (;;) {
    nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
    if (nfds == -1) {
        perror("epoll_pwait");
        exit(EXIT_FAILURE);
    }

    for (n = 0; n < nfds; ++n) {
        if (events[n].data.fd == listen_sock) {
            conn_sock = accept(listen_sock,
                        (struct sockaddr *) &local, &addrlen);
            if (conn_sock == -1) {
                perror("accept");
                exit(EXIT_FAILURE);
            }
            setnonblocking(conn_sock);
            ev.events = EPOLLIN | EPOLLET;
            ev.data.fd = conn_sock;
            if (epoll_ctl(epollfd, EPOLL_CTL_ADD, conn_sock,
                    &ev) == -1) {
                perror("epoll_ctl: conn_sock");
                exit(EXIT_FAILURE);
            }
        } else {
            do_use_fd(events[n].data.fd);
        }
    }
}

In function do_use_fd , i call nonblocked recv in while loop until EAGAIN ,the sample code works fine , I have a question about this sample code, suppose now I have 50 socket clients connections , suddenly 10 clients writes data at the same time, so epoll_wait() will return 10 and then go to for loop :

for (n = 0; n < nfds; ++n) 

it will call do_use_fd(events[n].data.fd); for those 10 clients , suppose n=5 is done , and n=6 is not yet finished , suddenly the file description of event n= 3 has receive new data , after all of those 10 events are done and back to epoll_wait , will I get the event inform me that there is a client has new data to read ? or I will miss it because when event happened , the code not in epoll_wait !!


As long as you read until you get an EAGAIN error, you will get the event the next time you are calling epoll_wait .

The event is only triggered when there is a change between empty and non-empty (or full and non-full for EPOLLOUT ), but that status then remains until the event is delivered via epoll_wait .

On a somewhat related note: if you register for EPOLLIN and EPOLLOUT events and assuming you never fill up the send buffer, you still get the EPOLLOUT flag set in the event returned by epoll_wait each time EPOLLIN is triggered - see https://lkml.org/lkml/2011/11/17/234 for a more detailed explanation.

And finally, the exact behaviour of edge-triggered mode actually depends on the socket type used and isn't really documented anywhere. I did some tests some time ago and documented my findings here: http://cmeerw.org/blog/753.html#753 - in short, for datagram sockets you might get more events than you would expect.


当你使用epoll下的边缘触发时,读一些东西,可以这样

int n = -1;
while (1)
{
    n = recv(fd, iobuf, init_buff_size, MSG_DONTWAIT);
    if (n > 0)
    {
        LOG(glogfd, LOG_TRACE, "fd[%d] recv len %dn", fd, n);
        mybuff_setdata(&(curcon->recv_buff), iobuf, n); // this is my func
        if (n == init_buff_size)
        {
            LOG(glogfd, LOG_DEBUG, "fd[%d] need recv nextloop %dn", fd, n);
            continue;
        }
        break;
    }
    if (n == 0)
    {
        LOG(glogfd, LOG_ERROR, "fd[%d] close %s:%d!n", fd, ID, LN);
        return do_close(fd);
    }
    if (errno == EINTR)
    {
        LOG(glogfd, LOG_TRACE, "fd[%d] need recv again!n", fd);
        continue;
    }
    else if (errno == EAGAIN)
    {
        LOG(glogfd, LOG_TRACE, "fd[%d] need recv next!n", fd);
        modify_fd_event(fd, EPOLLIN);   // this is the KEY, add read again
        break;
    }
    else
    {
        LOG(glogfd, LOG_ERROR, "fd[%d] close %s:%d!n", fd, ID, LN);
        return do_close(fd);
    }
}
链接地址: http://www.djcxy.com/p/61346.html

上一篇: 多线程epoll

下一篇: 带边缘触发事件的epoll