{"id":38652,"date":"2025-05-21T07:01:00","date_gmt":"2025-05-20T23:01:00","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/38652.html"},"modified":"2025-05-21T07:01:00","modified_gmt":"2025-05-20T23:01:00","slug":"%e6%9c%8d%e5%8a%a1%e5%99%a8%e8%bf%9e%e6%8e%a5%e5%a4%9a%e5%ae%a2%e6%88%b7%e7%ab%af","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/38652.html","title":{"rendered":"\u670d\u52a1\u5668\u8fde\u63a5\u591a\u5ba2\u6237\u7aef"},"content":{"rendered":"<h4>\u4e00\u3001epoll \u6838\u5fc3\u51fd\u6570\u8be6\u89e3<\/h4>\n<h5>1.\u00a0epoll_create\/epoll_create1\u00a0&#8211; \u521b\u5efa epoll \u5b9e\u4f8b<\/h5>\n<p>c<\/p>\n<p>#include &lt;sys\/epoll.h&gt;<br \/>\nint epoll_create(int size); \/\/ Linux 2.6.8\u524d\u9700\u6307\u5b9asize&#xff08;&gt;&#061;1&#xff09;&#xff0c;\u540e\u7eed\u7248\u672c\u53ef\u5ffd\u7565<br \/>\nint epoll_create1(int flags); \/\/ \u63a8\u8350\u4f7f\u7528&#xff0c;\u652f\u6301flags\u53c2\u6570<\/p>\n<ul>\n<li>\u529f\u80fd&#xff1a;\u521b\u5efa\u4e00\u4e2a epoll \u5b9e\u4f8b&#xff0c;\u8fd4\u56de\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;epfd&#xff09;&#xff0c;\u7528\u4e8e\u7ba1\u7406\u76d1\u89c6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/li>\n<li>\u53c2\u6570&#xff1a;\n<ul>\n<li>size&#xff1a;\u65e7\u7248\u672c\u9700\u6307\u5b9a\u9884\u5206\u914d\u5927\u5c0f&#xff08;\u65e0\u5b9e\u9645\u4f5c\u7528&#xff0c;\u53ef\u8bbe\u4e3a 1&#xff09;\u3002<\/li>\n<li>flags&#xff1a;\n<ul>\n<li>0&#xff1a;\u4e0eepoll_create\u529f\u80fd\u76f8\u540c\u3002<\/li>\n<li>EPOLL_CLOEXEC&#xff1a;\u4f7f epfd \u5728\u8fdb\u7a0b fork \u65f6\u81ea\u52a8\u5173\u95ed\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\u8fd4\u56de\u503c&#xff1a;\u6210\u529f\u8fd4\u56de epfd&#xff08;\u975e\u8d1f\u6574\u6570&#xff09;&#xff0c;\u5931\u8d25\u8fd4\u56de-1\u5e76\u7f6eerrno\u3002<\/li>\n<li>\u793a\u4f8b&#xff1a;\n<p>c<\/p>\n<p> int epfd &#061; epoll_create1(EPOLL_CLOEXEC); \/\/ \u521b\u5efa\u5e26\u81ea\u52a8\u5173\u95ed\u529f\u80fd\u7684epoll\u5b9e\u4f8b\n <\/li>\n<\/ul>\n<h5>2.\u00a0epoll_ctl\u00a0&#8211; \u7ba1\u7406\u76d1\u89c6\u5217\u8868<\/h5>\n<p>c<\/p>\n<p>int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);<\/p>\n<ul>\n<li>\u529f\u80fd&#xff1a;\u5411 epoll \u5b9e\u4f8b\u4e2d\u6dfb\u52a0\u3001\u4fee\u6539\u6216\u5220\u9664\u76d1\u89c6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ca\u5176\u4e8b\u4ef6\u3002<\/li>\n<li>\u53c2\u6570&#xff1a;\n<ul>\n<li>epfd&#xff1a;epoll \u5b9e\u4f8b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/li>\n<li>op&#xff1a;\u64cd\u4f5c\u7c7b\u578b&#xff1a;\n<ul>\n<li>EPOLL_CTL_ADD&#xff1a;\u6dfb\u52a0\u6587\u4ef6\u63cf\u8ff0\u7b26fd\u5230\u76d1\u89c6\u5217\u8868\u3002<\/li>\n<li>EPOLL_CTL_MOD&#xff1a;\u4fee\u6539fd\u5173\u8054\u7684\u4e8b\u4ef6\u3002<\/li>\n<li>EPOLL_CTL_DEL&#xff1a;\u4ece\u76d1\u89c6\u5217\u8868\u5220\u9664fd&#xff08;event\u53c2\u6570\u53ef\u7f6eNULL&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<li>fd&#xff1a;\u9700\u64cd\u4f5c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;\u5982\u5957\u63a5\u5b57&#xff09;\u3002<\/li>\n<li>event&#xff1a;\u6307\u5411epoll_event\u7ed3\u6784\u4f53\u7684\u6307\u9488&#xff0c;\u5305\u542b\u76d1\u89c6\u4e8b\u4ef6\u548c\u7528\u6237\u6570\u636e&#xff1a;\n<p>c<\/p>\n<p> struct epoll_event {<br \/>\n    uint32_t events;       \/\/ \u76d1\u89c6\u4e8b\u4ef6&#xff08;\u5982EPOLLIN\u3001EPOLLOUT\u3001EPOLLET&#xff09;<br \/>\n    epoll_data_t data;     \/\/ \u7528\u6237\u6570\u636e&#xff08;\u5982fd\u3001\u6307\u9488\u7b49&#xff09;<br \/>\n};<br \/>\ntypedef union epoll_data {<br \/>\n    void        *ptr;      \/\/ \u81ea\u5b9a\u4e49\u6307\u9488<br \/>\n    int          fd;       \/\/ \u76d1\u89c6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    uint32_t     u32;      \/\/ 32\u4f4d\u6574\u6570<br \/>\n    uint64_t     u64;      \/\/ 64\u4f4d\u6574\u6570<br \/>\n} epoll_data_t;\n <\/li>\n<\/ul>\n<\/li>\n<li>\u8fd4\u56de\u503c&#xff1a;\u6210\u529f\u8fd4\u56de0&#xff0c;\u5931\u8d25\u8fd4\u56de-1\u5e76\u7f6eerrno\u3002<\/li>\n<li>\u793a\u4f8b&#xff1a;\n<p>c<\/p>\n<p> struct epoll_event event;<br \/>\nevent.events &#061; EPOLLIN | EPOLLET; \/\/ \u8fb9\u7f18\u89e6\u53d1\u6a21\u5f0f &#043; \u8bfb\u4e8b\u4ef6<br \/>\nevent.data.fd &#061; sockfd;<br \/>\nepoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &amp;event); \/\/ \u6dfb\u52a0\u5957\u63a5\u5b57\u5230\u76d1\u89c6\u5217\u8868\n <\/li>\n<\/ul>\n<h5>3.\u00a0epoll_wait\u00a0&#8211; \u7b49\u5f85\u4e8b\u4ef6\u89e6\u53d1<\/h5>\n<p>c<\/p>\n<p>int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout);<\/p>\n<ul>\n<li>\u529f\u80fd&#xff1a;\u963b\u585e\u7b49\u5f85 epoll \u5b9e\u4f8b\u4e2d\u76d1\u89c6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u89e6\u53d1\u4e8b\u4ef6&#xff0c;\u8fd4\u56de\u6fc0\u6d3b\u7684\u4e8b\u4ef6\u5217\u8868\u3002<\/li>\n<li>\u53c2\u6570&#xff1a;\n<ul>\n<li>epfd&#xff1a;epoll \u5b9e\u4f8b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/li>\n<li>events&#xff1a;\u7528\u4e8e\u5b58\u50a8\u6fc0\u6d3b\u4e8b\u4ef6\u7684\u7ed3\u6784\u4f53\u6570\u7ec4\u3002<\/li>\n<li>maxevents&#xff1a;events\u6570\u7ec4\u7684\u6700\u5927\u957f\u5ea6&#xff08;\u9700\u5c0f\u4e8e\u7b49\u4e8e\u521b\u5efa\u65f6\u7684\u9884\u5206\u914d\u503c&#xff09;\u3002<\/li>\n<li>timeout&#xff1a;\u8d85\u65f6\u65f6\u95f4&#xff08;ms&#xff09;&#xff1a;\n<ul>\n<li>-1&#xff1a;\u6c38\u4e45\u963b\u585e&#xff0c;\u76f4\u5230\u4e8b\u4ef6\u89e6\u53d1\u3002<\/li>\n<li>0&#xff1a;\u975e\u963b\u585e&#xff0c;\u7acb\u5373\u8fd4\u56de\u3002<\/li>\n<li>&gt;0&#xff1a;\u8d85\u65f6\u65f6\u95f4&#xff0c;\u5230\u671f\u540e\u8fd4\u56de\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\u8fd4\u56de\u503c&#xff1a;\n<ul>\n<li>&gt;0&#xff1a;\u6fc0\u6d3b\u7684\u4e8b\u4ef6\u6570\u91cf&#xff08;events\u4e2d\u6709\u6548\u5143\u7d20\u4e2a\u6570&#xff09;\u3002<\/li>\n<li>0&#xff1a;\u8d85\u65f6\u4e14\u65e0\u4e8b\u4ef6\u89e6\u53d1\u3002<\/li>\n<li>-1&#xff1a;\u5931\u8d25\u5e76\u7f6eerrno\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u793a\u4f8b&#xff1a;\n<p>c<\/p>\n<p> struct epoll_event events[1024];<br \/>\nint n &#061; epoll_wait(epfd, events, sizeof(events)\/sizeof(events[0]), -1); \/\/ \u6c38\u4e45\u963b\u585e\u7b49\u5f85\u4e8b\u4ef6<br \/>\nfor (int i &#061; 0; i &lt; n; i&#043;&#043;) {<br \/>\n    int fd &#061; events[i].data.fd;<br \/>\n    \/\/ \u5904\u7406\u4e8b\u4ef6&#8230;<br \/>\n}\n <\/li>\n<\/ul>\n<h4>\u4e8c\u3001epoll \u5de5\u4f5c\u539f\u7406<\/h4>\n<li>\u6570\u636e\u7ed3\u6784&#xff1a;\n<ul>\n<li>\u76d1\u89c6\u5217\u8868&#xff1a;\u4f7f\u7528\u7ea2\u9ed1\u6811\u5b58\u50a8\u6240\u6709\u76d1\u89c6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u652f\u6301\u5feb\u901f\u589e\u5220\u6539\u67e5&#xff08;\u65f6\u95f4\u590d\u6742\u5ea6 O (logN)&#xff09;\u3002<\/li>\n<li>\u6fc0\u6d3b\u5217\u8868&#xff1a;\u4f7f\u7528\u94fe\u8868\u6216\u52a8\u6001\u6570\u7ec4\u5b58\u50a8\u5f53\u524d\u89e6\u53d1\u4e8b\u4ef6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u907f\u514d\u904d\u5386\u6574\u4e2a\u7ea2\u9ed1\u6811\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u4e8b\u4ef6\u89e6\u53d1\u6d41\u7a0b&#xff1a;\n<li>\u901a\u8fc7epoll_ctl\u5c06\u6587\u4ef6\u63cf\u8ff0\u7b26\u6dfb\u52a0\u5230\u7ea2\u9ed1\u6811&#xff08;\u76d1\u89c6\u5217\u8868&#xff09;\u3002<\/li>\n<li>\u5185\u6838\u76d1\u542c\u6587\u4ef6\u63cf\u8ff0\u7b26\u72b6\u6001&#xff0c;\u5f53\u4e8b\u4ef6&#xff08;\u5982\u53ef\u8bfb\u3001\u53ef\u5199&#xff09;\u53d1\u751f\u65f6&#xff0c;\u5c06\u5176\u52a0\u5165\u6fc0\u6d3b\u5217\u8868\u3002<\/li>\n<li>epoll_wait\u8fd4\u56de\u6fc0\u6d3b\u5217\u8868\u4e2d\u7684\u4e8b\u4ef6&#xff0c;\u7528\u6237\u7a7a\u95f4\u5904\u7406\u540e&#xff0c;\u6fc0\u6d3b\u5217\u8868\u6e05\u7a7a\u3002<\/li>\n<\/li>\n<h4>\u4e09\u3001LT&#xff08;\u6c34\u5e73\u89e6\u53d1&#xff09;\u4e0e ET&#xff08;\u8fb9\u7f18\u89e6\u53d1&#xff09;\u6a21\u5f0f\u5bf9\u6bd4<\/h4>\n<table>\n<tr>\u7279\u6027LT&#xff08;\u6c34\u5e73\u89e6\u53d1&#xff09;ET&#xff08;\u8fb9\u7f18\u89e6\u53d1&#xff09;<\/tr>\n<tbody>\n<tr>\n<td>\u89e6\u53d1\u6761\u4ef6<\/td>\n<td>\u53ea\u8981\u5185\u6838\u7f13\u51b2\u533a\u6709\u6570\u636e\u672a\u8bfb \/ \u672a\u5199&#xff0c;\u5c31\u6301\u7eed\u89e6\u53d1\u4e8b\u4ef6\u3002<\/td>\n<td>\u4ec5\u5728\u72b6\u6001\u53d8\u5316\u65f6\u89e6\u53d1\u4e00\u6b21\u4e8b\u4ef6&#xff08;\u5982\u6570\u636e\u4ece\u65e0\u5230\u6709&#xff09;\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u8bfb\u64cd\u4f5c<\/td>\n<td>\u5141\u8bb8\u5206\u591a\u6b21\u8bfb\u53d6&#xff0c;\u5269\u4f59\u6570\u636e\u4f1a\u518d\u6b21\u89e6\u53d1\u4e8b\u4ef6\u3002<\/td>\n<td>\u5fc5\u987b\u4e00\u6b21\u6027\u8bfb\u5b8c\u6240\u6709\u6570\u636e&#xff08;\u9700\u914d\u5408\u975e\u963b\u585e I\/O&#xff09;&#xff0c;\u5426\u5219\u5269\u4f59\u6570\u636e\u4e0d\u518d\u89e6\u53d1\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u5199\u64cd\u4f5c<\/td>\n<td>\u53ea\u8981\u5185\u6838\u7f13\u51b2\u533a\u6709\u7a7a\u95f4&#xff0c;\u5c31\u6301\u7eed\u89e6\u53d1\u53ef\u5199\u4e8b\u4ef6\u3002<\/td>\n<td>\u4ec5\u5728\u7f13\u51b2\u533a\u7531\u6ee1\u53d8\u7a7a\u65f6\u89e6\u53d1\u4e00\u6b21\u53ef\u5199\u4e8b\u4ef6\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u7f16\u7a0b\u590d\u6742\u5ea6<\/td>\n<td>\u4f4e&#xff0c;\u7c7b\u4f3c select\/poll&#xff0c;\u65e0\u9700\u5904\u7406\u975e\u963b\u585e\u3002<\/td>\n<td>\u9ad8&#xff0c;\u5fc5\u987b\u4f7f\u7528\u975e\u963b\u585e I\/O&#xff0c;\u907f\u514d\u963b\u585e\u5bfc\u81f4\u4e8b\u4ef6\u4e22\u5931\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u9002\u7528\u573a\u666f<\/td>\n<td>\u7b80\u5355\u573a\u666f\u3001\u4f4e\u901f\u8bbe\u5907\u6216\u4e0d\u786e\u5b9a\u6570\u636e\u91cf\u7684\u573a\u666f\u3002<\/td>\n<td>\u9ad8\u5e76\u53d1\u3001\u9ad8\u901f\u6570\u636e\u4f20\u8f93\u573a\u666f&#xff08;\u5982 Web \u670d\u52a1\u5668\u3001\u5b9e\u65f6\u901a\u4fe1&#xff09;\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>\u793a\u4f8b\u914d\u7f6e&#xff1a;\n<p>c<\/p>\n<p> \/\/ LT\u6a21\u5f0f&#xff08;\u9ed8\u8ba4&#xff0c;\u65e0\u9700\u6307\u5b9aEPOLLET&#xff09;<br \/>\nevent.events &#061; EPOLLIN; <\/p>\n<p>\/\/ ET\u6a21\u5f0f&#xff08;\u9700\u663e\u5f0f\u6307\u5b9aEPOLLET&#xff09;<br \/>\nevent.events &#061; EPOLLIN | EPOLLET;\n <\/li>\n<\/ul>\n<h4>\u56db\u3001select\/poll\/epoll \u5bf9\u6bd4&#xff08;\u9762\u8bd5\u91cd\u70b9&#xff09;<\/h4>\n<table>\n<tr>\u7279\u6027selectpollepoll<\/tr>\n<tbody>\n<tr>\n<td>\u6570\u636e\u7ed3\u6784<\/td>\n<td>\u4f4d\u56fe&#xff08;\u6570\u7ec4&#xff09;&#xff0c;\u6700\u5927\u8fde\u63a5\u6570\u53d7\u9650\u4e8eFD_SETSIZE&#xff08;\u9ed8\u8ba4 1024&#xff09;\u3002<\/td>\n<td>\u7ed3\u6784\u4f53\u6570\u7ec4&#xff0c;\u7406\u8bba\u65e0\u8fde\u63a5\u6570\u9650\u5236&#xff08;\u4ec5\u53d7\u5185\u5b58\u9650\u5236&#xff09;\u3002<\/td>\n<td>\u7ea2\u9ed1\u6811 &#043; \u6fc0\u6d3b\u5217\u8868&#xff0c;\u652f\u6301\u4e0a\u4e07\u7ea7\u8fde\u63a5&#xff08;\u5982 10,000&#043;&#xff09;\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u5185\u6838\u904d\u5386\u65b9\u5f0f<\/td>\n<td>\u6bcf\u6b21\u8c03\u7528\u904d\u5386\u6240\u6709\u76d1\u89c6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;O (n)&#xff09;\u3002<\/td>\n<td>\u6bcf\u6b21\u8c03\u7528\u904d\u5386\u6240\u6709\u76d1\u89c6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;O (n)&#xff09;\u3002<\/td>\n<td>\u4ec5\u904d\u5386\u6fc0\u6d3b\u5217\u8868\u4e2d\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;O (1)~O (k)&#xff0c;k \u4e3a\u6fc0\u6d3b\u6570&#xff09;\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u7528\u6237\u6001 \/ \u5185\u6838\u6001\u6570\u636e\u62f7\u8d1d<\/td>\n<td>\u6bcf\u6b21select\u9700\u62f7\u8d1d\u5168\u91cf\u6587\u4ef6\u63cf\u8ff0\u7b26\u5230\u5185\u6838\u3002<\/td>\n<td>\u6bcf\u6b21poll\u9700\u62f7\u8d1d\u5168\u91cf\u6587\u4ef6\u63cf\u8ff0\u7b26\u5230\u5185\u6838\u3002<\/td>\n<td>\u4ec5\u5728epoll_ctl\u65f6\u66f4\u65b0\u7ea2\u9ed1\u6811&#xff0c;epoll_wait\u65e0\u9700\u62f7\u8d1d\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u89e6\u53d1\u6a21\u5f0f<\/td>\n<td>\u4ec5\u652f\u6301 LT \u6a21\u5f0f\u3002<\/td>\n<td>\u4ec5\u652f\u6301 LT \u6a21\u5f0f\u3002<\/td>\n<td>\u652f\u6301 LT \u548c ET \u6a21\u5f0f\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u8de8\u5e73\u53f0\u6027<\/td>\n<td>\u652f\u6301&#xff08;Linux\/Windows\/macOS&#xff09;\u3002<\/td>\n<td>\u652f\u6301&#xff08;Linux\/UNIX&#xff09;\u3002<\/td>\n<td>\u4ec5 Linux \u652f\u6301\u3002<\/td>\n<\/tr>\n<tr>\n<td>\u9002\u7528\u573a\u666f<\/td>\n<td>\u4f4e\u5e76\u53d1\u3001\u8de8\u5e73\u53f0\u573a\u666f&#xff08;\u5982\u5d4c\u5165\u5f0f&#xff09;\u3002<\/td>\n<td>\u4e2d\u7b49\u5e76\u53d1\u573a\u666f\u3002<\/td>\n<td>\u9ad8\u5e76\u53d1\u3001Linux \u670d\u52a1\u5668\u573a\u666f&#xff08;\u5982 Nginx\u3001Redis&#xff09;\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u4e94\u3001epoll \u670d\u52a1\u5668\u793a\u4f8b\u4ee3\u7801\u5173\u952e\u70b9&#xff08;C \u8bed\u8a00&#xff09;<\/h4>\n<p>c<\/p>\n<p>\/\/ 1. \u521b\u5efaepoll\u5b9e\u4f8b\u5e76\u8bbe\u7f6e\u76d1\u542c\u5957\u63a5\u5b57\u4e3a\u975e\u963b\u585e<br \/>\nint epfd &#061; epoll_create1(EPOLL_CLOEXEC);<br \/>\nset_nonblocking(listen_fd); \/\/ \u81ea\u5b9a\u4e49\u51fd\u6570&#xff0c;\u901a\u8fc7fcntl\u8bbe\u7f6eO_NONBLOCK<\/p>\n<p>\/\/ 2. \u6ce8\u518c\u76d1\u542c\u5957\u63a5\u5b57\u5230epoll&#xff08;ET\u6a21\u5f0f&#xff09;<br \/>\nstruct epoll_event event &#061; {<br \/>\n    .events &#061; EPOLLIN | EPOLLET,<br \/>\n    .data &#061; {.fd &#061; listen_fd}<br \/>\n};<br \/>\nepoll_ctl(epfd, EPOLL_CTL_ADD, listen_fd, &amp;event);<\/p>\n<p>\/\/ 3. \u5904\u7406\u65b0\u8fde\u63a5&#xff08;ET\u6a21\u5f0f\u4e0b\u9700\u5faa\u73afaccept\u76f4\u5230EAGAIN&#xff09;<br \/>\nif (fd &#061;&#061; listen_fd) {<br \/>\n    while (1) {<br \/>\n        int connfd &#061; accept(listen_fd, &amp;cliaddr, &amp;addrlen);<br \/>\n        if (connfd &#061;&#061; -1) {<br \/>\n            if (errno &#061;&#061; EAGAIN || errno &#061;&#061; EWOULDBLOCK) break; \/\/ \u65e0\u65b0\u8fde\u63a5<br \/>\n            perror(&#034;accept&#034;);<br \/>\n            exit(EXIT_FAILURE);<br \/>\n        }<br \/>\n        set_nonblocking(connfd); \/\/ \u5ba2\u6237\u7aef\u5957\u63a5\u5b57\u4e5f\u9700\u8bbe\u4e3a\u975e\u963b\u585e<br \/>\n        epoll_ctl(epfd, EPOLL_CTL_ADD, connfd, &amp;event); \/\/ \u6ce8\u518c\u8bfb\u4e8b\u4ef6&#xff08;ET\u6a21\u5f0f&#xff09;<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ 4. \u5904\u7406\u8bfb\u4e8b\u4ef6&#xff08;ET\u6a21\u5f0f\u4e0b\u9700\u5faa\u73afrecv\u76f4\u5230EAGAIN&#xff09;<br \/>\nelse {<br \/>\n    char buf[BUFFER_SIZE];<br \/>\n    while (1) {<br \/>\n        ssize_t n &#061; recv(fd, buf, sizeof(buf), 0);<br \/>\n        if (n &#061;&#061; -1) {<br \/>\n            if (errno &#061;&#061; EAGAIN || errno &#061;&#061; EWOULDBLOCK) break; \/\/ \u6570\u636e\u8bfb\u5b8c<br \/>\n            perror(&#034;recv&#034;);<br \/>\n            close(fd);<br \/>\n            epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL);<br \/>\n            break;<br \/>\n        } else if (n &#061;&#061; 0) { \/\/ \u5ba2\u6237\u7aef\u5173\u95ed\u8fde\u63a5<br \/>\n            close(fd);<br \/>\n            epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL);<br \/>\n            break;<br \/>\n        }<br \/>\n        \/\/ \u5904\u7406\u6570\u636e&#8230;<br \/>\n    }<br \/>\n}<\/p>\n<h4>\u516d\u3001\u603b\u7ed3<\/h4>\n<ul>\n<li>epoll \u4f18\u52bf&#xff1a;\n<ul>\n<li>\u9ad8\u5e76\u53d1&#xff1a;\u57fa\u4e8e\u7ea2\u9ed1\u6811\u548c\u6fc0\u6d3b\u5217\u8868&#xff0c;\u9002\u5408\u7ba1\u7406\u4e0a\u4e07\u7ea7\u8fde\u63a5\u3002<\/li>\n<li>\u9ad8\u6027\u80fd&#xff1a;\u4e8b\u4ef6\u901a\u77e5\u590d\u6742\u5ea6 O (1)&#xff0c;\u51cf\u5c11 CPU \u6d88\u8017\u3002<\/li>\n<li>\u7075\u6d3b\u6027&#xff1a;\u652f\u6301 LT\/ET \u6a21\u5f0f&#xff0c;\u9002\u914d\u4e0d\u540c\u573a\u666f\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u4f7f\u7528\u5efa\u8bae&#xff1a;\n<ul>\n<li>\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u9996\u9009 epoll&#xff0c;\u914d\u5408 ET \u6a21\u5f0f\u548c\u975e\u963b\u585e I\/O\u3002<\/li>\n<li>\u7b80\u5355\u573a\u666f\u6216\u8de8\u5e73\u53f0\u9700\u6c42\u4f7f\u7528 select\/poll\u3002<\/li>\n<li>\u6ce8\u610f\u8bbe\u7f6e\u6587\u4ef6\u63cf\u8ff0\u7b26\u975e\u963b\u585e&#xff0c;\u907f\u514d ET \u6a21\u5f0f\u4e0b\u7684\u6570\u636e\u4e22\u5931\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb795\u6b21\uff0c\u70b9\u8d5e19\u6b21\uff0c\u6536\u85cf21\u6b21\u3002epoll \u4f18\u52bf\u9ad8\u5e76\u53d1\uff1a\u57fa\u4e8e\u7ea2\u9ed1\u6811\u548c\u6fc0\u6d3b\u5217\u8868\uff0c\u9002\u5408\u7ba1\u7406\u4e0a\u4e07\u7ea7\u8fde\u63a5\u3002\u9ad8\u6027\u80fd\uff1a\u4e8b\u4ef6\u901a\u77e5\u590d\u6742\u5ea6 O (1)\uff0c\u51cf\u5c11 CPU \u6d88\u8017\u3002\u7075\u6d3b\u6027\uff1a\u652f\u6301 LT\/ET \u6a21\u5f0f\uff0c\u9002\u914d\u4e0d\u540c\u573a\u666f\u3002\u4f7f\u7528\u5efa\u8bae\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u9996\u9009 epoll\uff0c\u914d\u5408 ET \u6a21\u5f0f\u548c\u975e\u963b\u585e I\/O\u3002\u7b80\u5355\u573a\u666f\u6216\u8de8\u5e73\u53f0\u9700\u6c42\u4f7f\u7528 select\/poll\u3002\u6ce8\u610f\u8bbe\u7f6e\u6587\u4ef6\u63cf\u8ff0\u7b26\u975e\u963b\u585e\uff0c\u907f\u514d ET \u6a21\u5f0f\u4e0b\u7684\u6570\u636e\u4e22\u5931\u3002<\/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":[99,187,427],"topic":[],"class_list":["post-38652","post","type-post","status-publish","format-standard","hentry","category-server","tag-java","tag-javascript","tag-427"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u670d\u52a1\u5668\u8fde\u63a5\u591a\u5ba2\u6237\u7aef - \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\/38652.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u670d\u52a1\u5668\u8fde\u63a5\u591a\u5ba2\u6237\u7aef - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb795\u6b21\uff0c\u70b9\u8d5e19\u6b21\uff0c\u6536\u85cf21\u6b21\u3002epoll \u4f18\u52bf\u9ad8\u5e76\u53d1\uff1a\u57fa\u4e8e\u7ea2\u9ed1\u6811\u548c\u6fc0\u6d3b\u5217\u8868\uff0c\u9002\u5408\u7ba1\u7406\u4e0a\u4e07\u7ea7\u8fde\u63a5\u3002\u9ad8\u6027\u80fd\uff1a\u4e8b\u4ef6\u901a\u77e5\u590d\u6742\u5ea6 O (1)\uff0c\u51cf\u5c11 CPU \u6d88\u8017\u3002\u7075\u6d3b\u6027\uff1a\u652f\u6301 LT\/ET \u6a21\u5f0f\uff0c\u9002\u914d\u4e0d\u540c\u573a\u666f\u3002\u4f7f\u7528\u5efa\u8bae\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u9996\u9009 epoll\uff0c\u914d\u5408 ET \u6a21\u5f0f\u548c\u975e\u963b\u585e I\/O\u3002\u7b80\u5355\u573a\u666f\u6216\u8de8\u5e73\u53f0\u9700\u6c42\u4f7f\u7528 select\/poll\u3002\u6ce8\u610f\u8bbe\u7f6e\u6587\u4ef6\u63cf\u8ff0\u7b26\u975e\u963b\u585e\uff0c\u907f\u514d ET \u6a21\u5f0f\u4e0b\u7684\u6570\u636e\u4e22\u5931\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/38652.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-20T23:01:00+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=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/38652.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/38652.html\",\"name\":\"\u670d\u52a1\u5668\u8fde\u63a5\u591a\u5ba2\u6237\u7aef - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-20T23:01:00+00:00\",\"dateModified\":\"2025-05-20T23:01:00+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/38652.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/38652.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/38652.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u670d\u52a1\u5668\u8fde\u63a5\u591a\u5ba2\u6237\u7aef\"}]},{\"@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":"\u670d\u52a1\u5668\u8fde\u63a5\u591a\u5ba2\u6237\u7aef - \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\/38652.html","og_locale":"zh_CN","og_type":"article","og_title":"\u670d\u52a1\u5668\u8fde\u63a5\u591a\u5ba2\u6237\u7aef - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb795\u6b21\uff0c\u70b9\u8d5e19\u6b21\uff0c\u6536\u85cf21\u6b21\u3002epoll \u4f18\u52bf\u9ad8\u5e76\u53d1\uff1a\u57fa\u4e8e\u7ea2\u9ed1\u6811\u548c\u6fc0\u6d3b\u5217\u8868\uff0c\u9002\u5408\u7ba1\u7406\u4e0a\u4e07\u7ea7\u8fde\u63a5\u3002\u9ad8\u6027\u80fd\uff1a\u4e8b\u4ef6\u901a\u77e5\u590d\u6742\u5ea6 O (1)\uff0c\u51cf\u5c11 CPU \u6d88\u8017\u3002\u7075\u6d3b\u6027\uff1a\u652f\u6301 LT\/ET \u6a21\u5f0f\uff0c\u9002\u914d\u4e0d\u540c\u573a\u666f\u3002\u4f7f\u7528\u5efa\u8bae\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u9996\u9009 epoll\uff0c\u914d\u5408 ET \u6a21\u5f0f\u548c\u975e\u963b\u585e I\/O\u3002\u7b80\u5355\u573a\u666f\u6216\u8de8\u5e73\u53f0\u9700\u6c42\u4f7f\u7528 select\/poll\u3002\u6ce8\u610f\u8bbe\u7f6e\u6587\u4ef6\u63cf\u8ff0\u7b26\u975e\u963b\u585e\uff0c\u907f\u514d ET \u6a21\u5f0f\u4e0b\u7684\u6570\u636e\u4e22\u5931\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/38652.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-20T23:01:00+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/38652.html","url":"https:\/\/www.wsisp.com\/helps\/38652.html","name":"\u670d\u52a1\u5668\u8fde\u63a5\u591a\u5ba2\u6237\u7aef - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-20T23:01:00+00:00","dateModified":"2025-05-20T23:01:00+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/38652.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/38652.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/38652.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u670d\u52a1\u5668\u8fde\u63a5\u591a\u5ba2\u6237\u7aef"}]},{"@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\/38652","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=38652"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/38652\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=38652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=38652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=38652"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=38652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}