天天看點

7、epoll兩種機制的差別

說明:本文來自翻譯epoll man文檔。

1、ET,LT這兩種事件分發機制的不同。我們假定一個環境:

   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.

示例代碼

7、epoll兩種機制的差別
7、epoll兩種機制的差別

View Code

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.

參考

繼續閱讀