{"id":48359,"date":"2025-07-30T11:38:06","date_gmt":"2025-07-30T03:38:06","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/48359.html"},"modified":"2025-07-30T11:38:06","modified_gmt":"2025-07-30T03:38:06","slug":"30-%e5%a4%a9%e8%87%aa%e5%88%b6-c-%e6%9c%8d%e5%8a%a1%e5%99%a8-day3","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/48359.html","title":{"rendered":"30 \u5929\u81ea\u5236 C++ \u670d\u52a1\u5668--Day3"},"content":{"rendered":"<h4>\u9ad8\u5e76\u53d1\u8fd8\u5f97\u7528epoll<\/h4>\n<p>\u5728\u4e0a\u4e00\u5929&#xff0c;\u6211\u4eec\u5199\u4e86\u4e00\u4e2a\u7b80\u5355\u7684echo\u670d\u52a1\u5668&#xff0c;\u4f46\u53ea\u80fd\u540c\u65f6\u5904\u7406\u4e00\u4e2a\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u3002\u4f46\u5728\u8fd9\u4e2a\u8fde\u63a5\u7684\u751f\u547d\u5468\u671f\u4e2d&#xff0c;\u7edd\u5927\u90e8\u5206\u65f6\u95f4\u90fd\u662f\u7a7a\u95f2\u7684&#xff0c;\u6d3b\u8dc3\u65f6\u95f4&#xff08;\u53d1\u9001\u6570\u636e\u548c\u63a5\u6536\u6570\u636e\u7684\u65f6\u95f4&#xff09;\u5360\u6bd4\u6781\u5c11&#xff0c;\u8fd9\u6837\u72ec\u5360\u4e00\u4e2a\u670d\u52a1\u5668\u662f\u4e25\u91cd\u7684\u8d44\u6e90\u6d6a\u8d39\u3002\u4e8b\u5b9e\u4e0a\u6240\u6709\u7684\u670d\u52a1\u5668\u90fd\u662f\u9ad8\u5e76\u53d1\u7684&#xff0c;\u53ef\u4ee5\u540c\u65f6\u4e3a\u6210\u5343\u4e0a\u4e07\u4e2a\u5ba2\u6237\u7aef\u63d0\u4f9b\u670d\u52a1&#xff0c;\u8fd9\u4e00\u6280\u672f\u53c8\u88ab\u79f0\u4e3aIO\u590d\u7528\u3002<\/p>\n<p>IO\u590d\u7528\u548c\u591a\u7ebf\u7a0b\u6709\u76f8\u4f3c\u4e4b\u5904&#xff0c;\u4f46\u7edd\u4e0d\u662f\u4e00\u4e2a\u6982\u5ff5\u3002IO\u590d\u7528\u662f\u9488\u5bf9IO\u63a5\u53e3&#xff0c;\u800c\u591a\u7ebf\u7a0b\u662f\u9488\u5bf9CPU\u3002<\/p>\n<p>IO\u590d\u7528\u7684\u57fa\u672c\u601d\u60f3\u662f\u4e8b\u4ef6\u9a71\u52a8&#xff0c;\u670d\u52a1\u5668\u540c\u65f6\u4fdd\u6301\u591a\u4e2a\u5ba2\u6237\u7aefIO\u8fde\u63a5&#xff0c;\u5f53\u8fd9\u4e2aIO\u4e0a\u6709\u53ef\u8bfb\u6216\u53ef\u5199\u4e8b\u4ef6\u53d1\u751f\u65f6&#xff0c;\u8868\u793a\u8fd9\u4e2aIO\u5bf9\u5e94\u7684\u5ba2\u6237\u7aef\u5728\u8bf7\u6c42\u670d\u52a1\u5668\u7684\u67d0\u9879\u670d\u52a1&#xff0c;\u6b64\u65f6\u670d\u52a1\u5668\u54cd\u5e94\u8be5\u670d\u52a1\u3002\u5728Linux\u7cfb\u7edf\u4e2d&#xff0c;IO\u590d\u7528\u4f7f\u7528select, poll\u548cepoll\u6765\u5b9e\u73b0\u3002epoll\u6539\u8fdb\u4e86\u524d\u4e24\u8005&#xff0c;\u66f4\u52a0\u9ad8\u6548\u3001\u6027\u80fd\u66f4\u597d&#xff0c;\u662f\u76ee\u524d\u51e0\u4e4e\u6240\u6709\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u7684\u57fa\u77f3\u3002\u8bf7\u8bfb\u8005\u52a1\u5fc5\u5148\u638c\u63e1epoll\u7684\u539f\u7406\u518d\u8fdb\u884c\u7f16\u7801\u5f00\u53d1\u3002<\/p>\n<h4>epoll\u539f\u7406<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730033756-6889939493aec.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730033756-68899394dbea0.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730033757-68899395522fe.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730033758-688993964b5d5.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730033759-6889939772c8c.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730033800-6889939809f4f.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730033800-6889939887334.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n\u603b\u7ed3&#xff1a;<\/p>\n<p>epoll \u7684\u6838\u5fc3\u539f\u7406\u5728\u4e8e\u6539\u53d8\u4e8b\u4ef6\u901a\u77e5\u6a21\u578b&#xff1a;\u5b83\u901a\u8fc7\u5728\u5185\u6838\u4f7f\u7528\u7ea2\u9ed1\u6811\u9ad8\u6548\u7ba1\u7406\u6ce8\u518c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u5229\u7528\u56de\u8c03\u51fd\u6570\u5728\u4e8b\u4ef6\u53d1\u751f\u65f6\u76f4\u63a5\u5c06\u5c31\u7eea\u9879\u653e\u5165\u94fe\u8868&#xff0c;\u5e76\u901a\u8fc7 epoll_wait \u4ec5\u8fd4\u56de\u5b9e\u9645\u5c31\u7eea\u7684\u4e8b\u4ef6\u5217\u8868&#xff0c;\u8f85\u4ee5 mmap \u51cf\u5c11\u6570\u636e\u62f7\u8d1d\u3002\u8fd9\u79cd\u8bbe\u8ba1\u5f7b\u5e95\u89e3\u51b3\u4e86 select\/poll \u5728\u5904\u7406\u5927\u91cf\u6587\u4ef6\u63cf\u8ff0\u7b26\u65f6\u5b58\u5728\u7684\u6027\u80fd\u74f6\u9888&#xff08;\u6bcf\u6b21\u8c03\u7528\u4f20\u9012\u5b8c\u6574\u96c6\u5408\u3001\u7ebf\u6027\u626b\u63cf\u5f00\u9500\u5927&#xff09;&#xff0c;\u4f7f\u5176\u6210\u4e3a\u6784\u5efa\u73b0\u4ee3\u9ad8\u6027\u80fd Linux \u7f51\u7edc\u670d\u52a1\u5668&#xff08;\u5982 Nginx, Redis, Memcached&#xff09;\u4e0d\u53ef\u6216\u7f3a\u7684\u57fa\u7840\u8bbe\u65bd\u3002\u5176\u9ad8\u6548\u7684\u6839\u6e90\u5728\u4e8e\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u4ece O(N) \u964d\u4f4e\u5230\u4e86 O(1)&#xff08;\u6216 O(\u5c31\u7eea\u4e8b\u4ef6\u6570)&#xff09;\u3002<\/p>\n<h4>epoll\u4f7f\u7528<\/h4>\n<p>epoll\u4e3b\u8981\u7531\u4e09\u4e2a\u7cfb\u7edf\u8c03\u7528\u7ec4\u6210&#xff1a;<\/p>\n<p>\/\/int epfd &#061; epoll_create(1024);  \/\/\u53c2\u6570\u8868\u793a\u76d1\u542c\u4e8b\u4ef6\u7684\u5927\u5c0f&#xff0c;\u5982\u8d85\u8fc7\u5185\u6838\u4f1a\u81ea\u52a8\u8c03\u6574&#xff0c;\u5df2\u7ecf\u88ab\u820d\u5f03&#xff0c;\u65e0\u5b9e\u9645\u610f\u4e49&#xff0c;\u4f20\u5165\u4e00\u4e2a\u5927\u4e8e0\u7684\u6570\u5373\u53ef<br \/>\nint epfd &#061; epoll_create1(0);       \/\/\u53c2\u6570\u662f\u4e00\u4e2aflag&#xff0c;\u4e00\u822c\u8bbe\u4e3a0&#xff0c;\u8be6\u7ec6\u53c2\u8003man epoll<\/p>\n<p>\u521b\u5efa\u4e00\u4e2aepoll\u6587\u4ef6\u63cf\u8ff0\u7b26\u5e76\u8fd4\u56de&#xff0c;\u5931\u8d25\u5219\u8fd4\u56de-1\u3002<\/p>\n<p>epoll\u76d1\u542c\u4e8b\u4ef6\u7684\u63cf\u8ff0\u7b26\u4f1a\u653e\u5728\u4e00\u9897\u7ea2\u9ed1\u6811\u4e0a&#xff0c;\u6211\u4eec\u5c06\u8981\u76d1\u542c\u7684IO\u53e3\u653e\u5165epoll\u7ea2\u9ed1\u6811\u4e2d&#xff0c;\u5c31\u53ef\u4ee5\u76d1\u542c\u8be5IO\u4e0a\u7684\u4e8b\u4ef6\u3002<\/p>\n<p>epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &amp;ev);    \/\/\u6dfb\u52a0\u4e8b\u4ef6\u5230epoll<br \/>\nepoll_ctl(epfd, EPOLL_CTL_MOD, sockfd, &amp;ev);    \/\/\u4fee\u6539epoll\u7ea2\u9ed1\u6811\u4e0a\u7684\u4e8b\u4ef6<br \/>\nepoll_ctl(epfd, EPOLL_CTL_DEL, sockfd, NULL);   \/\/\u5220\u9664\u4e8b\u4ef6<\/p>\n<p>\u5176\u4e2dsockfd\u8868\u793a\u6211\u4eec\u8981\u6dfb\u52a0\u7684IO\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;ev\u662f\u4e00\u4e2aepoll_event\u7ed3\u6784\u4f53&#xff0c;\u5176\u4e2d\u7684events\u8868\u793a\u4e8b\u4ef6&#xff0c;\u5982EPOLLIN\u7b49&#xff0c;data\u662f\u4e00\u4e2a\u7528\u6237\u6570\u636eunion:<\/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;<br \/>\nstruct epoll_event {<br \/>\n  uint32_t events;\/* Epoll events *\/<br \/>\n  epoll_data_t data;\/* User data variable *\/<br \/>\n} __EPOLL_PACKED;<\/p>\n<p>epoll\u9ed8\u8ba4\u91c7\u7528LT\u89e6\u53d1\u6a21\u5f0f&#xff0c;\u5373\u6c34\u5e73\u89e6\u53d1&#xff0c;\u53ea\u8981fd\u4e0a\u6709\u4e8b\u4ef6&#xff0c;\u5c31\u4f1a\u4e00\u76f4\u901a\u77e5\u5185\u6838\u3002\u8fd9\u6837\u53ef\u4ee5\u4fdd\u8bc1\u6240\u6709\u4e8b\u4ef6\u90fd\u5f97\u5230\u5904\u7406\u3001\u4e0d\u5bb9\u6613\u4e22\u5931&#xff0c;\u4f46\u53ef\u80fd\u53d1\u751f\u7684\u5927\u91cf\u91cd\u590d\u901a\u77e5\u4e5f\u4f1a\u5f71\u54cdepoll\u7684\u6027\u80fd\u3002\u5982\u4f7f\u7528ET\u6a21\u5f0f&#xff0c;\u5373\u8fb9\u7f18\u89e6\u6cd5&#xff0c;fd\u4ece\u65e0\u4e8b\u4ef6\u5230\u6709\u4e8b\u4ef6\u7684\u53d8\u5316\u4f1a\u901a\u77e5\u5185\u6838\u4e00\u6b21&#xff0c;\u4e4b\u540e\u5c31\u4e0d\u4f1a\u518d\u6b21\u901a\u77e5\u5185\u6838\u3002\u8fd9\u79cd\u65b9\u5f0f\u5341\u5206\u9ad8\u6548&#xff0c;\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u652f\u6301\u7684\u5e76\u53d1\u5ea6&#xff0c;\u4f46\u7a0b\u5e8f\u903b\u8f91\u5fc5\u987b\u4e00\u6b21\u6027\u5f88\u597d\u5730\u5904\u7406\u8be5fd\u4e0a\u7684\u4e8b\u4ef6&#xff0c;\u7f16\u7a0b\u6bd4LT\u66f4\u7e41\u7410\u3002\u6ce8\u610fET\u6a21\u5f0f\u5fc5\u987b\u642d\u914d\u975e\u963b\u585e\u5f0fsocket\u4f7f\u7528\u3002<\/p>\n<h4>\u975e\u963b\u585e\u5f0fsocket\u548c\u963b\u585e\u5f0f<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730033800-68899398e301a.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730033801-68899399191e6.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n\u793a\u4f8b\u4ee3\u7801&#xff1a;<\/p>\n<p>\/\/ \u963b\u585e\u5f0f\u670d\u52a1\u7aef\u4f2a\u4ee3\u7801<br \/>\nint client &#061; accept(server_sock); \/\/ \u963b\u585e\u76f4\u5230\u65b0\u8fde\u63a5<br \/>\nrecv(client, buf); \/\/ \u963b\u585e\u76f4\u5230\u6536\u5230\u6570\u636e<br \/>\nsend(client, response); \/\/ \u963b\u585e\u76f4\u5230\u53d1\u9001\u5b8c\u6210<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730033801-68899399458b8.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730033801-68899399921eb.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u8bbe\u7f6e\u65b9\u6cd5&#xff1a;<\/p>\n<p>\/\/ \u8bbe\u7f6e\u975e\u963b\u585e\u6a21\u5f0f<br \/>\nint flags &#061; fcntl(sockfd, F_GETFL, 0);<br \/>\nfcntl(sockfd, F_SETFL, flags | O_NONBLOCK);<\/p>\n<p>\u5178\u578b\u5de5\u4f5c\u6d41&#xff1a;<\/p>\n<p>epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, EPOLLIN); \/\/ \u6ce8\u518c\u8bfb\u4e8b\u4ef6<\/p>\n<p>while (1) {<br \/>\n    int n &#061; epoll_wait(epfd, events, MAX_EVENTS, -1);<br \/>\n    for (\u6bcf\u4e2a\u5c31\u7eea\u4e8b\u4ef6) {<br \/>\n        if (\u4e8b\u4ef6 &amp; EPOLLIN) {<br \/>\n            while ((len &#061; recv(fd, buf)) &gt; 0) {<br \/>\n                \/\/ \u5904\u7406\u6570\u636e<br \/>\n            }<br \/>\n            if (len &#061;&#061; -1 &amp;&amp; errno &#061;&#061; EAGAIN) break; \/\/ \u6570\u636e\u8bfb\u5b8c<br \/>\n        }<br \/>\n        if (\u4e8b\u4ef6 &amp; EPOLLOUT) {<br \/>\n            \/\/ \u5904\u7406\u53ef\u5199\u4e8b\u4ef6<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\u5173\u952e\u5dee\u5f02\u5bf9\u6bd4&#xff1a;<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730033801-68899399d8ea3.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n\u8fb9\u7f18\u89e6\u53d1 (ET) \u4e0e \u6c34\u5e73\u89e6\u53d1 (LT)<br \/>\n\u5f53\u4f7f\u7528 \u975e\u963b\u585e Socket &#043; epoll \u65f6&#xff0c;\u89e6\u53d1\u6a21\u5f0f\u7684\u9009\u62e9\u81f3\u5173\u91cd\u8981&#xff1a;<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730033802-6889939a32adf.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\nET \u6a21\u5f0f\u6700\u4f73\u5b9e\u8df5&#xff1a;<\/p>\n<p>\/\/ \u8fb9\u7f18\u89e6\u53d1\u5fc5\u987b\u5faa\u73af\u8bfb\u53d6\u76f4\u5230 EAGAIN<br \/>\nwhile (true) {<br \/>\n    ssize_t count &#061; read(fd, buf, sizeof(buf));<br \/>\n    if (count &#061;&#061; -1) {<br \/>\n        if (errno &#061;&#061; EAGAIN) break; \/\/ \u6570\u636e\u5df2\u8bfb\u5b8c<br \/>\n        \/\/ \u5904\u7406\u771f\u5b9e\u9519\u8bef<br \/>\n    }<br \/>\n    if (count &#061;&#061; 0) { \/* \u8fde\u63a5\u5173\u95ed *\/ }<br \/>\n    \/\/ \u5904\u7406\u6570\u636e<br \/>\n}<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730033804-6889939c20954.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730033804-6889939cdeda1.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.2k\u6b21\uff0c\u70b9\u8d5e16\u6b21\uff0c\u6536\u85cf5\u6b21\u3002\u7684\u6838\u5fc3\u539f\u7406\u5728\u4e8e\u6539\u53d8\u4e8b\u4ef6\u901a\u77e5\u6a21\u578b\uff1a\u5b83\u901a\u8fc7\u5728\u5185\u6838\u4f7f\u7528\u7ea2\u9ed1\u6811\u9ad8\u6548\u7ba1\u7406\u6ce8\u518c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u5229\u7528\u56de\u8c03\u51fd\u6570\u5728\u4e8b\u4ef6\u53d1\u751f\u65f6\u76f4\u63a5\u5c06\u5c31\u7eea\u9879\u653e\u5165\u94fe\u8868\uff0c\u5e76\u901a\u8fc7 epoll_wait \u4ec5\u8fd4\u56de\u5b9e\u9645\u5c31\u7eea\u7684\u4e8b\u4ef6\u5217\u8868\uff0c\u8f85\u4ee5 mmap \u51cf\u5c11\u6570\u636e\u62f7\u8d1d\u3002IO\u590d\u7528\u7684\u57fa\u672c\u601d\u60f3\u662f\u4e8b\u4ef6\u9a71\u52a8\uff0c\u670d\u52a1\u5668\u540c\u65f6\u4fdd\u6301\u591a\u4e2a\u5ba2\u6237\u7aefIO\u8fde\u63a5\uff0c\u5f53\u8fd9\u4e2aIO\u4e0a\u6709\u53ef\u8bfb\u6216\u53ef\u5199\u4e8b\u4ef6\u53d1\u751f\u65f6\uff0c\u8868\u793a\u8fd9\u4e2aIO\u5bf9\u5e94\u7684\u5ba2\u6237\u7aef\u5728\u8bf7\u6c42\u670d\u52a1\u5668\u7684\u67d0\u9879\u670d\u52a1\uff0c\u6b64\u65f6\u670d\u52a1\u5668\u54cd\u5e94\u8be5\u670d\u52a1\u3002epoll\u76d1\u542c\u4e8b\u4ef6\u7684\u63cf\u8ff0\u7b26\u4f1a\u653e\u5728\u4e00\u9897\u7ea2\u9ed1\u6811\u4e0a\uff0c\u6211\u4eec\u5c06\u8981\u76d1\u542c\u7684IO\u53e3\u653e\u5165epoll\u7ea2\u9ed1\u6811\u4e2d\uff0c\u5c31\u53ef\u4ee5\u76d1\u542c\u8be5IO\u4e0a\u7684\u4e8b\u4ef6\u3002<\/p>\n","protected":false},"author":2,"featured_media":48344,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[4675,55,43],"topic":[],"class_list":["post-48359","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-30cppserver","tag-c","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>30 \u5929\u81ea\u5236 C++ \u670d\u52a1\u5668-Day3 - \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\/48359.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"30 \u5929\u81ea\u5236 C++ \u670d\u52a1\u5668-Day3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.2k\u6b21\uff0c\u70b9\u8d5e16\u6b21\uff0c\u6536\u85cf5\u6b21\u3002\u7684\u6838\u5fc3\u539f\u7406\u5728\u4e8e\u6539\u53d8\u4e8b\u4ef6\u901a\u77e5\u6a21\u578b\uff1a\u5b83\u901a\u8fc7\u5728\u5185\u6838\u4f7f\u7528\u7ea2\u9ed1\u6811\u9ad8\u6548\u7ba1\u7406\u6ce8\u518c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u5229\u7528\u56de\u8c03\u51fd\u6570\u5728\u4e8b\u4ef6\u53d1\u751f\u65f6\u76f4\u63a5\u5c06\u5c31\u7eea\u9879\u653e\u5165\u94fe\u8868\uff0c\u5e76\u901a\u8fc7 epoll_wait \u4ec5\u8fd4\u56de\u5b9e\u9645\u5c31\u7eea\u7684\u4e8b\u4ef6\u5217\u8868\uff0c\u8f85\u4ee5 mmap \u51cf\u5c11\u6570\u636e\u62f7\u8d1d\u3002IO\u590d\u7528\u7684\u57fa\u672c\u601d\u60f3\u662f\u4e8b\u4ef6\u9a71\u52a8\uff0c\u670d\u52a1\u5668\u540c\u65f6\u4fdd\u6301\u591a\u4e2a\u5ba2\u6237\u7aefIO\u8fde\u63a5\uff0c\u5f53\u8fd9\u4e2aIO\u4e0a\u6709\u53ef\u8bfb\u6216\u53ef\u5199\u4e8b\u4ef6\u53d1\u751f\u65f6\uff0c\u8868\u793a\u8fd9\u4e2aIO\u5bf9\u5e94\u7684\u5ba2\u6237\u7aef\u5728\u8bf7\u6c42\u670d\u52a1\u5668\u7684\u67d0\u9879\u670d\u52a1\uff0c\u6b64\u65f6\u670d\u52a1\u5668\u54cd\u5e94\u8be5\u670d\u52a1\u3002epoll\u76d1\u542c\u4e8b\u4ef6\u7684\u63cf\u8ff0\u7b26\u4f1a\u653e\u5728\u4e00\u9897\u7ea2\u9ed1\u6811\u4e0a\uff0c\u6211\u4eec\u5c06\u8981\u76d1\u542c\u7684IO\u53e3\u653e\u5165epoll\u7ea2\u9ed1\u6811\u4e2d\uff0c\u5c31\u53ef\u4ee5\u76d1\u542c\u8be5IO\u4e0a\u7684\u4e8b\u4ef6\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/48359.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-30T03:38:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730033756-6889939493aec.png\" \/>\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=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/48359.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/48359.html\",\"name\":\"30 \u5929\u81ea\u5236 C++ \u670d\u52a1\u5668-Day3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-07-30T03:38:06+00:00\",\"dateModified\":\"2025-07-30T03:38:06+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/48359.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/48359.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/48359.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"30 \u5929\u81ea\u5236 C++ \u670d\u52a1\u5668--Day3\"}]},{\"@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":"30 \u5929\u81ea\u5236 C++ \u670d\u52a1\u5668-Day3 - \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\/48359.html","og_locale":"zh_CN","og_type":"article","og_title":"30 \u5929\u81ea\u5236 C++ \u670d\u52a1\u5668-Day3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.2k\u6b21\uff0c\u70b9\u8d5e16\u6b21\uff0c\u6536\u85cf5\u6b21\u3002\u7684\u6838\u5fc3\u539f\u7406\u5728\u4e8e\u6539\u53d8\u4e8b\u4ef6\u901a\u77e5\u6a21\u578b\uff1a\u5b83\u901a\u8fc7\u5728\u5185\u6838\u4f7f\u7528\u7ea2\u9ed1\u6811\u9ad8\u6548\u7ba1\u7406\u6ce8\u518c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u5229\u7528\u56de\u8c03\u51fd\u6570\u5728\u4e8b\u4ef6\u53d1\u751f\u65f6\u76f4\u63a5\u5c06\u5c31\u7eea\u9879\u653e\u5165\u94fe\u8868\uff0c\u5e76\u901a\u8fc7 epoll_wait \u4ec5\u8fd4\u56de\u5b9e\u9645\u5c31\u7eea\u7684\u4e8b\u4ef6\u5217\u8868\uff0c\u8f85\u4ee5 mmap \u51cf\u5c11\u6570\u636e\u62f7\u8d1d\u3002IO\u590d\u7528\u7684\u57fa\u672c\u601d\u60f3\u662f\u4e8b\u4ef6\u9a71\u52a8\uff0c\u670d\u52a1\u5668\u540c\u65f6\u4fdd\u6301\u591a\u4e2a\u5ba2\u6237\u7aefIO\u8fde\u63a5\uff0c\u5f53\u8fd9\u4e2aIO\u4e0a\u6709\u53ef\u8bfb\u6216\u53ef\u5199\u4e8b\u4ef6\u53d1\u751f\u65f6\uff0c\u8868\u793a\u8fd9\u4e2aIO\u5bf9\u5e94\u7684\u5ba2\u6237\u7aef\u5728\u8bf7\u6c42\u670d\u52a1\u5668\u7684\u67d0\u9879\u670d\u52a1\uff0c\u6b64\u65f6\u670d\u52a1\u5668\u54cd\u5e94\u8be5\u670d\u52a1\u3002epoll\u76d1\u542c\u4e8b\u4ef6\u7684\u63cf\u8ff0\u7b26\u4f1a\u653e\u5728\u4e00\u9897\u7ea2\u9ed1\u6811\u4e0a\uff0c\u6211\u4eec\u5c06\u8981\u76d1\u542c\u7684IO\u53e3\u653e\u5165epoll\u7ea2\u9ed1\u6811\u4e2d\uff0c\u5c31\u53ef\u4ee5\u76d1\u542c\u8be5IO\u4e0a\u7684\u4e8b\u4ef6\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/48359.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-07-30T03:38:06+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730033756-6889939493aec.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/48359.html","url":"https:\/\/www.wsisp.com\/helps\/48359.html","name":"30 \u5929\u81ea\u5236 C++ \u670d\u52a1\u5668-Day3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-07-30T03:38:06+00:00","dateModified":"2025-07-30T03:38:06+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/48359.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/48359.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/48359.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"30 \u5929\u81ea\u5236 C++ \u670d\u52a1\u5668--Day3"}]},{"@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\/48359","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=48359"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/48359\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/48344"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=48359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=48359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=48359"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=48359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}