tqsheng

go.....
随笔 - 366, 文章 - 18, 评论 - 101, 引用 - 0
数据加载中……

epoll两种机制的区别

7、epoll两种机制的区别

说明:本文来自翻译epoll man文档。

1、ETLT这两种事件分发机制的不同。我们假定一个环境:

   1. The file descriptor that represents the read side of a pipe ( RFD ) is added inside the epoll device.

   2. Pipe writer writes 2Kb of data on the write side of the pipe.

   3. A call to epoll_wait(2) is done that will return RFD as ready file descriptor.

   4. The pipe reader reads 1Kb of data from RFD.

   5. A call to epoll_wait(2) is done.

Edge Triggered 工作模式:

    如果我们在第1步将RFD添加到epoll描述符的时候使用了EPOLLET标志,那么在第5步调用epoll_wait(2)之后将有可能会挂起因为剩余的数据还存在于文件的输入缓冲区内而且数据发出端还在等待一个针对已经发出数据的反馈信息。只有在监视的文件句柄上发生了某个事件的时候ET工作模式才会汇报事件Edge Triggered event distribution delivers events。因此在第5步的时候调用者可能会放弃等待仍在存在于文件输入缓冲区内的剩余数据the caller might end up waiting for some data that is already present inside the input buffer.。在上面的例子中会有一个事件产生在RFD句柄上,是因为在第2步执行了一个写操作然后事件将会在第3步被销毁consumed。因为第4步的读取操作没有读空文件输入缓冲区内的数据因此我们在第5步调用 epoll_wait(2)完成后might lock indefinitely。epoll工作在ET模式的时候必须使用非阻塞套接口以避免由于一个文件句柄的阻塞读/阻塞写操作把处理多个文件描述符的任务饿死。最好以下面的方式调用ET模式的epoll接口在后面会介绍避免可能的缺陷。

    i 基于非阻塞文件句柄

    ii 只有当read(2)或者write(2)返回EAGAIN时才需要挂起等待(意为此时,缓存区满或无数据)。

Level Triggered 工作模式

相反的以LT方式调用epoll接口的时候,它就相当于一个速度比较快的poll,and can be used wherever the latter is used since it shares the same semantics。因为即使使用ET模式的epoll在收到多个chunk的数据的时候仍然会产生多个事件Since even with the Edge Triggered epoll multiple events can be generated up on receival of multiple chunks of data。the caller has the option to specify the EPOLLONESHOT flag, to tell epoll to disable the associated file descriptor after the receival of an event with epoll_wait(2). When the EPOLLONESHOT flag is specified, it is caller responsibility to rearm(重新设置) the file descriptor using epoll_ctl(2) with EPOLL_CTL_MOD.

2、While the usage of epoll when employed(使用) like a Level Triggered interface does have the same semantics of poll(2), an Edge Triggered usage requires more clarification(澄清) to avoid stalls(拖延) in the application event loop.

In this example, listener is a non-blocking socket on which listen(2) has been called. The function do_use_fd() uses the new ready file descriptor until EAGAIN is returned by either read(2) or write(2). An event driven state machine(事件驱动状态机) application should, after having received EAGAIN, record its current state so that at the next call to do_use_fd() it will continue to read(2) or write(2) from where it stopped before.

示例代码

View Code 

struct epoll_event ev, *events;
for(;;) {
    nfds 
= epoll_wait(kdpfd, events, maxevents, -1);
    
for(n = 0; n < nfds; ++n) {
        
if(events[n].data.fd == listener) {
            client 
= accept(listener, (struct sockaddr *&local,
                            
&addrlen);
            
if(client < 0){
                perror(
"accept");
                
continue;
            }

            setnonblocking(client);
            ev.events 
= EPOLLIN | EPOLLET;
            ev.data.fd 
= client;
            
if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, client, &ev) < 0{
                fprintf(stderr, 
"epoll set insertion error: fd=%d0,
                        client);
                
return -1;
            }

        }

        
else
            do_use_fd(events[n].data.fd);
    }

}


When used as an Edge triggered interface, for performance reasons, it is possible to add the file descriptor inside the epoll interface ( EPOLL_CTL_ADD ) once by specifying ( EPOLLIN|EPOLLOUT ). This allows you to avoid continuously switching between EPOLLIN and EPOLLOUT calling epoll_ctl(2) with EPOLL_CTL_MOD.



水平触发(LT, Level Triggered),默认方式
支持阻塞/非阻塞socket。
内核通知某fd就绪,如果不对fd操作,内核会继续通知
边缘触发(Edge-Triggered)
只支持非阻塞socket
内核通知某fd就绪,如果不对fd操作,内核不再继续通知

posted on 2012-06-26 14:14 tqsheng 阅读(145) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理