epoll的使用边沿触发事件 [英] epoll with Edge Triggered event

查看:136
本文介绍了epoll的使用边沿触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

epoll的的手册页:

The man page of epoll :

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

有样品code为边沿触发,如follwoing:

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);
        }
    }
}

在功能do_use_fd,我称之为while循环,直到EAGAIN nonblocked的recv,样品code正常工作

我有一个关于这个样本code的问题,假设我现在有50座客户端的连接,突然在同一时间10个客户端将数据写入,所以epoll_wait()将返回10,然后去循环:

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) 

它会调用do_use_fd(事件[N] .data.fd);对于那些10个客户端,假设N = 5完成后,和n = 6尚未完成,突然事件的文件说明N = 3具有接收新的数据,毕竟这10事件的完成和回epoll_wait,将我得到事件告诉我,有一个客户端有新的数据读取?或者我会想念它,因为当事件发生时,code不是epoll_wait !!

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

推荐答案

只要你读,直到你得到一个 EAGAIN 错误,将在下一次得到事件您正在呼叫 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.

当有(对 EPOLLOUT 或完整和非完整),但这一地位则一直保持到空和非空之间的变化事件时,才会触发事件正在通过 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.

在一个有点相关的注意事项:如果您注册了 EPOLLIN EPOLLOUT 事件,并假设你永远填补发送缓冲区,你仍然可以获得 EPOLLOUT 标记由 epoll_wait 返回的事件设置每个时间 EPOLLIN 被触发 - 见 https://lkml.org/lkml/2011/11/ 234分之17了更详细的解释。

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.

和最后的边沿触发模式的确切行为实际上取决于所使用的插座的类型和是不是真的记录任何地方。我做了一些测试,前一段时间在这里记录我的发现: http://cmeerw.org/blog/753.html#753 - 总之,对于数据报套接字你可能会比你所期望得到更多的事件。

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的使用边沿触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆