{"id":45616,"date":"2025-07-30T00:41:38","date_gmt":"2025-07-29T16:41:38","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/45616.html"},"modified":"2025-07-30T00:41:38","modified_gmt":"2025-07-29T16:41:38","slug":"epoll-%e8%af%a6%e8%a7%a3%ef%bc%9a%e9%ab%98%e6%80%a7%e8%83%bd-i-o-%e5%a4%9a%e8%b7%af%e5%a4%8d%e7%94%a8%e6%8a%80%e6%9c%af","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/45616.html","title":{"rendered":"epoll \u8be6\u89e3\uff1a\u9ad8\u6027\u80fd I\/O \u591a\u8def\u590d\u7528\u6280\u672f"},"content":{"rendered":"<h4>1.\u4e3a\u4ec0\u4e48\u6211\u4eec\u9700\u8981 epoll&#xff1f;<\/h4>\n<\/p>\n<p>\u60f3\u8c61\u4e00\u4e0b&#xff0c;\u4f60\u662f\u4e00\u5bb6\u9910\u5385\u7684\u670d\u52a1\u5458&#xff0c;\u8981\u540c\u65f6\u670d\u52a1\u5f88\u591a\u684c\u5ba2\u4eba\u3002<\/p>\n<ul>\n<li>\n<p>\u4f20\u7edf\u65b9\u5f0f (\u963b\u585eI\/O)&#xff1a;\u4f60\u8d70\u5230\u4e00\u5f20\u684c\u524d&#xff0c;\u95ee\u201c\u60a8\u8981\u70b9\u4ec0\u4e48&#xff1f;\u201d \u5982\u679c\u5ba2\u4eba\u8fd8\u6ca1\u60f3\u597d&#xff0c;\u4f60\u5c31\u5f97\u4e00\u76f4\u7b49&#xff0c;\u76f4\u5230\u4ed6\u4eec\u70b9\u5b8c\u83dc\u624d\u80fd\u53bb\u4e0b\u4e00\u684c\u3002\u8fd9\u6837\u6548\u7387\u975e\u5e38\u4f4e&#xff0c;\u56e0\u4e3a\u5927\u90e8\u5206\u65f6\u95f4\u90fd\u5728\u201c\u7b49\u201d\u3002<\/p>\n<\/li>\n<li>\n<p>\u8f6e\u8be2\u65b9\u5f0f (select\/poll)&#xff1a;\u4f60\u6bcf\u9694\u51e0\u5206\u949f\u5c31\u53bb\u6240\u6709\u684c\u5b50\u95ee\u4e00\u904d&#xff1a;\u201c\u8c01\u60f3\u70b9\u83dc\u4e86&#xff1f;\u8c01\u7684\u83dc\u597d\u4e86&#xff1f;\u201d \u5373\u4f7f\u5927\u90e8\u5206\u684c\u5b50\u90fd\u6ca1\u52a8\u9759&#xff0c;\u4f60\u4e5f\u8981\u95ee\u4e00\u904d\u3002\u8fd9\u6837\u6bd4\u7b2c\u4e00\u79cd\u597d\u4e00\u4e9b&#xff0c;\u4f46\u5982\u679c\u5ba2\u4eba\u975e\u5e38\u591a&#xff0c;\u4f60\u8fd8\u662f\u4f1a\u628a\u5927\u91cf\u65f6\u95f4\u82b1\u5728\u201c\u95ee\u201d\u800c\u4e0d\u662f\u201c\u670d\u52a1\u201d\u4e0a\u3002\u800c\u4e14&#xff0c;\u6bcf\u6b21\u95ee\u4e00\u904d&#xff0c;\u90fd\u9700\u8981\u628a\u6240\u6709\u5ba2\u4eba\u7684\u540d\u5355\u90fd\u8fc7\u4e00\u904d\u3002\u5ba2\u4eba\u8d8a\u591a&#xff0c;\u540d\u5355\u8d8a\u957f&#xff0c;\u4f60\u68c0\u67e5\u8d77\u6765\u5c31\u8d8a\u6162\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e8b\u4ef6\u901a\u77e5\u65b9\u5f0f (epoll)&#xff1a;\u73b0\u5728&#xff0c;\u9910\u5385\u7ed9\u4f60\u53d1\u4e86\u4e00\u4e2a\u201c\u5bf9\u8bb2\u673a\u201d\u548c\u4e00\u4e2a\u201c\u540d\u5355\u672c\u201d\u3002\u4f60\u4e0d\u7528\u4e3b\u52a8\u53bb\u95ee\u5ba2\u4eba\u3002\u5f53\u6709\u5ba2\u4eba\u9700\u8981\u670d\u52a1&#xff08;\u6bd4\u5982\u4e3e\u624b\u3001\u83dc\u505a\u597d\u4e86&#xff09;&#xff0c;\u201c\u5bf9\u8bb2\u673a\u201d\u4f1a\u7acb\u523b\u544a\u8bc9\u4f60&#xff1a;\u201c3\u53f7\u684c\u5ba2\u4eba\u4e3e\u624b\u4e86&#xff01;\u201d \u6216\u8005\u201c5\u53f7\u684c\u7684\u83dc\u597d\u4e86&#xff01;\u201d\u3002\u4f60\u53ea\u9700\u8981\u5728\u201c\u540d\u5355\u672c\u201d\u4e0a\u767b\u8bb0\u54ea\u4e9b\u684c\u5b50\u9700\u8981\u5173\u6ce8&#xff0c;\u7136\u540e\u5750\u5728\u90a3\u7b49\u901a\u77e5\u5c31\u884c\u4e86\u3002\u5f53\u6709\u901a\u77e5\u65f6&#xff0c;\u4f60\u76f4\u63a5\u53bb\u670d\u52a1\u5bf9\u5e94\u7684\u684c\u5b50\u3002\u8fd9\u6837\u6548\u7387\u5c31\u9ad8\u5f97\u591a&#xff0c;\u56e0\u4e3a\u4f60\u53ea\u5904\u7406\u6709\u4e8b\u4ef6\u53d1\u751f\u7684\u684c\u5b50\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u5728\u8ba1\u7b97\u673a\u7f51\u7edc\u7f16\u7a0b\u4e2d&#xff0c;\u201c\u5ba2\u4eba\u201d\u5c31\u662f\u7f51\u7edc\u8fde\u63a5&#xff0c;\u201c\u670d\u52a1\u5458\u201d\u5c31\u662f\u4f60\u7684\u7a0b\u5e8f&#xff0c;\u201c\u70b9\u83dc\/\u83dc\u597d\u4e86\u201d\u5c31\u662f\u6570\u636e\u5230\u8fbe\u6216\u53ef\u4ee5\u53d1\u9001\u6570\u636e\u7b49\u4e8b\u4ef6\u3002<\/p>\n<p>epoll \u5c31\u662f\u8fd9\u4e2a\u201c\u5bf9\u8bb2\u673a &#043; \u540d\u5355\u672c\u201d\u7684\u7ec4\u5408\u3002\u5b83\u662f\u4e00\u79cd Linux \u7279\u6709\u7684\u9ad8\u6027\u80fd I\/O \u591a\u8def\u590d\u7528\u6280\u672f\u3002\u5b83\u5141\u8bb8\u4e00\u4e2a\u7a0b\u5e8f\u540c\u65f6\u76d1\u542c\u6210\u5343\u4e0a\u4e07\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;\u7f51\u7edc\u8fde\u63a5&#xff09;&#xff0c;\u5f53\u8fd9\u4e9b\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u7684\u6570\u636e\u51c6\u5907\u597d&#xff08;\u53ef\u8bfb\u3001\u53ef\u5199&#xff09;\u65f6&#xff0c;epoll \u4f1a\u201c\u901a\u77e5\u201d\u4f60\u7684\u7a0b\u5e8f&#xff0c;\u4f60\u53ea\u9700\u8981\u5904\u7406\u90a3\u4e9b\u6d3b\u8dc3\u7684\u8fde\u63a5&#xff0c;\u800c\u4e0d\u7528\u50cf select\/poll \u90a3\u6837\u8f6e\u8be2\u6240\u6709\u8fde\u63a5\u3002<\/p>\n<hr \/>\n<h4>2. epoll \u7684 API \u8be6\u89e3\u548c\u4f7f\u7528\u6280\u5de7<\/h4>\n<\/p>\n<p>epoll \u63d0\u4f9b\u4e86\u4e09\u4e2a\u6838\u5fc3 API \u51fd\u6570&#xff1a;<\/p>\n<\/p>\n<h5>\u6838\u5fc3 API \u51fd\u6570&#xff1a;<\/h5>\n<\/p>\n<li>\n<p>epoll_create1() \/ epoll_create(): \u521b\u5efa\u4e00\u4e2a epoll \u5b9e\u4f8b\u3002<\/p>\n<ul>\n<li>\n<p>int epoll_create1(int flags): \u63a8\u8350\u4f7f\u7528 epoll_create1\u3002<\/p>\n<ul>\n<li>\n<p>flags: \u5982\u679c\u8bbe\u7f6e\u4e3a EPOLL_CLOEXEC&#xff0c;\u8868\u793a\u5728 exec \u7cfb\u5217\u51fd\u6570\u8c03\u7528\u65f6&#xff0c;epoll \u6587\u4ef6\u63cf\u8ff0\u7b26\u4f1a\u81ea\u52a8\u5173\u95ed&#xff0c;\u8fd9\u662f\u4e00\u79cd\u597d\u4e60\u60ef\u3002\u8bbe\u7f6e\u4e3a 0 \u5219\u7b49\u540c\u4e8e epoll_create(size)\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>int epoll_create(int size): \u8001\u7248\u672c\u51fd\u6570\u3002<\/p>\n<ul>\n<li>\n<p>size: \u8fd9\u4e2a\u53c2\u6570\u5728 Linux \u5185\u6838 2.6.8 \u7248\u672c\u4e4b\u540e\u5c31\u88ab\u5ffd\u7565\u4e86&#xff0c;\u4f46\u5fc5\u987b\u5927\u4e8e0\u3002\u5b83\u6700\u521d\u662f\u5185\u6838\u7528\u6765\u9884\u4f30\u76d1\u542c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf&#xff0c;\u73b0\u5728\u5185\u6838\u4f1a\u52a8\u6001\u8c03\u6574\u3002\u4e00\u822c\u8bbe\u4e3a 1 \u5373\u53ef\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8fd4\u56de\u503c: \u6210\u529f\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 epoll \u5b9e\u4f8b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff1b;\u5931\u8d25\u8fd4\u56de -1\u3002<\/p>\n<\/li>\n<li>\n<p>\u7406\u89e3: \u60f3\u8c61\u8fd9\u662f\u4f60\u4ece\u9910\u5385\u9886\u56de\u6765\u7684\u90a3\u4e2a\u201c\u5bf9\u8bb2\u673a\u7cfb\u7edf\u201d\u7684\u53e5\u67c4\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>epoll_ctl(): \u63a7\u5236 epoll \u5b9e\u4f8b&#xff0c;\u5411\u5176\u4e2d\u6dfb\u52a0\u3001\u4fee\u6539\u6216\u5220\u9664\u611f\u5174\u8da3\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/p>\n<ul>\n<li>\n<p>int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)<\/p>\n<ul>\n<li>\n<p>epfd: epoll_create1() \u8fd4\u56de\u7684 epoll \u5b9e\u4f8b\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/p>\n<\/li>\n<li>\n<p>op: \u64cd\u4f5c\u7c7b\u578b&#xff0c;\u53ef\u4ee5\u662f&#xff1a;<\/p>\n<ul>\n<li>\n<p>EPOLL_CTL_ADD: \u5c06\u6587\u4ef6\u63cf\u8ff0\u7b26 fd \u6ce8\u518c\u5230 epfd \u4e2d\u3002<\/p>\n<\/li>\n<li>\n<p>EPOLL_CTL_MOD: \u4fee\u6539\u5df2\u6ce8\u518c\u6587\u4ef6\u63cf\u8ff0\u7b26 fd \u7684\u4e8b\u4ef6\u7c7b\u578b\u3002<\/p>\n<\/li>\n<li>\n<p>EPOLL_CTL_DEL: \u4ece epfd \u4e2d\u5220\u9664\u6587\u4ef6\u63cf\u8ff0\u7b26 fd\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>fd: \u8981\u64cd\u4f5c\u7684\u76ee\u6807\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;\u4f8b\u5982\u4e00\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u7684 socket&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>event: \u4e00\u4e2a\u6307\u5411 epoll_event \u7ed3\u6784\u4f53\u7684\u6307\u9488&#xff0c;\u7528\u4e8e\u6307\u5b9a fd \u611f\u5174\u8da3\u7684\u4e8b\u4ef6\u7c7b\u578b\u3002<\/p>\n<p>C<\/p>\n<p>struct epoll_event {<br \/>\n    __uint32_t events;  \/\/ \u611f\u5174\u8da3\u7684\u4e8b\u4ef6\u7c7b\u578b<br \/>\n    epoll_data_t data;  \/\/ \u7528\u6237\u6570\u636e&#xff0c;\u901a\u5e38\u7528\u6765\u5b58\u653e\u548c\u8be5\u6587\u4ef6\u63cf\u8ff0\u7b26\u76f8\u5173\u7684\u4fe1\u606f<br \/>\n};<\/p>\n<p>typedef union epoll_data {<br \/>\n    void        *ptr;<br \/>\n    int          fd;    \/\/ \u6700\u5e38\u7528&#xff0c;\u76f4\u63a5\u5b58\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    uint32_t     u32;<br \/>\n    uint64_t     u64;<br \/>\n} epoll_data_t;<\/p>\n<ul>\n<li>\n<p>events \u5e38\u7528\u7684\u4e8b\u4ef6\u7c7b\u578b&#xff1a;<\/p>\n<ul>\n<li>\n<p>EPOLLIN: \u6587\u4ef6\u63cf\u8ff0\u7b26\u53ef\u8bfb&#xff08;\u6709\u6570\u636e\u5230\u8fbe&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>EPOLLOUT: \u6587\u4ef6\u63cf\u8ff0\u7b26\u53ef\u5199&#xff08;\u53ef\u4ee5\u53d1\u9001\u6570\u636e&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>EPOLLET: \u8fb9\u7f18\u89e6\u53d1 (Edge Triggered) \u6a21\u5f0f&#xff0c;\u662f epoll \u6700\u5f3a\u5927\u7684\u7279\u6027\u4e4b\u4e00\u3002<\/p>\n<\/li>\n<li>\n<p>EPOLLLT: \u6c34\u5e73\u89e6\u53d1 (Level Triggered) \u6a21\u5f0f&#xff08;\u9ed8\u8ba4&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>EPOLLERR: \u6587\u4ef6\u63cf\u8ff0\u7b26\u53d1\u751f\u9519\u8bef\u3002<\/p>\n<\/li>\n<li>\n<p>EPOLLHUP: \u6587\u4ef6\u63cf\u8ff0\u7b26\u88ab\u6302\u65ad&#xff08;\u5bf9\u7aef\u5173\u95ed\u8fde\u63a5&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>EPOLLONESHOT: \u4e00\u6b21\u6027\u4e8b\u4ef6&#xff0c;\u4e8b\u4ef6\u89e6\u53d1\u540e\u81ea\u52a8\u53d6\u6d88\u76d1\u542c&#xff0c;\u9700\u8981\u91cd\u65b0 MOD \u624d\u80fd\u518d\u6b21\u76d1\u542c\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8fd4\u56de\u503c: \u6210\u529f\u8fd4\u56de 0&#xff1b;\u5931\u8d25\u8fd4\u56de -1\u3002<\/p>\n<\/li>\n<li>\n<p>\u7406\u89e3: \u8fd9\u662f\u4f60\u62ff\u7740\u201c\u540d\u5355\u672c\u201d\u53bb\u767b\u8bb0\u5ba2\u4eba\u4fe1\u606f&#xff0c;\u544a\u8bc9\u9910\u5385&#xff08;\u5185\u6838&#xff09;\u4f60\u5173\u5fc3\u54ea\u4e9b\u684c\u5b50&#xff0c;\u4ee5\u53ca\u8fd9\u4e9b\u684c\u5b50\u53d1\u751f\u4ec0\u4e48\u4e8b&#xff08;\u53ef\u8bfb\u3001\u53ef\u5199&#xff09;\u65f6\u901a\u77e5\u4f60\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>epoll_wait(): \u7b49\u5f85 epoll \u5b9e\u4f8b\u4e0a\u6ce8\u518c\u7684\u4e8b\u4ef6\u53d1\u751f\u3002<\/p>\n<ul>\n<li>\n<p>int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout)<\/p>\n<ul>\n<li>\n<p>epfd: epoll_create1() \u8fd4\u56de\u7684 epoll \u5b9e\u4f8b\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/p>\n<\/li>\n<li>\n<p>events: \u4e00\u4e2a epoll_event \u7ed3\u6784\u4f53\u6570\u7ec4&#xff0c;epoll_wait \u4f1a\u628a\u53d1\u751f\u7684\u4e8b\u4ef6\u586b\u5145\u5230\u8fd9\u4e2a\u6570\u7ec4\u4e2d\u3002<\/p>\n<\/li>\n<li>\n<p>maxevents: events \u6570\u7ec4\u7684\u6700\u5927\u5bb9\u91cf&#xff0c;\u8868\u793a\u4e00\u6b21 epoll_wait \u6700\u591a\u80fd\u8fd4\u56de\u591a\u5c11\u4e2a\u4e8b\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<p>timeout: \u8d85\u65f6\u65f6\u95f4&#xff0c;\u5355\u4f4d\u6beb\u79d2\u3002<\/p>\n<ul>\n<li>\n<p>-1: \u6c38\u8fdc\u7b49\u5f85&#xff0c;\u76f4\u5230\u6709\u4e8b\u4ef6\u53d1\u751f\u3002<\/p>\n<\/li>\n<li>\n<p>0: \u7acb\u5373\u8fd4\u56de&#xff0c;\u4e0d\u7b49\u5f85\u3002<\/p>\n<\/li>\n<li>\n<p>&gt;0: \u7b49\u5f85\u6307\u5b9a\u6beb\u79d2\u6570\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8fd4\u56de\u503c: \u6210\u529f\u8fd4\u56de\u53d1\u751f\u4e8b\u4ef6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf&#xff1b;\u8d85\u65f6\u8fd4\u56de 0&#xff1b;\u5931\u8d25\u8fd4\u56de -1\u3002<\/p>\n<\/li>\n<li>\n<p>\u7406\u89e3: \u4f60\u5750\u5728\u90a3\u91cc&#xff0c;\u62ff\u7740\u201c\u5bf9\u8bb2\u673a\u201d\u7b49\u5f85\u901a\u77e5\u3002\u4e00\u65e6\u6709\u901a\u77e5&#xff0c;epoll_wait \u5c31\u4f1a\u544a\u8bc9\u4f60\u54ea\u4e9b\u684c\u5b50\u6709\u4e8b\u4e86&#xff0c;\u4ee5\u53ca\u5177\u4f53\u662f\u4ec0\u4e48\u4e8b\u3002\u4f60\u53ea\u5904\u7406\u90a3\u4e9b\u88ab\u901a\u77e5\u5230\u7684\u684c\u5b50&#xff0c;\u6548\u7387\u5f88\u9ad8\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<h5>epoll \u7684\u4e24\u79cd\u5de5\u4f5c\u6a21\u5f0f&#xff1a;<\/h5>\n<\/p>\n<p>\u7406\u89e3\u8fd9\u4e24\u79cd\u6a21\u5f0f\u5bf9\u4e8e\u6b63\u786e\u4f7f\u7528 epoll \u81f3\u5173\u91cd\u8981\u3002<\/p>\n<ul>\n<li>\n<p>\u6c34\u5e73\u89e6\u53d1 (LT &#8211; Level Triggered) &#8211; \u9ed8\u8ba4\u6a21\u5f0f&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u7279\u70b9: \u5982\u679c\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u6709\u6570\u636e\u53ef\u8bfb&#xff08;\u6216\u53ef\u5199&#xff09;&#xff0c;epoll_wait \u5c31\u4f1a\u4e00\u76f4\u901a\u77e5\u4f60&#xff0c;\u76f4\u5230\u4f60\u628a\u6570\u636e\u8bfb\u5b8c&#xff08;\u6216\u5199\u5b8c&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f18\u70b9: \u7b80\u5355&#xff0c;\u4e0d\u5bb9\u6613\u4e22\u4e8b\u4ef6\u3002\u5373\u4f7f\u4f60\u53ea\u8bfb\u4e86\u4e00\u90e8\u5206\u6570\u636e&#xff0c;\u4e0b\u6b21 epoll_wait \u4f9d\u7136\u4f1a\u901a\u77e5\u4f60&#xff0c;\u76f4\u5230\u7f13\u51b2\u533a\u6e05\u7a7a\u3002<\/p>\n<\/li>\n<li>\n<p>\u7f3a\u70b9: \u5982\u679c\u4f60\u6ca1\u6709\u4e00\u6b21\u6027\u5904\u7406\u5b8c\u6240\u6709\u6570\u636e&#xff0c;\u53ef\u80fd\u4f1a\u88ab\u91cd\u590d\u901a\u77e5&#xff0c;\u5bfc\u81f4\u4e0d\u5fc5\u8981\u7684\u7cfb\u7edf\u8c03\u7528\u3002<\/p>\n<\/li>\n<li>\n<p>\u7c7b\u6bd4: \u5ba2\u4eba\u684c\u4e0a\u7684\u6c34\u676f\u6ee1\u4e86&#xff0c;\u670d\u52a1\u5458\u4f1a\u4e00\u76f4\u544a\u8bc9\u4f60\u201c\u6c34\u676f\u6ee1\u4e86&#xff01;\u201d&#xff0c;\u76f4\u5230\u4f60\u628a\u6c34\u676f\u91cc\u7684\u6c34\u90fd\u559d\u5b8c\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8fb9\u7f18\u89e6\u53d1 (ET &#8211; Edge Triggered) &#8211; \u9ad8\u6548\u6a21\u5f0f&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u7279\u70b9: \u53ea\u6709\u5f53\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u72b6\u6001\u53d1\u751f\u53d8\u5316\u65f6&#xff08;\u4f8b\u5982\u4ece\u4e0d\u53ef\u8bfb\u53d8\u4e3a\u53ef\u8bfb&#xff0c;\u6216\u8005\u6709\u65b0\u6570\u636e\u5230\u6765&#xff09;&#xff0c;epoll_wait \u624d\u4f1a\u901a\u77e5\u4f60\u4e00\u6b21\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f18\u70b9: \u6548\u7387\u66f4\u9ad8\u3002\u53ea\u901a\u77e5\u4e00\u6b21&#xff0c;\u907f\u514d\u91cd\u590d\u901a\u77e5&#xff0c;\u51cf\u5c11\u7cfb\u7edf\u8c03\u7528\u6b21\u6570\u3002\u9002\u5408\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8fde\u63a5\u3002<\/p>\n<\/li>\n<li>\n<p>\u7f3a\u70b9: \u8981\u6c42\u7a0b\u5e8f\u4e00\u6b21\u6027\u5904\u7406\u5b8c\u6240\u6709\u6570\u636e\u3002\u5982\u679c\u53ea\u8bfb\u4e86\u4e00\u90e8\u5206&#xff0c;\u4e0b\u6b21 epoll_wait \u4e0d\u4f1a\u518d\u901a\u77e5\u4f60&#xff0c;\u5269\u4e0b\u7684\u6570\u636e\u5c31\u4f1a\u6ede\u7559\u5728\u5185\u6838\u7f13\u51b2\u533a&#xff0c;\u5bfc\u81f4\u6570\u636e\u4e22\u5931\u6216\u903b\u8f91\u9519\u8bef\u3002<\/p>\n<\/li>\n<li>\n<p>\u7c7b\u6bd4: \u5ba2\u4eba\u7b2c\u4e00\u6b21\u4e3e\u624b&#xff0c;\u670d\u52a1\u5458\u53ea\u901a\u77e5\u4f60\u4e00\u6b21\u3002\u5982\u679c\u4f60\u6ca1\u53ca\u65f6\u670d\u52a1&#xff0c;\u6216\u8005\u53ea\u670d\u52a1\u4e86\u4e00\u534a&#xff0c;\u5ba2\u4eba\u7b2c\u4e8c\u6b21\u4e3e\u624b&#xff0c;\u670d\u52a1\u5458\u624d\u4f1a\u518d\u901a\u77e5\u4f60\u3002\u56e0\u6b64&#xff0c;\u4f60\u9700\u8981\u4e00\u6b21\u6027\u628a\u5ba2\u4eba\u6240\u6709\u7684\u9700\u6c42\u90fd\u5904\u7406\u5b8c\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h5>\u4f7f\u7528\u6280\u5de7&#xff1a;<\/h5>\n<\/p>\n<li>\n<p>ET \u6a21\u5f0f\u914d\u5408\u975e\u963b\u585e I\/O&#xff1a;<\/p>\n<ul>\n<li>\n<p>epoll \u7684 ET \u6a21\u5f0f\u51e0\u4e4e\u603b\u662f\u548c \u975e\u963b\u585e I\/O (Non-blocking I\/O) \u7ed3\u5408\u4f7f\u7528\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f53 epoll_wait \u5728 ET \u6a21\u5f0f\u4e0b\u901a\u77e5\u4f60\u67d0\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ef\u8bfb\u65f6&#xff0c;\u4f60\u5e94\u8be5\u5728\u4e00\u4e2a\u5faa\u73af\u4e2d\u5c3d\u53ef\u80fd\u591a\u5730\u8bfb\u53d6\u6240\u6709\u53ef\u7528\u6570\u636e&#xff0c;\u76f4\u5230 read() \u8fd4\u56de EAGAIN \u6216 EWOULDBLOCK&#xff08;\u8868\u793a\u5f53\u524d\u6ca1\u6709\u66f4\u591a\u6570\u636e\u53ef\u8bfb\u4e86&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u8fd9\u6837\u505a\u662f\u4e3a\u4e86\u907f\u514d\u6570\u636e\u5728\u5185\u6838\u7f13\u51b2\u533a\u5806\u79ef&#xff0c;\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u611f\u77e5\u5230\u672a\u5904\u7406\u7684\u6570\u636e\u3002<\/p>\n<\/li>\n<li>\n<p>\u8981\u5c06 socket \u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\u6a21\u5f0f&#xff0c;\u53ef\u4ee5\u4f7f\u7528 fcntl(fd, F_SETFL, O_NONBLOCK)\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>EPOLLONESHOT \u6807\u5fd7&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5982\u679c\u4f60\u5e0c\u671b\u5728\u67d0\u4e2a\u4e8b\u4ef6&#xff08;\u6bd4\u5982\u4e00\u4e2a\u8fde\u63a5\u7684\u8bfb\u4e8b\u4ef6&#xff09;\u88ab\u89e6\u53d1\u540e&#xff0c;\u6682\u65f6\u4e0d\u518d\u76d1\u542c\u8be5\u8fde\u63a5\u7684\u5176\u4ed6\u4e8b\u4ef6&#xff0c;\u76f4\u5230\u4f60\u660e\u786e\u5730\u91cd\u65b0\u542f\u7528\u5b83&#xff0c;\u53ef\u4ee5\u4f7f\u7528 EPOLLONESHOT\u3002<\/p>\n<\/li>\n<li>\n<p>\u8fd9\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u7279\u522b\u6709\u7528&#xff0c;\u53ef\u4ee5\u786e\u4fdd\u4e00\u4e2a\u4e8b\u4ef6\u53ea\u88ab\u4e00\u4e2a\u7ebf\u7a0b\u5904\u7406&#xff0c;\u907f\u514d\u7ade\u4e89\u6761\u4ef6\u3002\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u5904\u7406\u5b8c\u4e8b\u4ef6\u540e&#xff0c;\u9700\u8981\u8c03\u7528 epoll_ctl(EPOLL_CTL_MOD, &#8230;) \u91cd\u65b0\u6dfb\u52a0 EPOLLONESHOT\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5408\u7406\u8bbe\u7f6e epoll_event \u7684 data \u6210\u5458&#xff1a;<\/p>\n<ul>\n<li>\n<p>epoll_event \u7ed3\u6784\u4f53\u4e2d\u7684 data \u8054\u5408\u4f53\u53ef\u4ee5\u5b58\u50a8 fd\u3001ptr\u3001u32\u3001u64\u3002<\/p>\n<\/li>\n<li>\n<p>\u6700\u5e38\u7528\u7684\u662f data.fd&#xff08;\u76f4\u63a5\u5b58\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff09;\u6216 data.ptr&#xff08;\u5b58\u4e00\u4e2a\u6307\u5411\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u7684\u6307\u9488&#xff0c;\u8fd9\u4e2a\u7ed3\u6784\u4f53\u53ef\u4ee5\u5305\u542b\u6587\u4ef6\u63cf\u8ff0\u7b26\u3001\u7f13\u51b2\u533a\u3001\u72b6\u6001\u7b49\u6240\u6709\u4e0e\u8be5\u8fde\u63a5\u76f8\u5173\u7684\u4fe1\u606f&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528 data.ptr \u662f\u4e00\u79cd\u975e\u5e38\u7075\u6d3b\u548c\u9ad8\u6548\u7684\u65b9\u5f0f&#xff0c;\u907f\u514d\u4e86\u6bcf\u6b21\u4e8b\u4ef6\u53d1\u751f\u65f6\u90fd\u53bb\u67e5\u627e\u5bf9\u5e94\u8fde\u63a5\u7684\u4fe1\u606f\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u4f18\u96c5\u5730\u5173\u95ed\u8fde\u63a5&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5f53 read() \u8fd4\u56de 0 \u65f6&#xff0c;\u8868\u793a\u5bf9\u7aef\u5173\u95ed\u4e86\u8fde\u63a5&#xff0c;\u6b64\u65f6\u4f60\u5e94\u8be5\u8c03\u7528 close() \u5173\u95ed\u672c\u5730\u7684 socket&#xff0c;\u5e76\u4ece epoll \u5b9e\u4f8b\u4e2d\u5220\u9664\u8be5\u6587\u4ef6\u63cf\u8ff0\u7b26 (EPOLL_CTL_DEL)\u3002<\/p>\n<\/li>\n<li>\n<p>\u5904\u7406 EPOLLHUP (\u6302\u65ad) \u548c EPOLLERR (\u9519\u8bef) \u4e8b\u4ef6\u3002\u8fd9\u4e9b\u901a\u5e38\u4e5f\u610f\u5473\u7740\u8fde\u63a5\u4e0d\u518d\u53ef\u7528&#xff0c;\u9700\u8981\u5173\u95ed\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<hr \/>\n<h4>3. epoll \u5b9e\u9645\u6848\u4f8b&#xff1a;\u4e00\u4e2a\u7b80\u5355\u7684 TCP \u670d\u52a1\u5668<\/h4>\n<\/p>\n<p>\u8fd9\u4e2a\u6848\u4f8b\u5c06\u5c55\u793a\u5982\u4f55\u4f7f\u7528 epoll \u6765\u5904\u7406\u591a\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u3002\u670d\u52a1\u5668\u4f1a\u63a5\u53d7\u65b0\u8fde\u63a5&#xff0c;\u5e76\u63a5\u6536\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6570\u636e&#xff0c;\u7136\u540e\u5c06\u6570\u636e\u56de\u663e\u7ed9\u5ba2\u6237\u7aef&#xff08;\u7c7b\u4f3c\u4e8e\u6211\u4eec\u4e4b\u524d\u6539\u9020\u7684\u56de\u58f0\u670d\u52a1\u5668&#xff0c;\u4f46\u7528 epoll \u5b9e\u73b0\u591a\u8fde\u63a5&#xff09;\u3002<\/p>\n<p>\u670d\u52a1\u5668\u7aef (epoll_server.c)<\/p>\n<p>C<\/p>\n<\/p>\n<p>#include &lt;stdio.h&gt;      \/\/ printf, perror<br \/>\n#include &lt;stdlib.h&gt;     \/\/ exit, EXIT_FAILURE<br \/>\n#include &lt;string.h&gt;     \/\/ memset, strlen<br \/>\n#include &lt;unistd.h&gt;     \/\/ close<br \/>\n#include &lt;arpa\/inet.h&gt;  \/\/ inet_pton, htons<br \/>\n#include &lt;sys\/socket.h&gt; \/\/ socket, bind, listen, accept<br \/>\n#include &lt;sys\/epoll.h&gt;  \/\/ epoll_create1, epoll_ctl, epoll_wait<br \/>\n#include &lt;fcntl.h&gt;      \/\/ fcntl, O_NONBLOCK<br \/>\n#include &lt;errno.h&gt;      \/\/ errno, EAGAIN<\/p>\n<p>#define PORT 8080<br \/>\n#define MAX_EVENTS 10 \/\/ epoll_wait \u4e00\u6b21\u6700\u591a\u8fd4\u56de\u7684\u4e8b\u4ef6\u6570\u91cf<br \/>\n#define BUFFER_SIZE 1024<\/p>\n<p>\/\/ \u8bbe\u7f6e\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e3a\u975e\u963b\u585e\u6a21\u5f0f<br \/>\nvoid set_nonblocking(int fd) {<br \/>\n    int flags &#061; fcntl(fd, F_GETFL, 0); \/\/ \u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u6807\u5fd7<br \/>\n    if (flags &#061;&#061; -1) {<br \/>\n        perror(&#034;fcntl(F_GETFL)&#034;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<br \/>\n    if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) &#061;&#061; -1) { \/\/ \u6dfb\u52a0\u975e\u963b\u585e\u6807\u5fd7<br \/>\n        perror(&#034;fcntl(F_SETFL, O_NONBLOCK)&#034;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    int listen_fd, conn_fd;<br \/>\n    struct sockaddr_in server_addr, client_addr;<br \/>\n    socklen_t client_len &#061; sizeof(client_addr);<br \/>\n    char buffer[BUFFER_SIZE];<\/p>\n<p>    \/\/ 1. \u521b\u5efa\u76d1\u542c socket<br \/>\n    if ((listen_fd &#061; socket(AF_INET, SOCK_STREAM, 0)) &#061;&#061; -1) {<br \/>\n        perror(&#034;socket \u521b\u5efa\u5931\u8d25&#034;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ \u8bbe\u7f6e socket \u9009\u9879&#xff0c;\u5141\u8bb8\u5730\u5740\u590d\u7528<br \/>\n    int opt &#061; 1;<br \/>\n    if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &amp;opt, sizeof(opt)) &#061;&#061; -1) {<br \/>\n        perror(&#034;setsockopt \u5931\u8d25&#034;);<br \/>\n        close(listen_fd);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ \u8bbe\u7f6e\u670d\u52a1\u5668\u5730\u5740<br \/>\n    memset(&amp;server_addr, 0, sizeof(server_addr));<br \/>\n    server_addr.sin_family &#061; AF_INET;<br \/>\n    server_addr.sin_addr.s_addr &#061; INADDR_ANY; \/\/ \u76d1\u542c\u6240\u6709\u53ef\u7528\u7f51\u7edc\u63a5\u53e3<br \/>\n    server_addr.sin_port &#061; htons(PORT);<\/p>\n<p>    \/\/ \u7ed1\u5b9a socket<br \/>\n    if (bind(listen_fd, (struct sockaddr *)&amp;server_addr, sizeof(server_addr)) &#061;&#061; -1) {<br \/>\n        perror(&#034;bind \u5931\u8d25&#034;);<br \/>\n        close(listen_fd);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ \u76d1\u542c\u8fde\u63a5<br \/>\n    if (listen(listen_fd, SOMAXCONN) &#061;&#061; -1) { \/\/ SOMAXCONN \u662f\u7cfb\u7edf\u9ed8\u8ba4\u7684\u6700\u5927\u961f\u5217\u957f\u5ea6<br \/>\n        perror(&#034;listen \u5931\u8d25&#034;);<br \/>\n        close(listen_fd);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ \u8bbe\u7f6e\u76d1\u542c socket \u4e3a\u975e\u963b\u585e\u6a21\u5f0f (\u867d\u7136\u5bf9\u4e8elisten fd\u4e0d\u662f\u5f3a\u5236&#xff0c;\u4f46\u517b\u6210\u4e60\u60ef)<br \/>\n    set_nonblocking(listen_fd);<br \/>\n    printf(&#034;\u670d\u52a1\u5668\u5df2\u542f\u52a8&#xff0c;\u6b63\u5728\u76d1\u542c\u7aef\u53e3 %d&#8230;\\\\n&#034;, PORT);<\/p>\n<p>    \/\/ 2. \u521b\u5efa epoll \u5b9e\u4f8b<br \/>\n    int epoll_fd &#061; epoll_create1(EPOLL_CLOEXEC); \/\/ EPOLL_CLOEXEC \u662f\u4e00\u79cd\u5b89\u5168\u5b9e\u8df5<br \/>\n    if (epoll_fd &#061;&#061; -1) {<br \/>\n        perror(&#034;epoll_create1 \u5931\u8d25&#034;);<br \/>\n        close(listen_fd);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ 3. \u5c06\u76d1\u542c socket \u6dfb\u52a0\u5230 epoll \u5b9e\u4f8b\u4e2d<br \/>\n    struct epoll_event event;<br \/>\n    event.events &#061; EPOLLIN; \/\/ \u76d1\u542c\u53ef\u8bfb\u4e8b\u4ef6<br \/>\n    event.data.fd &#061; listen_fd; \/\/ \u5c06\u76d1\u542c socket \u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u5b58\u5165 data \u8054\u5408\u4f53<br \/>\n    if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listen_fd, &amp;event) &#061;&#061; -1) {<br \/>\n        perror(&#034;epoll_ctl (ADD listen_fd) \u5931\u8d25&#034;);<br \/>\n        close(epoll_fd);<br \/>\n        close(listen_fd);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    struct epoll_event events[MAX_EVENTS]; \/\/ \u7528\u4e8e\u5b58\u50a8 epoll_wait \u8fd4\u56de\u7684\u4e8b\u4ef6<\/p>\n<p>    \/\/ 4. \u4e8b\u4ef6\u5faa\u73af<br \/>\n    while (1) {<br \/>\n        int num_events &#061; epoll_wait(epoll_fd, events, MAX_EVENTS, -1); \/\/ \u963b\u585e\u7b49\u5f85\u4e8b\u4ef6&#xff0c;-1 \u8868\u793a\u6c38\u4e45\u7b49\u5f85<br \/>\n        if (num_events &#061;&#061; -1) {<br \/>\n            perror(&#034;epoll_wait \u5931\u8d25&#034;);<br \/>\n            break;<br \/>\n        }<\/p>\n<p>        for (int i &#061; 0; i &lt; num_events; &#043;&#043;i) {<br \/>\n            \/\/ \u5982\u679c\u662f\u76d1\u542c socket \u6709\u4e8b\u4ef6 (\u65b0\u8fde\u63a5\u5230\u6765)<br \/>\n            if (events[i].data.fd &#061;&#061; listen_fd) {<br \/>\n                while (1) { \/\/ \u5faa\u73af\u63a5\u53d7\u6240\u6709\u7b49\u5f85\u8fde\u63a5 (ET\u6a21\u5f0f\u4e0b&#xff0c;\u5982\u679c\u4e00\u6b21accept\u4e0d\u5b8c&#xff0c;\u4e0b\u6b21\u4e0d\u4f1a\u518d\u901a\u77e5)<br \/>\n                    conn_fd &#061; accept(listen_fd, (struct sockaddr *)&amp;client_addr, &amp;client_len);<br \/>\n                    if (conn_fd &#061;&#061; -1) {<br \/>\n                        if (errno &#061;&#061; EAGAIN || errno &#061;&#061; EWOULDBLOCK) {<br \/>\n                            \/\/ \u6240\u6709\u7b49\u5f85\u8fde\u63a5\u90fd\u5df2\u63a5\u53d7<br \/>\n                            break;<br \/>\n                        } else {<br \/>\n                            perror(&#034;accept \u5931\u8d25&#034;);<br \/>\n                            break;<br \/>\n                        }<br \/>\n                    }<\/p>\n<p>                    char client_ip[INET_ADDRSTRLEN];<br \/>\n                    inet_ntop(AF_INET, &amp;client_addr.sin_addr, client_ip, INET_ADDRSTRLEN);<br \/>\n                    printf(&#034;\u63a5\u53d7\u65b0\u8fde\u63a5&#xff0c;\u6765\u81ea %s:%d (fd: %d)\\\\n&#034;, client_ip, ntohs(client_addr.sin_port), conn_fd);<\/p>\n<p>                    \/\/ \u8bbe\u7f6e\u65b0\u7684\u8fde\u63a5 socket \u4e3a\u975e\u963b\u585e\u6a21\u5f0f<br \/>\n                    set_nonblocking(conn_fd);<\/p>\n<p>                    \/\/ \u5c06\u65b0\u7684\u8fde\u63a5 socket \u6dfb\u52a0\u5230 epoll \u5b9e\u4f8b\u4e2d&#xff0c;\u76d1\u542c\u53ef\u8bfb\u548c\u6302\u65ad\u4e8b\u4ef6&#xff0c;\u5e76\u4f7f\u7528\u8fb9\u7f18\u89e6\u53d1\u6a21\u5f0f<br \/>\n                    event.events &#061; EPOLLIN | EPOLLRDHUP | EPOLLET; \/\/ EPOLLET: \u8fb9\u7f18\u89e6\u53d1\u6a21\u5f0f<br \/>\n                    event.data.fd &#061; conn_fd;<br \/>\n                    if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, conn_fd, &amp;event) &#061;&#061; -1) {<br \/>\n                        perror(&#034;epoll_ctl (ADD conn_fd) \u5931\u8d25&#034;);<br \/>\n                        close(conn_fd); \/\/ \u6dfb\u52a0\u5931\u8d25\u5219\u5173\u95ed\u8fde\u63a5<br \/>\n                    }<br \/>\n                }<br \/>\n            }<br \/>\n            \/\/ \u5982\u679c\u662f\u5df2\u8fde\u63a5\u7684\u5ba2\u6237\u7aef socket \u6709\u4e8b\u4ef6<br \/>\n            else {<br \/>\n                int current_fd &#061; events[i].data.fd;<\/p>\n<p>                \/\/ \u9519\u8bef\u6216\u8fde\u63a5\u5173\u95ed\u4e8b\u4ef6<br \/>\n                if (events[i].events &amp; (EPOLLRDHUP | EPOLLHUP | EPOLLERR)) {<br \/>\n                    \/\/ EPOLLRDHUP: \u5bf9\u7aef\u5173\u95ed\u8fde\u63a5\u6216\u5199\u534a\u90e8\u5173\u95ed<br \/>\n                    \/\/ EPOLLHUP: \u8fde\u63a5\u88ab\u6302\u65ad (\u5bf9\u7aef\u5173\u95ed)<br \/>\n                    \/\/ EPOLLERR: \u9519\u8bef<br \/>\n                    printf(&#034;\u5ba2\u6237\u7aef %d \u65ad\u5f00\u8fde\u63a5\u6216\u53d1\u751f\u9519\u8bef\u3002\\\\n&#034;, current_fd);<br \/>\n                    epoll_ctl(epoll_fd, EPOLL_CTL_DEL, current_fd, NULL); \/\/ \u4ece epoll \u4e2d\u5220\u9664<br \/>\n                    close(current_fd); \/\/ \u5173\u95ed\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n                    continue; \/\/ \u5904\u7406\u4e0b\u4e00\u4e2a\u4e8b\u4ef6<br \/>\n                }<\/p>\n<p>                \/\/ \u53ef\u8bfb\u4e8b\u4ef6<br \/>\n                if (events[i].events &amp; EPOLLIN) {<br \/>\n                    \/\/ ET \u6a21\u5f0f\u4e0b&#xff0c;\u9700\u8981\u5faa\u73af\u8bfb\u53d6\u6240\u6709\u6570\u636e<br \/>\n                    while (1) {<br \/>\n                        memset(buffer, 0, BUFFER_SIZE);<br \/>\n                        ssize_t bytes_read &#061; recv(current_fd, buffer, BUFFER_SIZE, 0);<\/p>\n<p>                        if (bytes_read &#061;&#061; 0) {<br \/>\n                            \/\/ \u5bf9\u7aef\u6b63\u5e38\u5173\u95ed\u8fde\u63a5<br \/>\n                            printf(&#034;\u5ba2\u6237\u7aef %d \u6b63\u5e38\u5173\u95ed\u8fde\u63a5\u3002\\\\n&#034;, current_fd);<br \/>\n                            epoll_ctl(epoll_fd, EPOLL_CTL_DEL, current_fd, NULL);<br \/>\n                            close(current_fd);<br \/>\n                            break; \/\/ \u9000\u51fa\u5f53\u524d\u5ba2\u6237\u7aef\u7684\u8bfb\u53d6\u5faa\u73af<br \/>\n                        } else if (bytes_read &lt; 0) {<br \/>\n                            if (errno &#061;&#061; EAGAIN || errno &#061;&#061; EWOULDBLOCK) {<br \/>\n                                \/\/ \u6ca1\u6709\u66f4\u591a\u6570\u636e\u53ef\u8bfb\u4e86&#xff0c;\u975e\u963b\u585e I\/O \u7684\u6b63\u5e38\u60c5\u51b5<br \/>\n                                \/\/printf(&#034;\u5ba2\u6237\u7aef %d \u6570\u636e\u8bfb\u53d6\u5b8c\u6bd5\u3002\\\\n&#034;, current_fd);<br \/>\n                                break; \/\/ \u9000\u51fa\u5f53\u524d\u5ba2\u6237\u7aef\u7684\u8bfb\u53d6\u5faa\u73af<br \/>\n                            } else {<br \/>\n                                perror(&#034;recv \u9519\u8bef&#034;);<br \/>\n                                epoll_ctl(epoll_fd, EPOLL_CTL_DEL, current_fd, NULL);<br \/>\n                                close(current_fd);<br \/>\n                                break; \/\/ \u9000\u51fa\u5f53\u524d\u5ba2\u6237\u7aef\u7684\u8bfb\u53d6\u5faa\u73af<br \/>\n                            }<br \/>\n                        } else {<br \/>\n                            \/\/ \u6210\u529f\u8bfb\u53d6\u5230\u6570\u636e&#xff0c;\u5e76\u56de\u663e\u7ed9\u5ba2\u6237\u7aef<br \/>\n                            printf(&#034;\u63a5\u6536\u5230\u5ba2\u6237\u7aef %d \u6570\u636e: %s&#034;, current_fd, buffer); \/\/ \u6ce8\u610f\u8fd9\u91cc buffer \u53ef\u80fd\u6ca1\u6709\u4ee5&#039;\\\\0&#039;\u7ed3\u5c3e&#xff0c;\u5982\u679c\u9700\u8981\u6253\u5370&#xff0c;\u6700\u597d\u9650\u5236\u957f\u5ea6<br \/>\n                            \/\/ \u7b80\u5355\u7684\u56de\u663e<br \/>\n                            send(current_fd, buffer, bytes_read, 0);<br \/>\n                        }<br \/>\n                    }<br \/>\n                }<br \/>\n                \/\/ \u5982\u679c\u6709\u53ef\u5199\u4e8b\u4ef6 (\u5982\u679c\u9700\u8981\u53d1\u9001\u5927\u91cf\u6570\u636e\u65f6\u624d\u9700\u8981\u76d1\u542c EPOLLOUT)<br \/>\n                \/\/ if (events[i].events &amp; EPOLLOUT) {<br \/>\n                \/\/     \/\/ \u53ef\u4ee5\u5728\u8fd9\u91cc\u5904\u7406\u6570\u636e\u7684\u53d1\u9001<br \/>\n                \/\/ }<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u5173\u95ed epoll \u5b9e\u4f8b\u548c\u76d1\u542c socket<br \/>\n    close(epoll_fd);<br \/>\n    close(listen_fd);<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p>\u5ba2\u6237\u7aef (client.c)<\/p>\n<p>\u4e3a\u4e86\u6d4b\u8bd5\u4e0a\u9762\u7684 epoll_server.c&#xff0c;\u6211\u4eec\u53ef\u4ee5\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u963b\u585e\u5ba2\u6237\u7aef\u3002\u4f60\u53ef\u4ee5\u6253\u5f00\u591a\u4e2a\u7ec8\u7aef\u8fd0\u884c\u8fd9\u4e2a\u5ba2\u6237\u7aef&#xff0c;\u6a21\u62df\u591a\u5e76\u53d1\u3002<\/p>\n<p>C<\/p>\n<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<\/p>\n<p>#define SERVER_IP &#034;127.0.0.1&#034;<br \/>\n#define PORT 8080<br \/>\n#define BUFFER_SIZE 1024<\/p>\n<p>int main() {<br \/>\n    int sock &#061; 0;<br \/>\n    struct sockaddr_in serv_addr;<br \/>\n    char buffer[BUFFER_SIZE] &#061; {0};<br \/>\n    char message[BUFFER_SIZE] &#061; {0};<\/p>\n<p>    \/\/ \u521b\u5efa socket<br \/>\n    if ((sock &#061; socket(AF_INET, SOCK_STREAM, 0)) &lt; 0) {<br \/>\n        perror(&#034;\u5ba2\u6237\u7aef socket \u521b\u5efa\u5931\u8d25&#034;);<br \/>\n        return EXIT_FAILURE;<br \/>\n    }<\/p>\n<p>    \/\/ \u8bbe\u7f6e\u670d\u52a1\u5668\u5730\u5740<br \/>\n    serv_addr.sin_family &#061; AF_INET;<br \/>\n    serv_addr.sin_port &#061; htons(PORT);<\/p>\n<p>    \/\/ \u5c06 IP \u5730\u5740\u4ece\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f<br \/>\n    if (inet_pton(AF_INET, SERVER_IP, &amp;serv_addr.sin_addr) &lt;&#061; 0) {<br \/>\n        perror(&#034;\u65e0\u6548\u7684 IP \u5730\u5740 \/ \u5730\u5740\u4e0d\u652f\u6301&#034;);<br \/>\n        close(sock);<br \/>\n        return EXIT_FAILURE;<br \/>\n    }<\/p>\n<p>    \/\/ \u8fde\u63a5\u670d\u52a1\u5668<br \/>\n    if (connect(sock, (struct sockaddr *)&amp;serv_addr, sizeof(serv_addr)) &lt; 0) {<br \/>\n        perror(&#034;\u8fde\u63a5\u670d\u52a1\u5668\u5931\u8d25&#034;);<br \/>\n        close(sock);<br \/>\n        return EXIT_FAILURE;<br \/>\n    }<\/p>\n<p>    printf(&#034;\u5df2\u8fde\u63a5\u5230\u670d\u52a1\u5668 %s:%d\\\\n&#034;, SERVER_IP, PORT);<br \/>\n    printf(&#034;\u8bf7\u8f93\u5165\u6d88\u606f (&#039;exit&#039; \u9000\u51fa):\\\\n&#034;);<\/p>\n<p>    while (1) {<br \/>\n        printf(&#034;&gt; &#034;);<br \/>\n        if (fgets(message, BUFFER_SIZE, stdin) &#061;&#061; NULL) { \/\/ \u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u53d6\u4e00\u884c<br \/>\n            break; \/\/ \u8bfb\u53d6\u5931\u8d25\u6216 EOF<br \/>\n        }<\/p>\n<p>        \/\/ \u79fb\u9664\u6362\u884c\u7b26<br \/>\n        message[strcspn(message, &#034;\\\\n&#034;)] &#061; 0;<\/p>\n<p>        if (strcmp(message, &#034;exit&#034;) &#061;&#061; 0) {<br \/>\n            printf(&#034;\u5ba2\u6237\u7aef\u9000\u51fa\u3002\\\\n&#034;);<br \/>\n            break;<br \/>\n        }<\/p>\n<p>        \/\/ \u53d1\u9001\u6d88\u606f<br \/>\n        if (send(sock, message, strlen(message), 0) &#061;&#061; -1) {<br \/>\n            perror(&#034;\u53d1\u9001\u5931\u8d25&#034;);<br \/>\n            break;<br \/>\n        }<\/p>\n<p>        \/\/ \u63a5\u6536\u670d\u52a1\u5668\u56de\u663e<br \/>\n        memset(buffer, 0, BUFFER_SIZE);<br \/>\n        ssize_t bytes_read &#061; recv(sock, buffer, BUFFER_SIZE &#8211; 1, 0); \/\/ \u7559\u4e00\u4e2a\u5b57\u8282\u7ed9\u7a7a\u5b57\u7b26<\/p>\n<p>        if (bytes_read &#061;&#061; 0) {<br \/>\n            printf(&#034;\u670d\u52a1\u5668\u5df2\u5173\u95ed\u8fde\u63a5\u3002\\\\n&#034;);<br \/>\n            break;<br \/>\n        } else if (bytes_read &#061;&#061; -1) {<br \/>\n            perror(&#034;\u63a5\u6536\u5931\u8d25&#034;);<br \/>\n            break;<br \/>\n        } else {<br \/>\n            buffer[bytes_read] &#061; &#039;\\\\0&#039;; \/\/ \u786e\u4fdd\u5b57\u7b26\u4e32\u4ee5\u7a7a\u5b57\u7b26\u7ed3\u5c3e<br \/>\n            printf(&#034;\u6536\u5230\u670d\u52a1\u5668\u56de\u663e: %s\\\\n&#034;, buffer);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    close(sock);<br \/>\n    return 0;<br \/>\n}<\/p>\n<hr \/>\n<h4>\u5982\u4f55\u7f16\u8bd1\u548c\u8fd0\u884c<\/h4>\n<\/p>\n<li>\n<p>\u4fdd\u5b58\u4ee3\u7801&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5c06\u670d\u52a1\u5668\u4ee3\u7801\u4fdd\u5b58\u4e3a epoll_server.c\u3002<\/p>\n<\/li>\n<li>\n<p>\u5c06\u5ba2\u6237\u7aef\u4ee3\u7801\u4fdd\u5b58\u4e3a client.c\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u7f16\u8bd1&#xff1a; \u5728 Linux \u7ec8\u7aef\u4e2d\u4f7f\u7528 gcc \u7f16\u8bd1&#xff1a;<\/p>\n<p>Bash<\/p>\n<p>gcc epoll_server.c -o epoll_server<br \/>\ngcc client.c -o client<\/p>\n<\/li>\n<li>\n<p>\u8fd0\u884c&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5148\u542f\u52a8\u670d\u52a1\u5668&#xff1a;<\/p>\n<p>Bash<\/p>\n<p>.\/epoll_server<\/p>\n<p>\u670d\u52a1\u5668\u4f1a\u663e\u793a &#034;\u670d\u52a1\u5668\u5df2\u542f\u52a8&#xff0c;\u6b63\u5728\u76d1\u542c\u7aef\u53e3 8080&#8230;&#034;<\/p>\n<\/li>\n<li>\n<p>\u518d\u542f\u52a8\u5ba2\u6237\u7aef&#xff1a; \u6253\u5f00\u4e00\u4e2a\u6216\u591a\u4e2a\u65b0\u7684\u7ec8\u7aef\u7a97\u53e3&#xff0c;\u8fd0\u884c\u5ba2\u6237\u7aef&#xff1a;<\/p>\n<p>Bash<\/p>\n<p>.\/client<\/p>\n<p>\u4f60\u53ef\u4ee5\u5728\u6bcf\u4e2a\u5ba2\u6237\u7aef\u7ec8\u7aef\u8f93\u5165\u6d88\u606f&#xff0c;\u5b83\u4eec\u90fd\u4f1a\u88ab\u670d\u52a1\u5668\u63a5\u6536\u5e76\u56de\u663e&#xff0c;\u800c\u670d\u52a1\u5668\u5728\u5904\u7406\u591a\u4e2a\u5ba2\u6237\u7aef\u65f6\u663e\u5f97\u975e\u5e38\u9ad8\u6548&#xff0c;\u8fd9\u5c31\u662f epoll \u7684\u9b45\u529b&#xff01;<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<p>\u901a\u8fc7\u8fd9\u4e2a\u4f8b\u5b50&#xff0c;\u4f60\u53ef\u4ee5\u770b\u5230 epoll \u5728\u670d\u52a1\u5668\u7aef\u662f\u5982\u4f55\u9ad8\u6548\u5730\u7ba1\u7406\u5927\u91cf\u5e76\u53d1\u8fde\u63a5\u7684&#xff0c;\u5b83\u53ea\u5173\u6ce8\u90a3\u4e9b\u771f\u6b63\u6709\u4e8b\u4ef6\u53d1\u751f\u7684\u8fde\u63a5&#xff0c;\u5927\u5927\u63d0\u9ad8\u4e86 I\/O \u541e\u5410\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb438\u6b21\uff0c\u70b9\u8d5e16\u6b21\uff0c\u6536\u85cf12\u6b21\u3002\u8fd9\u6837\u6bd4\u7b2c\u4e00\u79cd\u597d\u4e00\u4e9b\uff0c\u4f46\u5982\u679c\u5ba2\u4eba\u975e\u5e38\u591a\uff0c\u4f60\u8fd8\u662f\u4f1a\u628a\u5927\u91cf\u65f6\u95f4\u82b1\u5728\u201c\u95ee\u201d\u800c\u4e0d\u662f\u201c\u670d\u52a1\u201d\u4e0a\u3002\u5982\u679c\u4f60\u6ca1\u53ca\u65f6\u670d\u52a1\uff0c\u6216\u8005\u53ea\u670d\u52a1\u4e86\u4e00\u534a\uff0c\u5ba2\u4eba\u7b2c\u4e8c\u6b21\u4e3e\u624b\uff0c\u670d\u52a1\u5458\u624d\u4f1a\u518d\u901a\u77e5\u4f60\u3002: \u8fd9\u662f\u4f60\u62ff\u7740\u201c\u540d\u5355\u672c\u201d\u53bb\u767b\u8bb0\u5ba2\u4eba\u4fe1\u606f\uff0c\u544a\u8bc9\u9910\u5385\uff08\u5185\u6838\uff09\u4f60\u5173\u5fc3\u54ea\u4e9b\u684c\u5b50\uff0c\u4ee5\u53ca\u8fd9\u4e9b\u684c\u5b50\u53d1\u751f\u4ec0\u4e48\u4e8b\uff08\u53ef\u8bfb\u3001\u53ef\u5199\uff09\u65f6\u901a\u77e5\u4f60\u3002\u5982\u679c\u4f60\u5e0c\u671b\u5728\u67d0\u4e2a\u4e8b\u4ef6\uff08\u6bd4\u5982\u4e00\u4e2a\u8fde\u63a5\u7684\u8bfb\u4e8b\u4ef6\uff09\u88ab\u89e6\u53d1\u540e\uff0c\u6682\u65f6\u4e0d\u518d\u76d1\u542c\u8be5\u8fde\u63a5\u7684\u5176\u4ed6\u4e8b\u4ef6\uff0c\u76f4\u5230\u4f60\u660e\u786e\u5730\u91cd\u65b0\u542f\u7528\u5b83\uff0c\u53ef\u4ee5\u4f7f\u7528\u3002\u4f60\u53ef\u4ee5\u5728\u6bcf\u4e2a\u5ba2\u6237\u7aef\u7ec8\u7aef\u8f93\u5165\u6d88\u606f\uff0c\u5b83\u4eec\u90fd\u4f1a\u88ab\u670d\u52a1\u5668\u63a5\u6536\u5e76\u56de\u663e\uff0c\u800c\u670d\u52a1\u5668\u5728\u5904\u7406\u591a\u4e2a\u5ba2\u6237\u7aef\u65f6\u663e\u5f97\u975e\u5e38\u9ad8\u6548\uff0c\u8fd9\u5c31\u662f\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":[55,3884,58],"topic":[],"class_list":["post-45616","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-epoll","tag-linux"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>epoll \u8be6\u89e3\uff1a\u9ad8\u6027\u80fd I\/O \u591a\u8def\u590d\u7528\u6280\u672f - \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\/45616.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"epoll \u8be6\u89e3\uff1a\u9ad8\u6027\u80fd I\/O \u591a\u8def\u590d\u7528\u6280\u672f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb438\u6b21\uff0c\u70b9\u8d5e16\u6b21\uff0c\u6536\u85cf12\u6b21\u3002\u8fd9\u6837\u6bd4\u7b2c\u4e00\u79cd\u597d\u4e00\u4e9b\uff0c\u4f46\u5982\u679c\u5ba2\u4eba\u975e\u5e38\u591a\uff0c\u4f60\u8fd8\u662f\u4f1a\u628a\u5927\u91cf\u65f6\u95f4\u82b1\u5728\u201c\u95ee\u201d\u800c\u4e0d\u662f\u201c\u670d\u52a1\u201d\u4e0a\u3002\u5982\u679c\u4f60\u6ca1\u53ca\u65f6\u670d\u52a1\uff0c\u6216\u8005\u53ea\u670d\u52a1\u4e86\u4e00\u534a\uff0c\u5ba2\u4eba\u7b2c\u4e8c\u6b21\u4e3e\u624b\uff0c\u670d\u52a1\u5458\u624d\u4f1a\u518d\u901a\u77e5\u4f60\u3002: \u8fd9\u662f\u4f60\u62ff\u7740\u201c\u540d\u5355\u672c\u201d\u53bb\u767b\u8bb0\u5ba2\u4eba\u4fe1\u606f\uff0c\u544a\u8bc9\u9910\u5385\uff08\u5185\u6838\uff09\u4f60\u5173\u5fc3\u54ea\u4e9b\u684c\u5b50\uff0c\u4ee5\u53ca\u8fd9\u4e9b\u684c\u5b50\u53d1\u751f\u4ec0\u4e48\u4e8b\uff08\u53ef\u8bfb\u3001\u53ef\u5199\uff09\u65f6\u901a\u77e5\u4f60\u3002\u5982\u679c\u4f60\u5e0c\u671b\u5728\u67d0\u4e2a\u4e8b\u4ef6\uff08\u6bd4\u5982\u4e00\u4e2a\u8fde\u63a5\u7684\u8bfb\u4e8b\u4ef6\uff09\u88ab\u89e6\u53d1\u540e\uff0c\u6682\u65f6\u4e0d\u518d\u76d1\u542c\u8be5\u8fde\u63a5\u7684\u5176\u4ed6\u4e8b\u4ef6\uff0c\u76f4\u5230\u4f60\u660e\u786e\u5730\u91cd\u65b0\u542f\u7528\u5b83\uff0c\u53ef\u4ee5\u4f7f\u7528\u3002\u4f60\u53ef\u4ee5\u5728\u6bcf\u4e2a\u5ba2\u6237\u7aef\u7ec8\u7aef\u8f93\u5165\u6d88\u606f\uff0c\u5b83\u4eec\u90fd\u4f1a\u88ab\u670d\u52a1\u5668\u63a5\u6536\u5e76\u56de\u663e\uff0c\u800c\u670d\u52a1\u5668\u5728\u5904\u7406\u591a\u4e2a\u5ba2\u6237\u7aef\u65f6\u663e\u5f97\u975e\u5e38\u9ad8\u6548\uff0c\u8fd9\u5c31\u662f\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/45616.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-29T16:41:38+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=\"7 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/45616.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/45616.html\",\"name\":\"epoll \u8be6\u89e3\uff1a\u9ad8\u6027\u80fd I\/O \u591a\u8def\u590d\u7528\u6280\u672f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-07-29T16:41:38+00:00\",\"dateModified\":\"2025-07-29T16:41:38+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/45616.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/45616.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/45616.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"epoll \u8be6\u89e3\uff1a\u9ad8\u6027\u80fd I\/O \u591a\u8def\u590d\u7528\u6280\u672f\"}]},{\"@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":"epoll \u8be6\u89e3\uff1a\u9ad8\u6027\u80fd I\/O \u591a\u8def\u590d\u7528\u6280\u672f - \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\/45616.html","og_locale":"zh_CN","og_type":"article","og_title":"epoll \u8be6\u89e3\uff1a\u9ad8\u6027\u80fd I\/O \u591a\u8def\u590d\u7528\u6280\u672f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb438\u6b21\uff0c\u70b9\u8d5e16\u6b21\uff0c\u6536\u85cf12\u6b21\u3002\u8fd9\u6837\u6bd4\u7b2c\u4e00\u79cd\u597d\u4e00\u4e9b\uff0c\u4f46\u5982\u679c\u5ba2\u4eba\u975e\u5e38\u591a\uff0c\u4f60\u8fd8\u662f\u4f1a\u628a\u5927\u91cf\u65f6\u95f4\u82b1\u5728\u201c\u95ee\u201d\u800c\u4e0d\u662f\u201c\u670d\u52a1\u201d\u4e0a\u3002\u5982\u679c\u4f60\u6ca1\u53ca\u65f6\u670d\u52a1\uff0c\u6216\u8005\u53ea\u670d\u52a1\u4e86\u4e00\u534a\uff0c\u5ba2\u4eba\u7b2c\u4e8c\u6b21\u4e3e\u624b\uff0c\u670d\u52a1\u5458\u624d\u4f1a\u518d\u901a\u77e5\u4f60\u3002: \u8fd9\u662f\u4f60\u62ff\u7740\u201c\u540d\u5355\u672c\u201d\u53bb\u767b\u8bb0\u5ba2\u4eba\u4fe1\u606f\uff0c\u544a\u8bc9\u9910\u5385\uff08\u5185\u6838\uff09\u4f60\u5173\u5fc3\u54ea\u4e9b\u684c\u5b50\uff0c\u4ee5\u53ca\u8fd9\u4e9b\u684c\u5b50\u53d1\u751f\u4ec0\u4e48\u4e8b\uff08\u53ef\u8bfb\u3001\u53ef\u5199\uff09\u65f6\u901a\u77e5\u4f60\u3002\u5982\u679c\u4f60\u5e0c\u671b\u5728\u67d0\u4e2a\u4e8b\u4ef6\uff08\u6bd4\u5982\u4e00\u4e2a\u8fde\u63a5\u7684\u8bfb\u4e8b\u4ef6\uff09\u88ab\u89e6\u53d1\u540e\uff0c\u6682\u65f6\u4e0d\u518d\u76d1\u542c\u8be5\u8fde\u63a5\u7684\u5176\u4ed6\u4e8b\u4ef6\uff0c\u76f4\u5230\u4f60\u660e\u786e\u5730\u91cd\u65b0\u542f\u7528\u5b83\uff0c\u53ef\u4ee5\u4f7f\u7528\u3002\u4f60\u53ef\u4ee5\u5728\u6bcf\u4e2a\u5ba2\u6237\u7aef\u7ec8\u7aef\u8f93\u5165\u6d88\u606f\uff0c\u5b83\u4eec\u90fd\u4f1a\u88ab\u670d\u52a1\u5668\u63a5\u6536\u5e76\u56de\u663e\uff0c\u800c\u670d\u52a1\u5668\u5728\u5904\u7406\u591a\u4e2a\u5ba2\u6237\u7aef\u65f6\u663e\u5f97\u975e\u5e38\u9ad8\u6548\uff0c\u8fd9\u5c31\u662f\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/45616.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-07-29T16:41:38+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"7 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/45616.html","url":"https:\/\/www.wsisp.com\/helps\/45616.html","name":"epoll \u8be6\u89e3\uff1a\u9ad8\u6027\u80fd I\/O \u591a\u8def\u590d\u7528\u6280\u672f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-07-29T16:41:38+00:00","dateModified":"2025-07-29T16:41:38+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/45616.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/45616.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/45616.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"epoll \u8be6\u89e3\uff1a\u9ad8\u6027\u80fd I\/O \u591a\u8def\u590d\u7528\u6280\u672f"}]},{"@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\/45616","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=45616"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/45616\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=45616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=45616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=45616"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=45616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}