LinuxEpoll函数详解

上传人:平*** 文档编号:12777533 上传时间:2017-10-20 格式:DOC 页数:7 大小:60.50KB
返回 下载 相关 举报
LinuxEpoll函数详解_第1页
第1页 / 共7页
LinuxEpoll函数详解_第2页
第2页 / 共7页
LinuxEpoll函数详解_第3页
第3页 / 共7页
LinuxEpoll函数详解_第4页
第4页 / 共7页
LinuxEpoll函数详解_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《LinuxEpoll函数详解》由会员分享,可在线阅读,更多相关《LinuxEpoll函数详解(7页珍藏版)》请在金锄头文库上搜索。

1、NAMEepoll - I/O event notification facilitySYNOPSIS#include DESCRIPTIONepoll is a variant of poll(2) that can be used either as Edge or Level Triggered interface and scales well to large numbers of watched fds. Three system calls are provided to set up and control an epoll set: epoll_create(2), epol

2、l_ctl(2), epoll_wait(2).epoll 是 poll 的一种变体,可以按照 Level 与 Edge 两种方式触发,并且能够很好支持监视大量的 fds。An epoll set is connected to a file descriptor created by epoll_create(2). Interest for certain file descriptors is then registered via epoll_ctl(2). Finally, the actual wait is started by epoll_wait(2).一个 epoll 是关

3、联到由 epoll_create 创建的文件句柄。感兴趣的文件句柄随后被通过 epoll_ctl 注册,最后通过 epoll_wait 进行事实上的等待。NOTESThe epoll event distribution interface is able to behave both as Edge Triggered ( ET ) and Level Triggered ( LT). The difference between ET and LT event distribution mechanism can be described as follows. Suppose that

4、this scenario happens :Epoll 事件描述接口支持两种行为方式 ET、LT。1 The file descriptor that represents the read side of a pipe ( RFD ) is added inside the epoll device.一个管道读端的文件描述符已经注册到 epoll 中。2 Pipe writer writes 2Kb of data on the write side of the pipe.管道的写端往其中写了 2KB 的数据。3 A call to epoll_wait(2) is done that

5、will return RFD as ready file descriptor.一个 epoll_wait 将结束并且返回 RFD 为准备就绪的文件描述符。4 The pipe reader reads 1Kb of data from RFD.从 RFD 中读取 1KB。5 A call to epoll_wait(2) is done.第二次 epoll_wait 执行。If the RFD file descriptor has been added to the epoll interface using the EPOLLET flag, the call to epoll_wai

6、t(2) done in step 5 will probably hang because of the available data still present in the file input buffers and the remote peer might be expecting a response based on the data it already sent. The reason for this is that Edge Triggered event distribution delivers events only when events happens on

7、the monitored file.如果 RFD 文件描述符是以 EPOLLET 方式加入到 epoll 接口,则第二次epoll_wait 调用将很可能挂起,因为就绪的数据依然在输入缓冲区中并且对方可能希望得到一个在已经发送数据上的回复。之所以这样是由于 ET 模式只在发生事件的监视文件上传递事件(也就是一次事件只传递一次) 。So, in step 5 the caller might end up waiting for some data that is already present inside the input buffer. In the above example, an

8、 event on RFD will be generated because of the write done in 2 and the event is consumed in 3. 因此第五个步骤的调用可能是在已经有一部分就绪数据还在等待另外一些数据。在上述的例子中,RFD 上的一个事件将会在第二步时产生,并且在第三步使用掉。Since the read operation done in 4 does not consume the whole buffer data, the call to epoll_wait(2) done in step 5 might lock indef

9、initely. The epoll interface, when used with the EPOLLET flag ( Edge Triggered ) should use non-blocking file descriptors to avoid having a blocking read or write starve the task that is handling multiple file descriptors. The suggested way to use epoll as an Edge Triggered (EPOLLET) interface is be

10、low, and possible pitfalls to avoid follow.直到第四步完成缓冲区中还有数据,第五步调用 epoll_wait 可能无限期的锁住。使用 EPOLLET 接口必须使用非阻塞文件描述符以避免阻塞读或写在处理多描述符时。使用 ET 接口建议如下i、 with non-blocking file descriptors使用非阻塞文件描述符ii、 by going to wait for an event only after read(2) or write(2) return EAGAIN只有在 read、 write 返回 EAGAIN 时才进入等待事件发生

11、On the contrary, when used as a Level Triggered interface, epoll is by all means a faster poll(2), and can be used wherever the latter is used since it shares the same semantics. Since even with the Edge Triggered epoll multiple events can be generated up on receipt of multiple chunks of data, the c

12、aller has the option to specify the EPOLLONESHOT flag, to tell epoll to disable the associated file descriptor after the receipt 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.相反,

13、当使用 LT 接口时,epoll 就是一个快速的 poll,并且能够在之后各处以同样的语义使用。其后 ET 模式 epoll 多个事件能够接收多个大块数据,调用者可以选择指定 EPOLL 单次标记,告诉 epoll 关闭接收一个事件后文件描述符与 epoll_wait 的关系。单 EPOLL 单次标记指定,调用者通过使用EPOLL_CTL_MOD 的 epoll_ctl 来设置。EXAMPLE FOR SUGGESTED USAGE使用建议例子While the usage of epoll when employed like a Level Triggered interface does

14、 have the same semantics of poll(2), an Edge Triggered usage requires more clarification to avoid stalls in the application event loop. 以 LT 接口方式使用 epoll 与 poll 有相同的语义,而 ET 模式使用需要更多的澄清以健壮程序的事件循环。In this example, listener is a non-blocking socket on which listen(2) has been called. The function do_us

15、e_fd() uses the new ready file descriptor until EAGAIN is returned by either read(2) or write(2). 在这个例子,在被调用 listen 函数时接受者是一个非阻塞的socket。 Do_use_fd 函数使用新就绪的文件描述符直到 EAGAIN 在 read 或 write中返回。An event driven state machine application should, after having received EAGAIN, record its current state so that

16、 at the next call to do_use_fd() it will continue to read(2) or write(2) from where it stopped before.一个事件驱动状态程序,需要在接收到 EAGAIN,记录当前的状态以下次调用 do_use_fd 能够在断点处继续调用 read 或 write。struct epoll_event ev, *events;for(;) nfds = epoll_wait(kdpfd, events, maxevents, -1);-已经就绪的文件描述符从 events 中返回for(n = 0; n nfds; +n) if(eventsn.data.fd = listener) -监听的 socket 则调用 acceptclient = accept(listener, (struct sockaddr *) &l

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号