• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            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 閱讀(149) 評論(0)  編輯 收藏 引用

            久久久久亚洲AV无码观看| 久久久久国色AV免费看图片| 国产精品99久久久精品无码| 久久人做人爽一区二区三区 | 久久精品国产久精国产| 久久青青草原精品影院| 久久青青色综合| 精品久久久久久国产91| 亚洲午夜福利精品久久| 久久se精品一区精品二区| 久久综合亚洲鲁鲁五月天| 狠狠色丁香婷婷综合久久来| 久久人人爽人人爽人人片av麻烦| 精品久久久久香蕉网| 亚洲国产成人精品无码久久久久久综合 | 国内精品综合久久久40p| 亚洲综合婷婷久久| 色欲综合久久躁天天躁蜜桃| 久久高清一级毛片| 国产精品久久国产精品99盘| 伊人久久久AV老熟妇色| 亚洲欧洲精品成人久久奇米网| 久久久久亚洲AV片无码下载蜜桃| 天堂无码久久综合东京热| 色偷偷888欧美精品久久久| 国产精品一久久香蕉国产线看观看| 亚洲?V乱码久久精品蜜桃| 久久久久99精品成人片牛牛影视| 久久精品a亚洲国产v高清不卡| 久久久亚洲欧洲日产国码是AV | 久久综合九色欧美综合狠狠| 久久精品国内一区二区三区| 久久久久亚洲AV成人片| 伊人久久大香线蕉亚洲五月天| 91麻豆国产精品91久久久| 青青青青久久精品国产h久久精品五福影院1421 | 伊人久久大香线蕉AV一区二区 | 久久精品aⅴ无码中文字字幕重口| 久久天天躁狠狠躁夜夜avapp| 亚洲精品午夜国产va久久| 热99RE久久精品这里都是精品免费 |