{"id":124106,"date":"2026-11-18T15:01:57","date_gmt":"2026-11-18T07:01:57","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/124106.html"},"modified":"2026-11-18T15:01:57","modified_gmt":"2026-11-18T07:01:57","slug":"linux-epoll-%e4%b8%80%e7%af%87%e8%ae%b2%e9%80%8f%ef%bc%9a%e4%bb%8e-select-poll-%e5%88%b0%e7%ba%a2%e9%bb%91%e6%a0%91%e3%80%81lt-et%ef%bc%8c%e5%86%8d%e5%88%b0%e5%ae%8c%e6%95%b4-tcp-%e6%9c%8d%e5%8a%a1","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/124106.html","title":{"rendered":"Linux epoll \u4e00\u7bc7\u8bb2\u900f\uff1a\u4ece select\/poll \u5230\u7ea2\u9ed1\u6811\u3001LT\/ET\uff0c\u518d\u5230\u5b8c\u6574 TCP \u670d\u52a1\u5668"},"content":{"rendered":"<p>        \/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/<br \/>\n        #content_views .toc,<br \/>\n        \/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/<br \/>\n        #content_views.markdown_views &gt; p:empty:has(+ .toc),<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty,<br \/>\n        \/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/<br \/>\n        #content_views.htmledit_views #main-toc,<br \/>\n        #content_views.htmledit_views #hr-toc,<br \/>\n        #content_views.htmledit_views p[id*=\\&#8221;-toc\\&#8221;] {<br \/>\n          display: none !important;<br \/>\n        }<br \/>\n        \/* \u76ee\u5f55\u53bb\u6389\u540e\uff0c\u7d27\u8ddf\u7684\u9996\u4e2a\u6807\u9898\u4e0d\u518d\u591a\u51fa\u4e00\u5757\u4e0a\u8fb9\u8ddd *\/<br \/>\n        #content_views.markdown_views &gt; .toc + h1,<br \/>\n        #content_views.markdown_views &gt; .toc + h2,<br \/>\n        #content_views.markdown_views &gt; .toc + h3,<br \/>\n        #content_views.markdown_views &gt; .toc + h4,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h1,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h2,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h3,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h4 {<br \/>\n          margin-top: 0 !important;<br \/>\n        }<\/p>\n<h2 id=\"linux-epoll-\u4e00\u7bc7\u8bb2\u900f\uff1a\u4ece-selectpoll-\u5230\u7ea2\u9ed1\u6811\u3001ltet\uff0c\u518d\u5230\u5b8c\u6574-tcp-\u670d\u52a1\u5668\"><\/h2>\n<p>\u4e00\u4e07\u4e2a\u5ba2\u6237\u7aef\u5df2\u7ecf\u8fde\u4e0a\u670d\u52a1\u5668&#xff0c;\u773c\u4e0b\u53ea\u6709\u5341\u4e2a\u5ba2\u6237\u7aef\u53d1\u6765\u4e86\u6570\u636e\u3002\u670d\u52a1\u5668\u600e\u6837\u628a\u65f6\u95f4\u82b1\u5728\u8fd9\u5341\u4e2a\u8fde\u63a5\u4e0a&#xff1f;<\/p>\n<p>\u7406\u89e3\u8fd9\u4e2a\u95ee\u9898&#xff0c;\u5c31\u6293\u4f4f\u4e86 epoll \u7684\u4e3b\u7ebf\u3002\u63a5\u4e0b\u6765&#xff0c;\u6211\u4eec\u6cbf\u7740\u201c\u4e3a\u4ec0\u4e48\u9700\u8981\u5b83 \u2192 \u63a5\u53e3\u600e\u4e48\u7528 \u2192 \u5185\u6838\u5982\u4f55\u5b9e\u73b0 \u2192 LT\/ET \u600e\u4e48\u5199 \u2192 \u5b8c\u6574\u670d\u52a1\u5668\u600e\u4e48\u8fd0\u884c\u201d\u7684\u987a\u5e8f&#xff0c;\u628a\u77e5\u8bc6\u8fde\u8d77\u6765\u3002<\/p>\n<p>**\u9605\u8bfb\u57fa\u7840&#xff1a;**\u4e86\u89e3 socket\u3001bind\u3001listen\u3001accept \u7684\u57fa\u672c\u7528\u9014\u5373\u53ef\u3002\u672c\u6587\u793a\u4f8b\u9762\u5411 Linux&#xff0c;\u670d\u52a1\u5668\u4f7f\u7528 C&#043;&#043;17&#xff0c;\u5b9e\u9a8c\u811a\u672c\u4f7f\u7528 Python 3\u3002<\/p>\n<p>&#064;TOC<\/p>\n<h3 id=\"\u4e00\u3001\u5148\u628a\u95ee\u9898\u8bf4\u6e05\u695a\uff1aepoll-\u5230\u5e95\u5728\u7b49\u4ec0\u4e48\uff1f\">\u4e00\u3001\u5148\u628a\u95ee\u9898\u8bf4\u6e05\u695a&#xff1a;epoll \u5230\u5e95\u5728\u201c\u7b49\u201d\u4ec0\u4e48&#xff1f;<\/h3>\n<h4 id=\"11-\u4e00\u4e2a\u8fde\u63a5\u6ca1\u6709\u6570\u636e\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u5f71\u54cd\u5176\u4ed6\u8fde\u63a5\uff1f\">1.1 \u4e00\u4e2a\u8fde\u63a5\u6ca1\u6709\u6570\u636e&#xff0c;\u4e3a\u4ec0\u4e48\u4f1a\u5f71\u54cd\u5176\u4ed6\u8fde\u63a5&#xff1f;<\/h4>\n<p>\u5047\u8bbe\u670d\u52a1\u5668\u5df2\u7ecf\u4e0e\u4e09\u4e2a\u5ba2\u6237\u7aef\u5efa\u7acb\u8fde\u63a5&#xff0c;\u5bf9\u5e94\u4e09\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff1a;<\/p>\n<p>fd &#061; 5&#xff1a;\u6682\u65f6\u6ca1\u6709\u6570\u636e<br \/>\nfd &#061; 6&#xff1a;\u5df2\u7ecf\u53d1\u6765\u8bf7\u6c42<br \/>\nfd &#061; 7&#xff1a;\u5df2\u7ecf\u53d1\u6765\u8bf7\u6c42<\/p>\n<p>\u5982\u679c\u670d\u52a1\u5668\u8fd9\u6837\u5904\u7406\u963b\u585e\u5957\u63a5\u5b57&#xff1a;<\/p>\n<p>recv(5, buf, sizeof(buf), 0);<br \/>\nrecv(6, buf, sizeof(buf), 0);<br \/>\nrecv(7, buf, sizeof(buf), 0);<\/p>\n<p>\u7a0b\u5e8f\u53ef\u80fd\u505c\u5728\u7b2c\u4e00\u884c\u3002\u5373\u4f7f 6\u30017 \u53f7\u8fde\u63a5\u5df2\u7ecf\u6709\u6570\u636e&#xff0c;\u5f53\u524d\u7ebf\u7a0b\u4e5f\u6ca1\u6709\u673a\u4f1a\u5904\u7406\u5b83\u4eec\u3002<\/p>\n<p>\u7ed9\u6bcf\u4e2a\u8fde\u63a5\u5206\u914d\u4e00\u4e2a\u7ebf\u7a0b&#xff0c;\u53ef\u4ee5\u7ed5\u5f00\u8fd9\u4e2a\u95ee\u9898\u3002\u4f46\u968f\u7740\u8fde\u63a5\u6570\u589e\u957f&#xff0c;\u7ebf\u7a0b\u6808\u3001\u8c03\u5ea6\u3001\u4e0a\u4e0b\u6587\u5207\u6362\u4e5f\u4f1a\u5e26\u6765\u6210\u672c\u3002<\/p>\n<p>\u53e6\u4e00\u4e2a\u65b9\u5411\u662f&#xff1a;\u628a\u6240\u6709\u5957\u63a5\u5b57\u6539\u4e3a\u975e\u963b\u585e&#xff0c;\u4e0d\u505c\u5730\u6328\u4e2a recv\u3002\u8fd9\u6837\u4e0d\u4f1a\u5361\u5728\u4e00\u4e2a\u8fde\u63a5\u4e0a&#xff0c;\u5374\u4f1a\u5728\u5927\u91cf\u6ca1\u6709\u6570\u636e\u7684\u8fde\u63a5\u4e0a\u53cd\u590d\u5f97\u5230 EAGAIN&#xff0c;\u6d88\u8017 CPU\u3002<\/p>\n<p>\u6211\u4eec\u9700\u8981\u4e00\u79cd\u66f4\u5408\u9002\u7684\u7b49\u5f85\u65b9\u5f0f&#xff1a;<\/p>\n<p>\u628a\u591a\u4e2a fd \u4ea4\u7ed9\u5185\u6838\u7edf\u4e00\u5173\u6ce8&#xff1b;\u6ca1\u6709\u4e8b\u60c5\u53ef\u505a\u65f6\u4f11\u7720&#xff1b;\u6709\u4e8b\u60c5\u53ef\u505a\u65f6&#xff0c;\u53ea\u5904\u7406\u8fd4\u56de\u7684\u90a3\u4e9b fd\u3002<\/p>\n<p>\u8fd9\u5c31\u662f I\/O \u591a\u8def\u590d\u7528&#xff0c;\u4e5f\u53eb I\/O \u591a\u8def\u8f6c\u63a5\u3002select\u3001poll\u3001epoll \u90fd\u5728\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<p>\u201c\u591a\u8def\u201d\u6307\u591a\u4e2a I\/O \u5bf9\u8c61&#xff0c;\u201c\u590d\u7528\u201d\u6307\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u4ea4\u66ff\u670d\u52a1\u8fd9\u4e9b\u5bf9\u8c61\u3002\u5b83\u5e76\u4e0d\u610f\u5473\u7740\u4e00\u4e2a\u7ebf\u7a0b\u5728\u540c\u4e00\u77ac\u95f4\u6267\u884c\u591a\u6b21 recv\u3002<\/p>\n<h4 id=\"12-\u5c31\u7eea\u662f\u53ef\u4ee5\u5c1d\u8bd5\u64cd\u4f5c\uff0c\u4e1a\u52a1\u8bf7\u6c42\u8fd8\u9700\u8981\u81ea\u5df1\u5224\u65ad\">1.2 \u201c\u5c31\u7eea\u201d\u662f\u53ef\u4ee5\u5c1d\u8bd5\u64cd\u4f5c&#xff0c;\u4e1a\u52a1\u8bf7\u6c42\u8fd8\u9700\u8981\u81ea\u5df1\u5224\u65ad<\/h4>\n<p>\u5bf9\u666e\u901a TCP \u8fde\u63a5\u6765\u8bf4&#xff0c;\u5e38\u89c1\u7684\u5c31\u7eea\u60c5\u51b5\u5305\u62ec&#xff1a;<\/p>\n<table>\n<tr>\u573a\u666f\u901a\u5e38\u63a5\u4e0b\u6765\u505a\u4ec0\u4e48<\/tr>\n<tbody>\n<tr>\n<td>\u76d1\u542c\u5957\u63a5\u5b57\u8bfb\u5c31\u7eea<\/td>\n<td>\u8c03\u7528 accept \u53d6\u51fa\u5df2\u7ecf\u5efa\u7acb\u3001\u7b49\u5f85\u5e94\u7528\u63a5\u6536\u7684\u8fde\u63a5<\/td>\n<\/tr>\n<tr>\n<td>\u5df2\u8fde\u63a5\u5957\u63a5\u5b57\u8bfb\u5c31\u7eea<\/td>\n<td>\u8c03\u7528 recv \u8bfb\u53d6\u6570\u636e&#xff0c;\u4e5f\u53ef\u80fd\u8bfb\u5230 EOF \u6216\u9519\u8bef<\/td>\n<\/tr>\n<tr>\n<td>\u5df2\u8fde\u63a5\u5957\u63a5\u5b57\u5199\u5c31\u7eea<\/td>\n<td>\u5c1d\u8bd5 send&#xff0c;\u5177\u4f53\u80fd\u5199\u591a\u5c11\u4ee5\u8fd4\u56de\u503c\u4e3a\u51c6<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u56e0\u6b64&#xff0c;\u6536\u5230\u8bfb\u4e8b\u4ef6\u540e&#xff0c;\u4ecd\u7136\u9700\u8981\u68c0\u67e5 recv \u7684\u8fd4\u56de\u503c&#xff1b;\u6536\u5230\u5199\u4e8b\u4ef6\u540e&#xff0c;\u4ecd\u7136\u9700\u8981\u5904\u7406\u90e8\u5206\u53d1\u9001\u548c\u9519\u8bef\u3002<\/p>\n<p>\u5c24\u5176\u8981\u8bb0\u4f4f&#xff1a;<\/p>\n<p>\u8bfb\u5c31\u7eea\u4e0d\u4ee3\u8868\u4e00\u6761\u5b8c\u6574\u8bf7\u6c42\u5df2\u7ecf\u5230\u9f50&#xff1b;\u5199\u5c31\u7eea\u4e0d\u4ee3\u8868\u4efb\u610f\u957f\u5ea6\u7684\u6570\u636e\u90fd\u80fd\u4e00\u6b21\u53d1\u5b8c\u3002<\/p>\n<p>\u975e\u963b\u585e I\/O \u53ef\u4ee5\u5e94\u5bf9\u201c\u6536\u5230\u4e8b\u4ef6\u540e&#xff0c;\u5b9e\u9645\u64cd\u4f5c\u65f6\u6761\u4ef6\u5df2\u7ecf\u53d8\u5316\u201d\u7684\u60c5\u51b5\u3002\u4f8b\u5982&#xff0c;\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u5148\u8bfb\u8d70\u4e86\u6570\u636e&#xff0c;\u5f53\u524d\u7ebf\u7a0b\u518d\u8bfb\u5c31\u53ef\u80fd\u5f97\u5230 EAGAIN\u3002\u5c31\u7eea\u6761\u4ef6\u7684\u57fa\u7840\u5b9a\u4e49\u53ef\u53c2\u89c1 select(2)\u3002<\/p>\n<h4 id=\"13-epoll-\u8fd4\u56de\u4ee5\u540e\uff0c\u6570\u636e\u4ecd\u7136\u8981\u7531\u5e94\u7528\u8bfb\u53d6\">1.3 epoll \u8fd4\u56de\u4ee5\u540e&#xff0c;\u6570\u636e\u4ecd\u7136\u8981\u7531\u5e94\u7528\u8bfb\u53d6<\/h4>\n<p>\u53ef\u4ee5\u628a\u4e00\u6b21\u666e\u901a TCP \u63a5\u6536\u5206\u6210\u4e24\u4e2a\u9636\u6bb5&#xff1a;<\/p>\n<li>\u7b49\u5f85\u5185\u6838\u4e2d\u51fa\u73b0\u53ef\u5904\u7406\u7684 I\/O \u6761\u4ef6\u3002<\/li>\n<li>\u8c03\u7528 recv&#xff0c;\u628a\u6570\u636e\u4ea4\u7ed9\u7528\u6237\u7a7a\u95f4\u7684\u7a0b\u5e8f\u3002<\/li>\n<p>epoll \u4e3b\u8981\u7ec4\u7ec7\u7b2c\u4e00\u4e2a\u9636\u6bb5\u7684\u7b49\u5f85\u548c\u901a\u77e5\u3002\u666e\u901a recv \u7684\u6570\u636e\u63a5\u6536\u4ecd\u7531\u5e94\u7528\u53d1\u8d77&#xff0c;\u534f\u8bae\u89e3\u6790\u548c\u4e1a\u52a1\u5904\u7406\u4e5f\u9700\u8981\u5e94\u7528\u5b8c\u6210\u3002<\/p>\n<p>\u6240\u4ee5&#xff0c;epoll \u5c5e\u4e8e\u540c\u6b65 I\/O \u591a\u8def\u590d\u7528\u3002\u5185\u6838\u80fd\u591f\u4e3b\u52a8\u5524\u9192\u7b49\u5f85\u7ebf\u7a0b&#xff0c;\u5e76\u4e0d\u7b49\u4e8e\u5e94\u7528\u63d0\u4ea4\u7684\u6574\u4e2a\u8bfb\u53d6\u64cd\u4f5c\u5df2\u7ecf\u5f02\u6b65\u5b8c\u6210\u3002<\/p>\n<p>\u8fd8\u8981\u533a\u5206\u4e24\u4ef6\u4e8b&#xff1a;<\/p>\n<ul>\n<li>\u975e\u963b\u585e\u5957\u63a5\u5b57&#xff1a;recv\u3001send \u6682\u65f6\u65e0\u6cd5\u63a8\u8fdb\u65f6\u53ca\u65f6\u8fd4\u56de\u3002<\/li>\n<li>\u963b\u585e\u7684 epoll_wait&#xff1a;\u6ca1\u6709\u53ef\u5904\u7406\u4e8b\u4ef6\u65f6&#xff0c;\u8ba9\u4e8b\u4ef6\u5faa\u73af\u7ebf\u7a0b\u4f11\u7720\u3002<\/li>\n<\/ul>\n<p>\u201c\u975e\u963b\u585e socket &#043; \u53ef\u4ee5\u963b\u585e\u7684 epoll_wait\u201d\u6070\u597d\u662f\u4e8b\u4ef6\u9a71\u52a8\u670d\u52a1\u5668\u7684\u5e38\u89c1\u7ec4\u5408\u3002<\/p>\n<h3 id=\"\u4e8c\u3001\u4ece-poll-\u8d70\u5411-epoll\uff1a\u4e3a\u4ec0\u4e48\u8981\u628a\u6ce8\u518c\u548c\u7b49\u5f85\u62c6\u5f00\uff1f\">\u4e8c\u3001\u4ece poll \u8d70\u5411 epoll&#xff1a;\u4e3a\u4ec0\u4e48\u8981\u628a\u201c\u6ce8\u518c\u201d\u548c\u201c\u7b49\u5f85\u201d\u62c6\u5f00&#xff1f;<\/h3>\n<h4 id=\"21-poll-\u5df2\u7ecf\u6bd4-select-\u597d\u7528\u4e86\u4e00\u4e9b\">2.1 poll \u5df2\u7ecf\u6bd4 select \u597d\u7528\u4e86\u4e00\u4e9b<\/h4>\n<p>poll \u7684\u63a5\u53e3\u5982\u4e0b&#xff1a;<\/p>\n<p>#include &lt;poll.h&gt;<\/p>\n<p>int poll(struct pollfd *fds, nfds_t nfds, int timeout);<\/p>\n<p>struct pollfd {<br \/>\n    int   fd;<br \/>\n    short events;   \/\/ \u5e94\u7528\u5173\u5fc3\u54ea\u4e9b\u4e8b\u4ef6<br \/>\n    short revents;  \/\/ \u5185\u6838\u8fd4\u56de\u54ea\u4e9b\u4e8b\u4ef6\u5df2\u7ecf\u53d1\u751f<br \/>\n};<\/p>\n<p>\u4f8b\u5982&#xff0c;\u540c\u65f6\u5173\u5fc3\u4e24\u4e2a\u8fde\u63a5\u7684\u8bfb\u4e8b\u4ef6&#xff1a;<\/p>\n<p>struct pollfd fds[2] &#061; {<br \/>\n    {.fd &#061; fd1, .events &#061; POLLIN},<br \/>\n    {.fd &#061; fd2, .events &#061; POLLIN}<br \/>\n};<\/p>\n<p>int ready &#061; poll(fds, 2, 1000);  \/\/ \u6700\u591a\u7b49\u5f85 1000 \u6beb\u79d2<br \/>\nif (ready &gt; 0) {<br \/>\n    for (int i &#061; 0; i &lt; 2; &#043;&#043;i) {<br \/>\n        if (fds[i].revents &amp; POLLIN) {<br \/>\n            \/\/ \u5c1d\u8bd5\u8bfb\u53d6&#xff0c;\u5e76\u68c0\u67e5\u5b9e\u9645\u8fd4\u56de\u503c\u3002<br \/>\n        }<br \/>\n        \/\/ \u5b8c\u6574\u7a0b\u5e8f\u8fd8\u5e94\u5904\u7406 POLLERR\u3001POLLHUP\u3001POLLNVAL\u3002<br \/>\n    }<br \/>\n}<\/p>\n<p>\u8fd9\u91cc\u7684\u793a\u610f\u7247\u6bb5\u4f7f\u7528 C \u8bed\u6cd5\u3002\u6ce8\u610f\u4e8b\u4ef6\u4f7f\u7528 &amp; \u5224\u65ad&#xff0c;\u56e0\u4e3a\u4e00\u4e2a fd \u53ef\u80fd\u540c\u65f6\u5e26\u6709\u591a\u4e2a\u4e8b\u4ef6\u4f4d\u3002<\/p>\n<p>\u4e0e select \u76f8\u6bd4&#xff0c;poll \u7528\u6570\u7ec4\u4fdd\u5b58\u5173\u6ce8\u9879&#xff0c;\u628a\u8f93\u5165\u4e8b\u4ef6\u4e0e\u8fd4\u56de\u4e8b\u4ef6\u5206\u5f00&#xff0c;\u4e5f\u6ca1\u6709 fd_set \u56fa\u5b9a\u5927\u5c0f\u5e26\u6765\u7684\u90a3\u79cd\u9650\u5236\u3002Linux \u4e0a\u5e38\u89c1\u7684 glibc fd_set \u8981\u6c42 fd \u6570\u503c\u5c0f\u4e8e FD_SETSIZE&#xff0c;\u901a\u5e38\u662f 1024&#xff1b;\u8fd9\u4e5f\u4e0d\u7b49\u4e8e\u8fdb\u7a0b\u603b\u5171\u53ea\u80fd\u6253\u5f00 1024 \u4e2a\u6587\u4ef6\u3002\u63a5\u53e3\u7ec6\u8282\u5206\u522b\u89c1 poll(2) \u548c select(2)\u3002<\/p>\n<h4 id=\"22-\u95ee\u9898\u51fa\u5728\u6bcf\u6b21\u90fd\u628a\u5168\u90e8\u5173\u6ce8\u9879\u518d\u4ea4\u4e00\u904d\">2.2 \u95ee\u9898\u51fa\u5728\u201c\u6bcf\u6b21\u90fd\u628a\u5168\u90e8\u5173\u6ce8\u9879\u518d\u4ea4\u4e00\u904d\u201d<\/h4>\n<p>\u5047\u8bbe\u5173\u6ce8\u4e00\u4e07\u4e2a\u8fde\u63a5&#xff0c;\u8fd9\u4e00\u8f6e\u53ea\u6709\u5341\u4e2a\u6d3b\u8dc3&#xff1a;<\/p>\n<ul>\n<li>\u8c03\u7528 poll \u65f6&#xff0c;\u4ecd\u7136\u9700\u8981\u63d0\u4ea4\u8fd9\u4e00\u4e07\u4e2a\u5173\u6ce8\u9879\u3002<\/li>\n<li>\u5185\u6838\u9700\u8981\u68c0\u67e5\u8fd9\u4e9b\u5173\u6ce8\u9879\u3002<\/li>\n<li>\u8fd4\u56de\u4ee5\u540e&#xff0c;\u5e94\u7528\u8fd8\u9700\u8981\u68c0\u67e5\u6570\u7ec4\u4e2d\u7684 revents&#xff0c;\u627e\u5230\u53ef\u5904\u7406\u7684\u8fde\u63a5\u3002<\/li>\n<\/ul>\n<p>\u5982\u679c\u5927\u90e8\u5206\u8fde\u63a5\u957f\u671f\u7a7a\u95f2&#xff0c;\u8fd9\u4e9b\u91cd\u590d\u5de5\u4f5c\u5c31\u6bd4\u8f83\u660e\u663e\u3002<\/p>\n<p>epoll \u7684\u8bbe\u8ba1\u628a\u4e24\u7c7b\u5de5\u4f5c\u62c6\u5f00&#xff1a;<\/p>\n<table>\n<tr>\u5de5\u4f5c\u4ec0\u4e48\u65f6\u5019\u505a<\/tr>\n<tbody>\n<tr>\n<td>\u544a\u8bc9\u5185\u6838\u5173\u6ce8\u54ea\u4e9b fd\u3001\u54ea\u4e9b\u4e8b\u4ef6<\/td>\n<td>\u65b0\u8fde\u63a5\u52a0\u5165\u3001\u5173\u6ce8\u4e8b\u4ef6\u6539\u53d8\u3001\u8fde\u63a5\u9000\u51fa\u65f6<\/td>\n<\/tr>\n<tr>\n<td>\u7b49\u5f85\u5e76\u53d6\u56de\u5f53\u524d\u53ef\u5904\u7406\u7684\u4e8b\u4ef6<\/td>\n<td>\u6bcf\u4e00\u8f6e\u4e8b\u4ef6\u5faa\u73af<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e8e\u662f&#xff0c;\u4e09\u4e2a\u63a5\u53e3\u81ea\u7136\u51fa\u73b0\u4e86&#xff1a;<\/p>\n<p>epoll_create1&#xff1a;\u521b\u5efa\u4e00\u4e2a epoll \u5b9e\u4f8b<br \/>\nepoll_ctl&#xff1a;\u589e\u52a0\u3001\u4fee\u6539\u3001\u5220\u9664\u5173\u6ce8\u9879<br \/>\nepoll_wait&#xff1a;\u7b49\u5f85\u5e76\u53d6\u5f97\u4e00\u6279\u5c31\u7eea\u4e8b\u4ef6<\/p>\n<p>\u8fd9\u5c31\u662f\u5168\u6587\u7684\u7b2c\u4e00\u6761\u8054\u7cfb&#xff1a;\u63a5\u53e3\u62c6\u5206&#xff0c;\u662f\u4e3a\u4e86\u8ba9\u5173\u6ce8\u9879\u80fd\u591f\u957f\u671f\u4fdd\u5b58\u5728\u5185\u6838\u4e2d\u3002<\/p>\n<h3 id=\"\u4e09\u3001\u4e09\u4e2a\u63a5\u53e3\u8bb2\u900f\uff1a\u6bcf\u4e2a\u53c2\u6570\u5230\u5e95\u4ee3\u8868\u4ec0\u4e48\uff1f\">\u4e09\u3001\u4e09\u4e2a\u63a5\u53e3\u8bb2\u900f&#xff1a;\u6bcf\u4e2a\u53c2\u6570\u5230\u5e95\u4ee3\u8868\u4ec0\u4e48&#xff1f;<\/h3>\n<h4 id=\"31-epoll_create1\uff1a\u521b\u5efa\u5b9e\u4f8b\uff0c\u5f97\u5230-epfd\">3.1 epoll_create1&#xff1a;\u521b\u5efa\u5b9e\u4f8b&#xff0c;\u5f97\u5230 epfd<\/h4>\n<p>#include &lt;sys\/epoll.h&gt;<\/p>\n<p>int epoll_create(int size);<br \/>\nint epoll_create1(int flags);<\/p>\n<p>\u65b0\u4ee3\u7801\u53ef\u4ee5\u8fd9\u6837\u5199&#xff1a;<\/p>\n<p>int epfd &#061; epoll_create1(EPOLL_CLOEXEC);<br \/>\nif (epfd &#061;&#061; -1) {<br \/>\n    perror(&#034;epoll_create1&#034;);<br \/>\n    \/\/ \u5904\u7406\u521b\u5efa\u5931\u8d25\u3002<br \/>\n}<\/p>\n<p>\u6210\u529f\u8fd4\u56de\u975e\u8d1f\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u5931\u8d25\u8fd4\u56de -1\u3002<\/p>\n<p>EPOLL_CLOEXEC \u8bbe\u7f6e close-on-exec \u6807\u5fd7&#xff0c;\u5728\u6210\u529f\u6267\u884c exec \u7cfb\u5217\u64cd\u4f5c\u65f6\u5173\u95ed\u8fd9\u4e2a\u63cf\u8ff0\u7b26\u3002\u5b83\u4e0e\u5957\u63a5\u5b57\u662f\u5426\u975e\u963b\u585e\u6ca1\u6709\u5173\u7cfb\u3002<\/p>\n<p>\u65e7\u63a5\u53e3 epoll_create(size) \u4e2d&#xff0c;size \u5728\u73b0\u4ee3 Linux \u4e0a\u5df2\u7ecf\u4e0d\u7528\u4e8e\u51b3\u5b9a\u5bb9\u91cf&#xff0c;\u4f46\u4ecd\u5fc5\u987b\u5927\u4e8e 0\u3002\u56e0\u6b64&#xff1a;<\/p>\n<p>int epfd &#061; epoll_create(1);<\/p>\n<p>\u4e0d\u8868\u793a\u53ea\u80fd\u5173\u6ce8\u4e00\u4e2a fd&#xff0c;\u4e5f\u4e0d\u8981\u6c42\u540e\u9762\u7684 maxevents \u5c0f\u4e8e\u7b49\u4e8e 1\u3002\u76f8\u5173\u5386\u53f2\u4e0e\u53c2\u6570\u89c4\u5219\u89c1 epoll_create(2)\u3002<\/p>\n<p>\u4f7f\u7528\u7ed3\u675f\u540e\u8981\u5173\u95ed&#xff1a;<\/p>\n<p>close(epfd);<\/p>\n<p>**\u5173\u95ed epfd \u4f1a\u91ca\u653e\u5bf9\u5e94\u5b9e\u4f8b\u7684\u8d44\u6e90&#xff0c;\u4f46\u4e0d\u4f1a\u66ff\u5e94\u7528\u5173\u95ed\u6240\u6709\u88ab\u5173\u6ce8\u7684\u4e1a\u52a1 socket\u3002**\u4e1a\u52a1\u8fde\u63a5\u9700\u8981\u5355\u72ec\u7ba1\u7406\u3002<\/p>\n<h4 id=\"32-epoll_ctl\uff1a\u7ef4\u62a4\u5173\u6ce8\u9879\">3.2 epoll_ctl&#xff1a;\u7ef4\u62a4\u5173\u6ce8\u9879<\/h4>\n<p>int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);<\/p>\n<table>\n<tr>\u53c2\u6570\u542b\u4e49<\/tr>\n<tbody>\n<tr>\n<td>epfd<\/td>\n<td>\u8981\u64cd\u4f5c\u54ea\u4e2a epoll \u5b9e\u4f8b<\/td>\n<\/tr>\n<tr>\n<td>op<\/td>\n<td>\u589e\u52a0\u3001\u4fee\u6539\u8fd8\u662f\u5220\u9664<\/td>\n<\/tr>\n<tr>\n<td>fd<\/td>\n<td>\u88ab\u5173\u6ce8\u7684\u76ee\u6807\u6587\u4ef6\u63cf\u8ff0\u7b26<\/td>\n<\/tr>\n<tr>\n<td>event<\/td>\n<td>\u5173\u6ce8\u7684\u4e8b\u4ef6&#xff0c;\u4ee5\u53ca\u5e94\u7528\u9644\u5e26\u7684\u6570\u636e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e09\u4e2a\u64cd\u4f5c\u5206\u522b\u662f&#xff1a;<\/p>\n<p>EPOLL_CTL_ADD&#xff1a;\u589e\u52a0\u5173\u6ce8\u9879<br \/>\nEPOLL_CTL_MOD&#xff1a;\u4fee\u6539\u5df2\u6709\u5173\u6ce8\u9879<br \/>\nEPOLL_CTL_DEL&#xff1a;\u5220\u9664\u5173\u6ce8\u9879<\/p>\n<p>\u6ce8\u518c\u4e00\u4e2a\u8fde\u63a5\u7684\u8bfb\u4e8b\u4ef6&#xff1a;<\/p>\n<p>epoll_event ev{};<br \/>\nev.events &#061; EPOLLIN;<br \/>\nev.data.fd &#061; client_fd;<\/p>\n<p>if (epoll_ctl(epfd, EPOLL_CTL_ADD, client_fd, &amp;ev) &#061;&#061; -1) {<br \/>\n    perror(&#034;epoll_ctl ADD&#034;);<br \/>\n}<\/p>\n<p>\u5220\u9664\u5173\u6ce8\u9879&#xff1a;<\/p>\n<p>if (epoll_ctl(epfd, EPOLL_CTL_DEL, client_fd, nullptr) &#061;&#061; -1) {<br \/>\n    perror(&#034;epoll_ctl DEL&#034;);<br \/>\n}<\/p>\n<p>DEL \u7684\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u88ab\u5ffd\u7565&#xff0c;\u53ef\u4ee5\u4f20\u7a7a\u6307\u9488\u3002**\u5220\u9664\u5173\u6ce8\u9879\u4e0d\u4f1a\u5173\u95ed client_fd\u3002**\u8fd9\u4e24\u4e2a\u64cd\u4f5c\u7684\u8d23\u4efb\u4e0d\u540c\u3002\u5b9a\u4e49\u89c1 epoll_ctl(2)\u3002<\/p>\n<h4 id=\"33-epoll_event\uff1aevents-\u4f20\u4e8b\u4ef6\uff0cdata-\u5e2e\u4f60\u8ba4\u51fa\u8fde\u63a5\">3.3 epoll_event&#xff1a;events \u4f20\u4e8b\u4ef6&#xff0c;data \u5e2e\u4f60\u8ba4\u51fa\u8fde\u63a5<\/h4>\n<p>\u7528\u6237\u6001\u5e38\u7528\u7ed3\u6784\u53ef\u4ee5\u7406\u89e3\u4e3a&#xff1a;<\/p>\n<p>typedef union epoll_data {<br \/>\n    void     *ptr;<br \/>\n    int       fd;<br \/>\n    uint32_t  u32;<br \/>\n    uint64_t  u64;<br \/>\n} epoll_data_t;<\/p>\n<p>struct epoll_event {<br \/>\n    uint32_t     events;<br \/>\n    epoll_data_t data;<br \/>\n};<\/p>\n<p>\u8fd9\u91cc\u6700\u5bb9\u6613\u6df7\u6dc6\u7684\u662f data&#xff1a;<\/p>\n<p>\u5b83\u7531\u5e94\u7528\u586b\u5199&#xff0c;\u5185\u6838\u4fdd\u5b58&#xff0c;\u8fd4\u56de\u4e8b\u4ef6\u65f6\u518d\u5e26\u56de\u6765\u3002<\/p>\n<p>epoll_ctl \u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u5df2\u7ecf\u8bf4\u660e\u4e86\u8981\u5173\u6ce8\u54ea\u4e2a fd&#xff0c;\u6240\u4ee5 ev.data.fd &#061; client_fd \u5e76\u4e0d\u662f\u5728\u7b2c\u4e8c\u6b21\u6ce8\u518c fd&#xff0c;\u800c\u662f\u5728\u7ea6\u5b9a&#xff1a;\u201c\u4ee5\u540e\u8fd4\u56de\u4e8b\u4ef6\u65f6&#xff0c;\u8bf7\u628a\u8fd9\u4e2a\u503c\u4ea4\u8fd8\u7ed9\u6211\u3002\u201d<\/p>\n<p>\u4e5f\u53ef\u4ee5\u4fdd\u5b58\u8fde\u63a5\u7f16\u53f7&#xff1a;<\/p>\n<p>ev.data.u64 &#061; connection_id;<\/p>\n<p>\u6216\u8005\u4fdd\u5b58\u8fde\u63a5\u5bf9\u8c61\u6307\u9488&#xff1a;<\/p>\n<p>ev.data.ptr &#061; connection;<\/p>\n<p>\u4f46\u5b83\u662f\u8054\u5408\u4f53&#xff0c;\u6210\u5458\u5171\u4eab\u5b58\u50a8\u7a7a\u95f4&#xff0c;\u4e0d\u80fd\u540c\u65f6\u628a fd \u548c ptr \u5f53\u4f5c\u4e24\u4e2a\u72ec\u7acb\u5b57\u6bb5\u4f7f\u7528\u3002\u4fdd\u5b58\u6307\u9488\u65f6&#xff0c;\u5185\u6838\u53ea\u4fdd\u5b58\u6307\u9488\u503c&#xff0c;\u4e0d\u4f1a\u590d\u5236\u5bf9\u8c61&#xff0c;\u66f4\u4e0d\u4f1a\u5ef6\u957f\u5bf9\u8c61\u5bff\u547d\u3002\u7ed3\u6784\u5b9a\u4e49\u89c1 epoll_event(3type)\u3002<\/p>\n<h4 id=\"34-\u5e38\u7528\u4e8b\u4ef6\uff0c\u5148\u5206\u6e05\u72b6\u6001\u4e0e\u9009\u9879\">3.4 \u5e38\u7528\u4e8b\u4ef6&#xff0c;\u5148\u5206\u6e05\u201c\u72b6\u6001\u201d\u4e0e\u201c\u9009\u9879\u201d<\/h4>\n<table>\n<tr>\u6807\u5fd7\u7528\u9014<\/tr>\n<tbody>\n<tr>\n<td>EPOLLIN<\/td>\n<td>\u5173\u6ce8\u53ef\u8bfb\u6761\u4ef6<\/td>\n<\/tr>\n<tr>\n<td>EPOLLOUT<\/td>\n<td>\u5173\u6ce8\u53ef\u5199\u6761\u4ef6<\/td>\n<\/tr>\n<tr>\n<td>EPOLLRDHUP<\/td>\n<td>\u5bf9\u7aef\u5173\u95ed\u8fde\u63a5\u6216\u5173\u95ed TCP \u5199\u65b9\u5411&#xff0c;\u9700\u8981\u663e\u5f0f\u5173\u6ce8<\/td>\n<\/tr>\n<tr>\n<td>EPOLLERR<\/td>\n<td>\u53d1\u751f\u9519\u8bef&#xff0c;\u65e0\u987b\u5728\u5173\u6ce8\u96c6\u5408\u4e2d\u989d\u5916\u58f0\u660e\u4e5f\u4f1a\u62a5\u544a<\/td>\n<\/tr>\n<tr>\n<td>EPOLLHUP<\/td>\n<td>\u6302\u65ad&#xff0c;\u65e0\u987b\u989d\u5916\u58f0\u660e\u4e5f\u4f1a\u62a5\u544a&#xff1b;\u4ecd\u53ef\u80fd\u6709\u6570\u636e\u672a\u8bfb<\/td>\n<\/tr>\n<tr>\n<td>EPOLLPRI<\/td>\n<td>\u9ad8\u4f18\u5148\u7ea7\u6216\u5f02\u5e38\u6761\u4ef6&#xff0c;\u4f8b\u5982 TCP \u5e26\u5916\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>EPOLLET<\/td>\n<td>\u542f\u7528\u8fb9\u7f18\u89e6\u53d1&#xff0c;\u540e\u6587\u5c55\u5f00<\/td>\n<\/tr>\n<tr>\n<td>EPOLLONESHOT<\/td>\n<td>\u4e8b\u4ef6\u4ea4\u4ed8\u540e\u6682\u505c\u8be5\u5173\u6ce8\u9879\u7684\u540e\u7eed\u901a\u77e5&#xff0c;\u9700\u91cd\u65b0\u6fc0\u6d3b<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u524d\u51e0\u9879\u63cf\u8ff0 I\/O \u6761\u4ef6&#xff0c;\u540e\u4e24\u9879\u5f71\u54cd\u901a\u77e5\u65b9\u5f0f\u3002\u4e0d\u8981\u628a\u5b83\u4eec\u90fd\u7406\u89e3\u6210\u201c\u6536\u5230\u4e86\u4e00\u79cd\u4e1a\u52a1\u6d88\u606f\u201d\u3002\u8be6\u7ec6\u8bed\u4e49\u89c1 epoll_ctl(2)\u3002<\/p>\n<p>\u4e8b\u4ef6\u5224\u65ad\u4e5f\u8981\u6309\u4f4d\u8fdb\u884c&#xff1a;<\/p>\n<p>if (events[i].events &amp; EPOLLIN) {<br \/>\n    \/\/ \u5904\u7406\u8bfb\u65b9\u5411\u3002<br \/>\n}<br \/>\nif (events[i].events &amp; EPOLLOUT) {<br \/>\n    \/\/ \u5904\u7406\u5199\u65b9\u5411&#xff1b;\u5b83\u53ef\u80fd\u4e0e\u8bfb\u4e8b\u4ef6\u540c\u65f6\u51fa\u73b0\u3002<br \/>\n}<\/p>\n<h4 id=\"35-epoll_wait\uff1a\u53d6\u56de\u4e00\u6279\u4e8b\u4ef6\">3.5 epoll_wait&#xff1a;\u53d6\u56de\u4e00\u6279\u4e8b\u4ef6<\/h4>\n<p>int epoll_wait(int epfd, struct epoll_event *events,<br \/>\n               int maxevents, int timeout);<\/p>\n<p>\u5178\u578b\u8c03\u7528&#xff1a;<\/p>\n<p>epoll_event events[128];<br \/>\nint n &#061; epoll_wait(epfd, events, 128, -1);<\/p>\n<ul>\n<li>events&#xff1a;\u5e94\u7528\u9884\u5148\u51c6\u5907\u7684\u8f93\u51fa\u6570\u7ec4\u3002<\/li>\n<li>maxevents&#xff1a;\u8fd9\u4e00\u8f6e\u6700\u591a\u8fd4\u56de\u591a\u5c11\u9879&#xff0c;\u5fc5\u987b\u5927\u4e8e 0&#xff0c;\u5e76\u4e14\u4e0d\u80fd\u8d85\u8fc7\u5b9e\u9645\u6570\u7ec4\u5bb9\u91cf\u3002<\/li>\n<li>timeout&#xff1a;\u7b49\u5f85\u65f6\u957f&#xff0c;\u5355\u4f4d\u6beb\u79d2&#xff1b;-1 \u8868\u793a\u65e0\u9650\u7b49\u5f85&#xff0c;0 \u8868\u793a\u7acb\u5373\u68c0\u67e5\u5e76\u8fd4\u56de\u3002<\/li>\n<li>\u8fd4\u56de n &gt; 0&#xff1a;\u672c\u6b21\u8fd4\u56de\u4e86 n \u9879\u4e8b\u4ef6\u3002<\/li>\n<li>\u8fd4\u56de 0&#xff1a;\u672c\u6b21\u7b49\u5f85\u6ca1\u6709\u53d6\u5f97\u4e8b\u4ef6\u3002<\/li>\n<li>\u8fd4\u56de -1&#xff1a;\u51fa\u9519&#xff0c;\u901a\u8fc7 errno \u5224\u65ad\u539f\u56e0&#xff0c;\u4f8b\u5982 EINTR\u3002<\/li>\n<\/ul>\n<p>**128 \u662f\u5355\u6279\u4e0a\u9650&#xff0c;\u548c\u603b\u5171\u5173\u6ce8\u591a\u5c11\u8fde\u63a5\u6ca1\u6709\u76f4\u63a5\u5173\u7cfb\u3002**\u5173\u6ce8\u4e00\u4e07\u4e2a\u8fde\u63a5\u3001\u6bcf\u8f6e\u6700\u591a\u8fd4\u56de 128 \u9879&#xff0c;\u662f\u5b8c\u5168\u6b63\u5e38\u7684\u7528\u6cd5\u3002\u5982\u679c\u5f53\u524d\u53ef\u62a5\u544a\u9879\u8d85\u8fc7\u4e00\u6279&#xff0c;\u540e\u7eed\u8c03\u7528\u8fd8\u80fd\u7ee7\u7eed\u53d6\u5f97\u5176\u4ed6\u9879&#xff1b;\u5185\u6838\u4e5f\u6709\u8f6e\u8f6c\u5904\u7406\u673a\u5236\u3002\u89c4\u5219\u89c1 epoll_wait(2)\u3002<\/p>\n<p>\u53d6\u56de\u4e8b\u4ef6\u540e&#xff0c;\u53ea\u904d\u5386\u672c\u6b21\u6709\u6548\u8303\u56f4&#xff1a;<\/p>\n<p>for (int i &#061; 0; i &lt; n; &#043;&#043;i) {<br \/>\n    \/\/ events[0] \u5230 events[n &#8211; 1] \u624d\u662f\u672c\u6b21\u8fd4\u56de\u7684\u5185\u5bb9\u3002<br \/>\n}<\/p>\n<p>\u81f3\u6b64&#xff0c;\u53ef\u4ee5\u628a\u63a5\u53e3\u8bb0\u6210\u4e09\u53e5\u8bdd&#xff1a;<\/p>\n<p>create&#xff1a;\u5efa\u7acb\u5bb9\u5668\u3002ctl&#xff1a;\u7ef4\u62a4\u5173\u6ce8\u9879\u3002wait&#xff1a;\u53d6\u56de\u5c31\u7eea\u9879\u3002<\/p>\n<h3 id=\"\u56db\u3001\u56de\u5230\u5185\u6838\uff1a\u7ea2\u9ed1\u6811\u3001\u5c31\u7eea\u961f\u5217\u548c\u56de\u8c03\u600e\u6837\u914d\u5408\uff1f\">\u56db\u3001\u56de\u5230\u5185\u6838&#xff1a;\u7ea2\u9ed1\u6811\u3001\u5c31\u7eea\u961f\u5217\u548c\u56de\u8c03\u600e\u6837\u914d\u5408&#xff1f;<\/h3>\n<p>\u7406\u89e3\u63a5\u53e3\u540e&#xff0c;\u518d\u770b\u5b9e\u73b0&#xff0c;\u5f88\u591a\u7ed3\u6784\u5c31\u6709\u4e86\u7528\u9014\u3002<\/p>\n<h4 id=\"41-epfd-\u4e3a\u4ec0\u4e48\u4e5f\u662f\u6587\u4ef6\u63cf\u8ff0\u7b26\uff1f\">4.1 epfd \u4e3a\u4ec0\u4e48\u4e5f\u662f\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff1f;<\/h4>\n<p>fd \u53ef\u4ee5\u7406\u89e3\u4e3a\u8fdb\u7a0b\u8bbf\u95ee\u5185\u6838\u5bf9\u8c61\u7684\u4e00\u628a\u7d22\u5f15\u94a5\u5319\u3002\u5b83\u5bf9\u5e94\u7684\u5bf9\u8c61\u4e0d\u4e00\u5b9a\u662f\u78c1\u76d8\u6587\u4ef6&#xff0c;\u4e5f\u53ef\u4ee5\u662f socket \u6216 epoll \u5b9e\u4f8b\u3002<\/p>\n<p>\u5728\u5e38\u89c1 Linux \u5b9e\u73b0\u4e2d&#xff0c;\u53ef\u4ee5\u6cbf\u7740\u4e0b\u9762\u7684\u5173\u7cfb\u7406\u89e3 epfd&#xff1a;<\/p>\n<p>\u8fdb\u7a0b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868<br \/>\n        \u2193 \u7528 epfd \u627e\u5230<br \/>\nstruct file<br \/>\n        \u2193 private_data<br \/>\nstruct eventpoll<\/p>\n<p>\u8bfe\u4ef6\u91cc\u7684 eventpoll \u4fdd\u5b58\u4e86\u7ba1\u7406\u5173\u6ce8\u9879\u548c\u4ea4\u4ed8\u5c31\u7eea\u9879\u6240\u9700\u8981\u7684\u7ed3\u6784\u3002\u793a\u610f\u5982\u4e0b&#xff0c;\u7701\u7565\u9501\u3001\u5f15\u7528\u8ba1\u6570\u7b49\u5b9e\u73b0\u7ec6\u8282&#xff1a;<\/p>\n<p>eventpoll<br \/>\n  \u251c\u2500\u2500 \u5173\u6ce8\u96c6\u5408&#xff1a;\u5e38\u89c1\u5b9e\u73b0\u4f7f\u7528\u7ea2\u9ed1\u6811<br \/>\n  \u251c\u2500\u2500 \u5c31\u7eea\u961f\u5217&#xff1a;\u8bb0\u5f55\u9700\u8981\u68c0\u67e5\u548c\u4ea4\u4ed8\u7684\u5173\u6ce8\u9879<br \/>\n  \u2514\u2500\u2500 \u7b49\u5f85\u961f\u5217&#xff1a;\u7ec4\u7ec7\u6b63\u5728\u7b49\u5f85\u4e8b\u4ef6\u7684\u7ebf\u7a0b<\/p>\n<p>\u5173\u6ce8\u96c6\u5408\u3001\u5c31\u7eea\u961f\u5217\u3001\u7b49\u5f85\u7ebf\u7a0b\u7684\u961f\u5217&#xff0c;\u662f\u4e09\u79cd\u4e0d\u540c\u7528\u9014\u7684\u7ed3\u6784\u3002\u201c\u6709\u591a\u5c11\u8fde\u63a5\u201d\u201c\u54ea\u4e9b\u8fde\u63a5\u53ef\u80fd\u5c31\u7eea\u201d\u201c\u54ea\u4e9b\u7ebf\u7a0b\u6b63\u5728\u7761\u7720\u201d&#xff0c;\u4e0d\u80fd\u6df7\u6210\u4e00\u4e2a\u95ee\u9898\u3002<\/p>\n<h4 id=\"42-\u4e00\u4e2a-epitem-\u53ef\u4ee5\u540c\u65f6\u5c5e\u4e8e\u7ea2\u9ed1\u6811\u548c\u94fe\u8868\">4.2 \u4e00\u4e2a epitem \u53ef\u4ee5\u540c\u65f6\u5c5e\u4e8e\u7ea2\u9ed1\u6811\u548c\u94fe\u8868<\/h4>\n<p>\u6bcf\u4e2a\u6ce8\u518c\u7684\u5173\u6ce8\u9879\u901a\u5e38\u5bf9\u5e94\u4e00\u4e2a epitem\u3002\u540c\u4e00\u4e2a\u5bf9\u8c61\u5185&#xff0c;\u53ef\u4ee5\u5d4c\u5165\u7ea2\u9ed1\u6811\u8282\u70b9\u6210\u5458\u548c\u94fe\u8868\u8282\u70b9\u6210\u5458&#xff1a;<\/p>\n<p>epitem<br \/>\n  \u251c\u2500\u2500 \u7ea2\u9ed1\u6811\u8282\u70b9\u6210\u5458&#xff1a;\u7528\u4e8e\u7ec4\u7ec7\u5173\u6ce8\u96c6\u5408<br \/>\n  \u251c\u2500\u2500 \u5c31\u7eea\u94fe\u8868\u8282\u70b9\u6210\u5458&#xff1a;\u7528\u4e8e\u94fe\u63a5\u5c31\u7eea\u9879<br \/>\n  \u251c\u2500\u2500 \u76ee\u6807\u6587\u4ef6\u4e0e fd \u7684\u4fe1\u606f<br \/>\n  \u251c\u2500\u2500 \u5173\u6ce8\u4e8b\u4ef6\u4e0e\u7528\u6237 data<br \/>\n  \u2514\u2500\u2500 \u6240\u5c5e\u7684 eventpoll<\/p>\n<p>\u56e0\u6b64&#xff0c;\u4e00\u4e2a\u5bf9\u8c61\u80fd\u591f\u540c\u65f6\u53c2\u4e0e\u591a\u79cd\u6570\u636e\u7ed3\u6784&#xff1a;\u6811\u548c\u94fe\u8868\u901a\u8fc7\u4e0d\u540c\u7684\u94fe\u63a5\u6210\u5458\u5f15\u7528\u5b83&#xff0c;\u65e0\u987b\u5404\u81ea\u590d\u5236\u4e00\u4efd\u8fde\u63a5\u6570\u636e\u3002<\/p>\n<p>\u7ea2\u9ed1\u6811\u4e3b\u8981\u670d\u52a1\u4e8e\u5173\u6ce8\u9879\u7684\u67e5\u627e\u3001\u589e\u52a0\u3001\u5220\u9664\u7b49\u7ba1\u7406\u64cd\u4f5c\u3002\u6559\u5b66\u65f6\u5e38\u7b80\u5199\u4e3a\u201c\u6309 fd \u7ba1\u7406\u201d&#xff0c;\u66f4\u4e25\u683c\u5730\u8bf4&#xff0c;\u5173\u6ce8\u9879\u7684\u8eab\u4efd\u4e0e fd \u6570\u503c\u53ca\u5176\u5173\u8054\u7684\u5185\u6838\u6253\u5f00\u6587\u4ef6\u63cf\u8ff0\u6709\u5173&#xff0c;\u4e0d\u80fd\u628a\u6240\u6709\u573a\u666f\u90fd\u7b80\u5316\u6210\u4e00\u4e2a\u6574\u6570\u3002\u5c24\u5176\u5728\u6d89\u53ca dup\u3001fork \u65f6&#xff0c;\u8981\u7559\u610f\u8fd9\u4e00\u70b9\u3002<\/p>\n<p>\u6811\u4e2d\u6709 N \u4e2a\u8282\u70b9\u65f6&#xff0c;\u7ea2\u9ed1\u6811\u76f8\u5173\u64cd\u4f5c\u901a\u5e38\u7528 O(log N) \u63cf\u8ff0&#xff1b;\u8fd9\u91cc\u7684 N \u662f\u8282\u70b9\u6570\u91cf\u3002<\/p>\n<h4 id=\"43-fd-\u5c31\u7eea\u4ee5\u540e\uff0c\u8c01\u628a\u5b83\u653e\u8fdb\u5c31\u7eea\u961f\u5217\uff1f\">4.3 fd \u5c31\u7eea\u4ee5\u540e&#xff0c;\u8c01\u628a\u5b83\u653e\u8fdb\u5c31\u7eea\u961f\u5217&#xff1f;<\/h4>\n<p>\u8fd9\u4e00\u6b65\u7531\u5185\u6838\u4e2d\u7684\u901a\u77e5\u4e0e\u56de\u8c03\u673a\u5236\u8854\u63a5\u3002<\/p>\n<p>\u6ce8\u518c\u5173\u6ce8\u9879\u65f6&#xff0c;epoll \u4f1a\u501f\u52a9\u76ee\u6807\u6587\u4ef6\u7684 poll \u652f\u6301&#xff0c;\u628a\u76f8\u5173\u7b49\u5f85\u9879\u6302\u5230\u76ee\u6807\u5bf9\u8c61\u7684\u7b49\u5f85\u961f\u5217\u4e0a\u3002\u7f51\u7edc\u534f\u8bae\u6808\u4f7f socket \u72b6\u6001\u53d8\u5316\u5e76\u89e6\u53d1\u5524\u9192\u65f6&#xff0c;\u76f8\u5173\u56de\u8c03\u4fbf\u6709\u673a\u4f1a\u8fd0\u884c\u3002<\/p>\n<p>\u53ef\u4ee5\u628a\u666e\u901a\u63a5\u6536\u8def\u5f84\u6982\u62ec\u6210\u4e0b\u9762\u7684\u793a\u610f\u56fe&#xff1b;\u5b83\u7701\u7565\u4e86\u5177\u4f53\u7f51\u5361\u3001NAPI\u3001\u8f6f\u4e2d\u65ad\u7b49\u5b9e\u73b0\u7ec6\u8282&#xff1a;<\/p>\n<p>flowchart TD<br \/>\n    A[\u5e94\u7528\u901a\u8fc7 epoll_ctl \u6ce8\u518c\u5173\u6ce8\u9879] &#8211;&gt; B[\u5efa\u7acb\u76ee\u6807\u5bf9\u8c61\u7684\u7b49\u5f85\u4e0e\u56de\u8c03\u5173\u8054]<br \/>\n    C[\u7f51\u7edc\u6570\u636e\u7ecf\u8fc7\u534f\u8bae\u6808\u5230\u8fbe socket] &#8211;&gt; D[socket \u72b6\u6001\u53d8\u5316\u5e76\u89e6\u53d1\u5524\u9192]<br \/>\n    B &#8211;&gt; E[\u76f8\u5173 epoll \u56de\u8c03\u8fd0\u884c]<br \/>\n    D &#8211;&gt; E<br \/>\n    E &#8211;&gt; F[\u7b26\u5408\u6761\u4ef6\u7684\u5173\u6ce8\u9879\u8fdb\u5165\u5c31\u7eea\u961f\u5217]<br \/>\n    F &#8211;&gt; G[\u5524\u9192\u7b49\u5f85\u4e2d\u7684 epoll_wait]<br \/>\n    G &#8211;&gt; H[\u5411\u7528\u6237\u6570\u7ec4\u4ea4\u4ed8\u4e8b\u4ef6]<br \/>\n    H &#8211;&gt; I[\u5e94\u7528\u8c03\u7528 recv \u6216 send]<\/p>\n<p>\u8fd9\u91cc\u6709\u4e24\u4e2a\u5173\u952e\u7ec6\u8282&#xff1a;<\/p>\n<li>\u56de\u8c03\u5173\u8054\u5df2\u7ecf\u5efa\u7acb&#xff0c;\u56e0\u6b64\u56de\u8c03\u80fd\u591f\u5173\u8054\u5230\u5bf9\u5e94\u7684\u5173\u6ce8\u9879&#xff0c;\u4e0d\u9700\u8981\u6bcf\u6765\u4e00\u4efd\u6570\u636e\u90fd\u904d\u5386\u6574\u68f5\u5173\u6ce8\u6811\u3002<\/li>\n<li>\u6ce8\u518c\u65f6\u4e5f\u4f1a\u68c0\u67e5\u5f53\u524d\u72b6\u6001\u3002\u5373\u4f7f\u6570\u636e\u5148\u5230\u3001\u5e94\u7528\u540e\u6267\u884c ADD&#xff0c;\u4e5f\u4e0d\u80fd\u7b80\u5355\u8ba4\u4e3a\u201c\u9519\u8fc7\u5230\u8fbe\u65f6\u523b\u5c31\u6c38\u8fdc\u6536\u4e0d\u5230\u4e8b\u4ef6\u201d\u3002<\/li>\n<p>ep_poll_callback \u8fd0\u884c\u5728\u5185\u6838\u4e2d\u3002\u6211\u4eec\u5728\u5e94\u7528\u91cc\u7f16\u5199\u7684\u8bfb\u5904\u7406\u51fd\u6570&#xff0c;\u662f\u4e8b\u4ef6\u5faa\u73af\u62ff\u5230\u4e8b\u4ef6\u540e\u81ea\u884c\u8c03\u7528\u7684&#xff0c;\u4e24\u8005\u5904\u5728\u4e0d\u540c\u5c42\u6b21\u3002<\/p>\n<p>\u4e0a\u8ff0\u7ed3\u6784\u4e0e\u8def\u5f84\u53ef\u4ee5\u5bf9\u7167 Linux v6.12 \u7684 fs\/eventpoll.c \u9605\u8bfb\u3002\u8bfe\u4ef6\u622a\u53d6\u4e86\u8f83\u65e9\u7248\u672c\u6e90\u7801&#xff0c;\u5b57\u6bb5\u540d\u79f0\u4e0e\u73b0\u4ee3\u7248\u672c\u53ef\u80fd\u4e0d\u540c&#xff1b;\u5b66\u4e60\u65f6\u91cd\u70b9\u7406\u89e3\u804c\u8d23\u4e0e\u5173\u8054\u3002<\/p>\n<h4 id=\"44-\u5c31\u7eea\u961f\u5217\u4fdd\u5b58\u7684\u662f\u7ebf\u7d22\uff0c\u6570\u636e\u8fd8\u5728-socket-\u4e2d\">4.4 \u5c31\u7eea\u961f\u5217\u4fdd\u5b58\u7684\u662f\u7ebf\u7d22&#xff0c;\u6570\u636e\u8fd8\u5728 socket \u4e2d<\/h4>\n<p>\u201c\u5c31\u7eea\u961f\u5217\u91cc\u6709 fd\u201d\u5f88\u5bb9\u6613\u8ba9\u4eba\u4ea7\u751f\u53e6\u4e00\u4e2a\u8bef\u89e3&#xff1a;\u961f\u5217\u4e2d\u662f\u4e0d\u662f\u5df2\u7ecf\u653e\u597d\u4e86\u4e00\u6761\u6761\u5b8c\u6574\u8bf7\u6c42&#xff1f;<\/p>\n<p>\u5b9e\u9645\u5e94\u5f53\u8fd9\u6837\u7406\u89e3&#xff1a;<\/p>\n<ul>\n<li>\u961f\u5217\u7ec4\u7ec7\u7684\u662f\u5173\u6ce8\u9879\u53ca\u5176\u5c31\u7eea\u7ebf\u7d22\u3002<\/li>\n<li>\u63a5\u6536\u5230\u7684 TCP \u6570\u636e\u4ecd\u7531 socket \u7684\u63a5\u6536\u8def\u5f84\u7ba1\u7406\u3002<\/li>\n<li>\u591a\u6b21\u72b6\u6001\u53d8\u5316\u53ef\u80fd\u5408\u5e76\u6210\u4e00\u6b21\u4e8b\u4ef6\u4ea4\u4ed8\u3002<\/li>\n<li>\u5185\u6838\u4ea4\u4ed8\u4e8b\u4ef6\u65f6\u8fd8\u53ef\u80fd\u590d\u67e5\u5f53\u524d\u72b6\u6001\u3002<\/li>\n<li>\u5e94\u7528\u6700\u7ec8\u4ecd\u4ee5 recv\u3001send \u7684\u7ed3\u679c\u63a8\u8fdb\u8fde\u63a5\u72b6\u6001\u3002<\/li>\n<\/ul>\n<p>\u56e0\u6b64&#xff0c;\u4e8b\u4ef6\u6570\u91cf\u4e0d\u80fd\u5f53\u4f5c TCP \u62a5\u6587\u6bb5\u6570\u91cf&#xff0c;\u66f4\u4e0d\u80fd\u5f53\u4f5c\u5e94\u7528\u8bf7\u6c42\u6570\u91cf\u3002<\/p>\n<p>\u5c31\u7eea\u961f\u5217\u4e5f\u53ef\u4ee5\u4ece\u201c\u5185\u6838\u4ea7\u751f\u5f85\u5904\u7406\u7ebf\u7d22&#xff0c;\u5e94\u7528\u6d88\u8d39\u7ebf\u7d22\u201d\u7684\u89d2\u5ea6\u5e2e\u52a9\u7406\u89e3&#xff0c;\u4f46\u5b83\u5177\u6709\u5408\u5e76\u3001\u590d\u67e5\u3001\u91cd\u65b0\u5165\u961f\u7b49\u8bed\u4e49&#xff0c;\u4e0d\u80fd\u5957\u7528\u666e\u901a\u6d88\u606f\u961f\u5217\u7684\u9010\u6761\u6d88\u606f\u6a21\u578b\u3002<\/p>\n<h3 id=\"\u4e94\u3001epoll-\u4e3a\u4ec0\u4e48\u5feb\uff1f\u628a-o1-\u7684\u8bf4\u6cd5\u8bb2\u51c6\u786e\">\u4e94\u3001epoll \u4e3a\u4ec0\u4e48\u5feb&#xff1f;\u628a O(1) \u7684\u8bf4\u6cd5\u8bb2\u51c6\u786e<\/h3>\n<h4 id=\"51-\u4f18\u52bf\u6765\u81ea\u51cf\u5c11\u4e0e\u7a7a\u95f2\u8fde\u63a5\u6709\u5173\u7684\u91cd\u590d\u5de5\u4f5c\">5.1 \u4f18\u52bf\u6765\u81ea\u51cf\u5c11\u4e0e\u7a7a\u95f2\u8fde\u63a5\u6709\u5173\u7684\u91cd\u590d\u5de5\u4f5c<\/h4>\n<p>\u56de\u5230\u5f00\u5934\u7684\u4e00\u4e07\u4e2a\u8fde\u63a5\u3001\u5341\u4e2a\u6d3b\u8dc3\u8fde\u63a5&#xff1a;<\/p>\n<ul>\n<li>\u5173\u6ce8\u5173\u7cfb\u5df2\u7ecf\u4fdd\u5b58\u5728\u5185\u6838\u91cc\u3002<\/li>\n<li>\u72b6\u6001\u53d8\u5316\u901a\u8fc7\u5df2\u6709\u673a\u5236\u5173\u8054\u5230\u5173\u6ce8\u9879\u3002<\/li>\n<li>epoll_wait \u4ece\u5c31\u7eea\u5019\u9009\u9879\u4e2d\u6536\u96c6\u53ef\u4ea4\u4ed8\u4e8b\u4ef6\u3002<\/li>\n<li>\u5e94\u7528\u904d\u5386\u672c\u6b21\u8fd4\u56de\u7684\u4e8b\u4ef6\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<p>\u4e0e\u6bcf\u8f6e\u91cd\u65b0\u5904\u7406\u6574\u4efd\u5173\u6ce8\u6570\u7ec4\u76f8\u6bd4&#xff0c;\u8fd9\u79cd\u65b9\u5f0f\u51cf\u5c11\u4e86\u5927\u91cf\u7a7a\u95f2\u8fde\u63a5\u5e26\u6765\u7684\u91cd\u590d\u68c0\u67e5\u4e0e\u4f20\u9012\u6210\u672c\u3002<\/p>\n<h4 id=\"52-\u68c0\u67e5\u961f\u5217\u662f\u5426\u4e3a\u7a7a\u548c\u4ea4\u4ed8-k-\u4e2a\u4e8b\u4ef6\u8981\u5206\u5f00\">5.2 \u201c\u68c0\u67e5\u961f\u5217\u662f\u5426\u4e3a\u7a7a\u201d\u548c\u201c\u4ea4\u4ed8 K \u4e2a\u4e8b\u4ef6\u201d\u8981\u5206\u5f00<\/h4>\n<p>\u5047\u8bbe\u603b\u5173\u6ce8\u9879\u4e3a N&#xff0c;\u672c\u6b21\u5b9e\u9645\u4ea4\u4ed8 K \u9879\u4e8b\u4ef6&#xff1a;<\/p>\n<table>\n<tr>\u64cd\u4f5c\u5408\u7406\u7684\u7406\u89e3\u65b9\u5f0f<\/tr>\n<tbody>\n<tr>\n<td>\u770b\u5c31\u7eea\u961f\u5217\u662f\u5426\u4e3a\u7a7a<\/td>\n<td>\u8fd9\u4e2a\u5c40\u90e8\u5224\u65ad\u53ef\u4ee5\u662f\u5e38\u6570\u7ea7\u64cd\u4f5c<\/td>\n<\/tr>\n<tr>\n<td>\u7ba1\u7406\u7ea2\u9ed1\u6811\u4e2d\u7684\u5173\u6ce8\u9879<\/td>\n<td>\u901a\u5e38\u6d89\u53ca O(log N) \u7684\u6811\u64cd\u4f5c<\/td>\n<\/tr>\n<tr>\n<td>\u590d\u5236\u5e76\u904d\u5386\u672c\u6b21 K \u9879\u7ed3\u679c<\/td>\n<td>\u81f3\u5c11\u4e0e K \u6709\u5173<\/td>\n<\/tr>\n<tr>\n<td>\u8bfb\u53d6\u3001\u89e3\u6790\u3001\u53d1\u9001\u4e1a\u52a1\u6570\u636e<\/td>\n<td>\u4e0e\u5b9e\u9645\u6570\u636e\u91cf\u548c\u4e1a\u52a1\u903b\u8f91\u6709\u5173<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6240\u4ee5&#xff0c;\u201cepoll \u7684\u6240\u6709\u64cd\u4f5c\u90fd\u662f O(1)&#xff0c;\u8fde\u63a5\u518d\u591a\u4e5f\u4e0d\u5f71\u54cd\u6027\u80fd\u201d\u5e76\u4e0d\u51c6\u786e\u3002<\/p>\n<p>\u5e38\u89c1\u7684\u201cselect\/poll \u7ea6\u4e3a O(N)&#xff0c;epoll \u7ea6\u4e3a O(K)\u201d\u662f\u4e00\u79cd\u7a81\u51fa\u4e3b\u8981\u5dee\u5f02\u7684\u6559\u5b66\u6a21\u578b\u3002\u771f\u5b9e\u5185\u6838\u8fd8\u6709\u5019\u9009\u72b6\u6001\u590d\u67e5\u3001\u5e76\u53d1\u63a7\u5236\u3001\u56de\u8c03\u3001\u7f13\u5b58\u8bbf\u95ee\u7b49\u6210\u672c&#xff0c;\u4e0d\u80fd\u628a\u8fd9\u4e2a\u6a21\u578b\u5f53\u6210\u6240\u6709\u8d1f\u8f7d\u4e0b\u7684\u4e25\u683c\u6027\u80fd\u4fdd\u8bc1\u3002<\/p>\n<h4 id=\"53-epoll-\u4ecd\u7136\u9700\u8981\u590d\u5236\u4e8b\u4ef6\u7ed3\u679c\">5.3 epoll \u4ecd\u7136\u9700\u8981\u590d\u5236\u4e8b\u4ef6\u7ed3\u679c<\/h4>\n<p>\u6211\u4eec\u5df2\u7ecf\u770b\u5230&#xff0c;events \u6570\u7ec4\u7531\u5e94\u7528\u63d0\u4f9b&#xff0c;\u5185\u6838\u628a\u7ed3\u679c\u5199\u5230\u8fd9\u5757\u7528\u6237\u5185\u5b58\u4e2d\u3002<\/p>\n<p>\u6240\u4ee5&#xff0c;\u4e0d\u80fd\u628a epoll \u7684\u9ad8\u6548\u89e3\u91ca\u6210\u201c\u7528 mmap \u628a\u5c31\u7eea\u961f\u5217\u6620\u5c04\u7ed9\u7528\u6237&#xff0c;\u56e0\u6b64\u5b8c\u5168\u6ca1\u6709\u590d\u5236\u201d\u3002\u6b63\u786e\u7684\u7406\u89e3\u662f&#xff1a;\u5b83\u51cf\u5c11\u4e86\u91cd\u590d\u4f20\u9012\u5b8c\u6574\u5173\u6ce8\u96c6\u5408\u7684\u5de5\u4f5c&#xff0c;\u5e76\u96c6\u4e2d\u4ea4\u4ed8\u5f53\u524d\u4e8b\u4ef6\u7ed3\u679c\u3002<\/p>\n<p>\u666e\u901a recv \u7684\u6570\u636e\u590d\u5236\u53c8\u662f\u53e6\u4e00\u5c42\u5f00\u9500&#xff0c;\u4e0d\u5e94\u548c epoll \u7684\u4e8b\u4ef6\u7ed3\u679c\u590d\u5236\u6df7\u4e3a\u4e00\u8c08\u3002<\/p>\n<h4 id=\"54-\u6ca1\u6709-fd_set-\u90a3\u79cd\u56fa\u5b9a\u9650\u5236\uff0c\u4e5f\u4ecd\u7136\u6709\u8d44\u6e90\u4e0a\u9650\">5.4 \u6ca1\u6709 fd_set \u90a3\u79cd\u56fa\u5b9a\u9650\u5236&#xff0c;\u4e5f\u4ecd\u7136\u6709\u8d44\u6e90\u4e0a\u9650<\/h4>\n<p>epoll \u7684\u5b9e\u9645\u89c4\u6a21\u4f1a\u53d7\u5230\u8fdb\u7a0b fd \u9650\u989d\u3001\u7cfb\u7edf\u8d44\u6e90\u3001\u5185\u6838\u5173\u6ce8\u9879\u9650\u989d\u3001socket \u7f13\u51b2\u533a\u548c\u5e94\u7528\u5185\u5b58\u7b49\u56e0\u7d20\u5f71\u54cd\u3002<\/p>\n<p>\u53ef\u4ee5\u5148\u67e5\u770b&#xff1a;<\/p>\n<p>ulimit -n<br \/>\ncat \/proc\/sys\/fs\/epoll\/max_user_watches<\/p>\n<p>\u7b2c\u4e8c\u9879\u5c5e\u4e8e\u6309\u771f\u5b9e\u7528\u6237 ID \u8ba1\u7b97\u7684 epoll \u5173\u6ce8\u9879\u8d44\u6e90\u9650\u5236\u4e4b\u4e00\u3002\u5927\u89c4\u6a21\u670d\u52a1\u8fd8\u8981\u8bc4\u4f30\u8fde\u63a5\u5bf9\u8c61\u3001\u6536\u53d1\u7f13\u51b2\u533a\u548c\u603b\u5185\u5b58\u9884\u7b97\u3002<\/p>\n<h4 id=\"55-\u4ec0\u4e48\u573a\u666f\u6700\u5bb9\u6613\u4f53\u73b0\u4f18\u52bf\uff1f\">5.5 \u4ec0\u4e48\u573a\u666f\u6700\u5bb9\u6613\u4f53\u73b0\u4f18\u52bf&#xff1f;<\/h4>\n<p>\u201c\u8fde\u63a5\u591a&#xff0c;\u800c\u4e14\u5927\u90e8\u5206\u8fde\u63a5\u5728\u591a\u6570\u65f6\u5019\u7a7a\u95f2\u201d&#xff0c;\u662f\u5f88\u5178\u578b\u7684\u573a\u666f\u3002<\/p>\n<p>\u5982\u679c\u53ea\u6709\u51e0\u4e2a\u8fde\u63a5&#xff0c;\u6216\u8005\u51e0\u4e4e\u6240\u6709\u8fde\u63a5\u4e00\u76f4\u5f88\u5fd9&#xff0c;\u6216\u8005\u8fde\u63a5\u6ce8\u518c\u3001\u5220\u9664\u6781\u5176\u9891\u7e41&#xff0c;epoll \u7684\u4f18\u52bf\u53ef\u80fd\u4e0d\u660e\u663e&#xff0c;\u5176\u4ed6\u6210\u672c\u4e5f\u53ef\u80fd\u6210\u4e3a\u4e3b\u5bfc\u3002<\/p>\n<p>\u8fde\u63a5\u5c11\u65f6\u4e5f\u5b8c\u5168\u53ef\u4ee5\u4f7f\u7528 epoll&#xff0c;\u5c24\u5176\u662f\u5df2\u6709\u6846\u67b6\u5df2\u7ecf\u7edf\u4e00\u91c7\u7528\u4e8b\u4ef6\u5faa\u73af\u65f6\u3002\u5177\u4f53\u9009\u578b\u8981\u770b\u590d\u6742\u5ea6\u3001\u5e73\u53f0\u9700\u6c42\u548c\u5b9e\u6d4b\u7ed3\u679c\u3002<\/p>\n<h3 id=\"\u516d\u3001lt-\u4e0e-et\uff1a\u7528\u6765\u4e86-2kb\uff0c\u53ea\u8bfb-1kb\u7406\u89e3\u901a\u77e5\u65b9\u5f0f\">\u516d\u3001LT \u4e0e ET&#xff1a;\u7528\u201c\u6765\u4e86 2KB&#xff0c;\u53ea\u8bfb 1KB\u201d\u7406\u89e3\u901a\u77e5\u65b9\u5f0f<\/h3>\n<h4 id=\"61-lt\uff1a\u6761\u4ef6\u4ecd\u7136\u6210\u7acb\uff0c\u5c31\u8fd8\u80fd\u518d\u6b21\u5f97\u5230\u901a\u77e5\">6.1 LT&#xff1a;\u6761\u4ef6\u4ecd\u7136\u6210\u7acb&#xff0c;\u5c31\u8fd8\u80fd\u518d\u6b21\u5f97\u5230\u901a\u77e5<\/h4>\n<p>LT \u662f Level Triggered&#xff0c;\u6c34\u5e73\u89e6\u53d1&#xff0c;\u4e5f\u662f epoll \u7684\u9ed8\u8ba4\u65b9\u5f0f\u3002<\/p>\n<p>\u5bf9\u4e8e\u6ca1\u6709\u5173\u95ed\u3001\u6ca1\u6709\u5176\u4ed6\u7279\u6b8a\u72b6\u6001\u7684\u666e\u901a TCP \u8fde\u63a5&#xff0c;\u5047\u8bbe&#xff1a;<\/p>\n<li>\u63a5\u6536\u7f13\u51b2\u533a\u539f\u672c\u4e3a\u7a7a\u3002<\/li>\n<li>\u5bf9\u7aef\u53d1\u6765 2KB \u6570\u636e\u3002<\/li>\n<li>epoll_wait \u8fd4\u56de\u8bfb\u4e8b\u4ef6\u3002<\/li>\n<li>\u5e94\u7528\u53ea\u8bfb\u53d6 1KB&#xff0c;\u7f13\u51b2\u533a\u91cc\u8fd8\u6709 1KB\u3002<\/li>\n<li>\u5e94\u7528\u518d\u6b21\u8c03\u7528 epoll_wait\u3002<\/li>\n<p>\u56e0\u4e3a\u53ef\u8bfb\u6761\u4ef6\u4ecd\u7136\u6210\u7acb&#xff0c;\u8fd9\u4e2a\u8fde\u63a5\u4ecd\u53ef\u4ee5\u518d\u6b21\u88ab\u62a5\u544a\u3002<\/p>\n<p>LT \u7ed9\u5e94\u7528\u63d0\u4f9b\u4e86\u6bd4\u8f83\u76f4\u89c2\u7684\u5904\u7406\u65b9\u5f0f&#xff1a;\u8fd9\u4e00\u8f6e\u53ea\u5904\u7406\u4e00\u90e8\u5206&#xff0c;\u540e\u9762\u8fd8\u53ef\u4ee5\u7ee7\u7eed\u63a5\u5230\u901a\u77e5\u3002<\/p>\n<p>\u4f46\u5b83\u4e5f\u6709\u4ee3\u4ef7&#xff1a;\u5982\u679c\u5e94\u7528\u4e00\u76f4\u4e0d\u8bfb&#xff0c;\u8bfb\u5c31\u7eea\u53ef\u80fd\u53cd\u590d\u8fd4\u56de&#xff1b;\u5982\u679c\u4e00\u76f4\u5173\u6ce8\u4e00\u4e2a\u901a\u5e38\u53ef\u5199\u7684 socket&#xff0c;\u5199\u4e8b\u4ef6\u4e5f\u53ef\u80fd\u53cd\u590d\u8fd4\u56de\u3002<\/p>\n<p>\u8865\u5145\u4e00\u4e2a\u8fb9\u754c&#xff1a;\u5373\u4f7f\u7f13\u51b2\u533a\u7684\u6570\u636e\u8bfb\u5b8c\u4e86&#xff0c;EOF \u6216\u9519\u8bef\u7b49\u6761\u4ef6\u4ecd\u53ef\u80fd\u8ba9\u8bfb\u65b9\u5411\u53ef\u62a5\u544a\u3002\u56e0\u6b64&#xff0c;\u201c\u8bfb\u7a7a\u4ee5\u540e\u5c31\u7edd\u5bf9\u6ca1\u6709\u8bfb\u4e8b\u4ef6\u201d\u4e5f\u4e0d\u662f\u901a\u7528\u7ed3\u8bba\u3002<\/p>\n<h4 id=\"62-et\uff1a\u4e0d\u80fd\u4f9d\u8d56\u65e7\u6761\u4ef6\u6301\u7eed\u6210\u7acb\u6765\u83b7\u5f97\u91cd\u590d\u63d0\u9192\">6.2 ET&#xff1a;\u4e0d\u80fd\u4f9d\u8d56\u65e7\u6761\u4ef6\u6301\u7eed\u6210\u7acb\u6765\u83b7\u5f97\u91cd\u590d\u63d0\u9192<\/h4>\n<p>ET \u662f Edge Triggered&#xff0c;\u8fb9\u7f18\u89e6\u53d1&#xff0c;\u901a\u8fc7 EPOLLET \u542f\u7528&#xff1a;<\/p>\n<p>ev.events &#061; EPOLLIN | EPOLLET;<\/p>\n<p>\u4ecd\u7136\u4f7f\u7528\u521a\u624d\u7684\u4f8b\u5b50&#xff1a;\u6536\u5230 2KB&#xff0c;\u53ea\u8bfb\u53d6 1KB&#xff0c;\u7136\u540e\u76f4\u63a5\u56de\u5230 epoll_wait\u3002<\/p>\n<p>\u5728 ET \u4e0b&#xff0c;\u5269\u4f59 1KB \u5e76\u4e0d\u4fdd\u8bc1\u8ba9\u5185\u6838\u518d\u6b21\u63d0\u9192\u3002\u5982\u679c\u6b64\u540e\u6ca1\u6709\u65b0\u7684\u76f8\u5173\u53d8\u5316&#xff0c;\u7ebf\u7a0b\u53ef\u80fd\u4e00\u76f4\u7b49&#xff0c;\u800c\u6570\u636e\u5176\u5b9e\u5c31\u5728\u63a5\u6536\u7f13\u51b2\u533a\u91cc\u3002<\/p>\n<p>\u8fd9\u6b63\u662f\u6700\u5e38\u89c1\u7684 ET \u5361\u4f4f\u539f\u56e0\u3002<\/p>\n<table>\n<tr>\u540c\u4e00\u4e2a\u8fc7\u7a0bLTET<\/tr>\n<tbody>\n<tr>\n<td>\u6570\u636e\u5230\u8fbe\u5e76\u5f62\u6210\u53ef\u8bfb\u6761\u4ef6<\/td>\n<td>\u53ef\u4ee5\u62a5\u544a\u8bfb\u4e8b\u4ef6<\/td>\n<td>\u53ef\u4ee5\u62a5\u544a\u8bfb\u4e8b\u4ef6<\/td>\n<\/tr>\n<tr>\n<td>\u5e94\u7528\u53ea\u8bfb\u53d6\u4e00\u90e8\u5206<\/td>\n<td>\u53ef\u8bfb\u6761\u4ef6\u7ee7\u7eed\u5b58\u5728<\/td>\n<td>\u53ef\u8bfb\u6761\u4ef6\u7ee7\u7eed\u5b58\u5728<\/td>\n<\/tr>\n<tr>\n<td>\u518d\u6b21\u7b49\u5f85&#xff0c;\u671f\u95f4\u6ca1\u6709\u65b0\u7684\u76f8\u5173\u53d8\u5316<\/td>\n<td>\u901a\u5e38\u4ecd\u53ef\u62a5\u544a<\/td>\n<td>\u4e0d\u80fd\u4f9d\u8d56\u518d\u6b21\u62a5\u544a<\/td>\n<\/tr>\n<tr>\n<td>\u57fa\u7840\u5904\u7406\u7b56\u7565<\/td>\n<td>\u53ef\u4ee5\u5206\u6b21\u5904\u7406&#xff0c;\u4e5f\u53ef\u4ee5\u8bfb\u5230\u6682\u65f6\u65e0\u6570\u636e<\/td>\n<td>\u975e\u963b\u585e\u8bfb\u53d6\u5230 EAGAIN&#xff0c;\u6216\u81ea\u884c\u4fdd\u5b58\u5f85\u7ee7\u7eed\u5904\u7406\u7684\u72b6\u6001<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u91cc\u8981\u7ea0\u6b63\u4e24\u4e2a\u8fc7\u5ea6\u7b80\u5316\u7684\u8bb0\u5fc6\u6cd5&#xff1a;<\/p>\n<ul>\n<li>**ET \u4e0d\u7b49\u4e8e\u4e00\u4e2a\u8fde\u63a5\u4e00\u751f\u53ea\u901a\u77e5\u4e00\u6b21\u3002**\u540e\u7eed I\/O \u6d3b\u52a8\u8fd8\u53ef\u80fd\u89e6\u53d1\u901a\u77e5\u3002<\/li>\n<li>**ET \u4e5f\u4e0d\u5e94\u673a\u68b0\u7406\u89e3\u4e3a\u53ea\u5728\u201c\u7a7a\u53d8\u975e\u7a7a\u201d\u7684\u552f\u4e00\u65f6\u523b\u89e6\u53d1\u3002**Linux \u7684\u5177\u4f53\u901a\u77e5\u6765\u81ea\u5e95\u5c42\u5524\u9192\u673a\u5236&#xff0c;\u65b0\u6570\u636e\u3001\u91cd\u65b0\u6fc0\u6d3b\u7b49\u60c5\u51b5\u90fd\u9700\u8981\u7ed3\u5408\u5b9e\u9645\u8bed\u4e49\u7406\u89e3\u3002<\/li>\n<\/ul>\n<p>\u5bf9\u5199\u7a0b\u5e8f\u6700\u6709\u7528\u7684\u89c4\u5219\u662f&#xff1a;**\u6536\u5230\u901a\u77e5\u540e&#xff0c;\u81ea\u5df1\u63a8\u8fdb I\/O&#xff1b;\u4e0d\u8981\u9760\u4e0b\u4e00\u6b21\u901a\u77e5\u66ff\u4f60\u8865\u505a\u8fd9\u4e00\u8f6e\u5269\u4e0b\u7684\u5de5\u4f5c\u3002**LT\/ET \u7684\u57fa\u7840\u884c\u4e3a\u548c\u4f7f\u7528\u5efa\u8bae\u89c1 epoll(7)\u3002<\/p>\n<h4 id=\"63-\u4e3a\u4ec0\u4e48-et-\u901a\u5e38\u5fc5\u987b\u914d\u5408\u975e\u963b\u585e-io\uff1f\">6.3 \u4e3a\u4ec0\u4e48 ET \u901a\u5e38\u5fc5\u987b\u914d\u5408\u975e\u963b\u585e I\/O&#xff1f;<\/h4>\n<p>\u4e3a\u4e86\u89e3\u51b3\u201c\u53ea\u8bfb\u4e00\u90e8\u5206\u201d\u7684\u95ee\u9898&#xff0c;\u6211\u4eec\u4f1a\u5faa\u73af\u8bfb\u53d6&#xff1a;<\/p>\n<p>\u8bfb\u5230\u6570\u636e \u2192 \u7ee7\u7eed\u8bfb \u2192 \u7ee7\u7eed\u8bfb \u2192 \u2026\u2026<\/p>\n<p>\u5982\u679c socket \u662f\u963b\u585e\u7684&#xff0c;\u8bfb\u5b8c\u5f53\u524d\u5168\u90e8\u6570\u636e\u4ee5\u540e&#xff0c;\u518d\u6267\u884c\u4e00\u6b21 recv&#xff0c;\u7ebf\u7a0b\u5c31\u53ef\u80fd\u505c\u4e0b\u6765\u7b49\u4e0b\u4e00\u6279\u6570\u636e\u3002<\/p>\n<p>\u7ed3\u679c\u662f&#xff1a;\u4e00\u4e2a\u8fde\u63a5\u62d6\u4f4f\u6574\u4e2a\u4e8b\u4ef6\u5faa\u73af\u3002<\/p>\n<p>\u6539\u6210\u975e\u963b\u585e\u540e&#xff0c;\u5f53\u524d\u6ca1\u6709\u6570\u636e\u53ef\u8bfb\u65f6&#xff0c;recv \u4f1a\u8fd4\u56de -1&#xff0c;\u5e76\u628a errno \u8bbe\u7f6e\u4e3a EAGAIN \u6216 EWOULDBLOCK\u3002\u7ebf\u7a0b\u636e\u6b64\u7ed3\u675f\u5f53\u524d\u8bfb\u5904\u7406&#xff0c;\u8f6c\u53bb\u670d\u52a1\u5176\u4ed6\u8fde\u63a5\u3002<\/p>\n<p>\u6240\u4ee5\u8fd9\u91cc\u7684\u903b\u8f91\u662f\u5b8c\u6574\u7684\u4e00\u6761\u94fe&#xff1a;<\/p>\n<p>ET \u4e0d\u4fdd\u8bc1\u6301\u7eed\u63d0\u9192\u672a\u5904\u7406\u5b8c\u7684\u53ef\u8bfb\u6761\u4ef6<br \/>\n                    \u2193<br \/>\n\u5e94\u7528\u8981\u4e3b\u52a8\u63a8\u8fdb\u5f53\u524d\u53ef\u8bfb\u53d6\u7684\u6570\u636e<br \/>\n                    \u2193<br \/>\n\u5faa\u73af\u8bfb\u53d6\u9700\u8981\u4e00\u4e2a\u201c\u76ee\u524d\u8bfb\u5b8c\u4e86\u201d\u7684\u9000\u51fa\u4fe1\u53f7<br \/>\n                    \u2193<br \/>\n\u975e\u963b\u585e recv \u8fd4\u56de EAGAIN&#xff0c;\u4e8b\u4ef6\u5faa\u73af\u5f97\u4ee5\u7ee7\u7eed\u670d\u52a1\u5176\u4ed6\u8fde\u63a5<\/p>\n<p>\u5185\u6838\u63a5\u53e3\u5e76\u4e0d\u4f1a\u56e0\u4e3a socket \u662f\u963b\u585e\u7684\u5c31\u62d2\u7edd\u8bbe\u7f6e EPOLLET\u3002\u975e\u963b\u585e\u662f\u7f16\u5199\u53ef\u9760\u4e8b\u4ef6\u5faa\u73af\u7684\u5de5\u7a0b\u8981\u6c42\u3002<\/p>\n<p>\u540c\u6837&#xff0c;\u5373\u4f7f\u4f7f\u7528 LT&#xff0c;\u670d\u52a1\u5668\u901a\u5e38\u4e5f\u5efa\u8bae\u4f7f\u7528\u975e\u963b\u585e socket\u3002LT \u89e3\u51b3\u901a\u77e5\u65b9\u5f0f\u95ee\u9898&#xff0c;\u65e0\u6cd5\u66ff\u4f60\u6d88\u9664\u963b\u585e send \u6216\u5176\u4ed6\u963b\u585e\u64cd\u4f5c\u9020\u6210\u7684\u505c\u987f\u3002<\/p>\n<h4 id=\"64-\u975e\u963b\u585e\u8bfb\u7684\u56db\u79cd\u7ed3\u679c\uff0c\u8981\u5206\u522b\u5904\u7406\">6.4 \u975e\u963b\u585e\u8bfb\u7684\u56db\u79cd\u7ed3\u679c&#xff0c;\u8981\u5206\u522b\u5904\u7406<\/h4>\n<p>\u5bf9\u4e8e\u8bf7\u6c42\u957f\u5ea6\u5927\u4e8e 0 \u7684 TCP recv&#xff1a;<\/p>\n<table>\n<tr>\u8fd4\u56de\u7ed3\u679c\u542b\u4e49\u901a\u5e38\u600e\u4e48\u5904\u7406<\/tr>\n<tbody>\n<tr>\n<td>n &gt; 0<\/td>\n<td>\u6536\u5230 n \u5b57\u8282<\/td>\n<td>\u6309\u5b9e\u9645\u957f\u5ea6\u4fdd\u5b58&#xff0c;\u7ee7\u7eed\u63a8\u8fdb<\/td>\n<\/tr>\n<tr>\n<td>n &#061;&#061; 0<\/td>\n<td>\u5bf9\u7aef\u53d1\u9001\u65b9\u5411\u7ed3\u675f&#xff0c;\u8bfb\u53d6\u5230 EOF<\/td>\n<td>\u6807\u8bb0\u8bfb\u7aef\u7ed3\u675f&#xff0c;\u518d\u51b3\u5b9a\u4f55\u65f6\u5173\u95ed\u8fde\u63a5<\/td>\n<\/tr>\n<tr>\n<td>n &#061;&#061; -1&#xff0c;EINTR<\/td>\n<td>\u88ab\u4fe1\u53f7\u4e2d\u65ad<\/td>\n<td>\u91cd\u8bd5\u672c\u6b21\u64cd\u4f5c<\/td>\n<\/tr>\n<tr>\n<td>n &#061;&#061; -1&#xff0c;EAGAIN\/EWOULDBLOCK<\/td>\n<td>\u5f53\u524d\u6ca1\u6709\u66f4\u591a\u6570\u636e\u53ef\u8bfb<\/td>\n<td>\u7ed3\u675f\u5f53\u524d\u8bfb\u5904\u7406&#xff0c;\u7b49\u5f85\u540e\u7eed\u4e8b\u4ef6<\/td>\n<\/tr>\n<tr>\n<td>n &#061;&#061; -1&#xff0c;\u5176\u4ed6\u9519\u8bef<\/td>\n<td>\u8fde\u63a5\u6216\u64cd\u4f5c\u51fa\u9519<\/td>\n<td>\u6309\u9519\u8bef\u7b56\u7565\u5904\u7406&#xff0c;\u5e38\u89c1\u505a\u6cd5\u662f\u5173\u95ed\u8fde\u63a5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u4e9b\u7ed3\u679c\u7684\u533a\u522b\u53ef\u53c2\u89c1 recv(2)\u3002\u5bf9\u5e94\u7684\u57fa\u7840\u5faa\u73af\u662f&#xff1a;<\/p>\n<p>for (;;) {<br \/>\n    ssize_t n &#061; recv(fd, buf, sizeof(buf), 0);<br \/>\n    if (n &gt; 0) {<br \/>\n        input.append(buf, static_cast&lt;std::size_t&gt;(n));<br \/>\n        continue;<br \/>\n    }<br \/>\n    if (n &#061;&#061; 0) {<br \/>\n        peer_eof &#061; true;<br \/>\n        break;<br \/>\n    }<br \/>\n    if (errno &#061;&#061; EINTR) continue;<br \/>\n    if (errno &#061;&#061; EAGAIN || errno &#061;&#061; EWOULDBLOCK) break;<br \/>\n    \/\/ \u5176\u4ed6\u9519\u8bef&#xff1a;\u8bb0\u5f55\u3001\u6e05\u7406\u8fde\u63a5\u3002<br \/>\n    break;<br \/>\n}<\/p>\n<p>\u6ce8\u610f append \u5fc5\u987b\u5e26\u4e0a\u957f\u5ea6\u3002\u7f51\u7edc\u6570\u636e\u4e2d\u53ef\u80fd\u5305\u542b \\\\0&#xff0c;\u4e0d\u80fd\u628a\u6240\u6709\u63a5\u6536\u5185\u5bb9\u90fd\u5f53\u4f5c\u4ee5\u96f6\u7ed3\u5c3e\u7684 C \u5b57\u7b26\u4e32\u3002<\/p>\n<h4 id=\"65-eagain-\u662f\u4ea4\u8fd8\u6267\u884c\u6743\u7684\u65f6\u673a\">6.5 EAGAIN \u662f\u4ea4\u8fd8\u6267\u884c\u6743\u7684\u65f6\u673a<\/h4>\n<p>\u4e0b\u9762\u8fd9\u79cd\u5199\u6cd5\u5f88\u5371\u9669&#xff1a;<\/p>\n<p>if (errno &#061;&#061; EAGAIN) {<br \/>\n    continue;<br \/>\n}<\/p>\n<p>\u5982\u679c\u5b83\u4f4d\u4e8e\u540c\u4e00\u4e2a\u8fde\u63a5\u7684\u8bfb\u53d6\u5faa\u73af\u4e2d&#xff0c;\u4e14\u6ca1\u6709\u5176\u4ed6\u8c03\u5ea6\u673a\u5236&#xff0c;\u5c31\u4f1a\u53cd\u590d\u8c03\u7528\u4e00\u4e2a\u6682\u65f6\u8bfb\u4e0d\u5230\u6570\u636e\u7684 recv&#xff0c;\u9020\u6210\u5fd9\u7b49\u3002<\/p>\n<p>\u6b63\u786e\u7684\u57fa\u7840\u884c\u4e3a\u662f\u9000\u51fa\u8fd9\u4e2a\u8fde\u63a5\u7684\u8bfb\u5faa\u73af&#xff0c;\u56de\u5230\u5916\u5c42\u4e8b\u4ef6\u5faa\u73af\u3002<\/p>\n<p>\u5199\u65b9\u5411\u4e5f\u662f\u4e00\u6837\u3002EAGAIN \u8868\u793a\u73b0\u5728\u6682\u65f6\u63a8\u8fdb\u4e0d\u4e86&#xff0c;\u5e94\u7528\u5e94\u5f53\u4fdd\u7559\u72b6\u6001&#xff0c;\u7b49\u5408\u9002\u7684\u4e8b\u4ef6\u540e\u518d\u7ee7\u7eed\u3002<\/p>\n<h4 id=\"66-\u8bfb\u5230-eagain\uff0c\u4e5f\u4e0d\u4ee3\u8868\u4e00\u6761\u8bf7\u6c42\u5df2\u7ecf\u6536\u5b8c\">6.6 \u8bfb\u5230 EAGAIN&#xff0c;\u4e5f\u4e0d\u4ee3\u8868\u4e00\u6761\u8bf7\u6c42\u5df2\u7ecf\u6536\u5b8c<\/h4>\n<p>\u5047\u8bbe\u5e94\u7528\u534f\u8bae\u89c4\u5b9a&#xff1a;\u6bcf\u6761\u8bf7\u6c42\u56fa\u5b9a 10KB\u3002<\/p>\n<p>\u5ba2\u6237\u7aef\u5148\u53d1\u9001 3KB&#xff0c;\u5269\u4e0b 7KB \u8fc7\u4e00\u4f1a\u513f\u624d\u5230\u3002\u670d\u52a1\u5668\u8bfb\u5b8c\u5f53\u524d 3KB \u540e\u9047\u5230 EAGAIN&#xff0c;\u53ea\u80fd\u5f97\u51fa&#xff1a;<\/p>\n<p>\u5f53\u524d\u8fd9\u4e2a\u65f6\u523b&#xff0c;\u6ca1\u6709\u66f4\u591a\u5b57\u8282\u53ef\u8bfb\u3002<\/p>\n<p>\u5b83\u65e0\u6cd5\u5f97\u51fa\u201c10KB \u8bf7\u6c42\u5df2\u7ecf\u5b8c\u6574\u201d\u7684\u7ed3\u8bba\u3002\u5df2\u7ecf\u8bfb\u5230\u7684 3KB \u8981\u4fdd\u5b58\u5728\u8fde\u63a5\u8f93\u5165\u7f13\u51b2\u533a\u91cc&#xff0c;\u7b49\u540e\u7eed\u6570\u636e\u5230\u8fbe\u540e\u7ee7\u7eed\u62fc\u63a5\u3002<\/p>\n<p>\u540c\u7406&#xff0c;recv \u8fd4\u56de\u7684\u957f\u5ea6\u5c0f\u4e8e\u6240\u63d0\u4f9b\u7684\u7f13\u51b2\u533a\u957f\u5ea6&#xff0c;\u4e5f\u4e0d\u4ee3\u8868\u4e00\u4e2a\u5e94\u7528\u8bf7\u6c42\u7ed3\u675f\u3002\u57fa\u7840\u793a\u4f8b\u7edf\u4e00\u4ee5 EAGAIN\u3001EOF \u6216\u9519\u8bef\u4f5c\u4e3a\u8bfb\u5904\u7406\u7684\u505c\u6b62\u4f9d\u636e&#xff0c;\u66f4\u5bb9\u6613\u5f62\u6210\u6e05\u6670\u7684\u72b6\u6001\u673a\u3002<\/p>\n<h4 id=\"67-et-\u662f\u5426\u4e00\u5b9a\u6bd4-lt-\u5feb\uff1f\">6.7 ET \u662f\u5426\u4e00\u5b9a\u6bd4 LT \u5feb&#xff1f;<\/h4>\n<p>ET \u53ef\u4ee5\u51cf\u5c11\u67d0\u4e9b\u60c5\u51b5\u4e0b\u7684\u91cd\u590d\u901a\u77e5&#xff0c;\u4f46\u7ed3\u679c\u53d6\u51b3\u4e8e\u6d3b\u8dc3\u8fde\u63a5\u6bd4\u4f8b\u3001\u5904\u7406\u7b56\u7565\u3001\u6570\u636e\u5927\u5c0f\u548c\u8c03\u7528\u5f00\u9500\u3002<\/p>\n<p>LT \u5982\u679c\u6bcf\u6b21\u4e5f\u79ef\u6781\u5904\u7406\u5f53\u524d\u53ef\u5904\u7406\u7684\u6570\u636e&#xff0c;\u5c31\u4e0d\u4f1a\u56e0\u4e3a\u201c\u53ea\u8bfb\u4e00\u70b9\u201d\u800c\u4ea7\u751f\u90a3\u4e48\u591a\u91cd\u590d\u63d0\u9192\u3002\u4e24\u8005\u7684\u6027\u80fd\u4e0d\u80fd\u53ea\u51ed\u540d\u5b57\u5224\u65ad\u3002<\/p>\n<p>\u53e6\u5916&#xff0c;\u201cET \u5fc5\u987b\u7acb\u523b\u628a\u6240\u6709\u4e8b\u60c5\u505a\u5b8c\u201d\u4e5f\u592a\u7edd\u5bf9\u3002\u5e94\u7528\u53ef\u4ee5\u7ef4\u62a4\u81ea\u5df1\u7684\u5f85\u5904\u7406\u961f\u5217&#xff0c;\u628a\u8fde\u63a5\u5206\u6279\u8f6e\u8f6c\u5904\u7406\u3002\u5173\u952e\u662f\u4fdd\u7559\u201c\u5b83\u8fd8\u6709\u5de5\u4f5c\u6ca1\u505a\u5b8c\u201d\u7684\u72b6\u6001&#xff0c;\u4e0d\u80fd\u5c1a\u672a\u5904\u7406\u5b8c\u5c31\u5b8c\u5168\u4f9d\u8d56\u5185\u6838\u4e0b\u4e00\u6b21\u8fb9\u7f18\u901a\u77e5\u3002<\/p>\n<p>\u8fd9\u65e2\u5173\u7cfb\u5230\u6b63\u786e\u6027&#xff0c;\u4e5f\u5173\u7cfb\u5230\u516c\u5e73\u6027&#xff1a;\u4e00\u4e2a\u6301\u7eed\u53d1\u9001\u6570\u636e\u7684\u8fde\u63a5&#xff0c;\u4e0d\u80fd\u65e0\u9650\u5360\u7528\u4e8b\u4ef6\u5faa\u73af\u3002<\/p>\n<h3 id=\"\u4e03\u3001\u5199\u4e8b\u4ef6\u3001accept-\u4e0e\u5173\u95ed\uff1a\u628a\u4e00\u4e2a\u8fde\u63a5\u7684\u751f\u547d\u5468\u671f\u8865\u9f50\">\u4e03\u3001\u5199\u4e8b\u4ef6\u3001accept \u4e0e\u5173\u95ed&#xff1a;\u628a\u4e00\u4e2a\u8fde\u63a5\u7684\u751f\u547d\u5468\u671f\u8865\u9f50<\/h3>\n<h4 id=\"71-\u975e\u963b\u585e\u72b6\u6001\u9700\u8981\u8bbe\u7f6e\u5728\u6b63\u786e\u7684-fd-\u4e0a\">7.1 \u975e\u963b\u585e\u72b6\u6001\u9700\u8981\u8bbe\u7f6e\u5728\u6b63\u786e\u7684 fd \u4e0a<\/h4>\n<p>\u5bf9\u5df2\u6709 fd&#xff0c;\u53ef\u4ee5\u4fdd\u7559\u539f\u6709\u72b6\u6001\u4f4d&#xff0c;\u518d\u52a0\u4e0a\u975e\u963b\u585e\u6807\u5fd7&#xff1a;<\/p>\n<p>#include &lt;fcntl.h&gt;<\/p>\n<p>bool set_nonblocking(int fd) {<br \/>\n    int flags &#061; fcntl(fd, F_GETFL, 0);<br \/>\n    if (flags &#061;&#061; -1) return false;<br \/>\n    return fcntl(fd, F_SETFL, flags | O_NONBLOCK) !&#061; -1;<br \/>\n}<\/p>\n<p>Linux \u4e5f\u652f\u6301\u521b\u5efa socket \u65f6\u4f7f\u7528 SOCK_NONBLOCK&#xff0c;\u4ee5\u53ca\u7528 accept4 \u7ed9\u65b0\u8fde\u63a5\u8bbe\u7f6e SOCK_NONBLOCK | SOCK_CLOEXEC\u3002<\/p>\n<p>\u7279\u522b\u6ce8\u610f&#xff1a;**\u5728 Linux \u4e0a&#xff0c;\u4e0d\u80fd\u5047\u8bbe accept \u8fd4\u56de\u7684\u8fde\u63a5\u4f1a\u81ea\u52a8\u7ee7\u627f\u76d1\u542c socket \u7684 O_NONBLOCK\u3002**\u8981\u4e3a\u65b0\u8fde\u63a5\u660e\u786e\u8bbe\u7f6e\u3002\u76d1\u542c socket \u672c\u8eab\u5982\u679c\u91c7\u7528\u975e\u963b\u585e\u63a5\u53d7&#xff0c;\u4e5f\u9700\u8981\u4e8b\u5148\u8bbe\u7f6e\u597d\u3002\u76f8\u5173\u89c4\u5219\u89c1 accept(2)\u3002<\/p>\n<h4 id=\"72-\u76d1\u542c-socket-\u4f7f\u7528-et\uff0c\u4e5f\u8981\u63a5\u6536\u5230-eagain\">7.2 \u76d1\u542c socket \u4f7f\u7528 ET&#xff0c;\u4e5f\u8981\u63a5\u6536\u5230 EAGAIN<\/h4>\n<p>\u76d1\u542c socket \u53ef\u8bfb&#xff0c;\u901a\u5e38\u8bf4\u660e\u5b83\u7684\u5df2\u5b8c\u6210\u8fde\u63a5\u961f\u5217\u4e2d\u6709\u8fde\u63a5\u53ef\u4ee5\u53d6\u51fa\u3002<\/p>\n<p>\u5982\u679c\u8fd9\u4e00\u8f6e\u540c\u65f6\u6765\u4e86\u591a\u4e2a\u8fde\u63a5&#xff0c;\u53ea accept \u4e00\u6b21\u5c31\u56de\u53bb\u7b49\u5f85&#xff0c;\u5728 ET \u4e0b\u53ef\u80fd\u7559\u4e0b\u6ca1\u6709\u5904\u7406\u7684\u8fde\u63a5\u3002<\/p>\n<p>\u57fa\u7840\u5199\u6cd5\u662f&#xff1a;<\/p>\n<p>for (;;) {<br \/>\n    int fd &#061; accept4(listen_fd, nullptr, nullptr,<br \/>\n                     SOCK_NONBLOCK | SOCK_CLOEXEC);<br \/>\n    if (fd &gt;&#061; 0) {<br \/>\n        \/\/ \u4e3a\u65b0\u8fde\u63a5\u5efa\u7acb\u72b6\u6001&#xff0c;\u5e76\u6ce8\u518c\u5230 epoll\u3002<br \/>\n        continue;<br \/>\n    }<br \/>\n    if (errno &#061;&#061; EINTR) continue;<br \/>\n    if (errno &#061;&#061; EAGAIN || errno &#061;&#061; EWOULDBLOCK) break;<br \/>\n    \/\/ \u533a\u5206\u53ef\u91cd\u8bd5\u7684\u8fde\u63a5\u9519\u8bef\u4e0e\u8d44\u6e90\u8017\u5c3d\u7b49\u9519\u8bef\u3002<br \/>\n    break;<br \/>\n}<\/p>\n<p>\u8fd9\u91cc\u7684 break \u53ea\u8bf4\u660e\u793a\u610f\u5faa\u73af\u7684\u505c\u6b62\u4f4d\u7f6e\u3002\u751f\u4ea7\u7a0b\u5e8f\u9700\u8981\u5bf9 EMFILE\u3001ENFILE \u7b49\u60c5\u51b5\u5b89\u6392\u8d44\u6e90\u6062\u590d\u548c\u91cd\u8bd5&#xff0c;\u5c24\u5176\u4e0d\u80fd\u8ba9 ET \u76d1\u542c\u5668\u9057\u7559\u4efb\u52a1\u540e\u518d\u4e5f\u4e0d\u5c1d\u8bd5\u3002<\/p>\n<h4 id=\"73-\u4e3a\u4ec0\u4e48\u53ea\u76d1\u542c-epollin-\u7684-echo-\u793a\u4f8b\u8fd8\u4e0d\u591f\uff1f\">7.3 \u4e3a\u4ec0\u4e48\u53ea\u76d1\u542c EPOLLIN \u7684 echo \u793a\u4f8b\u8fd8\u4e0d\u591f&#xff1f;<\/h4>\n<p>\u4e00\u4e2a\u5e38\u89c1\u7684\u6559\u5b66\u793a\u4f8b\u662f&#xff1a;\u6536\u5230\u8bfb\u4e8b\u4ef6 \u2192 recv \u2192 \u5904\u7406 \u2192 send\u3002<\/p>\n<p>\u5c0f\u6570\u636e\u3001\u672c\u673a\u6d4b\u8bd5\u65f6&#xff0c;\u5b83\u5f80\u5f80\u770b\u8d77\u6765\u6b63\u5e38\u3002\u4f46\u5ba2\u6237\u7aef\u8bfb\u53d6\u5f88\u6162\u65f6&#xff0c;\u670d\u52a1\u5668\u53d1\u9001\u7f13\u51b2\u533a\u53ef\u80fd\u88c5\u4e0d\u4e0b\u5168\u90e8\u54cd\u5e94\u3002<\/p>\n<p>\u6b64\u65f6\u975e\u963b\u585e send \u53ef\u80fd&#xff1a;<\/p>\n<ul>\n<li>\u53ea\u63a5\u53d7\u4e00\u90e8\u5206\u5b57\u8282\u3002<\/li>\n<li>\u6682\u65f6\u65e0\u6cd5\u63a5\u53d7\u66f4\u591a\u5b57\u8282&#xff0c;\u8fd4\u56de EAGAIN\u3002<\/li>\n<\/ul>\n<p>\u5982\u679c\u5ffd\u7565\u8fd4\u56de\u503c&#xff0c;\u54cd\u5e94\u4f1a\u4e22&#xff1b;\u5982\u679c\u539f\u5730\u53cd\u590d\u91cd\u8bd5&#xff0c;\u4e8b\u4ef6\u5faa\u73af\u4f1a\u5fd9\u7b49\u3002<\/p>\n<p>\u6240\u4ee5\u6bcf\u4e2a\u8fde\u63a5\u9700\u8981\u4fdd\u5b58&#xff1a;<\/p>\n<p>\u5f85\u53d1\u9001\u7f13\u51b2\u533a &#043; \u5df2\u53d1\u9001\u4f4d\u7f6e<\/p>\n<p>\u8fd9\u91cc\u7684\u201c\u5df2\u53d1\u9001\u201d\u6307\u5df2\u7ecf\u4ea4\u7ed9\u672c\u673a\u5185\u6838&#xff0c;\u5e76\u4e0d\u4ee3\u8868\u5bf9\u7aef\u5e94\u7528\u5df2\u7ecf\u5904\u7406\u5b8c\u3002\u53d1\u9001\u63a5\u53e3\u7684\u8bed\u4e49\u89c1 send(2)\u3002<\/p>\n<h4 id=\"74-epollout-\u5e94\u5f53\u548c\u5f85\u53d1\u9001\u6570\u636e\u914d\u5408\">7.4 EPOLLOUT \u5e94\u5f53\u548c\u5f85\u53d1\u9001\u6570\u636e\u914d\u5408<\/h4>\n<p>\u4e00\u79cd\u5bb9\u6613\u7406\u89e3\u7684\u5904\u7406\u7b56\u7565\u662f&#xff1a;<\/p>\n<p>\u4ea7\u751f\u54cd\u5e94<br \/>\n   \u2193<br \/>\n\u5148\u7acb\u5373\u5c1d\u8bd5 send<br \/>\n   \u251c\u2500\u2500 \u5168\u90e8\u4ea4\u7ed9\u5185\u6838 \u2192 \u4e0d\u518d\u5173\u6ce8 EPOLLOUT<br \/>\n   \u2514\u2500\u2500 \u4ecd\u6709\u6570\u636e&#xff0c;\u9047\u5230 EAGAIN \u2192 \u4fdd\u5b58\u4f4d\u7f6e&#xff0c;\u5173\u6ce8 EPOLLOUT<br \/>\n                                      \u2193<br \/>\n                            \u6536\u5230\u5199\u4e8b\u4ef6\u540e\u7ee7\u7eed send<\/p>\n<p>\u5f53\u5f85\u53d1\u9001\u7f13\u51b2\u533a\u4e3a\u7a7a\u65f6&#xff0c;\u5c31\u6ca1\u6709\u5fc5\u8981\u4e3a\u4e86\u4e1a\u52a1\u54cd\u5e94\u4e00\u76f4\u5904\u7406\u53ef\u5199\u901a\u77e5\u3002<\/p>\n<p>\u5728 LT \u4e0b&#xff0c;\u6301\u7eed\u5173\u6ce8\u4e00\u4e2a\u7a7a\u95f2\u4e14\u901a\u5e38\u53ef\u5199\u7684\u8fde\u63a5&#xff0c;\u4f1a\u4f7f\u5199\u4e8b\u4ef6\u53cd\u590d\u8fd4\u56de&#xff0c;\u53ef\u80fd\u5bfc\u81f4 CPU \u5360\u7528\u5f88\u9ad8\u3002<\/p>\n<p>\u5728 ET \u4e0b&#xff0c;\u8fd8\u8981\u6ce8\u610f\u53e6\u4e00\u4e2a\u7ec6\u8282&#xff1a;**\u4ea7\u751f\u65b0\u7684\u5e94\u7528\u8f93\u51fa\u65f6&#xff0c;\u5e94\u4e3b\u52a8\u5c1d\u8bd5\u53d1\u9001&#xff0c;\u4e0d\u80fd\u53ea\u7b49\u672a\u6765\u7684 EPOLLOUT\u3002**socket \u53ef\u80fd\u4e00\u76f4\u53ef\u5199&#xff0c;\u6b64\u523b\u672a\u5fc5\u4f1a\u51fa\u73b0\u4e00\u4e2a\u65b0\u7684\u53ef\u5199\u8fb9\u7f18\u3002<\/p>\n<p>\u8fd9\u5957\u7b56\u7565\u4e0d\u662f\u552f\u4e00\u5b9e\u73b0\u65b9\u5f0f&#xff0c;\u4f46\u9002\u5408\u5efa\u7acb\u6b63\u786e\u7684\u57fa\u672c\u6a21\u578b\u3002<\/p>\n<h4 id=\"75-\u5bf9\u7aef\u534a\u5173\u95ed\u4ee5\u540e\uff0c\u670d\u52a1\u5668\u8fd8\u53ef\u80fd\u9700\u8981\u53d1\u9001\u54cd\u5e94\">7.5 \u5bf9\u7aef\u534a\u5173\u95ed\u4ee5\u540e&#xff0c;\u670d\u52a1\u5668\u8fd8\u53ef\u80fd\u9700\u8981\u53d1\u9001\u54cd\u5e94<\/h4>\n<p>\u5ba2\u6237\u7aef\u53ef\u4ee5\u8fd9\u6837\u505a&#xff1a;<\/p>\n<p>\u53d1\u9001\u8bf7\u6c42 \u2192 shutdown(SHUT_WR) \u2192 \u7ee7\u7eed\u8bfb\u53d6\u670d\u52a1\u5668\u54cd\u5e94<\/p>\n<p>\u8fd9\u610f\u5473\u7740\u5ba2\u6237\u7aef\u4e0d\u518d\u53d1\u9001\u6570\u636e&#xff0c;\u4f46\u5b83\u4ecd\u7136\u53ef\u4ee5\u63a5\u6536\u6570\u636e\u3002<\/p>\n<p>\u5982\u679c\u670d\u52a1\u5668\u4e00\u770b\u5230 EPOLLRDHUP \u5c31\u76f4\u63a5\u5173\u95ed&#xff0c;\u6709\u53ef\u80fd\u4e22\u6389\u5c1a\u672a\u8bfb\u53d6\u7684\u8bf7\u6c42\u6216\u5c1a\u672a\u53d1\u51fa\u7684\u54cd\u5e94\u3002<\/p>\n<p>\u66f4\u5408\u7406\u7684\u57fa\u7840\u6d41\u7a0b\u662f&#xff1a;<\/p>\n<li>\u6839\u636e\u8bfb\u4e8b\u4ef6\u6216\u5173\u95ed\u63d0\u793a&#xff0c;\u7ee7\u7eed\u8bfb\u53d6\u5df2\u7ecf\u5230\u8fbe\u7684\u6570\u636e\u3002<\/li>\n<li>\u901a\u8fc7 recv &#061;&#061; 0 \u786e\u8ba4\u8bfb\u65b9\u5411\u7ed3\u675f\u3002<\/li>\n<li>\u82e5\u534f\u8bae\u5141\u8bb8\u4e14\u6ca1\u6709\u81f4\u547d\u9519\u8bef&#xff0c;\u7ee7\u7eed\u53d1\u9001\u5df2\u7ecf\u4ea7\u751f\u7684\u54cd\u5e94\u3002<\/li>\n<li>\u5f85\u53d1\u9001\u6570\u636e\u5904\u7406\u5b8c\u540e&#xff0c;\u518d\u5173\u95ed\u8fde\u63a5\u3002<\/li>\n<p>EPOLLHUP \u4e5f\u4e0d\u80fd\u76f4\u63a5\u7b49\u540c\u4e8e\u201c\u63a5\u6536\u7f13\u51b2\u533a\u5df2\u7ecf\u7a7a\u4e86\u201d\u3002\u6302\u65ad\u4e0e\u6b8b\u7559\u6570\u636e\u53ef\u4ee5\u540c\u65f6\u5b58\u5728&#xff0c;\u8fd9\u4e00\u70b9\u5728 epoll_ctl(2) \u4e2d\u6709\u8bf4\u660e\u3002<\/p>\n<p>\u5bf9\u4e8e EPOLLERR&#xff0c;\u53ef\u4ee5\u901a\u8fc7 getsockopt(fd, SOL_SOCKET, SO_ERROR, &#8230;) \u83b7\u53d6\u76f8\u5173 socket \u9519\u8bef&#xff0c;\u5177\u4f53\u9009\u9879\u89c1 socket(7)\u3002\u81f4\u547d\u9519\u8bef\u53ef\u4ee5\u91c7\u7528\u7acb\u5373\u6e05\u7406\u7b56\u7565&#xff1b;\u5e76\u975e\u4efb\u4f55\u5173\u95ed\u60c5\u5f62\u90fd\u80fd\u4fdd\u8bc1\u7ee7\u7eed\u53d1\u9001\u6210\u529f\u3002<\/p>\n<h4 id=\"76-del\u3001close-\u4e0e\u5bf9\u8c61\u91ca\u653e\uff0c\u5404\u81ea\u8d1f\u8d23\u4ec0\u4e48\uff1f\">7.6 DEL\u3001close \u4e0e\u5bf9\u8c61\u91ca\u653e&#xff0c;\u5404\u81ea\u8d1f\u8d23\u4ec0\u4e48&#xff1f;<\/h4>\n<p>\u53ef\u4ee5\u6309\u8fd9\u4e2a\u987a\u5e8f\u7ec4\u7ec7\u6b63\u5e38\u6e05\u7406&#xff1a;<\/p>\n<p>\u4ece epoll \u5220\u9664\u5173\u6ce8\u9879 \u2192 close socket \u2192 \u91ca\u653e\u8fde\u63a5\u5bf9\u8c61\u548c\u5e94\u7528\u7f13\u51b2\u533a<\/p>\n<p>DEL \u7ed3\u675f\u5173\u6ce8\u5173\u7cfb&#xff0c;close \u5173\u95ed\u63cf\u8ff0\u7b26&#xff0c;\u5bf9\u8c61\u91ca\u653e\u5904\u7406\u7528\u6237\u6001\u8d44\u6e90\u3002<\/p>\n<p>\u5728\u6ca1\u6709\u5176\u4ed6\u5f15\u7528\u7684\u666e\u901a\u573a\u666f\u4e2d&#xff0c;\u5173\u95ed\u76f8\u5173 fd \u4f1a\u5f15\u8d77\u5185\u6838\u6e05\u7406\u5173\u6ce8\u9879&#xff1b;\u6d89\u53ca dup\u3001fork \u540e&#xff0c;\u540c\u4e00\u4e2a\u6253\u5f00\u6587\u4ef6\u63cf\u8ff0\u53ef\u80fd\u4ecd\u88ab\u5176\u4ed6 fd \u5f15\u7528&#xff0c;\u4e0d\u80fd\u7528\u201cclose \u4e00\u6b21\u5c31\u4e00\u5b9a\u5b8c\u5168\u6d88\u5931\u201d\u6982\u62ec\u3002\u7a0b\u5e8f\u663e\u5f0f\u7ba1\u7406\u6ce8\u9500\u4e0e\u5173\u95ed&#xff0c;\u804c\u8d23\u4f1a\u66f4\u6e05\u695a\u3002<\/p>\n<p>\u6b64\u5916&#xff0c;\u5df2\u7ecf\u7531 epoll_wait \u590d\u5236\u5230\u7528\u6237\u6001\u6570\u7ec4\u4e2d\u7684\u65e7\u4e8b\u4ef6&#xff0c;\u4e0d\u4f1a\u56e0\u4e3a\u540e\u7eed DEL \u5c31\u81ea\u52a8\u4ece\u6570\u7ec4\u91cc\u6d88\u5931\u3002\u5bf9\u8c61\u5bff\u547d\u4e0e\u4e8b\u4ef6\u6709\u6548\u6027&#xff0c;\u5fc5\u987b\u7531\u5e94\u7528\u81ea\u5df1\u534f\u8c03\u3002<\/p>\n<h3 id=\"\u516b\u3001\u5b8c\u6574\u5b9e\u6218\uff1a\u652f\u6301-ltet-\u7684\u975e\u963b\u585e-tcp-echo-\u670d\u52a1\u5668\">\u516b\u3001\u5b8c\u6574\u5b9e\u6218&#xff1a;\u652f\u6301 LT\/ET \u7684\u975e\u963b\u585e TCP Echo \u670d\u52a1\u5668<\/h3>\n<p>\u524d\u9762\u7684\u77e5\u8bc6&#xff0c;\u73b0\u5728\u7528\u4e00\u4e2a\u5b8c\u6574\u7a0b\u5e8f\u4e32\u8d77\u6765\u3002<\/p>\n<p>\u5b83\u91c7\u7528\u5355\u7ebf\u7a0b\u4e8b\u4ef6\u5faa\u73af&#xff0c;\u76d1\u542c 127.0.0.1&#xff0c;\u652f\u6301&#xff1a;<\/p>\n<ul>\n<li>\u901a\u8fc7\u547d\u4ee4\u884c\u5207\u6362 LT\/ET\u3002<\/li>\n<li>\u76d1\u542c socket \u4e0e\u8fde\u63a5 socket \u90fd\u4f7f\u7528\u975e\u963b\u585e\u6a21\u5f0f\u3002<\/li>\n<li>\u5faa\u73af accept4\u3001\u5faa\u73af\u8bfb\u53d6\u5f53\u524d\u6570\u636e\u3002<\/li>\n<li>\u4fdd\u7559\u672a\u53d1\u5b8c\u7684\u6570\u636e&#xff0c;\u6309\u9700\u5173\u6ce8 EPOLLOUT\u3002<\/li>\n<li>\u6b63\u786e\u56de\u663e\u5305\u62ec \\\\0 \u5728\u5185\u7684\u4e8c\u8fdb\u5236\u5b57\u8282\u3002<\/li>\n<li>\u8bc6\u522b\u8bfb\u65b9\u5411 EOF&#xff0c;\u5728\u8f93\u51fa\u5904\u7406\u5b8c\u540e\u5173\u95ed\u8fde\u63a5\u3002<\/li>\n<li>\u4f7f\u7528\u9012\u589e\u8fde\u63a5\u7f16\u53f7\u8bc6\u522b\u4e8b\u4ef6&#xff0c;\u907f\u514d\u76f4\u63a5\u628a\u53ef\u590d\u7528\u7684 fd \u6570\u503c\u5f53\u4f5c\u8fde\u63a5\u7684\u6c38\u4e45\u8eab\u4efd\u3002<\/li>\n<\/ul>\n<p>**\u793a\u4f8b\u7684\u8fb9\u754c&#xff1a;**\u6bcf\u4e2a\u8fde\u63a5\u6700\u591a\u4fdd\u7559 4MiB \u5f85\u53d1\u9001\u6570\u636e&#xff0c;\u8d85\u8fc7\u5c31\u5173\u95ed\u8be5\u8fde\u63a5\u3002\u5b83\u6f14\u793a\u7684\u662f\u65ad\u5f00\u8fc7\u8f7d\u8fde\u63a5\u7684\u7b80\u5355\u7b56\u7565\u3002\u6b63\u5f0f\u670d\u52a1\u8fd8\u9700\u8981\u5168\u5c40\u5185\u5b58\u9884\u7b97\u3001\u8f93\u5165\u9650\u989d\u3001\u8d85\u65f6\u3001\u516c\u5e73\u8c03\u5ea6&#xff0c;\u4ee5\u53ca\u8d44\u6e90\u8017\u5c3d\u540e\u7684\u6062\u590d\u7b56\u7565\u3002<\/p>\n<p>\u4fdd\u5b58\u4e3a epoll_echo.cpp&#xff1a;<\/p>\n<p>#ifndef _GNU_SOURCE<br \/>\n#define _GNU_SOURCE<br \/>\n#endif<\/p>\n<p>#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;sys\/epoll.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>#include &lt;cerrno&gt;<br \/>\n#include &lt;cstdint&gt;<br \/>\n#include &lt;cstdio&gt;<br \/>\n#include &lt;cstdlib&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;unordered_map&gt;<br \/>\n#include &lt;utility&gt;<\/p>\n<p>constexpr int MAX_EVENTS &#061; 128;<br \/>\nconstexpr std::size_t MAX_PENDING &#061; 4 * 1024 * 1024;<\/p>\n<p>struct Connection {<br \/>\n    int fd &#061; -1;<br \/>\n    std::string out;<br \/>\n    std::size_t sent &#061; 0;<br \/>\n    bool eof &#061; false;<br \/>\n    uint32_t interest &#061; 0;<br \/>\n};<\/p>\n<p>[[noreturn]] void die(const char* what) {<br \/>\n    std::perror(what);<br \/>\n    std::exit(EXIT_FAILURE);<br \/>\n}<\/p>\n<p>bool would_block(int error) {<br \/>\n    return error &#061;&#061; EAGAIN || error &#061;&#061; EWOULDBLOCK;<br \/>\n}<\/p>\n<p>bool ctl(int epfd, int op, int fd, uint32_t mask, uint64_t id) {<br \/>\n    epoll_event ev{};<br \/>\n    ev.events &#061; mask;<br \/>\n    ev.data.u64 &#061; id;<br \/>\n    return epoll_ctl(epfd, op, fd, &amp;ev) &#061;&#061; 0;<br \/>\n}<\/p>\n<p>\/\/ These errors belong to individual pending connections; keep accepting.<br \/>\nbool transient_accept_error(int error) {<br \/>\n    switch (error) {<br \/>\n        case ECONNABORTED: case ENETDOWN: case EPROTO:<br \/>\n        case ENOPROTOOPT: case EHOSTDOWN: case ENONET:<br \/>\n        case EHOSTUNREACH: case EOPNOTSUPP: case ENETUNREACH:<br \/>\n            return true;<br \/>\n        default:<br \/>\n            return false;<br \/>\n    }<br \/>\n}<\/p>\n<p>bool read_available(Connection&amp; c) {<br \/>\n    char buf[16 * 1024];<br \/>\n    for (;;) {<br \/>\n        ssize_t n &#061; recv(c.fd, buf, sizeof(buf), 0);<br \/>\n        if (n &gt; 0) {<br \/>\n            \/\/ Compacting happens before this handler, so out is all pending.<br \/>\n            if (static_cast&lt;std::size_t&gt;(n) &gt; MAX_PENDING &#8211; c.out.size()) {<br \/>\n                std::cerr &lt;&lt; &#034;pending output limit reached, fd&#061;&#034; &lt;&lt; c.fd &lt;&lt; &#039;\\\\n&#039;;<br \/>\n                return false;<br \/>\n            }<br \/>\n            c.out.append(buf, static_cast&lt;std::size_t&gt;(n));<br \/>\n            continue;<br \/>\n        }<br \/>\n        if (n &#061;&#061; 0) {<br \/>\n            c.eof &#061; true; \/\/ Peer finished sending; our output may remain.<br \/>\n            return true;<br \/>\n        }<br \/>\n        if (errno &#061;&#061; EINTR) continue;<br \/>\n        if (would_block(errno)) return true;<br \/>\n        std::perror(&#034;recv&#034;);<br \/>\n        return false;<br \/>\n    }<br \/>\n}<\/p>\n<p>bool write_available(Connection&amp; c) {<br \/>\n    while (c.sent &lt; c.out.size()) {<br \/>\n        ssize_t n &#061; send(c.fd, c.out.data() &#043; c.sent,<br \/>\n                         c.out.size() &#8211; c.sent, MSG_NOSIGNAL);<br \/>\n        if (n &gt; 0) {<br \/>\n            c.sent &#043;&#061; static_cast&lt;std::size_t&gt;(n);<br \/>\n            continue;<br \/>\n        }<br \/>\n        if (n &lt; 0 &amp;&amp; errno &#061;&#061; EINTR) continue;<br \/>\n        if (n &lt; 0 &amp;&amp; would_block(errno)) return true;<br \/>\n        if (n &lt; 0) std::perror(&#034;send&#034;);<br \/>\n        return false;<br \/>\n    }<br \/>\n    c.out.clear();<br \/>\n    c.sent &#061; 0;<br \/>\n    return true;<br \/>\n}<\/p>\n<p>int main(int argc, char** argv) {<br \/>\n    if (argc !&#061; 3 || (std::strcmp(argv[2], &#034;lt&#034;) !&#061; 0 &amp;&amp;<br \/>\n                      std::strcmp(argv[2], &#034;et&#034;) !&#061; 0)) {<br \/>\n        std::cerr &lt;&lt; &#034;usage: &#034; &lt;&lt; argv[0] &lt;&lt; &#034; &lt;port&gt; &lt;lt|et&gt;\\\\n&#034;;<br \/>\n        return EXIT_FAILURE;<br \/>\n    }<br \/>\n    char* end &#061; nullptr;<br \/>\n    errno &#061; 0;<br \/>\n    long port &#061; std::strtol(argv[1], &amp;end, 10);<br \/>\n    if (errno !&#061; 0 || end &#061;&#061; argv[1] || *end !&#061; &#039;\\\\0&#039; ||<br \/>\n        port &lt; 1 || port &gt; 65535) {<br \/>\n        std::cerr &lt;&lt; &#034;invalid port\\\\n&#034;;<br \/>\n        return EXIT_FAILURE;<br \/>\n    }<br \/>\n    const bool et &#061; std::strcmp(argv[2], &#034;et&#034;) &#061;&#061; 0;<br \/>\n    const uint32_t mode &#061; et ? static_cast&lt;uint32_t&gt;(EPOLLET) : 0u;<\/p>\n<p>    int lfd &#061; socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);<br \/>\n    if (lfd &lt; 0) die(&#034;socket&#034;);<br \/>\n    int one &#061; 1;<br \/>\n    if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &amp;one, sizeof(one)) &lt; 0)<br \/>\n        die(&#034;setsockopt&#034;);<\/p>\n<p>    sockaddr_in address{};<br \/>\n    address.sin_family &#061; AF_INET;<br \/>\n    address.sin_port &#061; htons(static_cast&lt;uint16_t&gt;(port));<br \/>\n    address.sin_addr.s_addr &#061; htonl(INADDR_LOOPBACK);<br \/>\n    if (bind(lfd, reinterpret_cast&lt;sockaddr*&gt;(&amp;address), sizeof(address)) &lt; 0)<br \/>\n        die(&#034;bind&#034;);<br \/>\n    if (listen(lfd, SOMAXCONN) &lt; 0) die(&#034;listen&#034;);<\/p>\n<p>    int epfd &#061; epoll_create1(EPOLL_CLOEXEC);<br \/>\n    if (epfd &lt; 0) die(&#034;epoll_create1&#034;);<br \/>\n    \/\/ ID 0 belongs to the listener; client IDs never follow reused fd numbers.<br \/>\n    if (!ctl(epfd, EPOLL_CTL_ADD, lfd, EPOLLIN | mode, 0)) die(&#034;ADD listener&#034;);<\/p>\n<p>    std::unordered_map&lt;uint64_t, Connection&gt; clients;<br \/>\n    uint64_t next_id &#061; 1;<br \/>\n    auto remove_client &#061; [&amp;](uint64_t id) {<br \/>\n        auto it &#061; clients.find(id);<br \/>\n        if (it &#061;&#061; clients.end()) return;<br \/>\n        if (epoll_ctl(epfd, EPOLL_CTL_DEL, it-&gt;second.fd, nullptr) &lt; 0)<br \/>\n            std::perror(&#034;DEL client&#034;);<br \/>\n        close(it-&gt;second.fd);<br \/>\n        clients.erase(it);<br \/>\n    };<\/p>\n<p>    epoll_event events[MAX_EVENTS];<br \/>\n    std::cout &lt;&lt; &#034;listen 127.0.0.1:&#034; &lt;&lt; port &lt;&lt; &#034; mode&#061;&#034;<br \/>\n              &lt;&lt; (et ? &#034;ET&#034; : &#034;LT&#034;) &lt;&lt; std::endl;<br \/>\n    bool running &#061; true;<br \/>\n    while (running) {<br \/>\n        int count &#061; epoll_wait(epfd, events, MAX_EVENTS, -1);<br \/>\n        if (count &lt; 0) {<br \/>\n            if (errno &#061;&#061; EINTR) continue;<br \/>\n            std::perror(&#034;epoll_wait&#034;);<br \/>\n            break;<br \/>\n        }<br \/>\n        for (int i &#061; 0; i &lt; count; &#043;&#043;i) {<br \/>\n            const uint64_t id &#061; events[i].data.u64;<br \/>\n            const uint32_t mask &#061; events[i].events;<br \/>\n            if (id &#061;&#061; 0) {<br \/>\n                if (mask &amp; (EPOLLERR | EPOLLHUP)) {<br \/>\n                    std::cerr &lt;&lt; &#034;listener error\\\\n&#034;;<br \/>\n                    running &#061; false;<br \/>\n                    break;<br \/>\n                }<br \/>\n                \/\/ The listener is nonblocking too: drain its accept queue.<br \/>\n                for (;;) {<br \/>\n                    int fd &#061; accept4(lfd, nullptr, nullptr,<br \/>\n                                     SOCK_NONBLOCK | SOCK_CLOEXEC);<br \/>\n                    if (fd &lt; 0) {<br \/>\n                        if (errno &#061;&#061; EINTR) continue;<br \/>\n                        if (would_block(errno)) break;<br \/>\n                        if (transient_accept_error(errno)) continue;<br \/>\n                        \/\/ Demo policy: fail on resource exhaustion, rather<br \/>\n                        \/\/ than silently strand an ET listener&#039;s pending work.<br \/>\n                        die(&#034;accept4&#034;);<br \/>\n                    }<br \/>\n                    if (next_id &#061;&#061; 0) {<br \/>\n                        std::cerr &lt;&lt; &#034;connection ID space exhausted\\\\n&#034;;<br \/>\n                        close(fd);<br \/>\n                        std::exit(EXIT_FAILURE);<br \/>\n                    }<br \/>\n                    uint64_t new_id &#061; next_id&#043;&#043;;<br \/>\n                    Connection c;<br \/>\n                    c.fd &#061; fd;<br \/>\n                    c.interest &#061; EPOLLIN | EPOLLRDHUP | mode;<br \/>\n                    if (!ctl(epfd, EPOLL_CTL_ADD, fd, c.interest, new_id)) {<br \/>\n                        std::perror(&#034;ADD client&#034;);<br \/>\n                        close(fd);<br \/>\n                        continue;<br \/>\n                    }<br \/>\n                    clients.emplace(new_id, std::move(c));<br \/>\n                }<br \/>\n                continue;<br \/>\n            }<\/p>\n<p>            auto it &#061; clients.find(id);<br \/>\n            if (it &#061;&#061; clients.end()) continue; \/\/ Ignore a retired connection.<br \/>\n            Connection&amp; c &#061; it-&gt;second;<br \/>\n            if (mask &amp; EPOLLERR) {<br \/>\n                int error &#061; 0;<br \/>\n                socklen_t length &#061; sizeof(error);<br \/>\n                if (getsockopt(c.fd, SOL_SOCKET, SO_ERROR, &amp;error, &amp;length) &lt; 0)<br \/>\n                    std::perror(&#034;SO_ERROR&#034;);<br \/>\n                else<br \/>\n                    std::cerr &lt;&lt; &#034;socket error: &#034; &lt;&lt; std::strerror(error) &lt;&lt; &#039;\\\\n&#039;;<br \/>\n                remove_client(id);<br \/>\n                continue;<br \/>\n            }<\/p>\n<p>            \/\/ Remove only bytes already accepted by send(), preserving the rest.<br \/>\n            if (c.sent !&#061; 0) {<br \/>\n                c.out.erase(0, c.sent);<br \/>\n                c.sent &#061; 0;<br \/>\n            }<br \/>\n            bool ok &#061; true;<br \/>\n            if (!c.eof &amp;&amp; (mask &amp; (EPOLLIN | EPOLLRDHUP | EPOLLHUP)))<br \/>\n                ok &#061; read_available(c);<br \/>\n            \/\/ Newly produced output is tried immediately, even without OUT.<br \/>\n            if (ok) ok &#061; write_available(c);<br \/>\n            if (!ok || (c.eof &amp;&amp; c.out.empty())) {<br \/>\n                remove_client(id);<br \/>\n                continue;<br \/>\n            }<\/p>\n<p>            uint32_t wanted &#061; mode;<br \/>\n            if (!c.eof) wanted |&#061; EPOLLIN | EPOLLRDHUP;<br \/>\n            if (c.sent &lt; c.out.size()) wanted |&#061; EPOLLOUT;<br \/>\n            if (wanted !&#061; c.interest) {<br \/>\n                if (!ctl(epfd, EPOLL_CTL_MOD, c.fd, wanted, id)) {<br \/>\n                    std::perror(&#034;MOD client&#034;);<br \/>\n                    remove_client(id);<br \/>\n                    continue;<br \/>\n                }<br \/>\n                c.interest &#061; wanted;<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    for (const auto&amp; entry : clients) close(entry.second.fd);<br \/>\n    close(lfd);<br \/>\n    close(epfd);<br \/>\n    return EXIT_FAILURE;<br \/>\n}<\/p>\n<h4 id=\"81-\u7f16\u8bd1\u548c\u8fd0\u884c\">8.1 \u7f16\u8bd1\u548c\u8fd0\u884c<\/h4>\n<p>\u5728\u5b89\u88c5\u4e86 g&#043;&#043; \u7684 Linux \u73af\u5883\u4e2d\u6267\u884c&#xff1a;<\/p>\n<p>g&#043;&#043; -std&#061;c&#043;&#043;17 -O2 -Wall -Wextra -pedantic epoll_echo.cpp -o epoll_echo<\/p>\n<p># \u5148\u7528 LT \u542f\u52a8<br \/>\n.\/epoll_echo 8080 lt<\/p>\n<p>\u53e6\u5f00\u4e00\u4e2a\u7ec8\u7aef&#xff1a;<\/p>\n<p>nc 127.0.0.1 8080<\/p>\n<p>\u8f93\u5165\u4e00\u884c\u6587\u672c&#xff0c;\u5c31\u53ef\u4ee5\u770b\u5230\u670d\u52a1\u5668\u56de\u663e\u3002nc \u662f\u5426\u663e\u793a\u672c\u5730\u8f93\u5165\u3001\u5982\u4f55\u53d1\u9001 EOF&#xff0c;\u4e0e\u5177\u4f53\u7ec8\u7aef\u548c\u7248\u672c\u6709\u5173&#xff1b;\u540e\u9762\u7684 Python \u811a\u672c\u66f4\u9002\u5408\u68c0\u67e5\u5b57\u8282\u7ea7\u7ed3\u679c\u3002<\/p>\n<p>\u505c\u6b62 LT \u670d\u52a1\u5668\u540e&#xff0c;\u6362\u6210 ET&#xff1a;<\/p>\n<p>.\/epoll_echo 8080 et<\/p>\n<p>\u7a0b\u5e8f\u7ed1\u5b9a\u56de\u73af\u5730\u5740&#xff0c;\u56e0\u6b64\u6d4b\u8bd5\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u8fd0\u884c\u5728\u540c\u4e00\u53f0\u673a\u5668\u3002\u9700\u8981\u4f9b\u5176\u4ed6\u673a\u5668\u8bbf\u95ee\u65f6&#xff0c;\u8981\u8c03\u6574\u7ed1\u5b9a\u5730\u5740&#xff0c;\u5e76\u6309\u5b9e\u9645\u73af\u5883\u914d\u7f6e\u8bbf\u95ee\u8def\u5f84\u3002<\/p>\n<h4 id=\"82-\u8bfb\u4ee3\u7801\u65f6\uff0c\u6293\u4f4f\u516d\u5904\u8054\u7cfb\">8.2 \u8bfb\u4ee3\u7801\u65f6&#xff0c;\u6293\u4f4f\u516d\u5904\u8054\u7cfb<\/h4>\n<p>\u7b2c\u4e00&#xff0c;MAX_EVENTS &#061; 128 \u53ea\u662f\u6bcf\u6279\u4e8b\u4ef6\u6570\u7ec4\u7684\u5bb9\u91cf\u3002<\/p>\n<p>\u5b9e\u9645\u8fde\u63a5\u72b6\u6001\u653e\u5728 clients \u4e2d&#xff0c;\u8fde\u63a5\u6570\u91cf\u5e76\u4e0d\u53d7 128 \u8fd9\u4e2a\u6570\u76f4\u63a5\u9650\u5236\u3002<\/p>\n<p>\u7b2c\u4e8c&#xff0c;data.u64 \u4fdd\u5b58\u7684\u662f\u8fde\u63a5\u7f16\u53f7\u3002<\/p>\n<p>\u76d1\u542c socket \u7ea6\u5b9a\u4f7f\u7528\u7f16\u53f7 0&#xff0c;\u5ba2\u6237\u7aef\u4f7f\u7528\u9012\u589e\u7f16\u53f7\u3002\u6570\u7ec4\u4e2d\u7684\u65e7\u4e8b\u4ef6\u5982\u679c\u627e\u4e0d\u5230\u5bf9\u5e94\u8fde\u63a5&#xff0c;\u5c31\u4f1a\u88ab\u5ffd\u7565\u3002fd \u5173\u95ed\u540e\u53ef\u80fd\u590d\u7528&#xff0c;\u8fde\u63a5\u7f16\u53f7\u4ecd\u80fd\u533a\u5206\u524d\u540e\u4e24\u4e2a\u8fde\u63a5\u3002<\/p>\n<p>\u7b2c\u4e09&#xff0c;read_available \u5230 EAGAIN\u3001EOF \u6216\u9519\u8bef\u624d\u7ed3\u675f\u3002<\/p>\n<p>\u8bfb\u5230\u7684\u5b57\u8282\u7acb\u5373\u6309\u957f\u5ea6\u8ffd\u52a0\u5230\u8f93\u51fa\u7f13\u51b2\u533a\u3002\u8fd9\u662f echo \u7684\u4e1a\u52a1\u89c4\u5219&#xff1b;\u6362\u6210\u771f\u6b63\u7684\u8bf7\u6c42\u534f\u8bae\u540e&#xff0c;\u5e94\u5148\u653e\u5165\u8f93\u5165\u7f13\u51b2\u533a\u5e76\u89e3\u6790&#xff0c;\u518d\u4ea7\u751f\u54cd\u5e94\u3002<\/p>\n<p>\u7b2c\u56db&#xff0c;write_available \u4fdd\u7559\u672a\u53d1\u9001\u5b8c\u7684\u4f4d\u7f6e\u3002<\/p>\n<p>\u53ea\u6709 send \u8fd4\u56de\u6b63\u6570\u65f6\u624d\u63a8\u8fdb sent\u3002\u9047\u5230 EAGAIN&#xff0c;\u4fdd\u7559\u5269\u4f59\u6570\u636e&#xff0c;\u7b49\u5f85\u540e\u7eed\u53ef\u5199\u4e8b\u4ef6\u3002<\/p>\n<p>\u7b2c\u4e94&#xff0c;\u8bfb\u5230 EOF \u540e\u4ecd\u53ef\u80fd\u5173\u6ce8 EPOLLOUT\u3002<\/p>\n<p>\u8bfb\u65b9\u5411\u7ed3\u675f\u65f6\u64a4\u6389\u8bfb\u76f8\u5173\u5173\u6ce8&#xff0c;\u8f93\u51fa\u5904\u7406\u5b8c\u540e\u5173\u95ed\u3002\u8fd9\u907f\u514d\u534a\u5173\u95ed\u8fde\u63a5\u53cd\u590d\u89e6\u53d1\u8bfb\u5173\u95ed\u63d0\u793a&#xff0c;\u4e5f\u4fdd\u7559\u4e86\u53d1\u9001\u54cd\u5e94\u7684\u673a\u4f1a\u3002<\/p>\n<p>\u7b2c\u516d&#xff0c;LT \u548c ET \u4f7f\u7528\u76f8\u540c\u7684\u975e\u963b\u585e\u5904\u7406\u51fd\u6570\u3002<\/p>\n<p>\u8fd9\u8bf4\u660e\u201c\u8bfb\u5199\u5faa\u73af\u6b63\u786e\u201d\u662f\u5e94\u7528\u81ea\u5df1\u7684\u8d23\u4efb\u3002\u5207\u6362 LT\/ET \u6539\u53d8\u901a\u77e5\u8bed\u4e49&#xff0c;\u4e0d\u5e94\u6539\u53d8\u5b57\u8282\u6d41\u672c\u8eab\u7684\u6b63\u786e\u5904\u7406\u89c4\u5219\u3002<\/p>\n<p>\u7a0b\u5e8f\u8fd8\u4f7f\u7528 MSG_NOSIGNAL&#xff0c;\u907f\u514d\u7ed9\u5df2\u5173\u95ed\u5199\u5165\u65b9\u5411\u7684\u8fde\u63a5\u53d1\u9001\u65f6&#xff0c;\u7531 SIGPIPE \u7684\u9ed8\u8ba4\u884c\u4e3a\u7ed3\u675f\u6574\u4e2a\u8fdb\u7a0b&#xff1b;\u53d1\u9001\u5931\u8d25\u4ecd\u8981\u68c0\u67e5\u8fd4\u56de\u503c\u3002<\/p>\n<p>\u8fd9\u4efd\u793a\u4f8b\u5bf9\u4e8e\u4e0d\u53ef\u6062\u590d\u7684\u76d1\u542c\u9519\u8bef\u91c7\u7528\u9000\u51fa\u7b56\u7565\u3002\u4e1a\u52a1\u8fde\u63a5\u53d1\u751f\u9519\u8bef\u65f6\u6e05\u7406\u8be5\u8fde\u63a5\u3002\u751f\u4ea7\u7a0b\u5e8f\u5219\u9700\u8981\u8fdb\u4e00\u6b65\u8bbe\u8ba1\u670d\u52a1\u6062\u590d\u4e0e\u4f18\u96c5\u9000\u51fa\u3002<\/p>\n<h3 id=\"\u4e5d\u3001\u52a8\u624b\u5b9e\u9a8c\uff1a\u628a\u901a\u77e5\u5dee\u5f02\u548c\u5b57\u8282\u6d41\u884c\u4e3a\u771f\u6b63\u8dd1\u51fa\u6765\">\u4e5d\u3001\u52a8\u624b\u5b9e\u9a8c&#xff1a;\u628a\u901a\u77e5\u5dee\u5f02\u548c\u5b57\u8282\u6d41\u884c\u4e3a\u771f\u6b63\u8dd1\u51fa\u6765<\/h3>\n<h4 id=\"91-\u5b9e\u9a8c\u4e00\uff1a\u7528-socketpair-\u5bf9\u6bd4-ltet\">9.1 \u5b9e\u9a8c\u4e00&#xff1a;\u7528 socketpair \u5bf9\u6bd4 LT\/ET<\/h4>\n<p>Python \u7684 select.epoll \u5c01\u88c5\u4e86 Linux epoll\u3002\u6211\u4eec\u53ef\u4ee5\u7528 socketpair \u521b\u5efa\u672c\u673a\u76f8\u8fde\u7684\u4e00\u5bf9 socket&#xff0c;\u8ba9\u5b9e\u9a8c\u5c3d\u91cf\u5c11\u53d7\u7f51\u7edc\u65f6\u5e8f\u5e72\u6270\u3002<\/p>\n<p>\u8fd9\u4e2a\u5b9e\u9a8c\u6709\u610f\u53ea\u8bfb\u4e00\u90e8\u5206\u6570\u636e&#xff0c;\u7528\u6765\u5c55\u793a\u5dee\u5f02&#xff1b;\u5b83\u4e0d\u662f\u670d\u52a1\u5668\u7684\u63a8\u8350\u8bfb\u53d6\u65b9\u5f0f\u3002<\/p>\n<p>\u4fdd\u5b58\u4e3a lt_et_demo.py&#xff0c;\u5728 Linux \u4e0a\u8fd0\u884c&#xff1a;<\/p>\n<p>&#034;&#034;&#034;Run with Python 3 on Linux; demonstrate partial reads under LT and ET.&#034;&#034;&#034;<br \/>\nimport select<br \/>\nimport socket<\/p>\n<p>def run(edge_triggered):<br \/>\n    reader, writer &#061; socket.socketpair()<br \/>\n    reader.setblocking(False)<br \/>\n    label &#061; &#034;ET&#034; if edge_triggered else &#034;LT&#034;<br \/>\n    with reader, writer, select.epoll() as ep:<br \/>\n        flags &#061; select.EPOLLIN<br \/>\n        if edge_triggered:<br \/>\n            flags |&#061; select.EPOLLET<br \/>\n        ep.register(reader.fileno(), flags)<br \/>\n        writer.sendall(b&#034;A&#034; * 2048)<br \/>\n        first &#061; ep.poll(1)<br \/>\n        assert first, &#034;initial event was not received&#034;<br \/>\n        print(label, &#034;first event count:&#034;, len(first))<br \/>\n        assert len(reader.recv(1024)) &#061;&#061; 1024<br \/>\n        second &#061; ep.poll(0.1)<br \/>\n        print(label, &#034;second event count:&#034;, len(second))<br \/>\n        assert bool(second) &#061;&#061; (not edge_triggered)<br \/>\n        # No second ET notification is required for these remaining bytes.<br \/>\n        assert len(reader.recv(4096)) &#061;&#061; 1024<br \/>\n        try:<br \/>\n            reader.recv(1)<br \/>\n        except BlockingIOError:<br \/>\n            print(label, &#034;remaining data drained; now EAGAIN&#034;)<br \/>\n        else:<br \/>\n            raise AssertionError(&#034;expected EAGAIN after draining the socket&#034;)<\/p>\n<p>if __name__ &#061;&#061; &#034;__main__&#034;:<br \/>\n    run(False)<br \/>\n    run(True)<\/p>\n<p>python3 lt_et_demo.py<\/p>\n<p>\u5728\u8be5\u5b9e\u9a8c\u6761\u4ef6\u4e0b&#xff0c;\u9884\u671f\u7ed3\u679c\u4e3a&#xff1a;<\/p>\n<p>LT first event count: 1<br \/>\nLT second event count: 1<br \/>\nLT remaining data drained; now EAGAIN<br \/>\nET first event count: 1<br \/>\nET second event count: 0<br \/>\nET remaining data drained; now EAGAIN<\/p>\n<p>\u89c2\u5bdf\u6700\u540e\u4e24\u6b65&#xff1a;ET \u7684\u7b2c\u4e8c\u6b21\u7b49\u5f85\u6ca1\u6709\u4e8b\u4ef6&#xff0c;\u5e94\u7528\u4ecd\u80fd\u8bfb\u51fa\u5269\u4e0b\u7684 1KB\u3002\u8fd9\u5c31\u8bc1\u660e\u4e86&#xff1a;<\/p>\n<p>\u6ca1\u6709\u65b0\u7684\u4e8b\u4ef6\u901a\u77e5&#xff0c;\u4e0d\u80fd\u63a8\u51fa socket \u91cc\u6ca1\u6709\u672a\u5904\u7406\u6570\u636e\u3002<\/p>\n<p>\u8bfb\u5b8c\u540e\u518d\u5c1d\u8bd5\u4e00\u6b21&#xff0c;\u624d\u5f97\u5230 EAGAIN\u3002<\/p>\n<h4 id=\"92-\u5b9e\u9a8c\u4e8c\uff1a\u9a8c\u8bc1-echo-\u670d\u52a1\u5668\u7684\u5b57\u8282\u7ed3\u679c\">9.2 \u5b9e\u9a8c\u4e8c&#xff1a;\u9a8c\u8bc1 Echo \u670d\u52a1\u5668\u7684\u5b57\u8282\u7ed3\u679c<\/h4>\n<p>\u5148\u542f\u52a8\u670d\u52a1\u5668&#xff0c;\u518d\u5c06\u4e0b\u9762\u811a\u672c\u4fdd\u5b58\u4e3a test_echo.py&#xff1a;<\/p>\n<p>&#034;&#034;&#034;Start epoll_echo first, then run: python3 test_echo.py [port].&#034;&#034;&#034;<br \/>\nfrom concurrent.futures import ThreadPoolExecutor<br \/>\nimport socket<br \/>\nimport sys<\/p>\n<p>PORT &#061; int(sys.argv[1]) if len(sys.argv) &gt; 1 else 8080<\/p>\n<p>def check_echo(payload, fragment_size&#061;None):<br \/>\n    with socket.create_connection((&#034;127.0.0.1&#034;, PORT), timeout&#061;5) as s:<br \/>\n        s.settimeout(10)<br \/>\n        if fragment_size:<br \/>\n            for pos in range(0, len(payload), fragment_size):<br \/>\n                s.sendall(payload[pos:pos &#043; fragment_size])<br \/>\n        else:<br \/>\n            s.sendall(payload)<br \/>\n        s.shutdown(socket.SHUT_WR)<br \/>\n        result &#061; bytearray()<br \/>\n        while True:<br \/>\n            part &#061; s.recv(65536)<br \/>\n            if not part:<br \/>\n                break<br \/>\n            result.extend(part)<br \/>\n        assert result &#061;&#061; payload, (len(payload), len(result))<\/p>\n<p>check_echo(b&#034;hello epoll\\\\n&#034;)<br \/>\ncheck_echo(bytes(range(256)) * 32, fragment_size&#061;7)<br \/>\ncheck_echo(bytes(range(256)) * 4096)<br \/>\ncheck_echo(b&#034;&#034;)<br \/>\nwith ThreadPoolExecutor(max_workers&#061;32) as pool:<br \/>\n    list(pool.map(lambda i: check_echo((f&#034;client-{i}\\\\n&#034;.encode()) * 200),<br \/>\n                  range(64)))<br \/>\nprint(&#034;PASS: text, fragmented writes, binary data, 1 MiB, half-close, 64 clients&#034;)<\/p>\n<p>\u6267\u884c&#xff1a;<\/p>\n<p>python3 test_echo.py 8080<\/p>\n<p>\u5168\u90e8\u65ad\u8a00\u901a\u8fc7\u65f6&#xff0c;\u811a\u672c\u4f1a\u8f93\u51fa&#xff1a;<\/p>\n<p>PASS: text, fragmented writes, binary data, 1 MiB, half-close, 64 clients<\/p>\n<p>\u7136\u540e\u5206\u522b\u5bf9 LT\u3001ET \u5404\u8fd0\u884c\u4e00\u904d\u3002<\/p>\n<p>\u8fd9\u4e9b\u7528\u4f8b\u68c0\u67e5\u4e86\u51e0\u4e2a\u5bb9\u6613\u88ab\u201c\u5c0f\u5b57\u7b26\u4e32\u6f14\u793a\u201d\u63a9\u76d6\u7684\u95ee\u9898&#xff1a;<\/p>\n<ul>\n<li>\u4e8c\u8fdb\u5236\u6570\u636e\u4e2d\u7684 \\\\0 \u6709\u6ca1\u6709\u88ab\u9519\u8bef\u622a\u65ad\u3002<\/li>\n<li>\u8d85\u8fc7\u5355\u6b21\u63a5\u6536\u7f13\u51b2\u533a\u5927\u5c0f\u7684\u6570\u636e\u80fd\u5426\u5b8c\u6574\u56de\u663e\u3002<\/li>\n<li>\u5ba2\u6237\u7aef\u591a\u6b21\u5199\u5165\u540e&#xff0c;\u7ed3\u679c\u662f\u5426\u4ecd\u6309\u5b57\u8282\u987a\u5e8f\u4e00\u81f4\u3002<\/li>\n<li>\u5ba2\u6237\u7aef\u7ed3\u675f\u5199\u65b9\u5411\u540e&#xff0c;\u662f\u5426\u4ecd\u80fd\u6536\u5230\u5b8c\u6574\u54cd\u5e94\u3002<\/li>\n<li>\u591a\u4e2a\u5ba2\u6237\u7aef\u662f\u5426\u80fd\u591f\u88ab\u540c\u4e00\u4e2a\u4e8b\u4ef6\u5faa\u73af\u670d\u52a1\u3002<\/li>\n<\/ul>\n<p>\u5176\u4e2d\u201c\u6bcf 7 \u5b57\u8282\u53d1\u9001\u4e00\u6b21\u201d\u53ea\u662f\u5ba2\u6237\u7aef\u5199\u5165\u65b9\u5f0f&#xff0c;\u4e0d\u4fdd\u8bc1\u670d\u52a1\u5668\u6070\u597d\u6bcf\u6b21\u6536\u5230 7 \u5b57\u8282\u3002\u8fd9\u4e5f\u662f TCP \u5b57\u8282\u6d41\u7684\u4e00\u90e8\u5206\u8bed\u4e49\u3002<\/p>\n<p>\u8fd9\u7ec4\u57fa\u7840\u7528\u4f8b\u4e0d\u4fdd\u8bc1\u6bcf\u4e00\u6b21\u8fd0\u884c\u90fd\u89e6\u53d1 send \u7684 EAGAIN\u3002\u8981\u4e13\u95e8\u89c2\u5bdf\u5199\u56de\u538b&#xff0c;\u53ef\u4ee5\u8c03\u5c0f\u670d\u52a1\u7aef\u8fde\u63a5\u7684 SO_SNDBUF&#xff0c;\u8ba9\u5ba2\u6237\u7aef\u5ef6\u8fdf\u6216\u7f13\u6162\u8bfb\u53d6&#xff0c;\u5e76\u89c2\u5bdf\u53d1\u9001\u8fd4\u56de\u503c\u4e0e EPOLLOUT \u7684\u542f\u505c&#xff1b;\u6d4b\u8bd5\u6570\u636e\u8981\u4e0e\u793a\u4f8b\u7684 4MiB \u9650\u989d\u534f\u8c03\u3002<\/p>\n<h4 id=\"93-\u5b9e\u9a8c\u4e09\uff1a\u89c2\u5bdf\u7cfb\u7edf\u8c03\u7528\">9.3 \u5b9e\u9a8c\u4e09&#xff1a;\u89c2\u5bdf\u7cfb\u7edf\u8c03\u7528<\/h4>\n<p>\u5728\u5b89\u88c5\u4e86 strace \u7684 Linux \u73af\u5883\u4e2d&#xff0c;\u53ef\u4ee5\u8fd9\u6837\u542f\u52a8\u670d\u52a1\u5668&#xff1a;<\/p>\n<p>strace -e trace&#061;epoll_create1,epoll_ctl,epoll_wait,accept4,recvfrom,sendto \\\\<br \/>\n    .\/epoll_echo 8080 et<\/p>\n<p>\u5728\u5e38\u89c1 Linux \u73af\u5883\u4e2d&#xff0c;socket \u7684 recv\u3001send \u8c03\u7528\u53ef\u80fd\u663e\u793a\u4e3a recvfrom\u3001sendto \u7b49\u5e95\u5c42\u7cfb\u7edf\u8c03\u7528\u540d\u79f0\u3002\u5177\u4f53\u663e\u793a\u4e0e\u67b6\u6784\u3001\u8fd0\u884c\u5e93\u6709\u5173\u3002<\/p>\n<p>\u91cd\u70b9\u89c2\u5bdf&#xff1a;<\/p>\n<li>\u65b0\u8fde\u63a5\u5efa\u7acb\u540e\u51fa\u73b0 EPOLL_CTL_ADD\u3002<\/li>\n<li>\u4e00\u6b21\u8bfb\u4e8b\u4ef6\u540e&#xff0c;\u53ef\u80fd\u8fde\u7eed\u51fa\u73b0\u591a\u6b21\u63a5\u6536\u8c03\u7528\u3002<\/li>\n<li>\u975e\u963b\u585e\u63a5\u6536\u6700\u7ec8\u53ef\u80fd\u8fd4\u56de EAGAIN\u3002<\/li>\n<li>\u6ca1\u6709\u4e8b\u60c5\u53ef\u505a\u65f6&#xff0c;\u7ebf\u7a0b\u505c\u5728 epoll_wait\u3002<\/li>\n<li>\u6709\u672a\u53d1\u9001\u5b8c\u7684\u6570\u636e\u65f6&#xff0c;\u5173\u6ce8\u96c6\u5408\u624d\u9700\u8981\u5305\u542b EPOLLOUT\u3002<\/li>\n<p>\u628a\u6e90\u7801\u3001\u4e8b\u4ef6\u548c\u7cfb\u7edf\u8c03\u7528\u5bf9\u5e94\u8d77\u6765&#xff0c;epoll \u5c31\u4e0d\u518d\u53ea\u662f\u51e0\u4e2a\u62bd\u8c61\u540d\u8bcd\u3002<\/p>\n<h3 id=\"\u5341\u3001\u77e5\u8bc6\u8865\u5168\uff1aepoll-\u65e0\u6cd5\u66ff\u4f60\u89e3\u51b3-tcp-\u6d88\u606f\u8fb9\u754c\">\u5341\u3001\u77e5\u8bc6\u8865\u5168&#xff1a;epoll \u65e0\u6cd5\u66ff\u4f60\u89e3\u51b3 TCP \u6d88\u606f\u8fb9\u754c<\/h3>\n<h4 id=\"101-\u4e00\u6b21-send-\u548c\u4e00\u6b21-recv-\u6ca1\u6709\u4e00\u4e00\u5bf9\u5e94\u5173\u7cfb\">10.1 \u4e00\u6b21 send \u548c\u4e00\u6b21 recv \u6ca1\u6709\u4e00\u4e00\u5bf9\u5e94\u5173\u7cfb<\/h4>\n<p>\u5ba2\u6237\u7aef\u6267\u884c&#xff1a;<\/p>\n<p>send(&#034;hello&#034;)<br \/>\nsend(&#034;world&#034;)<\/p>\n<p>\u670d\u52a1\u5668\u53ef\u80fd\u8bfb\u5230&#xff1a;<\/p>\n<p>\u4e00\u6b21&#xff1a;&#034;helloworld&#034;<\/p>\n<p>\u4e5f\u53ef\u80fd\u8bfb\u5230&#xff1a;<\/p>\n<p>\u7b2c\u4e00\u6b21&#xff1a;&#034;hel&#034;<br \/>\n\u7b2c\u4e8c\u6b21&#xff1a;&#034;lowo&#034;<br \/>\n\u7b2c\u4e09\u6b21&#xff1a;&#034;rld&#034;<\/p>\n<p>\u53ea\u8981\u5b57\u8282\u5185\u5bb9\u548c\u987a\u5e8f\u7b26\u5408 TCP \u8bed\u4e49&#xff0c;\u8fd9\u4e9b\u60c5\u51b5\u90fd\u53ef\u80fd\u662f\u6b63\u5e38\u7684\u3002TCP \u63d0\u4f9b\u53ef\u9760\u3001\u6709\u5e8f\u7684\u5b57\u8282\u6d41&#xff0c;\u4f46\u4e0d\u4fdd\u7559\u5e94\u7528\u5199\u5165\u7684\u6d88\u606f\u8fb9\u754c&#xff0c;\u89c1 tcp(7)\u3002<\/p>\n<p>\u56e0\u6b64&#xff0c;\u5927\u5bb6\u5e38\u8bf4\u7684\u201c\u7c98\u5305\u3001\u62c6\u5305\u201d&#xff0c;\u9700\u8981\u7531\u5e94\u7528\u534f\u8bae\u7684\u7ec4\u5e27\u89c4\u5219\u5904\u7406\u3002<\/p>\n<h4 id=\"102-\u8f93\u5165\u7f13\u51b2\u533a\u7684\u5bff\u547d\uff0c\u8981\u8de8\u8fc7\u591a\u6b21\u4e8b\u4ef6\">10.2 \u8f93\u5165\u7f13\u51b2\u533a\u7684\u5bff\u547d&#xff0c;\u8981\u8de8\u8fc7\u591a\u6b21\u4e8b\u4ef6<\/h4>\n<p>\u5e38\u89c1\u7684\u6d88\u606f\u8fb9\u754c\u8bbe\u8ba1\u6709&#xff1a;<\/p>\n<table>\n<tr>\u65b9\u5f0f\u4f8b\u5b50\u8981\u6ce8\u610f\u4ec0\u4e48<\/tr>\n<tbody>\n<tr>\n<td>\u56fa\u5b9a\u957f\u5ea6<\/td>\n<td>\u6bcf\u6761\u8bf7\u6c42\u56fa\u5b9a 128 \u5b57\u8282<\/td>\n<td>\u683c\u5f0f\u7075\u6d3b\u6027\u6709\u9650<\/td>\n<\/tr>\n<tr>\n<td>\u5206\u9694\u7b26<\/td>\n<td>\u6bcf\u884c\u4e00\u6761&#xff0c;\u4ee5 \\\\n \u7ed3\u675f<\/td>\n<td>\u6b63\u6587\u4e2d\u7684\u5206\u9694\u7b26\u9700\u8981\u8f6c\u4e49\u6216\u7ea6\u5b9a<\/td>\n<\/tr>\n<tr>\n<td>\u957f\u5ea6\u524d\u7f00<\/td>\n<td>4 \u5b57\u8282\u957f\u5ea6 &#043; \u6d88\u606f\u4f53<\/td>\n<td>\u957f\u5ea6\u5927\u5c0f\u3001\u5b57\u8282\u5e8f\u3001\u6700\u5927\u957f\u5ea6\u6821\u9a8c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4ee5\u201c4 \u5b57\u8282\u957f\u5ea6\u5934 &#043; \u6d88\u606f\u4f53\u201d\u4e3a\u4f8b&#xff0c;\u6bcf\u4e2a\u8fde\u63a5\u53ef\u4ee5\u7ef4\u62a4\u72ec\u7acb\u8f93\u5165\u7f13\u51b2\u533a&#xff1a;<\/p>\n<p>recv \u5f97\u5230\u65b0\u5b57\u8282 \u2192 \u8ffd\u52a0\u5230\u8be5\u8fde\u63a5\u7684\u8f93\u5165\u7f13\u51b2\u533a<br \/>\n                        \u2193<br \/>\n                  \u662f\u5426\u81f3\u5c11\u6709 4 \u5b57\u8282&#xff1f;<br \/>\n                  \u5426&#xff1a;\u4fdd\u7559&#xff0c;\u7b49\u4e0b\u6b21\u6570\u636e<br \/>\n                  \u662f&#xff1a;\u8bfb\u53d6\u5e76\u6821\u9a8c\u6d88\u606f\u957f\u5ea6<br \/>\n                        \u2193<br \/>\n                  \u6d88\u606f\u4f53\u662f\u5426\u5df2\u7ecf\u5230\u9f50&#xff1f;<br \/>\n                  \u5426&#xff1a;\u4fdd\u7559&#xff0c;\u7b49\u4e0b\u6b21\u6570\u636e<br \/>\n                  \u662f&#xff1a;\u53d6\u51fa\u4e00\u6761\u6d88\u606f\u5e76\u5904\u7406<br \/>\n                        \u2193<br \/>\n                  \u7ee7\u7eed\u68c0\u67e5\u662f\u5426\u8fd8\u6709\u4e0b\u4e00\u6761\u5b8c\u6574\u6d88\u606f<\/p>\n<p>\u957f\u5ea6\u5934\u5e94\u6309\u534f\u8bae\u6307\u5b9a\u7684\u5b57\u8282\u5e8f\u89e3\u7801&#xff0c;\u5e76\u5728\u5206\u914d\u5185\u5b58\u6216\u7b49\u5f85\u6d88\u606f\u4f53\u524d\u6821\u9a8c\u6700\u5927\u5141\u8bb8\u957f\u5ea6\u3002<\/p>\n<p>\u6709\u4e24\u4e2a\u8fb9\u754c\u60c5\u51b5\u5c24\u5176\u91cd\u8981&#xff1a;<\/p>\n<ul>\n<li>\u4e00\u6b21 recv \u5e26\u6765\u591a\u6761\u5b8c\u6574\u6d88\u606f&#xff0c;\u89e3\u6790\u5668\u8981\u80fd\u8fde\u7eed\u53d6\u51fa\u3002<\/li>\n<li>\u5bf9\u7aef EOF \u65f6\u8fd8\u5269\u4e0d\u5b8c\u6574\u6d88\u606f&#xff0c;\u8981\u6309\u534f\u8bae\u5224\u65ad\u622a\u65ad\u6216\u9519\u8bef\u3002<\/li>\n<\/ul>\n<p>\u524d\u9762 Echo \u670d\u52a1\u5668\u6ca1\u6709\u8f93\u5165\u89e3\u6790\u5668&#xff0c;\u662f\u56e0\u4e3a\u5b83\u7684\u4e1a\u52a1\u89c4\u5219\u5c31\u662f\u201c\u6536\u5230\u591a\u5c11\u5b57\u8282&#xff0c;\u5c31\u6309\u987a\u5e8f\u56de\u663e\u591a\u5c11\u5b57\u8282\u201d\u3002\u771f\u6b63\u7684\u804a\u5929\u534f\u8bae\u3001RPC \u534f\u8bae\u6216 HTTP \u670d\u52a1&#xff0c;\u9700\u8981\u8865\u4e0a\u81ea\u5df1\u7684\u89e3\u6790\u72b6\u6001\u3002<\/p>\n<p>\u81f3\u6b64&#xff0c;\u524d\u540e\u4e24\u6761\u7ebf\u63a5\u4e0a\u4e86&#xff1a;<\/p>\n<p>epoll \u7ba1\u7406\u4f55\u65f6\u63a8\u8fdb I\/O&#xff1b;\u8fde\u63a5\u72b6\u6001\u673a\u7ba1\u7406\u5b57\u8282\u5982\u4f55\u7ec4\u6210\u6d88\u606f\u3002<\/p>\n<h3 id=\"\u5341\u4e00\u3001\u8fdb\u9636\u8fb9\u754c\uff1a\u7ebf\u7a0b\u6c60\u3001oneshot\u3001\u516c\u5e73\u6027\u548c\u60ca\u7fa4\">\u5341\u4e00\u3001\u8fdb\u9636\u8fb9\u754c&#xff1a;\u7ebf\u7a0b\u6c60\u3001ONESHOT\u3001\u516c\u5e73\u6027\u548c\u60ca\u7fa4<\/h3>\n<h4 id=\"111-\u5355\u7ebf\u7a0b\u4e8b\u4ef6\u5faa\u73af\u4e0d\u9002\u5408\u505a\u8017\u65f6\u4e1a\u52a1\">11.1 \u5355\u7ebf\u7a0b\u4e8b\u4ef6\u5faa\u73af\u4e0d\u9002\u5408\u505a\u8017\u65f6\u4e1a\u52a1<\/h4>\n<p>\u5373\u4f7f\u7f51\u7edc\u8bfb\u5199\u90fd\u975e\u963b\u585e&#xff0c;\u4e0b\u9762\u7684\u903b\u8f91\u4ecd\u53ef\u80fd\u8ba9\u6240\u6709\u8fde\u63a5\u505c\u4e0b\u6765&#xff1a;<\/p>\n<p>\u6536\u5230\u4e00\u4e2a\u8fde\u63a5\u7684\u8bf7\u6c42 \u2192 \u5728\u4e8b\u4ef6\u5faa\u73af\u91cc\u6267\u884c 3 \u79d2\u8ba1\u7b97 \u2192 \u518d\u5904\u7406\u5176\u4ed6\u8fde\u63a5<\/p>\n<p>\u540c\u6b65\u6570\u636e\u5e93\u67e5\u8be2\u3001\u963b\u585e\u78c1\u76d8\u8bbf\u95ee\u3001\u7b49\u5f85\u4e92\u65a5\u9501&#xff0c;\u4e5f\u53ef\u80fd\u9020\u6210\u7c7b\u4f3c\u95ee\u9898\u3002epoll \u4e0d\u4f1a\u628a\u8fd9\u4e9b\u64cd\u4f5c\u81ea\u52a8\u53d8\u6210\u975e\u963b\u585e\u3002<\/p>\n<p>\u5e38\u89c1\u6539\u8fdb\u65b9\u5f0f\u662f&#xff1a;\u4e8b\u4ef6\u5faa\u73af\u8d1f\u8d23\u7f51\u7edc\u72b6\u6001\u4e0e\u8f7b\u91cf\u8c03\u5ea6&#xff0c;\u7ebf\u7a0b\u6c60\u5904\u7406\u9002\u5408\u79fb\u4ea4\u7684\u8017\u65f6\u4efb\u52a1&#xff0c;\u5b8c\u6210\u540e\u518d\u5b89\u5168\u5730\u628a\u7ed3\u679c\u4ea4\u56de\u8fde\u63a5\u6240\u5c5e\u7684\u4e8b\u4ef6\u5faa\u73af\u3002<\/p>\n<p>\u8fd9\u5c5e\u4e8e Reactor \u67b6\u6784\u7684\u5e38\u89c1\u7ec4\u7ec7\u65b9\u5f0f\u3002epoll \u63d0\u4f9b\u5176\u4e2d\u7684\u5c31\u7eea\u4e8b\u4ef6\u6765\u6e90&#xff0c;\u8fde\u63a5\u5bf9\u8c61\u3001\u4efb\u52a1\u8c03\u5ea6\u548c\u7ebf\u7a0b\u95f4\u901a\u4fe1\u4ecd\u9700\u6846\u67b6\u8bbe\u8ba1\u3002<\/p>\n<h4 id=\"112-epolloneshot\uff1a\u4ea4\u4ed8\u4e00\u6b21\u540e\u6682\u505c\uff0c\u4f7f\u7528-mod-\u91cd\u65b0\u6fc0\u6d3b\">11.2 EPOLLONESHOT&#xff1a;\u4ea4\u4ed8\u4e00\u6b21\u540e\u6682\u505c&#xff0c;\u4f7f\u7528 MOD \u91cd\u65b0\u6fc0\u6d3b<\/h4>\n<p>\u591a\u4e2a\u5de5\u4f5c\u7ebf\u7a0b\u5904\u7406\u540c\u4e00\u8fde\u63a5\u65f6&#xff0c;\u5e94\u7528\u901a\u5e38\u5e0c\u671b\u660e\u786e\u201c\u5f53\u524d\u7531\u8c01\u5904\u7406\u5b83\u201d\u3002EPOLLONESHOT \u53ef\u4ee5\u53c2\u4e0e\u8fd9\u79cd\u8bbe\u8ba1&#xff1a;<\/p>\n<p>ev.events &#061; EPOLLIN | EPOLLET | EPOLLONESHOT;<br \/>\nev.data.u64 &#061; connection_id;<br \/>\nepoll_ctl(epfd, EPOLL_CTL_ADD, fd, &amp;ev);<\/p>\n<p>\u5f53\u8be5\u5173\u6ce8\u9879\u7684\u4e00\u6b21\u4e8b\u4ef6\u88ab\u4ea4\u4ed8\u540e&#xff0c;\u540e\u7eed\u901a\u77e5\u6682\u505c\u3002\u5904\u7406\u5b8c\u5f53\u524d\u72b6\u6001&#xff0c;\u51c6\u5907\u7ee7\u7eed\u76d1\u542c\u65f6&#xff1a;<\/p>\n<p>ev.events &#061; EPOLLIN | EPOLLET | EPOLLONESHOT;<br \/>\nev.data.u64 &#061; connection_id;<br \/>\nif (epoll_ctl(epfd, EPOLL_CTL_MOD, fd, &amp;ev) &#061;&#061; -1) {<br \/>\n    \/\/ \u5904\u7406\u91cd\u65b0\u6fc0\u6d3b\u5931\u8d25\u3002<br \/>\n}<\/p>\n<p>**\u8fd9\u91cc\u4f7f\u7528 MOD&#xff0c;\u65e0\u9700\u5148\u5220\u9664\u518d ADD\u3002**\u5982\u679c\u8fde\u63a5\u8fd8\u6709\u5f85\u53d1\u9001\u5185\u5bb9&#xff0c;\u91cd\u65b0\u6fc0\u6d3b\u65f6\u4e5f\u8981\u628a\u76f8\u5e94\u5199\u5173\u6ce8\u4fdd\u7559\u4e0b\u6765\u3002\u5b9a\u4e49\u89c1 epoll_ctl(2)\u3002<\/p>\n<p>ONESHOT \u53ea\u63a7\u5236\u5173\u6ce8\u9879\u7684\u4e8b\u4ef6\u4ea4\u4ed8\u3002\u5176\u4ed6\u7ebf\u7a0b\u5982\u679c\u76f4\u63a5\u64cd\u4f5c\u540c\u4e00 socket&#xff0c;\u6216\u8005\u8fde\u63a5\u5bf9\u8c61\u7684\u8bbf\u95ee\u6ca1\u6709\u534f\u8c03&#xff0c;\u5b83\u5e76\u4e0d\u4f1a\u81ea\u52a8\u89e3\u51b3\u8fd9\u4e9b\u7ade\u4e89\u3002<\/p>\n<h4 id=\"113-\u5185\u6838\u63a5\u53e3\u80fd\u5e76\u53d1\u4f7f\u7528\uff0c\u4e0d\u4ee3\u8868\u4e1a\u52a1\u8fde\u63a5\u5929\u7136\u7ebf\u7a0b\u5b89\u5168\">11.3 \u5185\u6838\u63a5\u53e3\u80fd\u5e76\u53d1\u4f7f\u7528&#xff0c;\u4e0d\u4ee3\u8868\u4e1a\u52a1\u8fde\u63a5\u5929\u7136\u7ebf\u7a0b\u5b89\u5168<\/h4>\n<p>\u9700\u8981\u5206\u522b\u8003\u8651&#xff1a;<\/p>\n<ul>\n<li>\u5185\u6838\u5982\u4f55\u540c\u6b65\u5bf9\u540c\u4e00\u4e2a epoll \u5b9e\u4f8b\u7684\u64cd\u4f5c\u3002<\/li>\n<li>\u5e94\u7528\u5982\u4f55\u540c\u6b65\u8fde\u63a5\u8f93\u5165\u3001\u8f93\u51fa\u7f13\u51b2\u533a\u3002<\/li>\n<li>\u54ea\u4e2a\u7ebf\u7a0b\u6709\u6743\u5173\u95ed fd\u3002<\/li>\n<li>\u54ea\u4e2a\u7ebf\u7a0b\u6709\u6743\u91ca\u653e data.ptr \u6307\u5411\u7684\u5bf9\u8c61\u3002<\/li>\n<li>\u5f53\u524d\u4e8b\u4ef6\u6570\u7ec4\u91cc\u662f\u5426\u8fd8\u6709\u6307\u5411\u65e7\u8fde\u63a5\u7684\u4e8b\u4ef6\u3002<\/li>\n<\/ul>\n<p>\u7279\u522b\u662f\u4f7f\u7528 data.ptr \u65f6&#xff1a;\u6307\u9488\u5df2\u7ecf\u8fd4\u56de\u7ed9\u5e94\u7528&#xff0c;\u4f46\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u628a\u5bf9\u8c61\u91ca\u653e\u4e86&#xff0c;\u5c31\u53ef\u80fd\u53d1\u751f\u60ac\u7a7a\u8bbf\u95ee\u3002<\/p>\n<p>\u5e38\u89c1\u65b9\u6cd5\u5305\u62ec\u8fde\u63a5\u5f52\u5c5e\u7ebf\u7a0b\u3001\u5f15\u7528\u8ba1\u6570\u3001\u5ef6\u8fdf\u56de\u6536&#xff0c;\u4ee5\u53ca\u8fde\u63a5\u7f16\u53f7\u914d\u5408\u4ee3\u9645\u4fe1\u606f\u3002\u672c\u6587\u793a\u4f8b\u4f7f\u7528\u5355\u7ebf\u7a0b\u7ba1\u7406\u8fde\u63a5&#xff0c;\u5e76\u7528\u4e0d\u590d\u7528\u7684\u9012\u589e\u7f16\u53f7\u5173\u8054\u4e8b\u4ef6&#xff0c;\u4f7f\u751f\u547d\u5468\u671f\u66f4\u5bb9\u6613\u8ffd\u8e2a\u3002<\/p>\n<h4 id=\"114-\u516c\u5e73\u6027\uff1aet-\u4e0d\u80fd\u65e0\u9650\u8bfb\u4e00\u4e2a\u8fde\u63a5\">11.4 \u516c\u5e73\u6027&#xff1a;ET \u4e0d\u80fd\u65e0\u9650\u8bfb\u4e00\u4e2a\u8fde\u63a5<\/h4>\n<p>\u201c\u8bfb\u5230 EAGAIN\u201d\u5f88\u597d\u5730\u8bf4\u660e\u4e86\u57fa\u7840\u6b63\u786e\u6027&#xff0c;\u4f46\u9762\u5bf9\u6301\u7eed\u9ad8\u901f\u53d1\u9001\u7684\u8fde\u63a5&#xff0c;\u4e8b\u4ef6\u5faa\u73af\u53ef\u80fd\u957f\u65f6\u95f4\u505c\u7559\u5728\u8fd9\u4e00\u4e2a\u8fde\u63a5\u4e0a\u3002<\/p>\n<p>\u5de5\u7a0b\u4e0a\u53ef\u4ee5\u7ed9\u5355\u4e2a\u8fde\u63a5\u8bbe\u7f6e\u672c\u8f6e\u5b57\u8282\u9884\u7b97\u6216\u5904\u7406\u6b21\u6570\u9884\u7b97\u3002\u9884\u7b97\u8017\u5c3d\u540e&#xff0c;\u628a\u5b83\u7559\u5728\u5e94\u7528\u81ea\u5df1\u7684\u5f85\u5904\u7406\u961f\u5217\u91cc&#xff0c;\u4e0b\u4e00\u8f6e\u7ee7\u7eed\u63a8\u8fdb\u3002<\/p>\n<p>**ET \u4e0b\u63d0\u524d\u8ba9\u51fa\u6267\u884c\u6743\u65f6&#xff0c;\u5fc5\u987b\u8bb0\u4f4f\u5b83\u8fd8\u6709\u5de5\u4f5c\u3002**\u5982\u679c\u6ca1\u6709\u8bfb\u5230\u6682\u65f6\u65e0\u6570\u636e&#xff0c;\u5c31\u76f4\u63a5\u628a\u5b83\u9057\u5fd8\u5e76\u7b49\u5f85\u65b0\u8fb9\u7f18&#xff0c;\u4ecd\u53ef\u80fd\u56de\u5230\u524d\u9762\u201c\u5269\u4f59\u6570\u636e\u65e0\u4eba\u5904\u7406\u201d\u7684\u95ee\u9898\u3002<\/p>\n<p>\u672c\u6587\u4e3a\u4e86\u8ba9\u6838\u5fc3\u4ee3\u7801\u5bb9\u6613\u8ddf\u8e2a&#xff0c;\u6ca1\u6709\u52a0\u5165\u8fd9\u5957\u8c03\u5ea6\u5668&#xff0c;\u56e0\u6b64\u9ad8\u8d1f\u8f7d\u4e0b\u8fd8\u9700\u8981\u6269\u5c55\u516c\u5e73\u8c03\u5ea6\u3002<\/p>\n<h4 id=\"115-\u60ca\u7fa4\uff1a\u5148\u5206\u6e05\u5927\u5bb6\u5728\u7b49\u54ea\u4e00\u4e2a\u5bf9\u8c61\">11.5 \u60ca\u7fa4&#xff1a;\u5148\u5206\u6e05\u5927\u5bb6\u5728\u7b49\u54ea\u4e00\u4e2a\u5bf9\u8c61<\/h4>\n<p>\u60ca\u7fa4\u901a\u5e38\u6307&#xff1a;\u4e00\u4e2a\u4e8b\u4ef6\u5524\u9192\u591a\u4e2a\u7b49\u5f85\u8005&#xff0c;\u4f46\u53ea\u6709\u90e8\u5206\u7b49\u5f85\u8005\u5b9e\u9645\u53d6\u5f97\u5de5\u4f5c&#xff0c;\u5176\u4f59\u7b49\u5f85\u8005\u4ed8\u51fa\u5524\u9192\u4e0e\u7ade\u4e89\u6210\u672c\u3002<\/p>\n<p>epoll \u4e2d\u4e0d\u80fd\u628a\u6240\u6709\u60c5\u51b5\u6df7\u5728\u4e00\u8d77&#xff1a;<\/p>\n<table>\n<tr>\u60c5\u5f62\u9700\u8981\u7406\u89e3\u7684\u533a\u522b<\/tr>\n<tbody>\n<tr>\n<td>\u591a\u4e2a\u7ebf\u7a0b\u7b49\u5f85\u540c\u4e00\u4e2a epoll \u5b9e\u4f8b<\/td>\n<td>\u5bf9\u540c\u4e00\u5b9e\u4f8b\u4e2d\u4ee5 ET \u5173\u6ce8\u7684 fd&#xff0c;\u624b\u518c\u63cf\u8ff0\u5176\u5c31\u7eea\u65f6\u5524\u9192\u5176\u4e2d\u4e00\u4e2a\u7b49\u5f85\u8005\u7684\u884c\u4e3a<\/td>\n<\/tr>\n<tr>\n<td>\u591a\u4e2a epoll \u5b9e\u4f8b\u5173\u6ce8\u540c\u4e00\u4e2a\u76d1\u542c socket<\/td>\n<td>\u666e\u901a\u6ce8\u518c\u4e0b&#xff0c;\u591a\u4e2a\u5b9e\u4f8b\u53ef\u80fd\u6536\u5230\u4e8b\u4ef6&#xff1b;\u53ef\u7ed3\u5408 EPOLLEXCLUSIVE \u8ba8\u8bba<\/td>\n<\/tr>\n<tr>\n<td>\u591a\u4e2a\u76d1\u542c socket \u5206\u914d\u8fde\u63a5<\/td>\n<td>\u53ef\u7ed3\u5408 SO_REUSEPORT \u7b49\u4e0d\u540c\u67b6\u6784\u8ba8\u8bba<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>EPOLLEXCLUSIVE \u9488\u5bf9\u7684\u662f\u7279\u5b9a\u5171\u4eab\u5173\u6ce8\u573a\u666f&#xff0c;\u4e0d\u80fd\u4ee3\u66ff EPOLLONESHOT&#xff0c;\u4e5f\u4e0d\u80fd\u88ab\u7406\u89e3\u4e3a\u201c\u6240\u6709\u573a\u666f\u4e25\u683c\u53ea\u5524\u9192\u4e00\u4e2a\u201d\u3002\u4f8b\u5982\u5176\u4f7f\u7528\u9650\u5236\u5305\u62ec\u53ea\u80fd\u968f ADD \u8bbe\u7f6e\u3001\u4e0d\u80fd\u4e0e ONESHOT \u4efb\u610f\u7ec4\u5408\u3002\u9700\u8981\u843d\u5730\u65f6\u5e94\u67e5\u770b epoll_ctl(2) \u7684\u5b8c\u6574\u9650\u5236\u3002<\/p>\n<p>\u8ba8\u8bba\u5e76\u53d1\u4e4b\u524d&#xff0c;\u5148\u753b\u6e05\u695a\u201c\u8fdb\u7a0b\/\u7ebf\u7a0b \u2192 epoll \u5b9e\u4f8b \u2192 \u76d1\u542c socket\u201d\u7684\u5173\u7cfb&#xff0c;\u6bd4\u53ea\u80cc\u4e00\u4e2a\u201cepoll \u89e3\u51b3\u60ca\u7fa4\u201d\u7684\u7ed3\u8bba\u66f4\u6709\u7528\u3002<\/p>\n<h3 id=\"\u5341\u4e8c\u3001select\u3001poll\u3001epoll-\u653e\u5728\u4e00\u8d77\u6bd4\u8f83\">\u5341\u4e8c\u3001select\u3001poll\u3001epoll \u653e\u5728\u4e00\u8d77\u6bd4\u8f83<\/h3>\n<table>\n<tr>\u7ef4\u5ea6selectpollepoll<\/tr>\n<tbody>\n<tr>\n<td>\u5173\u6ce8\u9879\u5f62\u5f0f<\/td>\n<td>\u591a\u4e2a fd_set<\/td>\n<td>pollfd \u6570\u7ec4<\/td>\n<td>\u5185\u6838\u4e2d\u6301\u4e45\u4fdd\u5b58\u7684\u5173\u6ce8\u96c6\u5408<\/td>\n<\/tr>\n<tr>\n<td>\u8f93\u5165\u8f93\u51fa<\/td>\n<td>\u8fd4\u56de\u65f6\u4fee\u6539\u96c6\u5408<\/td>\n<td>events \u4e0e revents \u5206\u5f00<\/td>\n<td>ctl \u6ce8\u518c&#xff0c;wait \u8f93\u51fa<\/td>\n<\/tr>\n<tr>\n<td>\u6bcf\u8f6e\u662f\u5426\u63d0\u4ea4\u5b8c\u6574\u5173\u6ce8\u96c6\u5408<\/td>\n<td>\u662f<\/td>\n<td>\u662f<\/td>\n<td>\u65e0\u9700\u91cd\u590d\u63d0\u4ea4&#xff0c;\u6309\u53d8\u5316\u7ef4\u62a4<\/td>\n<\/tr>\n<tr>\n<td>\u5e94\u7528\u5982\u4f55\u627e\u51fa\u672c\u8f6e\u5c31\u7eea\u9879<\/td>\n<td>\u68c0\u67e5\u8fd4\u56de\u96c6\u5408<\/td>\n<td>\u68c0\u67e5\u6570\u7ec4\u7684 revents<\/td>\n<td>\u904d\u5386\u8fd4\u56de\u7684\u4e8b\u4ef6\u6570\u7ec4<\/td>\n<\/tr>\n<tr>\n<td>\u5927\u91cf\u7a7a\u95f2\u8fde\u63a5\u7684\u91cd\u590d\u68c0\u67e5\u6210\u672c<\/td>\n<td>\u901a\u5e38\u6bd4\u8f83\u660e\u663e<\/td>\n<td>\u901a\u5e38\u6bd4\u8f83\u660e\u663e<\/td>\n<td>\u901a\u5e38\u66f4\u6709\u4f18\u52bf<\/td>\n<\/tr>\n<tr>\n<td>\u901a\u77e5\u8bed\u4e49<\/td>\n<td>\u7c7b\u4f3c\u6c34\u5e73\u89e6\u53d1<\/td>\n<td>\u7c7b\u4f3c\u6c34\u5e73\u89e6\u53d1<\/td>\n<td>\u652f\u6301 LT \u4e0e ET<\/td>\n<\/tr>\n<tr>\n<td>\u5178\u578b\u89c4\u6a21\u9650\u5236<\/td>\n<td>\u5e38\u89c1 fd_set \u5b58\u5728\u56fa\u5b9a\u8868\u793a\u8303\u56f4<\/td>\n<td>\u6ca1\u6709\u8be5\u56fa\u5b9a\u8303\u56f4&#xff0c;\u4ecd\u53d7\u8d44\u6e90\u9650\u5236<\/td>\n<td>\u6ca1\u6709\u8be5\u56fa\u5b9a\u8303\u56f4&#xff0c;\u4ecd\u53d7\u8d44\u6e90\u9650\u5236<\/td>\n<\/tr>\n<tr>\n<td>\u5e73\u53f0<\/td>\n<td>\u5e7f\u6cdb\u53ef\u7528<\/td>\n<td>\u5e7f\u6cdb\u53ef\u7528<\/td>\n<td>Linux \u4e13\u6709\u63a5\u53e3<\/td>\n<\/tr>\n<tr>\n<td>\u7f16\u7a0b\u590d\u6742\u5ea6<\/td>\n<td>\u96c6\u5408\u7ef4\u62a4\u8f83\u7e41\u7410<\/td>\n<td>\u63a5\u53e3\u76f8\u5bf9\u76f4\u89c2<\/td>\n<td>\u8fde\u63a5\u72b6\u6001\u4e0e ET \u7b49\u9ad8\u7ea7\u7528\u6cd5\u9700\u8981\u66f4\u7ec6\u81f4\u8bbe\u8ba1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u4e9b\u673a\u5236\u7684\u9009\u62e9\u6700\u7ec8\u53d6\u51b3\u4e8e\u8fde\u63a5\u89c4\u6a21\u3001\u6d3b\u8dc3\u6bd4\u4f8b\u3001\u5e73\u53f0\u9700\u6c42\u4e0e\u6846\u67b6\u8bbe\u8ba1\u3002\u53ea\u6709\u5c11\u91cf fd \u65f6&#xff0c;\u7b80\u5355\u5f80\u5f80\u4e5f\u662f\u91cd\u8981\u6536\u76ca\u3002<\/p>\n<p>\u8fd8\u6709\u4e00\u4e2a\u8303\u56f4\u63d0\u9192&#xff1a;epoll \u9002\u5408\u5177\u6709\u76f8\u5e94\u4e8b\u4ef6\u901a\u77e5\u8bed\u4e49\u7684\u5bf9\u8c61&#xff0c;\u4f8b\u5982 socket\u3001\u7ba1\u9053\u53ca\u4e00\u4e9b\u7279\u6b8a fd\u3002**\u666e\u901a\u78c1\u76d8\u6587\u4ef6\u901a\u5e38\u4e0d\u80fd\u50cf\u7f51\u7edc socket \u90a3\u6837\u76f4\u63a5\u52a0\u5165 epoll&#xff0c;\u53ef\u80fd\u5f97\u5230 EPERM\u3002**\u4e0d\u8981\u628a\u201cLinux \u4e00\u5207\u7686\u6587\u4ef6\u201d\u7406\u89e3\u6210\u6240\u6709\u6587\u4ef6\u90fd\u652f\u6301\u76f8\u540c\u7684\u7b49\u5f85\u8bed\u4e49\u3002<\/p>\n<h3 id=\"\u5341\u4e09\u3001\u628a\u6700\u5bb9\u6613\u8bb0\u9519\u7684\u5730\u65b9\u96c6\u4e2d\u6821\u6b63\">\u5341\u4e09\u3001\u628a\u6700\u5bb9\u6613\u8bb0\u9519\u7684\u5730\u65b9\u96c6\u4e2d\u6821\u6b63<\/h3>\n<table>\n<tr>\u5bb9\u6613\u8bb0\u6210\u7684\u8bf4\u6cd5\u66f4\u51c6\u786e\u7684\u7406\u89e3<\/tr>\n<tbody>\n<tr>\n<td>epoll_create(100) \u6700\u591a\u5173\u6ce8 100 \u4e2a fd<\/td>\n<td>\u73b0\u4ee3 Linux \u5ffd\u7565\u65e7 size \u7684\u5bb9\u91cf\u542b\u4e49&#xff0c;\u4f46\u8981\u6c42\u5b83\u5927\u4e8e 0<\/td>\n<\/tr>\n<tr>\n<td>maxevents \u4e0d\u80fd\u8d85\u8fc7\u521b\u5efa\u65f6\u7684 size<\/td>\n<td>\u4e24\u8005\u6ca1\u6709\u8fd9\u79cd\u7ea6\u675f&#xff1b;maxevents \u53d7\u8f93\u51fa\u6570\u7ec4\u5bb9\u91cf\u7b49\u6761\u4ef6\u7ea6\u675f<\/td>\n<\/tr>\n<tr>\n<td>epoll \u6bcf\u4e2a\u64cd\u4f5c\u90fd\u662f O(1)<\/td>\n<td>\u961f\u5217\u7a7a\u5224\u65ad\u3001\u5173\u6ce8\u7ba1\u7406\u3001\u4e8b\u4ef6\u4ea4\u4ed8\u3001\u4e1a\u52a1\u5904\u7406\u8981\u5206\u522b\u5206\u6790<\/td>\n<\/tr>\n<tr>\n<td>\u7ea2\u9ed1\u6811\u4f7f epoll \u65e0\u987b\u505a\u4efb\u4f55\u904d\u5386<\/td>\n<td>\u6811\u7528\u4e8e\u7ba1\u7406\u5173\u6ce8\u9879&#xff0c;\u4e8b\u4ef6\u4ea4\u4ed8\u548c\u5e94\u7528\u5904\u7406\u4ecd\u6709\u904d\u5386<\/td>\n<\/tr>\n<tr>\n<td>epoll \u4f7f\u7528 mmap&#xff0c;\u56e0\u6b64\u5b8c\u5168\u6ca1\u6709\u590d\u5236<\/td>\n<td>\u4e8b\u4ef6\u7ed3\u679c\u4ecd\u5199\u5165\u7528\u6237\u6570\u7ec4&#xff0c;\u666e\u901a\u6536\u53d1\u8fd8\u6709\u81ea\u5df1\u7684\u6570\u636e\u8def\u5f84<\/td>\n<\/tr>\n<tr>\n<td>ET \u4e00\u4e2a\u8fde\u63a5\u53ea\u901a\u77e5\u4e00\u6b21<\/td>\n<td>\u540e\u7eed\u6d3b\u52a8\u4ecd\u53ef\u80fd\u901a\u77e5&#xff1b;\u91cd\u70b9\u662f\u4e0d\u80fd\u4f9d\u8d56\u65e7\u5c31\u7eea\u6761\u4ef6\u6301\u7eed\u63d0\u9192<\/td>\n<\/tr>\n<tr>\n<td>ET \u5728\u63a5\u53e3\u4e0a\u53ea\u5141\u8bb8\u975e\u963b\u585e fd<\/td>\n<td>\u63a5\u53e3\u4e0d\u4f1a\u636e\u6b64\u62d2\u7edd\u6ce8\u518c&#xff0c;\u53ef\u9760\u4e8b\u4ef6\u5faa\u73af\u5728\u5de5\u7a0b\u4e0a\u5e94\u91c7\u7528\u975e\u963b\u585e I\/O<\/td>\n<\/tr>\n<tr>\n<td>EAGAIN \u540e\u7acb\u523b\u539f\u5730\u91cd\u8bd5<\/td>\n<td>\u5148\u4fdd\u7559\u72b6\u6001\u5e76\u8ba9\u51fa\u6267\u884c\u6743&#xff0c;\u7b49\u5f85\u5408\u9002\u65f6\u673a\u7ee7\u7eed<\/td>\n<\/tr>\n<tr>\n<td>\u8bfb\u5230 EAGAIN \u5c31\u6536\u5b8c\u4e86\u4e00\u6761\u8bf7\u6c42<\/td>\n<td>\u53ea\u662f\u5f53\u524d\u65e0\u66f4\u591a\u5b57\u8282&#xff0c;\u6d88\u606f\u5b8c\u6210\u4e0e\u5426\u7531\u5e94\u7528\u534f\u8bae\u5224\u65ad<\/td>\n<\/tr>\n<tr>\n<td>EPOLL_CTL_DEL \u4f1a\u5173\u95ed socket<\/td>\n<td>\u5b83\u53ea\u6ce8\u9500\u5173\u6ce8&#xff0c;\u5e94\u7528\u4ecd\u9700 close<\/td>\n<\/tr>\n<tr>\n<td>ONESHOT \u5904\u7406\u540e\u91cd\u65b0 ADD<\/td>\n<td>\u901a\u5e38\u4f7f\u7528 EPOLL_CTL_MOD \u91cd\u65b0\u6fc0\u6d3b\u5df2\u6709\u9879<\/td>\n<\/tr>\n<tr>\n<td>\u53ef\u8bfb\u4e00\u5b9a\u6709\u6b63\u957f\u5ea6\u6570\u636e&#xff0c;\u53ef\u5199\u4e00\u5b9a\u80fd\u5168\u90e8\u53d1\u5b8c<\/td>\n<td>\u5e94\u4ee5\u5b9e\u9645 I\/O \u8fd4\u56de\u503c\u63a8\u8fdb\u72b6\u6001&#xff0c;\u5e76\u5904\u7406 EOF\u3001\u90e8\u5206\u64cd\u4f5c\u548c\u9519\u8bef<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5c24\u5176\u662f\u201cEAGAIN \u539f\u5730\u5faa\u73af\u201d\u548c\u201c\u534a\u5173\u95ed\u540e\u76f4\u63a5\u4e22\u5f03\u5f85\u53d1\u6570\u636e\u201d&#xff0c;\u90fd\u53ef\u80fd\u8ba9\u4e00\u4e2a\u53ea\u5728\u672c\u673a\u5c0f\u6570\u636e\u4e0b\u6b63\u5e38\u7684\u6f14\u793a&#xff0c;\u5728\u771f\u5b9e\u7f51\u7edc\u4e2d\u66b4\u9732\u95ee\u9898\u3002<\/p>\n<h3 id=\"\u5341\u56db\u3001\u7528\u516b\u4e2a\u95ee\u9898\u68c0\u67e5\u81ea\u5df1\u662f\u5426\u771f\u6b63\u7406\u89e3\">\u5341\u56db\u3001\u7528\u516b\u4e2a\u95ee\u9898\u68c0\u67e5\u81ea\u5df1\u662f\u5426\u771f\u6b63\u7406\u89e3<\/h3>\n<h4 id=\"141-epoll-\u4e3a\u4ec0\u4e48\u8981\u6709\u4e09\u4e2a\u63a5\u53e3\uff1f\">14.1 epoll \u4e3a\u4ec0\u4e48\u8981\u6709\u4e09\u4e2a\u63a5\u53e3&#xff1f;<\/h4>\n<p>\u4e3a\u4e86\u8ba9\u5173\u6ce8\u96c6\u5408\u957f\u671f\u4fdd\u5b58\u5728\u5185\u6838\u4e2d&#xff0c;\u628a\u5173\u6ce8\u9879\u7684\u7ef4\u62a4\u4e0e\u6bcf\u4e00\u8f6e\u7b49\u5f85\u3001\u4ea4\u4ed8\u5206\u5f00\u3002<\/p>\n<h4 id=\"142-epoll-\u4e3a\u4ec0\u4e48\u4f7f\u7528\u7ea2\u9ed1\u6811\uff0c\u8fd8\u8981\u6709\u5c31\u7eea\u961f\u5217\uff1f\">14.2 epoll \u4e3a\u4ec0\u4e48\u4f7f\u7528\u7ea2\u9ed1\u6811&#xff0c;\u8fd8\u8981\u6709\u5c31\u7eea\u961f\u5217&#xff1f;<\/h4>\n<p>\u4e24\u8005\u670d\u52a1\u4e0d\u540c\u95ee\u9898&#xff1a;\u6811\u7ba1\u7406\u5168\u90e8\u5173\u6ce8\u9879&#xff0c;\u961f\u5217\u7ec4\u7ec7\u5f53\u524d\u9700\u8981\u68c0\u67e5\u548c\u4ea4\u4ed8\u7684\u5c31\u7eea\u9879\u3002\u540c\u4e00\u4e2a epitem \u53ef\u4ee5\u901a\u8fc7\u4e0d\u540c\u6210\u5458\u540c\u65f6\u53c2\u4e0e\u8fd9\u4e24\u79cd\u7ed3\u6784\u3002<\/p>\n<h4 id=\"143-\u4e3a\u4ec0\u4e48-epoll-\u66f4\u9002\u5408\u5927\u91cf\u7a7a\u95f2\u8fde\u63a5\uff1f\">14.3 \u4e3a\u4ec0\u4e48 epoll \u66f4\u9002\u5408\u5927\u91cf\u7a7a\u95f2\u8fde\u63a5&#xff1f;<\/h4>\n<p>\u5b83\u51cf\u5c11\u6bcf\u4e00\u8f6e\u56f4\u7ed5\u5168\u90e8\u5173\u6ce8\u9879\u8fdb\u884c\u7684\u91cd\u590d\u4f20\u9012\u4e0e\u68c0\u67e5&#xff0c;\u628a\u66f4\u591a\u5de5\u4f5c\u96c6\u4e2d\u5230\u6d3b\u52a8\u9879\u4e0a\u3002\u8fde\u63a5\u6301\u7eed\u5fd9\u788c\u65f6&#xff0c;\u6570\u636e\u5904\u7406\u672c\u8eab\u4e5f\u4f1a\u5360\u636e\u5f88\u5927\u6210\u672c\u3002<\/p>\n<h4 id=\"144-lt-\u548c-et-\u6700\u5173\u952e\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f\">14.4 LT \u548c ET \u6700\u5173\u952e\u7684\u533a\u522b\u662f\u4ec0\u4e48&#xff1f;<\/h4>\n<p>LT \u53ef\u4ee5\u5728\u5c31\u7eea\u6761\u4ef6\u6301\u7eed\u6210\u7acb\u65f6\u7ee7\u7eed\u62a5\u544a\u3002ET \u4e0b&#xff0c;\u5e94\u7528\u4e0d\u80fd\u4f9d\u8d56\u8fd9\u79cd\u6301\u7eed\u6761\u4ef6\u53cd\u590d\u63d0\u9192\u81ea\u5df1&#xff0c;\u56e0\u6b64\u8981\u4e3b\u52a8\u63a8\u8fdb I\/O&#xff0c;\u5e76\u6b63\u786e\u4fdd\u5b58\u672a\u5b8c\u6210\u72b6\u6001\u3002<\/p>\n<h4 id=\"145-\u4e3a\u4ec0\u4e48-et-\u901a\u5e38\u914d\u5408\u975e\u963b\u585e\uff1f\">14.5 \u4e3a\u4ec0\u4e48 ET \u901a\u5e38\u914d\u5408\u975e\u963b\u585e&#xff1f;<\/h4>\n<p>\u5faa\u73af\u8bfb\u53d6\u6216\u53d1\u9001\u9700\u8981\u5728\u6682\u65f6\u65e0\u6cd5\u7ee7\u7eed\u65f6\u53ca\u65f6\u8fd4\u56de\u3002\u5426\u5219\u4e00\u4e2a\u8fde\u63a5\u53ef\u80fd\u963b\u585e\u6574\u4e2a\u4e8b\u4ef6\u5faa\u73af\u3002<\/p>\n<h4 id=\"146-send-\u8fd4\u56de-eagain-\u600e\u4e48\u529e\uff1f\">14.6 send \u8fd4\u56de EAGAIN \u600e\u4e48\u529e&#xff1f;<\/h4>\n<p>\u4fdd\u7559\u672a\u53d1\u5b8c\u7684\u6570\u636e\u53ca\u4f4d\u7f6e&#xff0c;\u5173\u6ce8\u9002\u5f53\u7684\u5199\u4e8b\u4ef6&#xff0c;\u56de\u5230\u4e8b\u4ef6\u5faa\u73af&#xff1b;\u540e\u7eed\u53ef\u5199\u65f6\u7ee7\u7eed\u53d1\u9001&#xff0c;\u8f93\u51fa\u6e05\u7a7a\u540e\u64a4\u6389\u4e0d\u5fc5\u8981\u7684\u5199\u5173\u6ce8\u3002<\/p>\n<h4 id=\"147-\u6536\u5230-epollrdhup-\u80fd\u7acb\u523b\u5173\u95ed\u5417\uff1f\">14.7 \u6536\u5230 EPOLLRDHUP \u80fd\u7acb\u523b\u5173\u95ed\u5417&#xff1f;<\/h4>\n<p>\u9700\u8981\u7ed3\u5408\u534f\u8bae\u548c\u72b6\u6001\u5224\u65ad\u3002\u53ef\u80fd\u8fd8\u6709\u8f93\u5165\u672a\u8bfb&#xff0c;\u4e5f\u53ef\u80fd\u9700\u8981\u7ee7\u7eed\u53d1\u9001\u54cd\u5e94\u3002\u57fa\u7840\u505a\u6cd5\u662f\u8bfb\u53d6\u5230 EOF&#xff0c;\u4fdd\u7559\u5e76\u5904\u7406\u5f85\u53d1\u6570\u636e&#xff0c;\u518d\u6309\u751f\u547d\u5468\u671f\u7b56\u7565\u5173\u95ed\u3002<\/p>\n<h4 id=\"148-epoll-\u80fd\u89e3\u51b3-tcp-\u7c98\u5305\u5417\uff1f\">14.8 epoll \u80fd\u89e3\u51b3 TCP \u7c98\u5305\u5417&#xff1f;<\/h4>\n<p>\u6d88\u606f\u8fb9\u754c\u7531\u5e94\u7528\u534f\u8bae\u89c4\u5b9a\u3002epoll \u89e3\u51b3\u7b49\u5f85\u4e0e\u5c31\u7eea\u901a\u77e5&#xff0c;\u8f93\u5165\u7f13\u51b2\u533a\u548c\u89e3\u6790\u5668\u8d1f\u8d23\u4ece\u5b57\u8282\u6d41\u4e2d\u6062\u590d\u6d88\u606f\u3002<\/p>\n<p>\u73b0\u5728&#xff0c;\u518d\u56de\u5230\u6700\u521d\u7684\u573a\u666f&#xff1a;\u4e00\u4e07\u4e2a\u8fde\u63a5&#xff0c;\u5341\u4e2a\u6d3b\u8dc3\u3002<\/p>\n<p>\u670d\u52a1\u5668\u5148\u7ef4\u62a4\u597d\u4e00\u4e07\u4e2a\u8fde\u63a5\u7684\u5173\u6ce8\u5173\u7cfb&#xff1b;\u7b49\u5f85\u671f\u95f4\u7531\u5185\u6838\u7ec4\u7ec7\u5c31\u7eea\u901a\u77e5&#xff1b;\u8fd4\u56de\u540e&#xff0c;\u5e94\u7528\u9488\u5bf9\u8fd9\u4e00\u6279\u8fde\u63a5\u63a8\u8fdb\u975e\u963b\u585e I\/O&#xff1b;\u534f\u8bae\u89e3\u6790\u5668\u5224\u65ad\u8bf7\u6c42\u662f\u5426\u5b8c\u6574&#xff1b;\u8f93\u51fa\u7f13\u51b2\u533a\u4fdd\u5b58\u5c1a\u672a\u53d1\u5b8c\u7684\u54cd\u5e94&#xff1b;\u8fde\u63a5\u72b6\u6001\u673a\u51b3\u5b9a\u7ee7\u7eed\u7b49\u5f85\u3001\u5904\u7406\u534a\u5173\u95ed\u8fd8\u662f\u6e05\u7406\u9000\u51fa\u3002<\/p>\n<p>\u8fd9\u6761\u8def\u5f84\u4e2d\u7684\u6bcf\u4e00\u6b65\u90fd\u6709\u660e\u786e\u8d23\u4efb\u3002\u628a\u5b83\u4eec\u63a5\u8d77\u6765&#xff0c;\u624d\u80fd\u4ece\u201c\u4f1a\u8c03\u7528 epoll\u201d\u8d70\u5230\u201c\u80fd\u5199\u51fa\u6b63\u786e\u7684\u4e8b\u4ef6\u9a71\u52a8\u670d\u52a1\u5668\u201d\u3002<\/p>\n<\/p>\n<p>      <span id=\"quality-guide-article-end\" aria-hidden=\"true\" style=\"display:block;height:1px;margin-bottom:-1px\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/#content_views .toc,\/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/#content_views.markdown_views &gt; p:empty:has(+ .toc),#content_views.markdown_views &gt; .toc + p:empty,\/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/#content<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,205,43],"topic":[],"class_list":["post-124106","post","type-post","status-publish","format-standard","hentry","category-server","tag-linux","tag-tcp-ip","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Linux epoll \u4e00\u7bc7\u8bb2\u900f\uff1a\u4ece select\/poll \u5230\u7ea2\u9ed1\u6811\u3001LT\/ET\uff0c\u518d\u5230\u5b8c\u6574 TCP \u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/124106.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linux epoll \u4e00\u7bc7\u8bb2\u900f\uff1a\u4ece select\/poll \u5230\u7ea2\u9ed1\u6811\u3001LT\/ET\uff0c\u518d\u5230\u5b8c\u6574 TCP \u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/#content_views .toc,\/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/#content_views.markdown_views &gt; p:empty:has(+ .toc),#content_views.markdown_views &gt; .toc + p:empty,\/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/#content\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/124106.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-11-18T07:01:57+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"19 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/124106.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/124106.html\",\"name\":\"Linux epoll \u4e00\u7bc7\u8bb2\u900f\uff1a\u4ece select\/poll \u5230\u7ea2\u9ed1\u6811\u3001LT\/ET\uff0c\u518d\u5230\u5b8c\u6574 TCP \u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-11-18T07:01:57+00:00\",\"dateModified\":\"2026-11-18T07:01:57+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/124106.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/124106.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/124106.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux epoll \u4e00\u7bc7\u8bb2\u900f\uff1a\u4ece select\/poll \u5230\u7ea2\u9ed1\u6811\u3001LT\/ET\uff0c\u518d\u5230\u5b8c\u6574 TCP \u670d\u52a1\u5668\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Linux epoll \u4e00\u7bc7\u8bb2\u900f\uff1a\u4ece select\/poll \u5230\u7ea2\u9ed1\u6811\u3001LT\/ET\uff0c\u518d\u5230\u5b8c\u6574 TCP \u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/124106.html","og_locale":"zh_CN","og_type":"article","og_title":"Linux epoll \u4e00\u7bc7\u8bb2\u900f\uff1a\u4ece select\/poll \u5230\u7ea2\u9ed1\u6811\u3001LT\/ET\uff0c\u518d\u5230\u5b8c\u6574 TCP \u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/#content_views .toc,\/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/#content_views.markdown_views &gt; p:empty:has(+ .toc),#content_views.markdown_views &gt; .toc + p:empty,\/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/#content","og_url":"https:\/\/www.wsisp.com\/helps\/124106.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-11-18T07:01:57+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"19 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/124106.html","url":"https:\/\/www.wsisp.com\/helps\/124106.html","name":"Linux epoll \u4e00\u7bc7\u8bb2\u900f\uff1a\u4ece select\/poll \u5230\u7ea2\u9ed1\u6811\u3001LT\/ET\uff0c\u518d\u5230\u5b8c\u6574 TCP \u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-11-18T07:01:57+00:00","dateModified":"2026-11-18T07:01:57+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/124106.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/124106.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/124106.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Linux epoll \u4e00\u7bc7\u8bb2\u900f\uff1a\u4ece select\/poll \u5230\u7ea2\u9ed1\u6811\u3001LT\/ET\uff0c\u518d\u5230\u5b8c\u6574 TCP \u670d\u52a1\u5668"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/124106","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=124106"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/124106\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=124106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=124106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=124106"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=124106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}