{"id":91168,"date":"2026-08-07T07:24:35","date_gmt":"2026-08-06T23:24:35","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/91168.html"},"modified":"2026-08-07T07:24:35","modified_gmt":"2026-08-06T23:24:35","slug":"day-24-25-%e5%b9%b6%e5%8f%91%e6%9c%8d%e5%8a%a1%e5%99%a8-io%e5%a4%8d%e7%94%a8%ef%bc%9a%e8%ae%a9%e4%bd%a0%e7%9a%84%e7%a8%8b%e5%ba%8f%e5%90%8c%e6%97%b6%e6%9c%8d%e5%8a%a1%e6%88%90%e5%8d%83%e4%b8%8a","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/91168.html","title":{"rendered":"Day 24-25 \u5e76\u53d1\u670d\u52a1\u5668 + IO\u590d\u7528\uff1a\u8ba9\u4f60\u7684\u7a0b\u5e8f\u540c\u65f6\u670d\u52a1\u6210\u5343\u4e0a\u4e07\u4e2a\u201c\u5ba2\u4eba\u201c"},"content":{"rendered":"<p>\u5355\u8fde\u63a5\u7684 Echo \u670d\u52a1\u5668\u5c31\u50cf\u4e00\u5bb6\u53ea\u6709\u4e00\u4e2a\u670d\u52a1\u5458\u7684\u9910\u9986\u2014\u2014\u5ba2\u4eba\u6765\u4e86\u5f97\u6392\u961f\u7b49\u3002\u4eca\u5929\u6211\u4eec\u7ed9\u9910\u5385\u62db\u6ee1&#034;\u670d\u52a1\u5458&#034;&#xff08;\u8fdb\u7a0b\/\u7ebf\u7a0b&#xff09;&#xff0c;\u518d\u7ed9\u6bcf\u4e2a\u4eba\u914d\u4e0a\u4e00\u90e8&#034;\u667a\u80fd\u5bf9\u8bb2\u673a&#034;&#xff08;epoll&#xff09;&#xff0c;\u8ba9\u5ba2\u4eba\u4eec\u968f\u5230\u968f\u5403&#xff0c;\u540c\u65f6\u8fd8\u80fd\u76d1\u63a7\u540e\u53a8\u3001\u6536\u94f6\u53f0\u3001\u5916\u5356\u7a97\u53e3&#xff0c;\u771f\u6b63\u5b9e\u73b0\u591a\u9762\u624b\u3002<\/p>\n<hr \/>\n<h3 id=\"\u524d\u8a00\">\u524d\u8a00<\/h3>\n<p>\u5728 Day 22-23 \u4e2d&#xff0c;\u6211\u4eec\u5b9e\u73b0\u4e86\u4e00\u4e2a\u6807\u51c6\u7684 TCP Echo \u670d\u52a1\u5668\u3002\u4f46\u5b83\u6709\u4e00\u4e2a\u81f4\u547d\u7684\u7f3a\u9677&#xff1a;\u4e00\u6b21\u53ea\u80fd\u670d\u52a1\u4e00\u4e2a\u5ba2\u6237\u7aef\u3002<\/p>\n<p>\u901a\u4fd7\u7c7b\u6bd4&#xff1a;\u60f3\u8c61\u4f60\u5f00\u4e86\u4e00\u5bb6\u5916\u5356\u5e97&#xff0c;\u5e97\u91cc\u53ea\u6709\u4f60\u4e00\u4e2a\u4eba\u3002\u4f60\u63a5\u4e86\u4e00\u4e2a\u7535\u8bdd&#xff08;accept&#xff09;&#xff0c;\u7136\u540e\u5f00\u59cb\u7b49\u5bf9\u65b9\u62a5\u83dc\u540d&#xff08;read&#xff09;\u3002\u5982\u679c\u5bf9\u65b9\u78e8\u78e8\u8e6d\u8e6d\u4e0d\u8bf4\u8981\u4ec0\u4e48&#xff0c;\u4f60\u5c31\u4e00\u76f4\u63e1\u7740\u7535\u8bdd\u5e72\u7b49\u2014\u2014\u540e\u9762\u7684\u7535\u8bdd\u6839\u672c\u63a5\u4e0d\u8fdb\u6765\u3002\u8fd9\u5c31\u662f&#034;\u963b\u585e&#034;\u7684\u56f0\u5883\u3002<\/p>\n<p>\u4eca\u5929&#xff0c;\u6211\u4eec\u5206\u4e09\u6b65\u8de8\u8d8a\u8fd9\u9053\u574e&#xff1a;<\/p>\n<li>\u591a\u8fdb\u7a0b\u670d\u52a1\u5668&#xff08;Fork&#xff09;&#xff1a;\u6bcf\u6765\u4e00\u4e2a\u5ba2\u4eba&#xff0c;\u5c31\u96c7\u4e00\u4e2a\u4e34\u65f6\u5de5\u53bb\u670d\u52a1\u5b83\u3002<\/li>\n<li>\u591a\u7ebf\u7a0b\u670d\u52a1\u5668&#xff08;Pthread&#xff09;&#xff1a;\u4e0d\u7528\u96c7\u4eba&#xff0c;\u7528&#034;\u5206\u8eab\u672f&#034;&#xff08;\u7ebf\u7a0b&#xff09;&#xff0c;\u6bcf\u4e2a\u5206\u8eab\u53bb\u8ddf\u4e00\u4e2a\u5ba2\u4eba\u804a\u5929\u3002<\/li>\n<li>IO \u590d\u7528\u6280\u672f&#xff08;select \/ poll \/ epoll&#xff09;&#xff1a;\u4e0d\u96c7\u4eba\u3001\u4e0d\u5206\u8eab&#xff0c;\u7ed9\u4e00\u4e2a\u4eba\u914d\u4e0a\u4e00\u53f0&#034;\u591a\u9891\u9053\u5bf9\u8bb2\u673a&#034;&#xff0c;\u540c\u65f6\u76d1\u63a7\u6240\u6709\u5ba2\u4eba&#xff0c;\u8c01\u5f00\u53e3\u5c31\u8ddf\u8c01\u804a\u2014\u2014\u4e00\u4e2a\u670d\u52a1\u5458\u641e\u5b9a\u5168\u573a\u3002<\/li>\n<hr \/>\n<h3 id=\"\u7b2c\u4e00\u90e8\u5206\u4e3a\u4ec0\u4e48\u5355\u7ebf\u7a0b\u5355\u8fdb\u7a0b\u670d\u52a1\u5668\u4e0d\u884c\">\u7b2c\u4e00\u90e8\u5206&#xff1a;\u4e3a\u4ec0\u4e48\u5355\u7ebf\u7a0b\/\u5355\u8fdb\u7a0b\u670d\u52a1\u5668\u4e0d\u884c&#xff1f;<\/h3>\n<p>\u6211\u4eec\u5148\u56de\u987e\u4e00\u4e0b\u5355\u7ebf\u7a0b\u670d\u52a1\u5668\u7684\u6838\u5fc3\u4ee3\u7801&#xff1a;<\/p>\n<p>while (1) {<br \/>\n    client_fd &#061; accept(server_fd, &#8230;);   \/\/ \u963b\u585e\u7b49\u5f85&#xff1a;\u6ca1\u4eba\u6765\u5c31\u4e00\u76f4\u7b49<br \/>\n    \/\/ \u26a0\ufe0f \u53ea\u6709\u5f53\u524d\u5ba2\u6237\u7aef\u65ad\u5f00\u540e&#xff0c;\u624d\u80fd\u63a5\u4e0b\u4e00\u4e2a&#xff01;<br \/>\n    while ((n &#061; read(client_fd, buffer, &#8230;)) &gt; 0) {<br \/>\n        write(client_fd, buffer, n);      \/\/ \u963b\u585e\u7b49\u5f85&#xff1a;\u5bf9\u65b9\u6ca1\u53d1\u9001\u6570\u636e\u5c31\u4e00\u76f4\u7b49<br \/>\n    }<br \/>\n    close(client_fd);<br \/>\n}<\/p>\n<h4 id=\"11-\u963b\u585e\u5230\u5e95\u662f\u4ec0\u4e48\">1.1 &#034;\u963b\u585e&#034;\u5230\u5e95\u662f\u4ec0\u4e48&#xff1f;<\/h4>\n<p>\u901a\u4fd7\u7c7b\u6bd4&#xff1a;\u4f60\u53bb\u53d6\u6b3e\u673a\u53d6\u94b1&#xff0c;\u524d\u9762\u7684\u4eba\u64cd\u4f5c\u5f88\u6162\u3002\u4f60\u4e0d\u80fd\u540c\u65f6\u5e2e\u540e\u9762\u7684\u4eba\u53d6\u94b1&#xff0c;\u53ea\u80fd\u7ad9\u7740\u5e72\u7b49\u3002\u8fd9\u5c31\u662f\u963b\u585e\u2014\u2014\u4f60&#xff08;\u8fdb\u7a0b&#xff09;\u88ab&#034;\u5361&#034;\u4f4f\u4e86\u3002<\/p>\n<p>\u4ece\u64cd\u4f5c\u7cfb\u7edf\u89d2\u5ea6\u770b&#xff1a;<\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">\u72b6\u6001<\/td>\n<td style=\"background-color:#dddddd\">\u8fdb\u7a0b\u5728\u5e72\u561b<\/td>\n<td style=\"background-color:#dddddd\">\u5360CPU\u5417<\/td>\n<\/tr>\n<tr>\n<td>\u8fd0\u884c\u6001<\/td>\n<td>\u6b63\u5728CPU\u4e0a\u6267\u884c\u4ee3\u7801<\/td>\n<td>\u5360<\/td>\n<\/tr>\n<tr>\n<td>\u5c31\u7eea\u6001<\/td>\n<td>\u51c6\u5907\u597d\u4e86&#xff0c;\u7b49CPU\u8f6e\u5230\u6211<\/td>\n<td>\u4e0d\u5360<\/td>\n<\/tr>\n<tr>\n<td>\u963b\u585e\u6001&#xff08;\u7761\u7720\u6001&#xff09;<\/td>\n<td>\u5728\u7b49\u67d0\u4e2a\u6761\u4ef6&#xff08;\u6570\u636e\u5230\u8fbe\u3001\u8fde\u63a5\u5230\u6765&#xff09;&#xff0c;\u6761\u4ef6\u6ee1\u8db3\u524d\u65e0\u6cd5\u8fd0\u884c<\/td>\n<td>\u4e0d\u5360<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5173\u952e\u7406\u89e3&#xff1a;\u963b\u585e\u5e76\u4e0d\u4f1a\u6d6a\u8d39 CPU&#xff08;CPU \u4f1a\u53bb\u6267\u884c\u5176\u4ed6\u8fdb\u7a0b&#xff09;&#xff0c;\u4f46\u5b83\u6d6a\u8d39\u4e86\u673a\u4f1a\u2014\u2014\u56e0\u4e3a\u8be5\u8fdb\u7a0b\u65e0\u6cd5\u53bb\u5904\u7406\u5176\u4ed6\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u3002\u8fd9\u5c31\u662f\u5e76\u53d1\u8981\u89e3\u51b3\u7684\u6839\u672c\u77db\u76fe\u3002<\/p>\n<hr \/>\n<h3 id=\"\u7b2c\u4e8c\u90e8\u5206\u591a\u8fdb\u7a0b\u5e76\u53d1\u670d\u52a1\u5668fork-\u6a21\u578b\">\u7b2c\u4e8c\u90e8\u5206&#xff1a;\u591a\u8fdb\u7a0b\u5e76\u53d1\u670d\u52a1\u5668&#xff08;Fork \u6a21\u578b&#xff09;<\/h3>\n<h4 id=\"21-\u6838\u5fc3\u601d\u60f3\u4e00\u5f20\u56fe\u770b\u61c2\">2.1 \u6838\u5fc3\u601d\u60f3\u2014\u2014\u4e00\u5f20\u56fe\u770b\u61c2<\/h4>\n<p>\u7236\u8fdb\u7a0b&#xff08;\u8fce\u5bbe\u5458&#xff09;<br \/>\n  \u2502<br \/>\n  \u251c\u2500\u2500 accept() \u2190 \u6765\u5ba2\u4eba\u4e86&#xff01;<br \/>\n  \u2502<br \/>\n  \u251c\u2500\u2500 fork() \u2192 \u5b50\u8fdb\u7a0b1&#xff08;\u670d\u52a1\u54581&#xff09;\u2192 \u670d\u52a1\u5ba2\u6237A \u2192 \u9000\u51fa<br \/>\n  \u2502<br \/>\n  \u251c\u2500\u2500 accept() \u2190 \u53c8\u6765\u4e00\u4e2a&#xff01;<br \/>\n  \u2502<br \/>\n  \u251c\u2500\u2500 fork() \u2192 \u5b50\u8fdb\u7a0b2&#xff08;\u670d\u52a1\u54582&#xff09;\u2192 \u670d\u52a1\u5ba2\u6237B \u2192 \u9000\u51fa<br \/>\n  &#8230;<\/p>\n<p>\u901a\u4fd7\u7c7b\u6bd4&#xff1a;\u94f6\u884c\u5927\u5802\u7ecf\u7406&#xff08;\u7236\u8fdb\u7a0b&#xff09;\u53ea\u7ba1\u63a5\u5f85\u548c\u5206\u6d41&#xff0c;\u6bcf\u6765\u4e00\u4e2a\u5ba2\u6237\u5c31\u558a\u4e00\u4e2a\u67dc\u5458&#xff08;\u5b50\u8fdb\u7a0b&#xff09;\u4e13\u95e8\u670d\u52a1\u3002\u7ecf\u7406\u81ea\u5df1\u9a6c\u4e0a\u56de\u5934\u63a5\u5f85\u4e0b\u4e00\u4f4d\u3002<\/p>\n<h4 id=\"22-\u73af\u5883\u51c6\u5907\">2.2 \u73af\u5883\u51c6\u5907<\/h4>\n<p>\u7b2c 1 \u6b65 \u2192\u00a0\u786e\u8ba4\u4f60\u7684 Linux \u73af\u5883\u5df2\u7ecf\u88c5\u597d gcc&#xff1a;<\/p>\n<p># \u68c0\u67e5 gcc \u662f\u5426\u5b89\u88c5<br \/>\ngcc &#8211;version<\/p>\n<p># \u5982\u679c\u6ca1\u88c5&#xff08;Ubuntu\/Debian&#xff09;<br \/>\nsudo apt install gcc -y<\/p>\n<p># \u5982\u679c\u6ca1\u88c5&#xff08;CentOS\/RHEL&#xff09;<br \/>\nsudo yum install gcc -y<\/p>\n<p>\u7b2c 2 \u6b65 \u2192\u00a0\u521b\u5efa\u9879\u76ee\u76ee\u5f55\u5e76\u8fdb\u5165&#xff1a;<\/p>\n<p>mkdir -p ~\/linux_study\/day24-25 &amp;&amp; cd ~\/linux_study\/day24-25<\/p>\n<h4 id=\"23-\u7f16\u5199\u4ee3\u7801\">2.3 \u7f16\u5199\u4ee3\u7801<\/h4>\n<p>\u7b2c 3 \u6b65 \u2192\u00a0\u7528\u7f16\u8f91\u5668\u521b\u5efa\u00a0fork_server.c&#xff1a;<\/p>\n<p>vim fork_server.c   # \u4e5f\u53ef\u7528 nano \u6216 gedit<\/p>\n<p>\u7b2c 4 \u6b65 \u2192\u00a0\u8f93\u5165\u5b8c\u6574\u4ee3\u7801&#xff08;\u4e0b\u9762\u6bcf\u4e00\u884c\u00a0\/\/\u00a0\u6ce8\u91ca\u90fd\u8bf7\u4f60\u8ba4\u771f\u8bfb&#xff0c;\u8fd9\u4e9b\u6ce8\u91ca\u5c31\u662f\u4f60\u7684&#034;\u4e2d\u6587\u8bf4\u660e\u4e66&#034;&#xff09;&#xff1a;<\/p>\n<p>#include &lt;stdio.h&gt;       \/\/ \u6807\u51c6\u8f93\u5165\u8f93\u51fa&#xff0c;printf \u9700\u8981<br \/>\n#include &lt;stdlib.h&gt;      \/\/ \u6807\u51c6\u5e93&#xff0c;exit \u9700\u8981<br \/>\n#include &lt;string.h&gt;      \/\/ \u5b57\u7b26\u4e32\u51fd\u6570&#xff0c;memset \u9700\u8981<br \/>\n#include &lt;unistd.h&gt;      \/\/ POSIX API&#xff0c;fork \/ close \/ read \/ write \u9700\u8981<br \/>\n#include &lt;sys\/socket.h&gt;  \/\/ \u5957\u63a5\u5b57\u51fd\u6570&#xff0c;socket \/ bind \/ listen \/ accept \u9700\u8981<br \/>\n#include &lt;netinet\/in.h&gt;  \/\/ sockaddr_in \u7ed3\u6784\u4f53&#xff0c;\u5b58\u50a8 IP \u548c\u7aef\u53e3<br \/>\n#include &lt;arpa\/inet.h&gt;   \/\/ inet_ntoa&#xff0c;\u628aIP\u7684\u6570\u5b57\u683c\u5f0f\u8f6c\u6210\u53ef\u8bfb\u5b57\u7b26\u4e32<br \/>\n#include &lt;sys\/wait.h&gt;    \/\/ waitpid&#xff0c;\u56de\u6536\u5b50\u8fdb\u7a0b\u7528<br \/>\n#include &lt;signal.h&gt;      \/\/ signal&#xff0c;\u6ce8\u518c\u4fe1\u53f7\u5904\u7406\u51fd\u6570<\/p>\n<p>#define PORT 8080         \/\/ \u670d\u52a1\u5668\u76d1\u542c\u7aef\u53e3&#xff0c;\u53ef\u6539\u6210\u4f60\u559c\u6b22\u7684<br \/>\n#define BUFFER_SIZE 1024  \/\/ \u63a5\u6536\u7f13\u51b2\u533a\u5927\u5c0f&#xff0c;1KB \u591f\u7528<\/p>\n<p>\/\/ \u4fe1\u53f7\u5904\u7406\u51fd\u6570&#xff1a;\u5f53\u5b50\u8fdb\u7a0b\u9000\u51fa\u65f6&#xff0c;\u5185\u6838\u53d1 SIGCHLD \u7ed9\u7236\u8fdb\u7a0b<br \/>\n\/\/ \u5982\u679c\u4e0d\u5904\u7406&#xff0c;\u5b50\u8fdb\u7a0b\u4f1a\u53d8\u6210&#034;\u50f5\u5c38&#034;&#xff08;\u5360\u7528\u7cfb\u7edf PID \u540d\u989d&#xff09;<br \/>\nvoid sigchld_handler(int sig) {<br \/>\n    \/\/ waitpid&#xff1a;\u56de\u6536\u6240\u6709\u5df2\u9000\u51fa\u7684\u5b50\u8fdb\u7a0b<br \/>\n    \/\/ -1 \u8868\u793a&#034;\u56de\u6536\u4efb\u610f\u5b50\u8fdb\u7a0b&#034;&#xff0c;WNOHANG \u8868\u793a&#034;\u4e0d\u963b\u585e&#xff0c;\u6ca1\u6b7b\u5c31\u8fd4\u56de&#034;<br \/>\n    while (waitpid(-1, NULL, WNOHANG) &gt; 0);<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    int server_fd, client_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>    \/\/ \u2460 \u6ce8\u518c\u4fe1\u53f7\u5904\u7406\u2014\u2014\u5c31\u50cf\u7ed9\u7236\u8fdb\u7a0b\u88c5\u4e86\u4e2a&#034;\u5b50\u8fdb\u7a0b\u53bb\u4e16\u901a\u77e5\u94c3&#034;<br \/>\n    signal(SIGCHLD, sigchld_handler);<\/p>\n<p>    \/\/ \u2461 \u521b\u5efa\u5957\u63a5\u5b57&#xff08;socket&#xff1a;\u5728\u7cfb\u7edf\u4e2d\u5360\u4e00\u4e2a&#034;\u7535\u8bdd\u53f7\u7801&#034;&#xff09;<br \/>\n    server_fd &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n    \/\/ AF_INET&#xff1a;\u4f7f\u7528 IPv4<br \/>\n    \/\/ SOCK_STREAM&#xff1a;\u4f7f\u7528 TCP&#xff08;\u53ef\u9760\u3001\u6709\u8fde\u63a5&#xff09;<br \/>\n    \/\/ 0&#xff1a;\u534f\u8bae\u81ea\u52a8\u9009\u62e9&#xff08;TCP \u5c31\u662f IPPROTO_TCP&#xff09;<\/p>\n<p>    \/\/ \u2462 \u8bbe\u7f6e\u7aef\u53e3\u590d\u7528\u2014\u2014\u9632\u6b62\u91cd\u542f\u65f6 &#034;Address already in use&#034; \u62a5\u9519<br \/>\n    int opt &#061; 1;<br \/>\n    setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &amp;opt, sizeof(opt));<\/p>\n<p>    \/\/ \u2463 \u914d\u7f6e\u670d\u52a1\u5668\u5730\u5740&#xff08;\u8eab\u4efd\u8bc1\u4fe1\u606f&#xff09;<br \/>\n    memset(&amp;server_addr, 0, sizeof(server_addr));  \/\/ \u5148\u6e05\u96f6&#xff0c;\u9632\u6b62\u5783\u573e\u6570\u636e<br \/>\n    server_addr.sin_family &#061; AF_INET;              \/\/ \u5730\u5740\u5bb6\u65cf&#xff1a;IPv4<br \/>\n    server_addr.sin_port &#061; htons(PORT);            \/\/ \u7aef\u53e3\u53f7&#xff0c;htons \u628a\u5b57\u8282\u5e8f\u8f6c\u6210\u7f51\u7edc\u5e8f<br \/>\n    server_addr.sin_addr.s_addr &#061; INADDR_ANY;      \/\/ \u76d1\u542c\u672c\u673a\u6240\u6709\u7f51\u5361&#xff08;0.0.0.0&#xff09;<\/p>\n<p>    \/\/ \u2464 \u7ed1\u5b9a\u2014\u2014\u628a&#034;\u7535\u8bdd\u53f7\u7801&#034;\u548c&#034;\u8eab\u4efd\u8bc1&#034;\u7ed1\u5728\u4e00\u8d77<br \/>\n    bind(server_fd, (struct sockaddr *)&amp;server_addr, sizeof(server_addr));<\/p>\n<p>    \/\/ \u2465 \u5f00\u59cb\u76d1\u542c\u2014\u2014\u50cf\u9910\u5385\u6253\u5f00\u95e8\u5f00\u59cb\u8fce\u5ba2&#xff0c;5 \u662f\u6392\u961f\u4e0a\u9650<br \/>\n    listen(server_fd, 5);<br \/>\n    printf(&#034;\u591a\u8fdb\u7a0b\u670d\u52a1\u5668\u542f\u52a8&#xff0c;PID&#061;%d&#xff0c;\u76d1\u542c\u7aef\u53e3 %d\\\\n&#034;, getpid(), PORT);<\/p>\n<p>    \/\/ \u2466 \u4e3b\u5faa\u73af&#xff1a;\u4e0d\u505c\u63a5\u5ba2&#xff0c;\u6bcf\u63a5\u4e00\u4f4d\u5c31\u558a\u4e00\u4e2a\u65b0\u67dc\u5458&#xff08;fork&#xff09;<br \/>\n    while (1) {<br \/>\n        client_fd &#061; accept(server_fd,<br \/>\n                          (struct sockaddr *)&amp;client_addr,<br \/>\n                          &amp;client_len);<br \/>\n        if (client_fd &lt; 0) {<br \/>\n            perror(&#034;accept&#034;);  \/\/ \u6253\u5370\u9519\u8bef\u539f\u56e0<br \/>\n            continue;          \/\/ \u51fa\u9519\u4e5f\u522b\u5d29\u6e83&#xff0c;\u7ee7\u7eed\u7b49\u4e0b\u4e00\u4f4d<br \/>\n        }<\/p>\n<p>        pid_t pid &#061; fork();<br \/>\n        if (pid &#061;&#061; 0) {<br \/>\n            \/\/ &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u5b50\u8fdb\u7a0b&#xff1a;\u6211\u5c31\u662f\u65b0\u67dc\u5458 &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n            close(server_fd);  \/\/ \u5b50\u8fdb\u7a0b\u4e0d\u8fce\u5ba2&#xff0c;\u5173\u6389\u76d1\u542c\u5957\u63a5\u5b57<\/p>\n<p>            printf(&#034;\u5b50\u8fdb\u7a0b %d \u5f00\u59cb\u670d\u52a1 %s:%d\\\\n&#034;,<br \/>\n                   getpid(),<br \/>\n                   inet_ntoa(client_addr.sin_addr),   \/\/ IP \u5730\u5740<br \/>\n                   ntohs(client_addr.sin_port));       \/\/ \u7aef\u53e3\u53f7<\/p>\n<p>            \/\/ Echo&#xff1a;\u6536\u5230\u4ec0\u4e48\u5c31\u56de\u4ec0\u4e48&#xff08;\u50cf\u590d\u8bfb\u673a&#xff09;<br \/>\n            int n;<br \/>\n            while ((n &#061; read(client_fd, buffer, sizeof(buffer) &#8211; 1)) &gt; 0) {<br \/>\n                buffer[n] &#061; &#039;\\\\0&#039;;                     \/\/ \u624b\u52a8\u52a0\u5b57\u7b26\u4e32\u7ed3\u5c3e<br \/>\n                printf(&#034;\u5b50\u8fdb\u7a0b %d \u6536\u5230: %s&#034;, getpid(), buffer);<br \/>\n                write(client_fd, buffer, n);          \/\/ \u539f\u6837\u53d1\u56de<br \/>\n            }<\/p>\n<p>            close(client_fd);<br \/>\n            printf(&#034;\u5b50\u8fdb\u7a0b %d \u4e0b\u73ed\\\\n&#034;, getpid());<br \/>\n            exit(0);  \/\/ \u5b50\u8fdb\u7a0b\u4efb\u52a1\u5b8c\u6210&#xff0c;\u4f18\u96c5\u9000\u51fa<\/p>\n<p>        } else if (pid &gt; 0) {<br \/>\n            \/\/ &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u7236\u8fdb\u7a0b&#xff1a;\u8fce\u5bbe\u5458\u7ee7\u7eed\u8fce\u5ba2 &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n            close(client_fd);  \/\/ \u7236\u8fdb\u7a0b\u4e0d\u670d\u52a1\u5ba2\u6237&#xff0c;\u5173\u6389\u5ba2\u6237\u5957\u63a5\u5b57<br \/>\n            \/\/ \u7ee7\u7eed\u5faa\u73af&#xff0c;\u7b49\u4e0b\u4e00\u4f4d<\/p>\n<p>        } else {<br \/>\n            perror(&#034;fork&#034;);    \/\/ fork \u5931\u8d25&#xff08;\u6bd4\u5982\u7cfb\u7edf\u8fdb\u7a0b\u6570\u6ee1\u4e86&#xff09;<br \/>\n            close(client_fd);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    close(server_fd);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u7b2c 5 \u6b65 \u2192\u00a0\u7f16\u8bd1\u8fd0\u884c&#xff1a;<\/p>\n<p># \u7f16\u8bd1&#xff08;\u591a\u7ebf\u7a0b\u7248\u672c\u9700\u8981 -lpthread&#xff0c;\u591a\u8fdb\u7a0b\u4e0d\u9700\u8981\u989d\u5916\u94fe\u63a5\u5e93&#xff09;<br \/>\ngcc -Wall -g fork_server.c -o fork_server<\/p>\n<p># \u8fd0\u884c<br \/>\n.\/fork_server<\/p>\n<p>\u770b\u5230\u8f93\u51fa\u00a0\u591a\u8fdb\u7a0b\u670d\u52a1\u5668\u542f\u52a8&#xff0c;PID&#061;xxxxx&#xff0c;\u76d1\u542c\u7aef\u53e3 8080\u00a0\u5c31\u8bf4\u660e\u6210\u529f\u3002<\/p>\n<p>\u7b2c 6 \u6b65 \u2192\u00a0\u6d4b\u8bd5&#xff1a;\u5728\u53e6\u4e00\u4e2a\u7ec8\u7aef\u7528 telnet \u8fde\u63a5&#xff1a;<\/p>\n<p># \u53e6\u5f00\u4e00\u4e2a\u7ec8\u7aef<br \/>\ntelnet 127.0.0.1 8080<\/p>\n<p># \u8f93\u5165\u4efb\u610f\u6587\u5b57&#xff0c;\u56de\u8f66&#xff0c;\u670d\u52a1\u5668\u4f1a\u539f\u6837\u8fd4\u56de<br \/>\nhello  # \u4f60\u4f1a\u770b\u5230\u670d\u52a1\u5668\u56de\u663e &#034;hello&#034;<\/p>\n<p>\u518d\u5f00\u7b2c3\u4e2a\u7ec8\u7aef\u91cd\u590d\u4e0a\u8ff0\u64cd\u4f5c\u2014\u2014\u4f60\u4f1a\u53d1\u73b0\u6bcf\u4e2a\u5ba2\u6237\u7aef\u90fd\u88ab\u72ec\u7acb\u670d\u52a1&#xff0c;\u4e92\u4e0d\u5f71\u54cd\u3002<\/p>\n<h4 id=\"24-\u591a\u8fdb\u7a0b\u6a21\u578b\u5173\u952e\u70b9\u901f\u67e5\">2.4 \u591a\u8fdb\u7a0b\u6a21\u578b\u5173\u952e\u70b9\u901f\u67e5<\/h4>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">\u5173\u952e\u70b9<\/td>\n<td style=\"background-color:#dddddd\">\u4e3a\u4ec0\u4e48\u5fc5\u987b\u8fd9\u6837\u505a<\/td>\n<td style=\"background-color:#dddddd\">\u4e0d\u505a\u7684\u540e\u679c<\/td>\n<\/tr>\n<tr>\n<td>\u6ce8\u518c\u00a0SIGCHLD\u00a0\u4fe1\u53f7<\/td>\n<td>\u5b50\u8fdb\u7a0b\u9000\u51fa\u65f6\u901a\u77e5\u7236\u8fdb\u7a0b\u56de\u6536<\/td>\n<td>\u50f5\u5c38\u8fdb\u7a0b\u5806\u79ef&#xff0c;ps aux\u00a0\u663e\u793a\u5927\u91cf\u00a0&lt;defunct&gt;<\/td>\n<\/tr>\n<tr>\n<td>\u7236\u8fdb\u7a0b\u00a0close(client_fd)<\/td>\n<td>\u5b50\u8fdb\u7a0b\u00a0fork\u00a0\u540e\u590d\u5236\u4e86\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u7236\u8fdb\u7a0b\u4e0d\u5173\u4f1a\u5bfc\u81f4\u5f15\u7528\u8ba1\u6570 &gt; 1<\/td>\n<td>\u5b50\u8fdb\u7a0b\u9000\u51fa\u540e\u8fde\u63a5\u53ef\u80fd\u65e0\u6cd5\u5b8c\u5168\u91ca\u653e<\/td>\n<\/tr>\n<tr>\n<td>\u5b50\u8fdb\u7a0b\u00a0close(server_fd)<\/td>\n<td>\u5b50\u8fdb\u7a0b\u4e0d\u9700\u8981\u76d1\u542c\u65b0\u8fde\u63a5<\/td>\n<td>\u8d44\u6e90\u6cc4\u6f0f&#xff0c;\u4e14\u53ef\u80fd\u5e72\u6270\u7236\u8fdb\u7a0b<\/td>\n<\/tr>\n<tr>\n<td>\u8d44\u6e90\u5f00\u9500\u8ba4\u77e5<\/td>\n<td>\u6bcf\u4e2a\u5ba2\u6237\u7aef &#061; \u4e00\u4e2a\u72ec\u7acb\u8fdb\u7a0b&#xff08;\u5185\u5b58\u3001\u9875\u8868\u3001PID&#xff09;<\/td>\n<td>\u51e0\u767e\u4e2a\u5ba2\u6237\u7aef\u53ef\u80fd\u5c31\u6491\u4e0d\u4f4f\u4e86<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u26a0\ufe0f\u00a0\u9884\u8b66&#xff1a;\u5982\u679c\u4f60\u5fd8\u4e86\u6ce8\u518c\u00a0SIGCHLD&#xff0c;\u8fd0\u884c\u4e00\u6bb5\u65f6\u95f4\u540e\u00a0ps aux | grep fork_server\u00a0\u4f1a\u53d1\u73b0\u4e00\u5806\u00a0&lt;defunct&gt;\u00a0\u50f5\u5c38\u8fdb\u7a0b\u3002\u5b83\u4eec\u5360\u7740 PID \u4e0d\u653e&#xff0c;\u6700\u7ec8\u53ef\u80fd\u5bfc\u81f4\u7cfb\u7edf\u65e0\u6cd5\u521b\u5efa\u65b0\u8fdb\u7a0b\u3002<\/p>\n<hr \/>\n<h3 id=\"\u7b2c\u4e09\u90e8\u5206\u591a\u7ebf\u7a0b\u5e76\u53d1\u670d\u52a1\u5668pthread-\u6a21\u578b\">\u7b2c\u4e09\u90e8\u5206&#xff1a;\u591a\u7ebf\u7a0b\u5e76\u53d1\u670d\u52a1\u5668&#xff08;Pthread \u6a21\u578b&#xff09;<\/h3>\n<h4 id=\"31-\u6838\u5fc3\u601d\u60f3\u4e00\u5f20\u56fe\u770b\u61c2\">3.1 \u6838\u5fc3\u601d\u60f3\u2014\u2014\u4e00\u5f20\u56fe\u770b\u61c2<\/h4>\n<p>\u4e3b\u7ebf\u7a0b&#xff08;\u8fce\u5bbe\u5458&#xff09;<br \/>\n  \u2502<br \/>\n  \u251c\u2500\u2500 accept() \u2190 \u6765\u5ba2\u4eba\u4e86&#xff01;<br \/>\n  \u2502<br \/>\n  \u251c\u2500\u2500 pthread_create() \u2192 \u7ebf\u7a0bA&#xff08;\u5206\u8eabA&#xff09;\u2192 \u670d\u52a1\u5ba2\u6237A \u2192 \u9000\u51fa<br \/>\n  \u2502<br \/>\n  \u251c\u2500\u2500 accept() \u2190 \u53c8\u6765\u4e00\u4e2a&#xff01;<br \/>\n  \u2502<br \/>\n  \u251c\u2500\u2500 pthread_create() \u2192 \u7ebf\u7a0bB&#xff08;\u5206\u8eabB&#xff09;\u2192 \u670d\u52a1\u5ba2\u6237B \u2192 \u9000\u51fa<br \/>\n  &#8230;<\/p>\n<p>\u901a\u4fd7\u7c7b\u6bd4&#xff1a;\u7ebf\u7a0b\u5c31\u50cf&#034;\u5206\u8eab\u672f&#034;\u3002\u4f60\u4e0d\u662f\u96c7\u65b0\u5458\u5de5&#xff0c;\u800c\u662f\u628a\u81ea\u5df1\u590d\u5236\u51fa\u4e00\u4efd&#034;\u6295\u5f71&#034;&#xff0c;\u6bcf\u4e2a\u6295\u5f71\u53bb\u670d\u52a1\u4e00\u4e2a\u5ba2\u4eba\u3002\u56e0\u4e3a\u8fd9\u4e9b\u6295\u5f71\u5171\u4eab\u540c\u4e00\u4e2a\u5927\u8111&#xff08;\u5185\u5b58\u7a7a\u95f4&#xff09;&#xff0c;\u6240\u4ee5\u521b\u5efa\u5feb\u3001\u5207\u6362\u5feb&#xff0c;\u4f46\u4e00\u65e6\u6709\u4e00\u4e2a\u6295\u5f71\u6454\u5012\u4e86&#xff08;\u5d29\u6e83&#xff09;&#xff0c;\u6240\u6709\u4eba\u90fd\u5f97\u8ddf\u7740\u5012\u3002<\/p>\n<h4 id=\"32-\u7f16\u5199\u4ee3\u7801\">3.2 \u7f16\u5199\u4ee3\u7801<\/h4>\n<p>\u7b2c 1 \u6b65 \u2192\u00a0\u521b\u5efa\u00a0thread_server.c&#xff1a;<\/p>\n<p>vim thread_server.c<\/p>\n<p>\u7b2c 2 \u6b65 \u2192\u00a0\u8f93\u5165\u5b8c\u6574\u4ee3\u7801&#xff1a;<\/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;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;pthread.h&gt;    \/\/ \u591a\u7ebf\u7a0b\u9700\u8981\u8fd9\u4e2a\u5934\u6587\u4ef6<\/p>\n<p>#define PORT 8080<br \/>\n#define BUFFER_SIZE 1024<\/p>\n<p>\/\/ \u7ebf\u7a0b\u51fd\u6570&#xff1a;\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u5728\u8fd9\u4e2a\u51fd\u6570\u91cc\u670d\u52a1\u81ea\u5df1\u7684\u5ba2\u6237<br \/>\n\/\/ \u53c2\u6570 arg \u662f\u4e00\u4e2a\u6307\u5411 int \u7684\u6307\u9488&#xff08;\u5ba2\u6237\u7aef\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff09;<br \/>\nvoid* client_handler(void *arg) {<br \/>\n    int client_fd &#061; *(int*)arg;  \/\/ \u5148\u628a\u53c2\u6570\u89e3\u51fa\u6765<br \/>\n    free(arg);                    \/\/ \u7528\u5b8c\u7acb\u523b\u91ca\u653e&#xff0c;\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f&#xff01;<br \/>\n    char buffer[BUFFER_SIZE];<br \/>\n    int n;<\/p>\n<p>    \/\/ \u6253\u5370\u7ebf\u7a0b ID&#xff08;\u7528 pthread_self() \u83b7\u53d6&#xff09;<br \/>\n    printf(&#034;\u7ebf\u7a0b %lu \u5f00\u59cb\u670d\u52a1\\\\n&#034;, pthread_self());<\/p>\n<p>    \/\/ Echo \u5faa\u73af&#xff1a;\u8bfb\u5230\u5c31\u56de<br \/>\n    while ((n &#061; read(client_fd, buffer, sizeof(buffer) &#8211; 1)) &gt; 0) {<br \/>\n        buffer[n] &#061; &#039;\\\\0&#039;;<br \/>\n        printf(&#034;\u7ebf\u7a0b %lu \u6536\u5230: %s&#034;, pthread_self(), buffer);<br \/>\n        write(client_fd, buffer, n);<br \/>\n    }<\/p>\n<p>    close(client_fd);<br \/>\n    printf(&#034;\u7ebf\u7a0b %lu \u4e0b\u73ed\\\\n&#034;, pthread_self());<br \/>\n    return NULL;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    int server_fd, *client_fd_ptr;  \/\/ \u26a0\ufe0f \u6ce8\u610f&#xff1a;client_fd_ptr \u662f\u6307\u9488&#xff0c;\u4e0d\u662f\u666e\u901a int<br \/>\n    struct sockaddr_in server_addr, client_addr;<br \/>\n    socklen_t client_len &#061; sizeof(client_addr);<br \/>\n    pthread_t tid;<\/p>\n<p>    \/\/ \u521b\u5efa\u5957\u63a5\u5b57\u3001\u8bbe\u7f6e\u7aef\u53e3\u590d\u7528<br \/>\n    server_fd &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n    int opt &#061; 1;<br \/>\n    setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &amp;opt, sizeof(opt));<\/p>\n<p>    \/\/ \u914d\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_port &#061; htons(PORT);<br \/>\n    server_addr.sin_addr.s_addr &#061; INADDR_ANY;<\/p>\n<p>    \/\/ \u7ed1\u5b9a &#043; \u76d1\u542c<br \/>\n    bind(server_fd, (struct sockaddr *)&amp;server_addr, sizeof(server_addr));<br \/>\n    listen(server_fd, 5);<br \/>\n    printf(&#034;\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u542f\u52a8&#xff0c;\u76d1\u542c\u7aef\u53e3 %d\\\\n&#034;, PORT);<\/p>\n<p>    \/\/ \u4e3b\u5faa\u73af<br \/>\n    while (1) {<br \/>\n        \/\/ \u26a0\ufe0f \u5173\u952e&#xff1a;\u5fc5\u987b\u5728\u5806\u4e0a\u5206\u914d\u5185\u5b58&#xff0c;\u4e0d\u80fd\u4f20\u6808\u4e0a\u53d8\u91cf&#xff01;<br \/>\n        \/\/ \u539f\u56e0&#xff1a;\u5982\u679c\u4f20 &amp;client_fd&#xff08;\u6808\u53d8\u91cf&#xff09;&#xff0c;\u4e0b\u4e00\u6b21 accept \u4f1a\u8986\u76d6\u5b83\u7684\u503c<br \/>\n        \/\/ \u7ebf\u7a0b\u53ef\u80fd\u8bfb\u5230\u9519\u8bef\u7684\u6570\u636e<br \/>\n        client_fd_ptr &#061; malloc(sizeof(int));<br \/>\n        *client_fd_ptr &#061; accept(server_fd,<br \/>\n                                (struct sockaddr *)&amp;client_addr,<br \/>\n                                &amp;client_len);<\/p>\n<p>        if (*client_fd_ptr &lt; 0) {<br \/>\n            perror(&#034;accept&#034;);<br \/>\n            free(client_fd_ptr);  \/\/ \u51fa\u9519\u4e5f\u522b\u5fd8\u91ca\u653e<br \/>\n            continue;<br \/>\n        }<\/p>\n<p>        printf(&#034;\u65b0\u5ba2\u6237\u7aef %s:%d \u8fde\u63a5\\\\n&#034;,<br \/>\n               inet_ntoa(client_addr.sin_addr),<br \/>\n               ntohs(client_addr.sin_port));<\/p>\n<p>        \/\/ \u521b\u5efa\u7ebf\u7a0b\u53bb\u670d\u52a1<br \/>\n        \/\/ \u53c2\u6570\u8bf4\u660e&#xff1a;&amp;tid&#061;\u7ebf\u7a0bID\u8f93\u51fa, NULL&#061;\u9ed8\u8ba4\u5c5e\u6027,<br \/>\n        \/\/          client_handler&#061;\u7ebf\u7a0b\u51fd\u6570, client_fd_ptr&#061;\u4f20\u7ed9\u7ebf\u7a0b\u7684\u53c2\u6570<br \/>\n        pthread_create(&amp;tid, NULL, client_handler, client_fd_ptr);<\/p>\n<p>        \/\/ \u5206\u79bb\u7ebf\u7a0b&#xff1a;\u544a\u8bc9\u7cfb\u7edf&#034;\u4e0d\u7528\u7b49\u6211&#xff0c;\u6211\u7ed3\u675f\u540e\u81ea\u52a8\u56de\u6536&#034;<br \/>\n        \/\/ \u8fd9\u6837\u4e3b\u7ebf\u7a0b\u4e0d\u9700\u8981\u8c03\u7528 pthread_join \u963b\u585e\u7b49\u5f85<br \/>\n        pthread_detach(tid);<br \/>\n    }<\/p>\n<p>    close(server_fd);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u7b2c 3 \u6b65 \u2192\u00a0\u7f16\u8bd1\u8fd0\u884c&#xff08;\u6ce8\u610f\u8981\u52a0\u00a0-lpthread&#xff09;&#xff1a;<\/p>\n<p># \u7f16\u8bd1\u2014\u2014pthread \u4e0d\u662f\u6807\u51c6 C \u5e93\u7684\u4e00\u90e8\u5206&#xff0c;\u5fc5\u987b\u663e\u5f0f\u94fe\u63a5<br \/>\ngcc -Wall -g thread_server.c -o thread_server -lpthread<\/p>\n<p># \u8fd0\u884c<br \/>\n.\/thread_server<\/p>\n<p>\u26a0\ufe0f\u00a0\u6700\u5bb9\u6613\u72af\u7684\u9519&#xff1a;\u7f16\u8bd1\u65f6\u5fd8\u4e86\u00a0-lpthread&#xff0c;\u94fe\u63a5\u9636\u6bb5\u62a5\u4e00\u5806\u00a0undefined reference to pthread_create\u3002\u52a0\u4e0a\u5c31\u597d\u4e86\u3002<\/p>\n<p>\u7b2c 4 \u6b65 \u2192\u00a0\u6d4b\u8bd5&#xff1a;\u540c\u6837\u7528 telnet&#xff1a;<\/p>\n<p># \u7ec8\u7aef2<br \/>\ntelnet 127.0.0.1 8080<\/p>\n<p># \u7ec8\u7aef3<br \/>\ntelnet 127.0.0.1 8080<\/p>\n<h4 id=\"33-\u591a\u7ebf\u7a0b\u6a21\u578b\u5173\u952e\u70b9\u901f\u67e5\">3.3 \u591a\u7ebf\u7a0b\u6a21\u578b\u5173\u952e\u70b9\u901f\u67e5<\/h4>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">\u5173\u952e\u70b9<\/td>\n<td style=\"background-color:#dddddd\">\u4e3a\u4ec0\u4e48\u5fc5\u987b\u8fd9\u6837\u505a<\/td>\n<td style=\"background-color:#dddddd\">\u4e0d\u505a\u7684\u540e\u679c<\/td>\n<\/tr>\n<tr>\n<td>\u53c2\u6570\u7528\u00a0malloc\u00a0\u5806\u5206\u914d<\/td>\n<td>\u6808\u53d8\u91cf\u5728\u5faa\u73af\u4e2d\u4f1a\u88ab\u8986\u76d6&#xff0c;\u7ebf\u7a0b\u8bfb\u5230\u7684\u53ef\u80fd\u662f\u4e0b\u4e2a\u5ba2\u6237\u7aef\u7684 fd<\/td>\n<td>\u4e24\u4e2a\u7ebf\u7a0b\u670d\u52a1\u540c\u4e00\u4e2a\u5ba2\u6237\u7aef&#xff0c;\u6570\u636e\u4e71\u5957<\/td>\n<\/tr>\n<tr>\n<td>pthread_detach<\/td>\n<td>\u4e3b\u7ebf\u7a0b\u4e0d\u963b\u585e\u7b49\u5b50\u7ebf\u7a0b\u7ed3\u675f<\/td>\n<td>\u5982\u679c\u4e0d join \u4e5f\u4e0d detach&#xff0c;\u7ebf\u7a0b\u8d44\u6e90\u6c38\u8fdc\u4e0d\u4f1a\u56de\u6536&#xff08;\u5185\u5b58\u6cc4\u6f0f&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u5171\u4eab\u6570\u636e\u52a0\u9501<\/td>\n<td>\u591a\u7ebf\u7a0b\u540c\u65f6\u5199\u5168\u5c40\u53d8\u91cf\u4f1a\u53d1\u751f\u7ade\u4e89<\/td>\n<td>\u8ba1\u6570\u5668\u9519\u4e71\u3001\u94fe\u8868\u65ad\u94fe&#xff0c;\u4e14\u6781\u96be\u590d\u73b0\u8c03\u8bd5<\/td>\n<\/tr>\n<tr>\n<td>\u5d29\u6e83\u4f20\u64ad<\/td>\n<td>\u4e00\u4e2a\u7ebf\u7a0b\u6bb5\u9519\u8bef &#061; \u6574\u4e2a\u8fdb\u7a0b\u6b7b<\/td>\n<td>\u4e0d\u50cf\u591a\u8fdb\u7a0b\u90a3\u6837\u80fd\u9694\u79bb\u6545\u969c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u26a0\ufe0f\u00a0\u9884\u8b66&#xff1a;\u5982\u679c\u4f60\u7528\u00a0malloc\u00a0\u5206\u914d\u4e86\u00a0client_fd_ptr\u00a0\u4f46\u5fd8\u4e86\u5728\u7ebf\u7a0b\u51fd\u6570\u91cc\u00a0free(arg)&#xff0c;\u6bcf\u8fde\u63a5\u4e00\u4e2a\u5ba2\u6237\u7aef\u5c31\u6cc4\u6f0f 4 \u5b57\u8282&#xff08;\u6216 8 \u5b57\u8282&#xff09;\u3002\u770b\u4e0a\u53bb\u5f88\u5c0f&#xff0c;\u8dd1\u4e00\u5929\u4e0b\u6765\u53ef\u80fd\u6cc4\u6f0f\u51e0\u767e MB\u3002<\/p>\n<hr \/>\n<h3 id=\"\u7b2c\u56db\u90e8\u5206\u591a\u8fdb\u7a0b-vs-\u591a\u7ebf\u7a0b-\u7ec8\u6781\u5bf9\u51b3\">\u7b2c\u56db\u90e8\u5206&#xff1a;\u591a\u8fdb\u7a0b vs \u591a\u7ebf\u7a0b \u2014\u2014 \u7ec8\u6781\u5bf9\u51b3<\/h3>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">\u7ef4\u5ea6<\/td>\n<td style=\"background-color:#dddddd\">\u591a\u8fdb\u7a0b&#xff08;Fork&#xff09;<\/td>\n<td style=\"background-color:#dddddd\">\u591a\u7ebf\u7a0b&#xff08;Pthread&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u521b\u5efa\u5f00\u9500<\/td>\n<td>\u9ad8&#xff08;\u590d\u5236\u9875\u8868\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868\u7b49&#xff09;<\/td>\n<td>\u4f4e&#xff08;\u53ea\u9700\u65b0\u5efa\u6808\u548c\u5bc4\u5b58\u5668&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u5207\u6362\u5f00\u9500<\/td>\n<td>\u9ad8&#xff08;\u5207\u6362\u9875\u8868&#xff0c;TLB \u5237\u65b0&#xff09;<\/td>\n<td>\u4f4e&#xff08;\u53ea\u5207\u6362\u5bc4\u5b58\u5668\u4e0a\u4e0b\u6587&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u5185\u5b58\u5360\u7528<\/td>\n<td>\u9ad8&#xff08;\u72ec\u7acb\u5730\u5740\u7a7a\u95f4&#xff09;<\/td>\n<td>\u4f4e&#xff08;\u5171\u4eab\u5730\u5740\u7a7a\u95f4&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u901a\u4fe1\u65b9\u5f0f<\/td>\n<td>\u9700\u8981 IPC&#xff08;\u7ba1\u9053\u3001\u5171\u4eab\u5185\u5b58\u3001\u6d88\u606f\u961f\u5217&#xff09;<\/td>\n<td>\u76f4\u63a5\u8bfb\u5199\u5168\u5c40\u53d8\u91cf&#xff08;\u4f46\u8981\u52a0\u9501&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u6570\u636e\u9694\u79bb\/\u5b89\u5168\u6027<\/td>\n<td>\u6781\u9ad8&#xff08;\u4e00\u4e2a\u5d29\u4e86\u4e0d\u5f71\u54cd\u5176\u4ed6&#xff09;<\/td>\n<td>\u6781\u4f4e&#xff08;\u4e00\u4e2a\u5d29\u4e86\u5168\u6302&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u6700\u5927\u5e76\u53d1\u6570<\/td>\n<td>\u901a\u5e38\u51e0\u767e~\u51e0\u5343&#xff08;\u53d7 PID \u548c\u5185\u5b58\u9650\u5236&#xff09;<\/td>\n<td>\u53ef\u8fbe\u6570\u4e07&#xff08;\u53d7\u5185\u5b58\u9650\u5236&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u7f16\u7a0b\u96be\u5ea6<\/td>\n<td>\u8f83\u4f4e&#xff08;\u4fe1\u53f7\u5904\u7406\u7a0d\u9ebb\u70e6&#xff09;<\/td>\n<td>\u8f83\u9ad8&#xff08;\u9501\u3001\u6b7b\u9501\u3001\u6761\u4ef6\u53d8\u91cf&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u73b0\u5b9e\u4e16\u754c\u8c01\u5728\u7528<\/td>\n<td>Apache Prefork\u3001Chrome \u591a\u8fdb\u7a0b\u67b6\u6784<\/td>\n<td>Nginx\u3001MySQL \u8fde\u63a5\u6c60\u3001Java \u7ebf\u7a0b\u6c60<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4 id=\"\u9009\u578b\u53e3\u8bc0\">\u9009\u578b\u53e3\u8bc0<\/h4>\n<ul>\n<li>\u6c42\u7a33&#xff08;\u94f6\u884c\/\u5de5\u63a7\/\u5173\u952e\u4e1a\u52a1&#xff09;\u2192 \u591a\u8fdb\u7a0b\u3002\u6302\u4e86\u53ef\u4ee5\u91cd\u542f&#xff0c;\u4e0d\u5f71\u54cd\u5176\u4ed6\u3002<\/li>\n<li>\u6c42\u5feb&#xff08;Web \u670d\u52a1\u5668\/\u6e38\u620f\u670d\u52a1\u5668\/API \u7f51\u5173&#xff09;\u2192 \u591a\u7ebf\u7a0b\u3002\u80fd\u652f\u6491\u66f4\u9ad8\u5e76\u53d1\u3002<\/li>\n<li>\u4e24\u5168\u5176\u7f8e&#xff08;\u73b0\u4ee3\u4e3b\u6d41&#xff09;\u2192 IO \u590d\u7528&#xff08;epoll&#xff09;&#043; \u5c11\u91cf\u7ebf\u7a0b\u6c60\u3002\u8be6\u89c1\u7b2c\u4e94\u90e8\u5206\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3 id=\"\u7b2c\u4e94\u90e8\u5206io-\u590d\u7528\u6280\u672f-\u4e00\u4e2a\u670d\u52a1\u5458\u641e\u5b9a\u5168\u573a\">\u7b2c\u4e94\u90e8\u5206&#xff1a;IO \u590d\u7528\u6280\u672f \u2014\u2014 \u4e00\u4e2a\u670d\u52a1\u5458\u641e\u5b9a\u5168\u573a<\/h3>\n<h4 id=\"50-\u963b\u585e\u548c\u975e\u963b\u585e\u7684\u5c40\u9650\">5.0 \u963b\u585e\u548c\u975e\u963b\u585e\u7684\u5c40\u9650<\/h4>\n<p>\u5728\u8bb2 IO \u590d\u7528\u4e4b\u524d&#xff0c;\u6211\u4eec\u5148\u641e\u6e05\u695a\u4e24\u4e2a\u6982\u5ff5&#xff1a;<\/p>\n<p>\u963b\u585e I\/O&#xff08;Blocking I\/O&#xff09;\u2014\u2014\u9ed8\u8ba4\u6a21\u5f0f&#xff1a;<\/p>\n<p>read(fd, buf, size);  \/\/ fd \u6ca1\u6570\u636e&#xff1f;\u8fdb\u7a0b\u5c31&#034;\u7761\u7740&#034;&#xff0c;\u76f4\u5230\u6709\u6570\u636e\u624d\u9192<\/p>\n<p>\u901a\u4fd7\u7c7b\u6bd4&#xff1a;\u6253\u7535\u8bdd\u65f6\u5bf9\u65b9\u4e0d\u8bf4\u8bdd&#xff0c;\u4f60\u5c31\u5728\u90a3\u5934\u7b49\u7740&#xff0c;\u4e0d\u6302\u7535\u8bdd&#xff08;\u8fdb\u7a0b\u6302\u8d77&#xff09;&#xff0c;\u5f53\u7136\u4e5f\u4e0d\u80fd\u63a5\u522b\u4eba\u7684\u7535\u8bdd\u3002<\/p>\n<p>\u975e\u963b\u585e I\/O&#xff08;Non-blocking I\/O&#xff09;\u2014\u2014\u624b\u52a8\u8bbe\u7f6e\u540e&#xff1a;<\/p>\n<p>int flags &#061; fcntl(fd, F_GETFL, 0);<br \/>\nfcntl(fd, F_SETFL, flags | O_NONBLOCK);  \/\/ \u8bbe\u4e3a\u975e\u963b\u585e<\/p>\n<p>int n &#061; read(fd, buf, size);  \/\/ \u6ca1\u6570\u636e&#xff1f;\u7acb\u523b\u8fd4\u56de -1&#xff0c;errno&#061;EAGAIN<\/p>\n<p>\u901a\u4fd7\u7c7b\u6bd4&#xff1a;\u4f60\u6bcf\u9694\u4e00\u79d2\u770b\u4e00\u773c\u5bf9\u65b9\u6709\u6ca1\u6709\u8bf4\u8bdd&#xff0c;\u6ca1\u8bf4\u5c31\u626d\u5934\u770b\u4e00\u773c\u522b\u4eba\u3002\u4f46\u5982\u679c\u7528\u00a0for\u00a0\u5faa\u73af\u75af\u72c2\u53bb\u68c0\u67e5&#xff08;\u8f6e\u8be2&#xff09;&#xff0c;CPU \u4f1a\u98d9\u5230 100%&#xff0c;\u5927\u90e8\u5206\u65f6\u95f4\u90fd\u5728\u505a\u65e0\u7528\u529f\u2014\u2014\u8fd9\u53eb&#034;\u5fd9\u7b49\u5f85&#034;\u3002<\/p>\n<p>IO \u590d\u7528\u7684\u89e3\u51b3\u601d\u8def&#xff1a;\u522b\u81ea\u5df1\u6328\u4e2a\u95ee\u4e86&#xff0c;\u8ba9\u5185\u6838\u5e2e\u4f60\u76ef\u7740&#xff01;\u4f60\u53ea\u9700\u8981\u544a\u8bc9\u5185\u6838&#034;\u5e2e\u6211\u76ef\u7740\u8fd9 100 \u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u8c01\u6709\u6570\u636e\u4e86\u53eb\u9192\u6211&#034;\u3002\u8fd9\u6837\u4f60\u53ea\u9700\u8981\u963b\u585e\u5728\u4e00\u4e2a\u5730\u65b9&#xff08;\u6bd4\u5982\u00a0epoll_wait&#xff09;&#xff0c;\u800c\u4e0d\u662f\u5728 100 \u4e2a\u00a0read\u00a0\u4e4b\u95f4\u75b2\u4e8e\u5954\u547d\u3002<\/p>\n<p>\u4e00\u53e5\u8bdd\u603b\u7ed3&#xff1a;IO \u590d\u7528\u5c31\u662f&#034;\u7528\u4e00\u4e2a\u963b\u585e&#xff0c;\u6362\u6389 100 \u4e2a\u963b\u585e&#034;\u3002<\/p>\n<hr \/>\n<h4 id=\"51-\u4e09\u5144\u5f1f\u767b\u573aselect-poll-epoll\">5.1 \u4e09\u5144\u5f1f\u767b\u573a&#xff1a;select \/ poll \/ epoll<\/h4>\n<p>\u65f6\u95f4\u7ebf&#xff1a;1983  select&#xff08;\u8001\u5927\u54e5&#xff09;\u2192 1997  poll&#xff08;\u4e8c\u54e5&#xff09;\u2192 2002  epoll&#xff08;\u4e09\u5f1f&#xff0c;\u738b\u8005&#xff09;<\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\"><\/td>\n<td style=\"background-color:#dddddd\">select<\/td>\n<td style=\"background-color:#dddddd\">poll<\/td>\n<td style=\"background-color:#dddddd\">epoll<\/td>\n<\/tr>\n<tr>\n<td>\u5e74\u4ee3<\/td>\n<td>1983 (BSD)<\/td>\n<td>1997 (SVR4)<\/td>\n<td>2002 (Linux 2.5.44)<\/td>\n<\/tr>\n<tr>\n<td>\u539f\u7406<\/td>\n<td>\u5185\u6838\u8f6e\u8be2\u6240\u6709 fd<\/td>\n<td>\u5185\u6838\u8f6e\u8be2\u6240\u6709 fd<\/td>\n<td>\u4e8b\u4ef6\u9a71\u52a8&#xff0c;\u5185\u6838\u4e3b\u52a8\u901a\u77e5<\/td>\n<\/tr>\n<tr>\n<td>fd \u6570\u91cf\u9650\u5236<\/td>\n<td>\u786c\u9650\u5236 FD_SETSIZE&#xff08;\u9ed8\u8ba4 1024&#xff09;<\/td>\n<td>\u65e0\u786c\u9650\u5236<\/td>\n<td>\u65e0\u786c\u9650\u5236<\/td>\n<\/tr>\n<tr>\n<td>\u6570\u636e\u7ed3\u6784<\/td>\n<td>3 \u4e2a\u4f4d\u56fe&#xff08;fd_set&#xff09;<\/td>\n<td>\u52a8\u6001\u6570\u7ec4&#xff08;struct pollfd[]&#xff09;<\/td>\n<td>\u7ea2\u9ed1\u6811 &#043; \u5c31\u7eea\u94fe\u8868<\/td>\n<\/tr>\n<tr>\n<td>\u6bcf\u6b21\u8c03\u7528\u662f\u5426\u91cd\u65b0\u4f20\u5165 fd \u5217\u8868<\/td>\n<td>\u662f&#xff08;\u4ece\u7528\u6237\u6001\u62f7\u8d1d\u5230\u5185\u6838\u6001&#xff09;<\/td>\n<td>\u662f&#xff08;\u4ece\u7528\u6237\u6001\u62f7\u8d1d\u5230\u5185\u6838\u6001&#xff09;<\/td>\n<td>\u5426&#xff08;fd \u6ce8\u518c\u5728\u5185\u6838&#xff0c;\u957f\u671f\u6709\u6548&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u590d\u6742\u5ea6<\/td>\n<td>O(n) \u2014\u2014 n \u4e3a\u6700\u5927 fd \u503c<\/td>\n<td>O(n) \u2014\u2014 n \u4e3a fd \u603b\u6570<\/td>\n<td>O(1)\u00a0\u2014\u2014 \u53ea\u904d\u5386\u5c31\u7eea\u7684 fd<\/td>\n<\/tr>\n<tr>\n<td>\u9002\u7528\u573a\u666f<\/td>\n<td>\u5c11\u91cf fd&#xff0c;\u8de8\u5e73\u53f0<\/td>\n<td>\u4e2d\u7b49\u91cf fd<\/td>\n<td>\u5927\u89c4\u6a21\u5e76\u53d1&#xff08;C10K&#043;&#xff09;&#xff0c;Linux \u4e13\u5c5e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e00\u53e5\u8bdd\u603b\u7ed3&#xff1a;select \u548c poll \u662f&#034;\u6328\u4e2a\u70b9\u540d&#034;&#xff0c;epoll \u662f&#034;\u8c01\u4e3e\u624b\u53eb\u8c01&#034;\u3002<\/p>\n<hr \/>\n<h4 id=\"52-select-\u8001\u5927\u54e5\u7684\u7b28\u529e\u6cd5\">5.2 select \u2014\u2014 \u8001\u5927\u54e5\u7684\u7b28\u529e\u6cd5<\/h4>\n<h5 id=\"521-\u6838\u5fc3-api\">5.2.1 \u6838\u5fc3 API<\/h5>\n<p>#include &lt;sys\/select.h&gt;<\/p>\n<p>int select(int nfds,                    \/\/ \u6700\u5927 fd \u503c &#043; 1<br \/>\n           fd_set *readfds,             \/\/ \u60f3\u76d1\u63a7&#034;\u53ef\u8bfb&#034;\u7684 fd \u96c6\u5408<br \/>\n           fd_set *writefds,            \/\/ \u60f3\u76d1\u63a7&#034;\u53ef\u5199&#034;\u7684 fd \u96c6\u5408<br \/>\n           fd_set *exceptfds,           \/\/ \u60f3\u76d1\u63a7&#034;\u5f02\u5e38&#034;\u7684 fd \u96c6\u5408<br \/>\n           struct timeval *timeout);    \/\/ \u8d85\u65f6\u65f6\u95f4<\/p>\n<p>\/\/ \u64cd\u4f5c fd_set \u7684\u5b8f<br \/>\nFD_ZERO(fd_set *set);     \/\/ \u6e05\u7a7a\u96c6\u5408<br \/>\nFD_SET(int fd, fd_set *set);   \/\/ \u628a fd \u52a0\u5165\u96c6\u5408<br \/>\nFD_CLR(int fd, fd_set *set);   \/\/ \u628a fd \u4ece\u96c6\u5408\u79fb\u9664<br \/>\nFD_ISSET(int fd, fd_set *set); \/\/ \u68c0\u67e5 fd \u662f\u5426\u5728\u96c6\u5408\u4e2d&#xff08;\u5373\u662f\u5426\u5c31\u7eea&#xff09;<\/p>\n<h5 id=\"522-\u5de5\u4f5c\u6d41\u7a0b\">5.2.2 \u5de5\u4f5c\u6d41\u7a0b<\/h5>\n<p>\u7b2c 1 \u6b65&#xff1a;\u628a\u6240\u6709\u60f3\u76d1\u63a7\u7684 fd \u585e\u8fdb fd_set<br \/>\n\u7b2c 2 \u6b65&#xff1a;\u8c03\u7528 select()&#xff0c;\u8fdb\u7a0b\u963b\u585e&#xff0c;\u5185\u6838\u53bb\u8f6e\u8be2\u6240\u6709 fd<br \/>\n\u7b2c 3 \u6b65&#xff1a;\u6709 fd \u5c31\u7eea \u2192 select \u8fd4\u56de&#xff0c;\u5185\u6838\u4fee\u6539 fd_set&#xff08;\u53ea\u7559\u4e0b\u5c31\u7eea\u7684 fd&#xff09;<br \/>\n\u7b2c 4 \u6b65&#xff1a;\u904d\u5386\u6240\u6709 fd&#xff0c;\u7528 FD_ISSET \u6570\u54ea\u4e9b\u5c31\u7eea\u4e86<br \/>\n\u7b2c 5 \u6b65&#xff1a;\u5904\u7406\u5c31\u7eea\u7684 fd&#xff0c;\u56de\u5230\u7b2c 1 \u6b65<\/p>\n<p>\u901a\u4fd7\u7c7b\u6bd4&#xff1a;select \u5c31\u50cf\u4e00\u4e2a\u8001\u5e08&#xff0c;\u6bcf\u5929\u65e9\u6668\u628a\u5168\u73ed 200 \u4e2a\u5b66\u751f\u540d\u5355\u6284\u4e00\u904d\u4ea4\u4e0a\u53bb\u3002\u4e0a\u8bfe\u65f6\u60f3\u77e5\u9053\u8c01\u4e3e\u624b\u2014\u2014\u4ed6\u6328\u4e2a\u70b9\u540d&#xff1a;&#034;\u5f20\u4e09\u4e3e\u624b\u4e86\u5417&#xff1f;\u674e\u56db\u4e3e\u624b\u4e86\u5417&#xff1f;&#8230;&#034; \u6bcf\u5929\u8fd8\u8981\u628a 200 \u4eba\u540d\u5355\u91cd\u65b0\u6284\u4e00\u904d\u3002\u4eba\u5c11\u8fd8\u884c&#xff0c;\u4eba\u591a\u4e86\u6548\u7387\u5947\u4f4e\u3002<\/p>\n<h5 id=\"523-\u5b8c\u6574\u4ee3\u7801select_serverc\">5.2.3 \u5b8c\u6574\u4ee3\u7801&#xff1a;select_server.c<\/h5>\n<p>\u7b2c 1 \u6b65 \u2192\u00a0\u521b\u5efa\u6587\u4ef6&#xff1a;<\/p>\n<p>vim select_server.c<\/p>\n<p>\u7b2c 2 \u6b65 \u2192\u00a0\u8f93\u5165\u4ee3\u7801&#xff1a;<\/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;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;sys\/select.h&gt;  \/\/ select \u9700\u8981\u8fd9\u4e2a\u5934\u6587\u4ef6<\/p>\n<p>#define PORT 8080<br \/>\n#define BUFFER_SIZE 1024<br \/>\n#define MAX_CLIENTS 1024   \/\/ FD_SETSIZE \u9ed8\u8ba4\u5c31\u662f 1024<\/p>\n<p>int main() {<br \/>\n    int server_fd, client_fd, max_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>    \/\/ \u5b58\u50a8\u6240\u6709\u5ba2\u6237\u7aef fd&#xff0c;\u65b9\u4fbf\u904d\u5386&#xff08;\u5f25\u8865 select \u7684\u4e0d\u8db3&#xff09;<br \/>\n    int client_fds[MAX_CLIENTS];<br \/>\n    for (int i &#061; 0; i &lt; MAX_CLIENTS; i&#043;&#043;) client_fds[i] &#061; -1;  \/\/ -1 \u8868\u793a\u7a7a\u4f4d<\/p>\n<p>    \/\/ \u2460 \u521b\u5efa\u5957\u63a5\u5b57<br \/>\n    server_fd &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n    int opt &#061; 1;<br \/>\n    setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &amp;opt, sizeof(opt));<\/p>\n<p>    \/\/ \u2461 \u7ed1\u5b9a &#043; \u76d1\u542c<br \/>\n    memset(&amp;server_addr, 0, sizeof(server_addr));<br \/>\n    server_addr.sin_family &#061; AF_INET;<br \/>\n    server_addr.sin_port &#061; htons(PORT);<br \/>\n    server_addr.sin_addr.s_addr &#061; INADDR_ANY;<br \/>\n    bind(server_fd, (struct sockaddr *)&amp;server_addr, sizeof(server_addr));<br \/>\n    listen(server_fd, 5);<\/p>\n<p>    printf(&#034;select \u670d\u52a1\u5668\u542f\u52a8&#xff0c;\u76d1\u542c\u7aef\u53e3 %d\\\\n&#034;, PORT);<br \/>\n    printf(&#034;\u6700\u5927\u652f\u6301 %d \u4e2a\u5ba2\u6237\u7aef&#xff08;\u786c\u9650\u5236&#xff09;\\\\n&#034;, MAX_CLIENTS);<\/p>\n<p>    while (1) {<br \/>\n        fd_set read_fds;              \/\/ \u6bcf\u6b21\u5faa\u73af\u91cd\u65b0\u521b\u5efa\u96c6\u5408<br \/>\n        FD_ZERO(&amp;read_fds);           \/\/ \u6e05\u7a7a<br \/>\n        FD_SET(server_fd, &amp;read_fds); \/\/ \u628a server_fd \u52a0\u5165\u76d1\u63a7\u5217\u8868<br \/>\n        max_fd &#061; server_fd;           \/\/ \u8bb0\u5f55\u6700\u5927 fd \u503c<\/p>\n<p>        \/\/ \u2461 \u628a\u6240\u6709\u5ba2\u6237\u7aef fd \u52a0\u5165\u76d1\u63a7\u96c6\u5408<br \/>\n        for (int i &#061; 0; i &lt; MAX_CLIENTS; i&#043;&#043;) {<br \/>\n            if (client_fds[i] !&#061; -1) {<br \/>\n                FD_SET(client_fds[i], &amp;read_fds);<br \/>\n                if (client_fds[i] &gt; max_fd) max_fd &#061; client_fds[i];<br \/>\n            }<br \/>\n        }<\/p>\n<p>        \/\/ \u2462 \u8c03\u7528 select \u2014 \u5185\u6838\u5e2e\u4f60\u76ef\u7740\u6240\u6709 fd<br \/>\n        \/\/    \u53c2\u6570&#xff1a;max_fd&#043;1&#xff08;\u6700\u5927 fd \u7f16\u53f7&#043;1&#xff09;\u3001\u8bfb\u96c6\u5408\u3001\u5199\u96c6\u5408&#xff08;NULL&#061;\u4e0d\u76d1\u63a7&#xff09;\u3001<br \/>\n        \/\/          \u5f02\u5e38\u96c6\u5408&#xff08;NULL&#061;\u4e0d\u76d1\u63a7&#xff09;\u3001\u8d85\u65f6&#xff08;NULL&#061;\u6c38\u8fdc\u7b49&#xff09;<br \/>\n        int ready &#061; select(max_fd &#043; 1, &amp;read_fds, NULL, NULL, NULL);<br \/>\n        if (ready &lt; 0) {<br \/>\n            perror(&#034;select&#034;);<br \/>\n            continue;<br \/>\n        }<\/p>\n<p>        \/\/ \u2463 \u68c0\u67e5 server_fd \u662f\u5426\u6709\u65b0\u8fde\u63a5<br \/>\n        if (FD_ISSET(server_fd, &amp;read_fds)) {<br \/>\n            client_fd &#061; accept(server_fd,<br \/>\n                              (struct sockaddr *)&amp;client_addr,<br \/>\n                              &amp;client_len);<br \/>\n            if (client_fd &gt;&#061; 0) {<br \/>\n                printf(&#034;\u65b0\u5ba2\u6237\u7aef %s:%d \u8fde\u63a5&#xff0c;fd&#061;%d\\\\n&#034;,<br \/>\n                       inet_ntoa(client_addr.sin_addr),<br \/>\n                       ntohs(client_addr.sin_port), client_fd);<\/p>\n<p>                \/\/ \u627e\u4e2a\u7a7a\u4f4d\u5b58\u8d77\u6765<br \/>\n                for (int i &#061; 0; i &lt; MAX_CLIENTS; i&#043;&#043;) {<br \/>\n                    if (client_fds[i] &#061;&#061; -1) {<br \/>\n                        client_fds[i] &#061; client_fd;<br \/>\n                        break;<br \/>\n                    }<br \/>\n                }<br \/>\n            }<br \/>\n            ready&#8211;;  \/\/ \u8fd9\u4e2a\u4e8b\u4ef6\u5904\u7406\u5b8c\u4e86<br \/>\n        }<\/p>\n<p>        \/\/ \u2464 \u904d\u5386\u68c0\u67e5\u6bcf\u4e2a\u5ba2\u6237\u7aef\u662f\u5426\u6709\u6570\u636e<br \/>\n        for (int i &#061; 0; i &lt; MAX_CLIENTS &amp;&amp; ready &gt; 0; i&#043;&#043;) {<br \/>\n            int fd &#061; client_fds[i];<br \/>\n            if (fd &#061;&#061; -1) continue;<\/p>\n<p>            if (FD_ISSET(fd, &amp;read_fds)) {<br \/>\n                int n &#061; read(fd, buffer, sizeof(buffer) &#8211; 1);<\/p>\n<p>                if (n &lt;&#061; 0) {<br \/>\n                    \/\/ \u5ba2\u6237\u7aef\u65ad\u5f00\u8fde\u63a5&#xff08;read \u8fd4\u56de 0&#xff09;\u6216\u51fa\u9519&#xff08;\u8fd4\u56de -1&#xff09;<br \/>\n                    if (n &#061;&#061; 0) {<br \/>\n                        printf(&#034;\u5ba2\u6237\u7aef fd&#061;%d \u65ad\u5f00\u8fde\u63a5\\\\n&#034;, fd);<br \/>\n                    } else {<br \/>\n                        perror(&#034;read&#034;);<br \/>\n                    }<br \/>\n                    close(fd);<br \/>\n                    client_fds[i] &#061; -1;  \/\/ \u91ca\u653e\u4f4d\u7f6e<br \/>\n                } else {<br \/>\n                    \/\/ \u6b63\u5e38\u6536\u5230\u6570\u636e&#xff0c;Echo \u56de\u53bb<br \/>\n                    buffer[n] &#061; &#039;\\\\0&#039;;<br \/>\n                    printf(&#034;\u5ba2\u6237\u7aef fd&#061;%d \u53d1\u6765: %s&#034;, fd, buffer);<br \/>\n                    write(fd, buffer, n);<br \/>\n                }<br \/>\n                ready&#8211;;<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    close(server_fd);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u7b2c 3 \u6b65 \u2192\u00a0\u7f16\u8bd1\u8fd0\u884c&#xff1a;<\/p>\n<p>gcc -Wall -g select_server.c -o select_server<br \/>\n.\/select_server<\/p>\n<h5 id=\"524-select-\u7684\u81f4\u547d\u7f3a\u9677\">5.2.4 select \u7684\u81f4\u547d\u7f3a\u9677<\/h5>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">\u7f3a\u9677<\/td>\n<td style=\"background-color:#dddddd\">\u5f71\u54cd<\/td>\n<\/tr>\n<tr>\n<td>fd \u6570\u91cf\u4e0a\u9650 &#061; FD_SETSIZE&#xff08;\u9ed8\u8ba4 1024&#xff09;<\/td>\n<td>\u6539\u8fd9\u4e2a\u503c\u9700\u8981\u91cd\u65b0\u7f16\u8bd1\u5185\u6838&#xff0c;\u6781\u4e0d\u53cb\u597d<\/td>\n<\/tr>\n<tr>\n<td>\u6bcf\u6b21\u8c03\u7528\u90fd\u8981\u628a\u6574\u4e2a fd_set \u4ece\u7528\u6237\u6001\u62f7\u5230\u5185\u6838\u6001<\/td>\n<td>\u8fde\u63a5\u6570\u591a\u65f6&#xff0c;\u62f7\u8d1d\u5f00\u9500\u5de8\u5927<\/td>\n<\/tr>\n<tr>\n<td>\u5185\u6838\u904d\u5386\u6240\u6709\u00a0fd&#xff08;\u4e0d\u662f\u53ea\u904d\u5386\u5c31\u7eea\u7684&#xff09;<\/td>\n<td>O(n)&#xff0c;1 \u4e07\u4e2a\u8fde\u63a5\u8981\u904d\u5386 1 \u4e07\u6b21<\/td>\n<\/tr>\n<tr>\n<td>\u8fd4\u56de\u540e\u7528\u6237\u8fd8\u8981\u904d\u5386\u4e00\u6b21\u627e\u5c31\u7eea fd<\/td>\n<td>\u53c8\u4e00\u904d O(n)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>select\u00a0\u7528\u4e8e\u5b66\u4e60\u7406\u89e3 IO \u590d\u7528\u539f\u7406\u5f88\u5408\u9002&#xff0c;\u4f46\u751f\u4ea7\u73af\u5883\u7edd\u4e0d\u8981\u7528\u5b83\u5904\u7406\u9ad8\u5e76\u53d1\u3002<\/p>\n<hr \/>\n<h4 id=\"53-poll-\u4e8c\u54e5\u7684\u5c0f\u6539\u8fdb\">5.3 poll \u2014\u2014 \u4e8c\u54e5\u7684\u5c0f\u6539\u8fdb<\/h4>\n<h5 id=\"531-\u6838\u5fc3-api\">5.3.1 \u6838\u5fc3 API<\/h5>\n<p>#include &lt;poll.h&gt;<\/p>\n<p>struct pollfd {<br \/>\n    int   fd;       \/\/ \u8981\u76d1\u63a7\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    short events;   \/\/ \u60f3\u76d1\u63a7\u7684\u4e8b\u4ef6&#xff08;POLLIN&#061;\u53ef\u8bfb, POLLOUT&#061;\u53ef\u5199&#xff09;<br \/>\n    short revents;  \/\/ \u5185\u6838\u8fd4\u56de&#xff1a;\u5b9e\u9645\u53d1\u751f\u7684\u4e8b\u4ef6<br \/>\n};<\/p>\n<p>int poll(struct pollfd *fds,   \/\/ pollfd \u6570\u7ec4<br \/>\n         nfds_t nfds,          \/\/ \u6570\u7ec4\u5143\u7d20\u4e2a\u6570<br \/>\n         int timeout);         \/\/ \u8d85\u65f6&#xff08;\u6beb\u79d2&#xff09;&#xff0c;-1 \u8868\u793a\u6c38\u8fdc\u7b49<\/p>\n<h5 id=\"532-poll-\u76f8\u6bd4-select-\u7684\u6539\u8fdb\">5.3.2 poll \u76f8\u6bd4 select \u7684\u6539\u8fdb<\/h5>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">\u6539\u8fdb\u70b9<\/td>\n<td style=\"background-color:#dddddd\">select<\/td>\n<td style=\"background-color:#dddddd\">poll<\/td>\n<\/tr>\n<tr>\n<td>fd \u6570\u91cf\u4e0a\u9650<\/td>\n<td>\u786c\u7f16\u7801 FD_SETSIZE&#061;1024<\/td>\n<td>\u65e0\u786c\u9650\u5236&#xff0c;\u53ea\u53d7\u5185\u5b58\u9650\u5236<\/td>\n<\/tr>\n<tr>\n<td>\u6570\u636e\u7ed3\u6784<\/td>\n<td>\u4f4d\u56fe&#xff08;fd_set&#xff09;&#xff0c;\u53ea\u80fd\u7528\u5b8f\u64cd\u4f5c<\/td>\n<td>\u7ed3\u6784\u4f53\u6570\u7ec4&#xff08;pollfd&#xff09;&#xff0c;\u66f4\u76f4\u89c2<\/td>\n<\/tr>\n<tr>\n<td>\u4e8b\u4ef6\u548c\u7ed3\u679c\u5206\u79bb<\/td>\n<td>\u4e0d\u5206\u79bb&#xff08;\u540c\u4e00\u4e2a fd_set \u6539\u6765\u6539\u53bb&#xff09;<\/td>\n<td>\u5206\u79bb&#xff1a;events \u662f\u4f60\u60f3\u76d1\u63a7\u7684&#xff0c;revents \u662f\u5185\u6838\u8fd4\u56de\u7684<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u901a\u4fd7\u7c7b\u6bd4&#xff1a;poll \u4e0d\u518d\u7528&#034;\u82b1\u540d\u518c&#034;&#xff0c;\u6539\u7528&#034;\u7b7e\u5230\u8868&#034;\u3002\u6bcf\u884c\u5199\u4e00\u4e2a\u5b66\u751f\u540d\u5b57\u548c\u60f3\u8981\u5173\u6ce8\u7684\u9879\u76ee&#xff0c;\u8001\u5e08\u4e0d\u7528\u5168\u90e8\u91cd\u65b0\u6284\u2014\u2014\u53ef\u4ee5\u589e\u5220\u6539\u3002\u4f46\u8001\u5e08\u8fd8\u662f\u8981\u5168\u4f53\u70b9\u540d\u3002<\/p>\n<h5 id=\"533-\u5b8c\u6574\u4ee3\u7801poll_serverc\">5.3.3 \u5b8c\u6574\u4ee3\u7801&#xff1a;poll_server.c<\/h5>\n<p>\u7b2c 1 \u6b65 \u2192\u00a0\u521b\u5efa\u6587\u4ef6&#xff1a;<\/p>\n<p>vim poll_server.c<\/p>\n<p>\u7b2c 2 \u6b65 \u2192\u00a0\u8f93\u5165\u4ee3\u7801&#xff1a;<\/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;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;poll.h&gt;      \/\/ poll \u9700\u8981\u8fd9\u4e2a\u5934\u6587\u4ef6<\/p>\n<p>#define PORT 8080<br \/>\n#define BUFFER_SIZE 1024<br \/>\n#define MAX_CLIENTS 10240   \/\/ poll \u6ca1\u6709\u786c\u9650\u5236&#xff0c;\u8fd9\u91cc\u8bbe\u4e00\u4e2a\u5408\u7406\u7684\u8f6f\u4e0a\u9650<\/p>\n<p>int main() {<br \/>\n    int server_fd, client_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>    \/\/ pollfd \u6570\u7ec4&#xff1a;\u5b58\u6240\u6709\u8981\u76d1\u63a7\u7684 fd<br \/>\n    struct pollfd fds[MAX_CLIENTS &#043; 1];  \/\/ &#043;1 \u7ed9 server_fd<br \/>\n    int nfds &#061; 0;  \/\/ \u5f53\u524d\u6570\u7ec4\u4e2d\u6709\u591a\u5c11\u4e2a\u6709\u6548\u6761\u76ee<\/p>\n<p>    \/\/ \u2460 \u521b\u5efa\u5957\u63a5\u5b57<br \/>\n    server_fd &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n    int opt &#061; 1;<br \/>\n    setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &amp;opt, sizeof(opt));<\/p>\n<p>    \/\/ \u2461 \u7ed1\u5b9a &#043; \u76d1\u542c<br \/>\n    memset(&amp;server_addr, 0, sizeof(server_addr));<br \/>\n    server_addr.sin_family &#061; AF_INET;<br \/>\n    server_addr.sin_port &#061; htons(PORT);<br \/>\n    server_addr.sin_addr.s_addr &#061; INADDR_ANY;<br \/>\n    bind(server_fd, (struct sockaddr *)&amp;server_addr, sizeof(server_addr));<br \/>\n    listen(server_fd, 5);<\/p>\n<p>    \/\/ \u2462 \u628a server_fd \u52a0\u5165\u76d1\u63a7\u5217\u8868<br \/>\n    fds[0].fd &#061; server_fd;<br \/>\n    fds[0].events &#061; POLLIN;  \/\/ POLLIN \u8868\u793a&#034;\u53ef\u8bfb&#034;&#xff08;\u65b0\u8fde\u63a5\u6216\u6570\u636e\u5230\u8fbe&#xff09;<br \/>\n    nfds &#061; 1;<\/p>\n<p>    printf(&#034;poll \u670d\u52a1\u5668\u542f\u52a8&#xff0c;\u76d1\u542c\u7aef\u53e3 %d\\\\n&#034;, PORT);<\/p>\n<p>    while (1) {<br \/>\n        \/\/ \u2463 \u8c03\u7528 poll&#xff1a;\u963b\u585e\u7b49\u5f85&#xff0c;\u6709\u4e8b\u4ef6\u65f6\u8fd4\u56de<br \/>\n        \/\/    timeout&#061;-1 \u8868\u793a\u6c38\u8fdc\u7b49<br \/>\n        int ready &#061; poll(fds, nfds, -1);<br \/>\n        if (ready &lt; 0) {<br \/>\n            perror(&#034;poll&#034;);<br \/>\n            continue;<br \/>\n        }<\/p>\n<p>        \/\/ \u2464 \u904d\u5386\u68c0\u67e5\u6bcf\u4e2a fd<br \/>\n        for (int i &#061; 0; i &lt; nfds &amp;&amp; ready &gt; 0; i&#043;&#043;) {<br \/>\n            \/\/ revents \u4e2d\u662f\u5426\u6709\u6211\u4eec\u5173\u5fc3\u7684\u4e8b\u4ef6<br \/>\n            if (fds[i].revents &#061;&#061; 0) continue;<\/p>\n<p>            if (fds[i].fd &#061;&#061; server_fd) {<br \/>\n                \/\/ &#061;&#061;&#061;&#061;&#061; server_fd&#xff1a;\u6709\u65b0\u8fde\u63a5 &#061;&#061;&#061;&#061;&#061;<br \/>\n                if (fds[i].revents &amp; POLLIN) {<br \/>\n                    client_fd &#061; accept(server_fd,<br \/>\n                                      (struct sockaddr *)&amp;client_addr,<br \/>\n                                      &amp;client_len);<br \/>\n                    if (client_fd &gt;&#061; 0 &amp;&amp; nfds &lt; MAX_CLIENTS &#043; 1) {<br \/>\n                        printf(&#034;\u65b0\u5ba2\u6237\u7aef %s:%d \u8fde\u63a5&#xff0c;fd&#061;%d\\\\n&#034;,<br \/>\n                               inet_ntoa(client_addr.sin_addr),<br \/>\n                               ntohs(client_addr.sin_port), client_fd);<\/p>\n<p>                        fds[nfds].fd &#061; client_fd;<br \/>\n                        fds[nfds].events &#061; POLLIN;  \/\/ \u76d1\u63a7\u53ef\u8bfb<br \/>\n                        nfds&#043;&#043;;<br \/>\n                    } else if (client_fd &gt;&#061; 0) {<br \/>\n                        printf(&#034;\u5ba2\u6237\u7aef\u6570\u91cf\u5df2\u8fbe\u4e0a\u9650&#xff0c;\u62d2\u7edd\u65b0\u8fde\u63a5\\\\n&#034;);<br \/>\n                        close(client_fd);<br \/>\n                    }<br \/>\n                }<br \/>\n                ready&#8211;;<\/p>\n<p>            } else {<br \/>\n                \/\/ &#061;&#061;&#061;&#061;&#061; \u5ba2\u6237\u7aef fd&#xff1a;\u6709\u6570\u636e\u6216\u65ad\u5f00 &#061;&#061;&#061;&#061;&#061;<br \/>\n                int fd &#061; fds[i].fd;<\/p>\n<p>                if (fds[i].revents &amp; POLLIN) {<br \/>\n                    int n &#061; read(fd, buffer, sizeof(buffer) &#8211; 1);<\/p>\n<p>                    if (n &lt;&#061; 0) {<br \/>\n                        \/\/ \u65ad\u5f00\u6216\u51fa\u9519<br \/>\n                        if (n &#061;&#061; 0) printf(&#034;\u5ba2\u6237\u7aef fd&#061;%d \u65ad\u5f00\\\\n&#034;, fd);<br \/>\n                        else perror(&#034;read&#034;);<br \/>\n                        close(fd);<\/p>\n<p>                        \/\/ \u4ece\u6570\u7ec4\u4e2d\u79fb\u9664&#xff1a;\u628a\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u642c\u8fc7\u6765&#xff0c;\u7136\u540e nfds&#8211;<br \/>\n                        fds[i] &#061; fds[nfds &#8211; 1];<br \/>\n                        nfds&#8211;;<br \/>\n                        i&#8211;;  \/\/ \u56de\u9000\u4e00\u6b65&#xff0c;\u56e0\u4e3a\u5f53\u524d\u4f4d\u7f6e\u73b0\u5728\u662f\u65b0\u7684\u5143\u7d20<br \/>\n                    } else {<br \/>\n                        buffer[n] &#061; &#039;\\\\0&#039;;<br \/>\n                        printf(&#034;\u5ba2\u6237\u7aef fd&#061;%d \u53d1\u6765: %s&#034;, fd, buffer);<br \/>\n                        write(fd, buffer, n);<br \/>\n                    }<br \/>\n                }<\/p>\n<p>                \/\/ \u4e5f\u53ef\u4ee5\u68c0\u67e5 POLLERR&#xff08;\u8fde\u63a5\u51fa\u9519&#xff09;\u548c POLLHUP&#xff08;\u5ba2\u6237\u7aef\u6302\u65ad&#xff09;<br \/>\n                if (fds[i].revents &amp; (POLLERR | POLLHUP)) {<br \/>\n                    printf(&#034;\u5ba2\u6237\u7aef fd&#061;%d \u5f02\u5e38\u65ad\u5f00\\\\n&#034;, fd);<br \/>\n                    close(fd);<br \/>\n                    fds[i] &#061; fds[nfds &#8211; 1];<br \/>\n                    nfds&#8211;;<br \/>\n                    i&#8211;;<br \/>\n                }<\/p>\n<p>                ready&#8211;;<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    close(server_fd);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u7b2c 3 \u6b65 \u2192\u00a0\u7f16\u8bd1\u8fd0\u884c&#xff1a;<\/p>\n<p>gcc -Wall -g poll_server.c -o poll_server<br \/>\n.\/poll_server<\/p>\n<h5 id=\"534-poll-\u7684\u4e0d\u8db3\">5.3.4 poll \u7684\u4e0d\u8db3<\/h5>\n<p>poll \u89e3\u51b3\u4e86 select \u7684&#034;1024 \u4e0a\u9650&#034;\u95ee\u9898&#xff0c;\u4f46\u904d\u5386\u6240\u6709 fd \u7684 O(n) \u95ee\u9898\u4ecd\u5728\u3002\u5f53\u4f60\u6709 1 \u4e07\u4e2a\u8fde\u63a5\u4f46\u53ea\u6709 3 \u4e2a\u6d3b\u8dc3\u65f6&#xff0c;poll \u4ecd\u7136\u8981\u626b\u63cf\u5168\u90e8 1 \u4e07\u4e2a\u3002\u8fd9\u5c31\u662f epoll \u8981\u89e3\u51b3\u7684\u3002<\/p>\n<hr \/>\n<h4 id=\"54-epoll-linux-\u738b\u724c\u767b\u573a\">5.4 epoll \u2014\u2014 Linux \u738b\u724c\u767b\u573a<\/h4>\n<h5 id=\"541-\u6838\u5fc3\u601d\u60f3\">5.4.1 \u6838\u5fc3\u601d\u60f3<\/h5>\n<p>select\/poll&#xff1a;\u6bcf\u6b21\u8c03\u7528\u90fd\u628a&#034;\u5168\u73ed\u540d\u5355&#034;\u4ea4\u4e0a\u53bb&#xff0c;\u8001\u5e08\u8bf4&#034;\u81ea\u5df1\u53bb\u6570\u8c01\u4e3e\u624b&#034;<br \/>\nepoll&#xff1a;     \u53ea\u5728\u5b66\u671f\u521d\u767b\u8bb0\u4e00\u6b21\u3002\u4e4b\u540e\u6709\u4eba\u4e3e\u624b&#xff0c;\u8001\u5e08\u76f4\u63a5\u9012\u7ed9\u4f60\u4e00\u5f20<br \/>\n            &#034;\u4e3e\u624b\u540d\u5355&#034;\u2014\u2014\u4f60\u53ea\u9700\u8981\u5904\u7406\u540d\u5355\u4e0a\u7684\u4eba&#xff01;<\/p>\n<p>epoll \u7684\u4e09\u4e2a\u6838\u5fc3 API&#xff1a;<\/p>\n<p>#include &lt;sys\/epoll.h&gt;<\/p>\n<p>\/\/ \u2460 \u521b\u5efa epoll \u5b9e\u4f8b&#xff08;\u76f8\u5f53\u4e8e\u5728\u8001\u5e08\u90a3\u91cc\u6ce8\u518c\u4e00\u4e2a&#034;\u4e3e\u624b\u76d1\u542c\u7cfb\u7edf&#034;&#xff09;<br \/>\nint epoll_create1(int flags);   \/\/ flags \u901a\u5e38\u8bbe\u4e3a 0<\/p>\n<p>\/\/ \u2461 \u6ce8\u518c\/\u4fee\u6539\/\u5220\u9664\u8981\u76d1\u63a7\u7684 fd&#xff08;\u76f8\u5f53\u4e8e\u628a\u5b66\u751f\u52a0\u5165\u4e3e\u624b\u76d1\u542c\u7cfb\u7edf&#xff09;<br \/>\nint epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);<br \/>\n\/\/ op: EPOLL_CTL_ADD&#xff08;\u52a0\u5165&#xff09;\/ EPOLL_CTL_MOD&#xff08;\u4fee\u6539&#xff09;\/ EPOLL_CTL_DEL&#xff08;\u5220\u9664&#xff09;<\/p>\n<p>\/\/ \u2462 \u7b49\u5f85\u4e8b\u4ef6&#xff08;\u76f8\u5f53\u4e8e\u8001\u5e08\u8bf4&#xff1a;&#034;\u8fd9\u662f\u4e3e\u624b\u540d\u5355&#xff0c;\u4f60\u5904\u7406\u5427&#034;&#xff09;<br \/>\nint epoll_wait(int epfd, struct epoll_event *events,<br \/>\n               int maxevents, int timeout);<\/p>\n<p>struct epoll_event {<br \/>\n    uint32_t events;    \/\/ EPOLLIN&#xff08;\u53ef\u8bfb&#xff09;\/ EPOLLOUT&#xff08;\u53ef\u5199&#xff09;\/ EPOLLERR&#xff08;\u9519\u8bef&#xff09;<br \/>\n    epoll_data_t data;  \/\/ \u53ef\u4ee5\u5b58 fd \u6216\u81ea\u5b9a\u4e49\u6307\u9488<br \/>\n};<\/p>\n<h5 id=\"542-epoll-\u4e3a\u4ec0\u4e48\u5feb\">5.4.2 epoll \u4e3a\u4ec0\u4e48\u5feb&#xff1f;<\/h5>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">\u539f\u56e0<\/td>\n<td style=\"background-color:#dddddd\">\u89e3\u91ca<\/td>\n<\/tr>\n<tr>\n<td>\u4e8b\u4ef6\u9a71\u52a8<\/td>\n<td>\u5c31\u7eea\u7684 fd \u4f1a\u81ea\u52a8\u6302\u5230\u5185\u6838\u7684\u5c31\u7eea\u94fe\u8868\u4e0a&#xff0c;epoll_wait \u53ea\u8fd4\u56de\u8fd9\u4e9b\u5c31\u7eea fd&#xff0c;\u4e0d\u626b\u63cf\u5168\u91cf<\/td>\n<\/tr>\n<tr>\n<td>\u7ea2\u9ed1\u6811\u5b58\u50a8<\/td>\n<td>\u6240\u6709\u6ce8\u518c\u7684 fd \u5b58\u5728\u7ea2\u9ed1\u6811\u4e2d&#xff0c;\u589e\u5220\u6539\u67e5\u90fd\u662f O(log n)<\/td>\n<\/tr>\n<tr>\n<td>\u53ea\u62f7\u8d1d\u5c31\u7eea\u4e8b\u4ef6<\/td>\n<td>epoll_wait \u53ea\u628a\u5c31\u7eea\u7684 fd \u4fe1\u606f\u4ece\u5185\u6838\u62f7\u5230\u7528\u6237\u6001&#xff0c;\u4e0d\u662f\u5168\u90e8 fd<\/td>\n<\/tr>\n<tr>\n<td>\u957f\u671f\u6ce8\u518c<\/td>\n<td>fd \u6ce8\u518c\u4e00\u6b21\u540e\u957f\u671f\u6709\u6548&#xff0c;\u4e0d\u9700\u8981\u6bcf\u6b21\u5faa\u73af\u91cd\u65b0\u4f20\u5165<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u901a\u4fd7\u7c7b\u6bd4&#xff1a;select\/poll \u50cf\u662f\u4f60\u6bcf\u5929\u628a\u5168\u73ed\u540d\u5355\u4ea4\u7ed9\u8001\u5e08&#xff0c;\u8001\u5e08\u6bcf\u5929\u4ece\u5934\u6570\u4e00\u904d\u3002epoll \u50cf\u662f\u8001\u5e08\u624b\u91cc\u6709\u4e2a&#034;\u4e3e\u624b\u94c3&#034;\u2014\u2014\u53ea\u6709\u4e3e\u624b\u7684\u5b66\u751f&#xff08;\u5c31\u7eea fd&#xff09;\u624d\u4f1a\u89e6\u53d1\u94c3\u54cd&#xff0c;\u8001\u5e08\u76f4\u63a5\u628a\u4e3e\u624b\u540d\u5355\u7ed9\u4f60\u3002<\/p>\n<h5 id=\"543-\u4e24\u79cd\u89e6\u53d1\u6a21\u5f0f\">5.4.3 \u4e24\u79cd\u89e6\u53d1\u6a21\u5f0f<\/h5>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">\u6a21\u5f0f<\/td>\n<td style=\"background-color:#dddddd\">\u884c\u4e3a<\/td>\n<td style=\"background-color:#dddddd\">\u9002\u7528\u573a\u666f<\/td>\n<\/tr>\n<tr>\n<td>LT&#xff08;\u6c34\u5e73\u89e6\u53d1&#xff0c;\u9ed8\u8ba4&#xff09;<\/td>\n<td>\u53ea\u8981\u7f13\u51b2\u533a\u6709\u6570\u636e&#xff0c;\u6bcf\u6b21 epoll_wait \u90fd\u4f1a\u901a\u77e5\u4f60<\/td>\n<td>\u7b80\u5355\u53ef\u9760&#xff0c;\u517c\u5bb9 select\/poll \u7f16\u7a0b\u6a21\u578b&#xff0c;\u4e0d\u6613\u6f0f\u4e8b\u4ef6<\/td>\n<\/tr>\n<tr>\n<td>ET&#xff08;\u8fb9\u7f18\u89e6\u53d1&#xff09;<\/td>\n<td>\u53ea\u5728\u72b6\u6001\u53d8\u5316\u65f6\u901a\u77e5\u4e00\u6b21&#xff08;\u4ece\u65e0\u5230\u6709&#xff09;<\/td>\n<td>\u9ad8\u6027\u80fd&#xff0c;\u51cf\u5c11\u91cd\u590d\u901a\u77e5&#xff0c;\u4f46\u8981\u6c42 fd \u5fc5\u987b\u8bbe\u4e3a\u975e\u963b\u585e&#xff0c;\u5426\u5219\u5bb9\u6613\u5361\u6b7b<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>ET \u6a21\u5f0f\u901a\u4fd7\u89e3\u91ca&#xff1a;<br \/>\nLT &#061; &#034;\u4f60\u7897\u91cc\u8fd8\u6709\u996d&#xff0c;\u6211\u6bcf\u6b21\u8d70\u8fc7\u90fd\u63d0\u9192\u4f60\u4e00\u4e0b&#034;<br \/>\nET &#061; &#034;\u6211\u558a\u4e00\u6b21&#039;\u5f00\u996d\u4e86&#039;&#xff0c;\u4f60\u81ea\u5df1\u8bb0\u5f97\u5403\u5230\u7897\u7a7a&#xff0c;\u6211\u4e0d\u4f1a\u518d\u558a&#034;<\/p>\n<p>\u26a0\ufe0f\u00a0ET \u6a21\u5f0f\u7684\u7ea2\u7ebf&#xff1a;\u4f7f\u7528 ET \u6a21\u5f0f\u65f6&#xff0c;fd \u5fc5\u987b\u662f\u975e\u963b\u585e\u7684&#xff0c;\u4e14\u5fc5\u987b\u5faa\u73af\u00a0read\u00a0\u76f4\u5230\u8fd4\u56de\u00a0EAGAIN&#xff08;\u8868\u793a\u8bfb\u7a7a\u4e86&#xff09;&#xff0c;\u5426\u5219\u53ef\u80fd\u6f0f\u6389\u6570\u636e\u3002<\/p>\n<h5 id=\"544-\u5b8c\u6574\u4ee3\u7801epoll_serverclt-\u6a21\u5f0f\u5373\u9ed8\u8ba4\u6a21\u5f0f\u6700\u5b89\u5168\">5.4.4 \u5b8c\u6574\u4ee3\u7801&#xff1a;epoll_server.c&#xff08;LT \u6a21\u5f0f&#xff0c;\u5373\u9ed8\u8ba4\u6a21\u5f0f&#xff0c;\u6700\u5b89\u5168&#xff09;<\/h5>\n<p>\u7b2c 1 \u6b65 \u2192\u00a0\u521b\u5efa\u6587\u4ef6&#xff1a;<\/p>\n<p>vim epoll_server.c<\/p>\n<p>\u7b2c 2 \u6b65 \u2192\u00a0\u8f93\u5165\u4ee3\u7801&#xff1a;<\/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;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;sys\/epoll.h&gt;  \/\/ epoll \u9700\u8981\u8fd9\u4e2a\u5934\u6587\u4ef6<\/p>\n<p>#define PORT 8080<br \/>\n#define BUFFER_SIZE 1024<br \/>\n#define MAX_EVENTS 10240  \/\/ \u4e00\u6b21\u6700\u591a\u5904\u7406\u8fd9\u4e48\u591a\u5c31\u7eea\u4e8b\u4ef6<\/p>\n<p>int main() {<br \/>\n    int server_fd, client_fd, epfd;<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>    \/\/ \u2460 \u521b\u5efa\u5957\u63a5\u5b57<br \/>\n    server_fd &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n    int opt &#061; 1;<br \/>\n    setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &amp;opt, sizeof(opt));<\/p>\n<p>    \/\/ \u2461 \u7ed1\u5b9a &#043; \u76d1\u542c<br \/>\n    memset(&amp;server_addr, 0, sizeof(server_addr));<br \/>\n    server_addr.sin_family &#061; AF_INET;<br \/>\n    server_addr.sin_port &#061; htons(PORT);<br \/>\n    server_addr.sin_addr.s_addr &#061; INADDR_ANY;<br \/>\n    bind(server_fd, (struct sockaddr *)&amp;server_addr, sizeof(server_addr));<br \/>\n    listen(server_fd, 5);<\/p>\n<p>    \/\/ \u2462 \u521b\u5efa epoll \u5b9e\u4f8b<br \/>\n    \/\/    epoll_create1(0) \u7b49\u4ef7\u4e8e\u65e7\u7248\u7684 epoll_create(\u4efb\u610f\u6b63\u6570)<br \/>\n    epfd &#061; epoll_create1(0);<br \/>\n    if (epfd &#061;&#061; -1) {<br \/>\n        perror(&#034;epoll_create1&#034;);<br \/>\n        exit(1);<br \/>\n    }<\/p>\n<p>    \/\/ \u2463 \u628a server_fd \u6ce8\u518c\u5230 epoll<br \/>\n    struct epoll_event ev, events[MAX_EVENTS];<br \/>\n    ev.events &#061; EPOLLIN;          \/\/ \u5173\u5fc3&#034;\u53ef\u8bfb&#034;\u4e8b\u4ef6&#xff08;\u65b0\u8fde\u63a5\u5230\u6765&#xff09;<br \/>\n    ev.data.fd &#061; server_fd;       \/\/ \u628a fd \u5b58\u8fdb\u53bb&#xff0c;\u65b9\u4fbf\u540e\u9762\u53d6\u51fa\u6765<br \/>\n    epoll_ctl(epfd, EPOLL_CTL_ADD, server_fd, &amp;ev);<\/p>\n<p>    printf(&#034;epoll \u670d\u52a1\u5668\u542f\u52a8&#xff08;LT \u6a21\u5f0f&#xff09;&#xff0c;\u76d1\u542c\u7aef\u53e3 %d\\\\n&#034;, PORT);<\/p>\n<p>    while (1) {<br \/>\n        \/\/ \u2464 epoll_wait&#xff1a;\u963b\u585e\u7b49\u5f85&#xff0c;\u6709\u4e8b\u4ef6\u65f6\u8fd4\u56de<br \/>\n        \/\/    \u53c2\u6570&#xff1a;epoll\u5b9e\u4f8b\u3001\u5b58\u653e\u4e8b\u4ef6\u7684\u6570\u7ec4\u3001\u6570\u7ec4\u5927\u5c0f\u3001\u8d85\u65f6&#xff08;-1&#061;\u6c38\u8fdc\u7b49&#xff09;<br \/>\n        int nfds &#061; epoll_wait(epfd, events, MAX_EVENTS, -1);<br \/>\n        if (nfds &lt; 0) {<br \/>\n            perror(&#034;epoll_wait&#034;);<br \/>\n            continue;<br \/>\n        }<\/p>\n<p>        \/\/ \u2465 \u904d\u5386\u6240\u6709\u5c31\u7eea\u4e8b\u4ef6\u2014\u2014\u6ce8\u610f&#xff01;\u53ea\u904d\u5386&#034;\u5c31\u7eea&#034;\u7684&#xff0c;\u4e0d\u662f\u5168\u90e8 fd<br \/>\n        for (int i &#061; 0; i &lt; nfds; i&#043;&#043;) {<br \/>\n            int fd &#061; events[i].data.fd;<\/p>\n<p>            if (fd &#061;&#061; server_fd) {<br \/>\n                \/\/ &#061;&#061;&#061;&#061;&#061; server_fd&#xff1a;\u6709\u65b0\u8fde\u63a5 &#061;&#061;&#061;&#061;&#061;<br \/>\n                client_fd &#061; accept(server_fd,<br \/>\n                                  (struct sockaddr *)&amp;client_addr,<br \/>\n                                  &amp;client_len);<br \/>\n                if (client_fd &gt;&#061; 0) {<br \/>\n                    printf(&#034;\u65b0\u5ba2\u6237\u7aef %s:%d \u8fde\u63a5&#xff0c;fd&#061;%d\\\\n&#034;,<br \/>\n                           inet_ntoa(client_addr.sin_addr),<br \/>\n                           ntohs(client_addr.sin_port), client_fd);<\/p>\n<p>                    \/\/ \u628a\u65b0\u5ba2\u6237\u7aef\u4e5f\u6ce8\u518c\u5230 epoll<br \/>\n                    ev.events &#061; EPOLLIN;<br \/>\n                    ev.data.fd &#061; client_fd;<br \/>\n                    epoll_ctl(epfd, EPOLL_CTL_ADD, client_fd, &amp;ev);<br \/>\n                }<\/p>\n<p>            } else {<br \/>\n                \/\/ &#061;&#061;&#061;&#061;&#061; \u5ba2\u6237\u7aef fd&#xff1a;\u6709\u6570\u636e\u6216\u65ad\u5f00 &#061;&#061;&#061;&#061;&#061;<br \/>\n                int n &#061; read(fd, buffer, sizeof(buffer) &#8211; 1);<\/p>\n<p>                if (n &lt;&#061; 0) {<br \/>\n                    \/\/ \u65ad\u5f00\u6216\u51fa\u9519\u2014\u2014\u4ece epoll \u4e2d\u79fb\u9664\u8fd9\u4e2a fd<br \/>\n                    if (n &#061;&#061; 0) {<br \/>\n                        printf(&#034;\u5ba2\u6237\u7aef fd&#061;%d \u65ad\u5f00\u8fde\u63a5\\\\n&#034;, fd);<br \/>\n                    } else {<br \/>\n                        perror(&#034;read&#034;);<br \/>\n                    }<br \/>\n                    epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL);  \/\/ \u4ece epoll \u79fb\u9664<br \/>\n                    close(fd);<br \/>\n                } else {<br \/>\n                    \/\/ \u6536\u5230\u6570\u636e&#xff0c;\u539f\u6837\u53d1\u56de<br \/>\n                    buffer[n] &#061; &#039;\\\\0&#039;;<br \/>\n                    printf(&#034;\u5ba2\u6237\u7aef fd&#061;%d \u53d1\u6765: %s&#034;, fd, buffer);<br \/>\n                    write(fd, buffer, n);<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    close(server_fd);<br \/>\n    close(epfd);  \/\/ \u5173\u95ed epoll \u5b9e\u4f8b<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u7b2c 3 \u6b65 \u2192\u00a0\u7f16\u8bd1\u8fd0\u884c&#xff1a;<\/p>\n<p>gcc -Wall -g epoll_server.c -o epoll_server<br \/>\n.\/epoll_server<\/p>\n<h5 id=\"545-et-\u6a21\u5f0f\u7248\u672c\u8fdb\u9636\">5.4.5 ET \u6a21\u5f0f\u7248\u672c&#xff08;\u8fdb\u9636&#xff09;<\/h5>\n<p>\u5982\u679c\u4f60\u60f3\u4f53\u9a8c ET \u6a21\u5f0f\u7684\u9ad8\u6027\u80fd&#xff08;\u4ee5\u53ca\u5b83\u5e26\u6765\u7684\u590d\u6742\u5ea6&#xff09;&#xff0c;\u5c06\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u7684\u6838\u5fc3\u90e8\u5206\u6539\u4e3a&#xff1a;<\/p>\n<p>\/\/ \u2460 \u8bbe\u7f6e server_fd \u548c\u6240\u6709 client_fd \u4e3a\u975e\u963b\u585e<br \/>\nint set_nonblocking(int fd) {<br \/>\n    int flags &#061; fcntl(fd, F_GETFL, 0);<br \/>\n    return fcntl(fd, F_SETFL, flags | O_NONBLOCK);<br \/>\n}<br \/>\nset_nonblocking(server_fd);<\/p>\n<p>\/\/ \u2461 \u6ce8\u518c\u65f6\u52a0\u4e0a EPOLLET \u6807\u5fd7<br \/>\nev.events &#061; EPOLLIN | EPOLLET;  \/\/ \u8fb9\u7f18\u89e6\u53d1\u6a21\u5f0f<br \/>\nev.data.fd &#061; server_fd;<br \/>\nepoll_ctl(epfd, EPOLL_CTL_ADD, server_fd, &amp;ev);<\/p>\n<p>\/\/ \u2462 \u8bfb\u53d6\u6570\u636e\u65f6\u5fc5\u987b\u5faa\u73af\u8bfb\u5230 EAGAIN<br \/>\nwhile (1) {<br \/>\n    n &#061; read(fd, buffer, sizeof(buffer) &#8211; 1);<br \/>\n    if (n &#061;&#061; -1) {<br \/>\n        if (errno &#061;&#061; EAGAIN || errno &#061;&#061; EWOULDBLOCK) {<br \/>\n            break;  \/\/ \u8bfb\u5b8c\u4e86&#xff0c;\u6ca1\u6709\u66f4\u591a\u6570\u636e<br \/>\n        }<br \/>\n        perror(&#034;read&#034;);<br \/>\n        break;<br \/>\n    }<br \/>\n    if (n &#061;&#061; 0) {<br \/>\n        \/\/ \u5ba2\u6237\u7aef\u5173\u95ed<br \/>\n        epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL);<br \/>\n        close(fd);<br \/>\n        break;<br \/>\n    }<br \/>\n    buffer[n] &#061; &#039;\\\\0&#039;;<br \/>\n    write(fd, buffer, n);<br \/>\n}<\/p>\n<p>\u26a0\ufe0f\u00a0ET \u6a21\u5f0f\u8b66\u544a&#xff1a;\u4e0d\u8bbe\u975e\u963b\u585e &#043; \u4e0d\u5faa\u73af read \u5230 EAGAIN \u2192 \u6570\u636e\u53ef\u80fd\u6c38\u8fdc\u4e22\u5931\u3002\u521d\u5b66\u8005\u5efa\u8bae\u5148\u7528 LT \u6a21\u5f0f\u8dd1\u901a&#xff0c;\u518d\u5c1d\u8bd5 ET\u3002<\/p>\n<hr \/>\n<h4 id=\"55-\u4e09\u5144\u5f1f\u7ec8\u6781\u5bf9\u51b3-\u4e00\u8868\u5b9a\u4e7e\u5764\">5.5 \u4e09\u5144\u5f1f\u7ec8\u6781\u5bf9\u51b3 \u2014\u2014 \u4e00\u8868\u5b9a\u4e7e\u5764<\/h4>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">\u7ef4\u5ea6<\/td>\n<td style=\"background-color:#dddddd\">select<\/td>\n<td style=\"background-color:#dddddd\">poll<\/td>\n<td style=\"background-color:#dddddd\">epoll<\/td>\n<\/tr>\n<tr>\n<td>fd \u4e0a\u9650<\/td>\n<td>FD_SETSIZE&#xff08;\u901a\u5e38 1024&#xff09;&#xff0c;\u6539\u5b83\u8981\u91cd\u7f16\u8bd1\u5185\u6838<\/td>\n<td>\u65e0\u786c\u9650\u5236&#xff0c;\u53d7\u5185\u5b58\u9650\u5236<\/td>\n<td>\u65e0\u786c\u9650\u5236&#xff0c;\u53d7\u5185\u5b58\u9650\u5236<\/td>\n<\/tr>\n<tr>\n<td>\u6bcf\u6b21\u8c03\u7528\u662f\u5426\u91cd\u4f20 fd \u5217\u8868<\/td>\n<td>\u662f&#xff08;\u62f7\u8d1d\u6574\u4e2a fd_set&#xff09;<\/td>\n<td>\u662f&#xff08;\u62f7\u8d1d\u6574\u4e2a pollfd \u6570\u7ec4&#xff09;<\/td>\n<td>\u5426&#xff08;fd \u6ce8\u518c\u5728\u5185\u6838\u7a7a\u95f4&#xff0c;\u4e00\u6b21\u6ce8\u518c\u957f\u671f\u6709\u6548&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u5185\u6838\u884c\u4e3a<\/td>\n<td>\u904d\u5386\u5168\u90e8 fd \u627e\u5230\u5c31\u7eea\u7684<\/td>\n<td>\u904d\u5386\u5168\u90e8 fd \u627e\u5230\u5c31\u7eea\u7684<\/td>\n<td>\u4e8b\u4ef6\u9a71\u52a8&#xff1a;fd \u5c31\u7eea\u65f6\u81ea\u52a8\u6302\u5230\u5c31\u7eea\u94fe\u8868<\/td>\n<\/tr>\n<tr>\n<td>\u590d\u6742\u5ea6<\/td>\n<td>O(n)&#xff0c;n &#061; \u6700\u5927 fd \u503c<\/td>\n<td>O(n)&#xff0c;n &#061; fd \u603b\u6570<\/td>\n<td>O(1)&#xff0c;\u53ea\u5173\u6ce8\u5c31\u7eea fd \u6570\u91cf<\/td>\n<\/tr>\n<tr>\n<td>\u8fd4\u56de\u540e\u7684\u5de5\u4f5c\u91cf<\/td>\n<td>\u904d\u5386\u6240\u6709 fd \u7528 FD_ISSET \u68c0\u67e5<\/td>\n<td>\u904d\u5386\u6240\u6709 fd \u68c0\u67e5 revents<\/td>\n<td>\u53ea\u904d\u5386\u8fd4\u56de\u7684\u5c31\u7eea\u4e8b\u4ef6&#xff08;epoll_wait \u8fd4\u56de\u7684 nfds \u4e2a&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u8de8\u5e73\u53f0<\/td>\n<td>\u662f&#xff08;POSIX&#xff0c;\u5168\u5e73\u53f0&#xff09;<\/td>\n<td>\u662f&#xff08;POSIX&#xff0c;\u5168\u5e73\u53f0&#xff09;<\/td>\n<td>\u5426&#xff08;Linux \u4e13\u5c5e\u3002BSD \u6709 kqueue&#xff0c;Windows \u6709 IOCP&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>C10K \u95ee\u9898<\/td>\n<td>\u65e0\u6cd5\u652f\u6491<\/td>\n<td>\u52c9\u5f3a\u652f\u6491\u4f46\u6548\u7387\u4f4e<\/td>\n<td>\u8f7b\u677e\u5e94\u5bf9&#xff08;Nginx \u5355\u673a\u767e\u4e07\u8fde\u63a5\u9760\u7684\u5c31\u662f epoll&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u9002\u7528\u573a\u666f<\/td>\n<td>\u5b66\u4e60\u539f\u7406\u3001\u5c11\u91cf fd\u3001\u8de8\u5e73\u53f0\u9700\u6c42<\/td>\n<td>\u4e2d\u5c0f\u89c4\u6a21\u3001\u8de8\u5e73\u53f0\u9700\u6c42<\/td>\n<td>\u9ad8\u5e76\u53d1\u751f\u4ea7\u73af\u5883\u3001Linux \u670d\u52a1\u5668<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#x1f4cc;\u00a0\u672f\u8bed\u901f\u67e5&#xff1a;C10K \u95ee\u9898&#xff08;1999 \u5e74\u63d0\u51fa&#xff09;\u2014\u2014\u5373&#034;\u5982\u4f55\u5728\u4e00\u53f0\u670d\u52a1\u5668\u4e0a\u540c\u65f6\u670d\u52a1 1 \u4e07\u4e2a\u5ba2\u6237\u7aef&#034;\u3002epoll \u662f Linux \u7ed9\u51fa\u7684\u6807\u51c6\u7b54\u6848\u3002\u73b0\u4ee3\u5df2\u8fdb\u5316\u5230\u00a0C10M&#xff08;\u5343\u4e07\u7ea7&#xff09;\u3002<\/p>\n<hr \/>\n<h4 id=\"56-epoll-\u5b9e\u6218\u538b\u6d4b\u5bf9\u6bd4\">5.6 epoll \u5b9e\u6218&#xff1a;\u538b\u6d4b\u5bf9\u6bd4<\/h4>\n<p>\u6211\u4eec\u6765\u76f4\u89c2\u611f\u53d7\u4e00\u4e0b\u4e09\u79cd\u6a21\u578b\u7684\u5dee\u8ddd\u3002<\/p>\n<p>\u7b2c 1 \u6b65 \u2192\u00a0\u5206\u522b\u542f\u52a8\u4e09\u4e2a\u670d\u52a1\u5668&#xff08;\u6bcf\u4e2a\u5728\u5355\u72ec\u7684\u7ec8\u7aef&#xff09;&#xff1a;<\/p>\n<p># \u7ec8\u7aef1<br \/>\n.\/select_server<\/p>\n<p># \u7ec8\u7aef2<br \/>\n.\/poll_server<\/p>\n<p># \u7ec8\u7aef3<br \/>\n.\/epoll_server<\/p>\n<p>\u7b2c 2 \u6b65 \u2192\u00a0\u67e5\u770b CPU \u548c\u5185\u5b58\u5360\u7528&#xff08;\u5f53\u8fde\u63a5\u6570\u589e\u591a\u65f6&#xff09;&#xff1a;<\/p>\n<p># \u65b0\u5f00\u7ec8\u7aef&#xff0c;\u7528 top \u6216 htop \u89c2\u5bdf<br \/>\ntop -p $(pgrep -d&#039;,&#039; select_server)<br \/>\ntop -p $(pgrep -d&#039;,&#039; poll_server)<br \/>\ntop -p $(pgrep -d&#039;,&#039; epoll_server)<\/p>\n<p>\u73b0\u8c61&#xff1a;\u5f53\u6709 1000 \u4e2a\u7a7a\u95f2\u8fde\u63a5&#xff08;\u8fde\u63a5\u4e86\u4f46\u4e0d\u53d1\u6570\u636e&#xff09;\u65f6&#xff1a;<\/p>\n<ul>\n<li>select\/poll \u6bcf\u6b21\u00a0select()\/poll()\u00a0\u8c03\u7528\u90fd\u4f1a\u904d\u5386 1000 \u4e2a fd&#xff0c;CPU \u4f7f\u7528\u7387\u660e\u663e\u4e0a\u5347\u3002<\/li>\n<li>epoll \u53ea\u8fd4\u56de\u5c31\u7eea\u7684 fd \u6570&#xff08;\u53ef\u80fd\u662f 0&#xff09;&#xff0c;CPU \u51e0\u4e4e\u4e0d\u6da8\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3 id=\"\u7b2c\u516d\u90e8\u5206\u5b9e\u6218\u5bf9\u6bd4-\u7528-telnet-\u540c\u65f6\u8fde\u63a5\u591a\u4e2a\u670d\u52a1\u5668\">\u7b2c\u516d\u90e8\u5206&#xff1a;\u5b9e\u6218\u5bf9\u6bd4 \u2014\u2014 \u7528 telnet \u540c\u65f6\u8fde\u63a5\u591a\u4e2a\u670d\u52a1\u5668<\/h3>\n<h4 id=\"61-\u6a21\u62df\u591a\u4e2a\u5ba2\u6237\u7aef\">6.1 \u6a21\u62df\u591a\u4e2a\u5ba2\u6237\u7aef<\/h4>\n<p>\u5728\u591a\u4e2a\u7ec8\u7aef\u4e2d\u540c\u65f6\u8fd0\u884c\u5ba2\u6237\u7aef&#xff1a;<\/p>\n<p># \u7ec8\u7aef1<br \/>\ntelnet 127.0.0.1 8080<\/p>\n<p># \u7ec8\u7aef2<br \/>\ntelnet 127.0.0.1 8080<\/p>\n<p># \u7ec8\u7aef3<br \/>\ntelnet 127.0.0.1 8080<\/p>\n<h4 id=\"62-\u67e5\u770b\u8fdb\u7a0b\u7ebf\u7a0bfd-\u6570\u91cf\u53d8\u5316\">6.2 \u67e5\u770b\u8fdb\u7a0b\/\u7ebf\u7a0b\/fd \u6570\u91cf\u53d8\u5316<\/h4>\n<p># \u67e5\u770b\u591a\u8fdb\u7a0b\u670d\u52a1\u5668\u7684\u5b50\u8fdb\u7a0b<br \/>\nps aux | grep fork_server<\/p>\n<p># \u67e5\u770b\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u7684\u7ebf\u7a0b\u6570&#xff08;-T \u663e\u793a\u7ebf\u7a0b&#xff0c;-p PID \u6307\u5b9a\u8fdb\u7a0b&#xff09;<br \/>\nps -T -p $(pgrep thread_server)<\/p>\n<p># \u67e5\u770b epoll \u670d\u52a1\u5668\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\nls -la \/proc\/$(pgrep epoll_server)\/fd | wc -l<\/p>\n<h4 id=\"63-\u7528-python-\u811a\u672c\u6a21\u62df\u5927\u91cf\u8fde\u63a5\">6.3 \u7528 Python \u811a\u672c\u6a21\u62df\u5927\u91cf\u8fde\u63a5<\/h4>\n<p>\u4ee5\u4e0b\u811a\u672c\u53ef\u4ee5\u5feb\u901f\u521b\u5efa 500 \u4e2a\u8fde\u63a5\u6765\u6d4b\u8bd5\u4f60\u7684\u670d\u52a1\u5668&#xff1a;<\/p>\n<p># \u4fdd\u5b58\u4e3a test_client.py&#xff0c;\u8fd0\u884c&#xff1a;python3 test_client.py<br \/>\nimport socket<br \/>\nimport time<\/p>\n<p>sockets &#061; []<br \/>\nfor i in range(500):<br \/>\n    try:<br \/>\n        s &#061; socket.socket(socket.AF_INET, socket.SOCK_STREAM)<br \/>\n        s.connect((&#039;127.0.0.1&#039;, 8080))<br \/>\n        s.send(f&#034;Hello from client {i}\\\\n&#034;.encode())<br \/>\n        sockets.append(s)<br \/>\n        print(f&#034;Client {i} connected&#034;)<br \/>\n    except Exception as e:<br \/>\n        print(f&#034;Client {i} failed: {e}&#034;)<br \/>\n        break<\/p>\n<p>print(f&#034;Total connected: {len(sockets)}&#034;)<br \/>\nprint(&#034;Press Ctrl&#043;C to disconnect all&#8230;&#034;)<br \/>\ntime.sleep(60)  # \u4fdd\u6301\u8fde\u63a5 60 \u79d2<\/p>\n<hr \/>\n<h3 id=\"\u7b2c\u4e03\u90e8\u5206\u907f\u5751\u6307\u5357\u9762\u8bd5\u5b9e\u6218\u9ad8\u9891\">\u7b2c\u4e03\u90e8\u5206&#xff1a;\u907f\u5751\u6307\u5357&#xff08;\u9762\u8bd5\/\u5b9e\u6218\u9ad8\u9891&#xff09;<\/h3>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">\u5751<\/td>\n<td style=\"background-color:#dddddd\">\u73b0\u8c61<\/td>\n<td style=\"background-color:#dddddd\">\u89e3\u51b3\u65b9\u6848<\/td>\n<\/tr>\n<tr>\n<td>\u8fdb\u7a0b\u6a21\u578b\u50f5\u5c38\u6cdb\u6ee5<\/td>\n<td>ps aux\u00a0\u770b\u5230\u5927\u91cf\u00a0&lt;defunct&gt;<\/td>\n<td>\u6ce8\u518c\u00a0SIGCHLD\u00a0&#043;\u00a0waitpid\u00a0\u5faa\u73af\u56de\u6536&#xff1b;\u6216\u00a0signal(SIGCHLD, SIG_IGN)<\/td>\n<\/tr>\n<tr>\n<td>\u7ebf\u7a0b\u6a21\u578b\u53c2\u6570\u4f20\u9012\u9519\u8bef<\/td>\n<td>\u4e24\u4e2a\u7ebf\u7a0b\u670d\u52a1\u540c\u4e00\u4e2a fd<\/td>\n<td>\u53c2\u6570\u5fc5\u987b\u5728\u5806\u4e0a\u00a0malloc&#xff0c;\u4e0d\u80fd\u7528\u6808\u53d8\u91cf\u5730\u5740<\/td>\n<\/tr>\n<tr>\n<td>\u7ebf\u7a0b\u5fd8\u8bb0 detach\/join<\/td>\n<td>\u5185\u5b58\u6301\u7eed\u589e\u957f<\/td>\n<td>\u521b\u5efa\u540e\u7528\u00a0pthread_detach&#xff0c;\u6216\u5728\u7ebf\u7a0b\u5185\u00a0pthread_detach(pthread_self())<\/td>\n<\/tr>\n<tr>\n<td>select fd \u8d85\u9650<\/td>\n<td>\u8fde\u63a5\u6570\u8d85\u8fc7 1024 \u540e\u62d2\u7edd\u65b0\u8fde\u63a5<\/td>\n<td>\u6362\u6210 poll \u6216 epoll<\/td>\n<\/tr>\n<tr>\n<td>epoll ET \u6a21\u5f0f\u5361\u6b7b<\/td>\n<td>read\u00a0\u8fd4\u56de\u540e\u7a0b\u5e8f\u4e0d\u518d\u54cd\u5e94<\/td>\n<td>ET \u5fc5\u987b\u914d\u5408\u975e\u963b\u585e fd &#043; \u5faa\u73af read \u5230 EAGAIN<\/td>\n<\/tr>\n<tr>\n<td>select \u6bcf\u6b21\u91cd\u8bbe fd_set<\/td>\n<td>\u7a0b\u5e8f\u884c\u4e3a\u5f02\u5e38&#xff0c;\u67d0\u4e9b fd &#034;\u4e22\u4e86&#034;<\/td>\n<td>\u6bcf\u6b21\u5faa\u73af\u5fc5\u987b\u00a0FD_ZERO\u00a0&#043; \u91cd\u65b0\u00a0FD_SET&#xff08;select \u4f1a\u4fee\u6539 fd_set&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>epoll_ctl \u5fd8\u8bb0 EPOLL_CTL_DEL<\/td>\n<td>\u5173\u95ed fd \u540e epoll \u4ecd\u62a5\u544a\u4e8b\u4ef6<\/td>\n<td>\u5173\u95ed fd \u524d\u5148\u00a0epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL)<\/td>\n<\/tr>\n<tr>\n<td>\u975e\u963b\u585e read \u8bef\u5224 EAGAIN<\/td>\n<td>\u7a0b\u5e8f\u9891\u7e41\u62a5\u9519\u9000\u51fa<\/td>\n<td>EAGAIN\/EWOULDBLOCK \u662f\u6b63\u5e38\u72b6\u6001&#xff0c;\u4e0d\u662f\u9519\u8bef&#xff0c;\u5e94\u00a0continue\u00a0\u7b49\u5f85<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3 id=\"\u603b\u7ed3\">\u603b\u7ed3<\/h3>\n<p>\u8fd9\u4e24\u5929\u6211\u4eec\u8de8\u8d8a\u4e86\u5355\u7528\u6237\u670d\u52a1\u7684\u85e9\u7bf1&#xff0c;\u6b63\u5f0f\u8fdb\u5165\u4e86\u9ad8\u5e76\u53d1\u7f51\u7edc\u7f16\u7a0b\u7684\u4e16\u754c&#xff1a;<\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">\u6280\u672f<\/td>\n<td style=\"background-color:#dddddd\">\u6838\u5fc3\u601d\u8def<\/td>\n<td style=\"background-color:#dddddd\">\u4e00\u53e5\u8bdd\u603b\u7ed3<\/td>\n<\/tr>\n<tr>\n<td>\u591a\u8fdb\u7a0b&#xff08;fork&#xff09;<\/td>\n<td>\u6bcf\u4e2a\u5ba2\u6237 &#061; \u4e00\u4e2a\u5b50\u8fdb\u7a0b<\/td>\n<td>\u7a33\u5982\u6cf0\u5c71&#xff0c;\u4f46\u625b\u4e0d\u4f4f\u6d77\u91cf\u8fde\u63a5<\/td>\n<\/tr>\n<tr>\n<td>\u591a\u7ebf\u7a0b&#xff08;pthread&#xff09;<\/td>\n<td>\u6bcf\u4e2a\u5ba2\u6237 &#061; \u4e00\u4e2a\u7ebf\u7a0b<\/td>\n<td>\u8f7b\u91cf\u5feb\u901f&#xff0c;\u4f46\u4e00\u635f\u4ff1\u635f<\/td>\n<\/tr>\n<tr>\n<td>select<\/td>\n<td>\u5185\u6838\u8f6e\u8be2 &#043; \u4f4d\u56fe<\/td>\n<td>\u7ecf\u5178\u5165\u95e8&#xff0c;\u4e0a\u9650 1024&#xff0c;\u904d\u5386\u5168\u91cf<\/td>\n<\/tr>\n<tr>\n<td>poll<\/td>\n<td>\u5185\u6838\u8f6e\u8be2 &#043; \u52a8\u6001\u6570\u7ec4<\/td>\n<td>\u53bb\u6389 1024 \u9650\u5236&#xff0c;\u4f46\u904d\u5386\u5168\u91cf\u95ee\u9898\u4ecd\u5728<\/td>\n<\/tr>\n<tr>\n<td>epoll<\/td>\n<td>\u4e8b\u4ef6\u9a71\u52a8 &#043; \u7ea2\u9ed1\u6811 &#043; \u5c31\u7eea\u94fe\u8868<\/td>\n<td>Linux \u738b\u8005&#xff0c;\u53ea\u5173\u6ce8\u5c31\u7eea\u4e8b\u4ef6&#xff0c;O(1) \u6548\u7387<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4 id=\"\u6280\u672f\u6f14\u8fdb\u8def\u7ebf\u56fe\">\u6280\u672f\u6f14\u8fdb\u8def\u7ebf\u56fe<\/h4>\n<p>\u5355\u7ebf\u7a0b\u963b\u585e\u670d\u52a1\u5668&#xff08;\u6392\u961f\u6a21\u5f0f&#xff09;<br \/>\n    \u2502<br \/>\n    \u251c\u2500\u2500\u2192 \u591a\u8fdb\u7a0b&#xff08;fork&#xff09;\u2500\u2500\u2192 \u7ebf\u7a0b\u6c60&#xff08;\u9884\u521b\u5efa\u7ebf\u7a0b\u590d\u7528&#xff09;<br \/>\n    \u2502<br \/>\n    \u251c\u2500\u2500\u2192 \u591a\u7ebf\u7a0b&#xff08;pthread&#xff09;\u2500\u2500\u2192 \u7ebf\u7a0b\u6c60 &#043; \u4efb\u52a1\u961f\u5217<br \/>\n    \u2502<br \/>\n    \u2514\u2500\u2500\u2192 IO \u590d\u7528 \u2500\u2500\u2192 select&#xff08;1983&#xff09;<br \/>\n                    \u2502<br \/>\n                    \u2514\u2500\u2500\u2192 poll&#xff08;1997&#xff09;<br \/>\n                            \u2502<br \/>\n                            \u2514\u2500\u2500\u2192 epoll&#xff08;2002&#xff09;\u2500\u2500\u2192 \u73b0\u4ee3\u9ad8\u6027\u80fd\u670d\u52a1\u5668\u6807\u914d<br \/>\n                                                    \u2502<br \/>\n                                                    \u2514\u2500\u2500\u2192 Reactor \u6a21\u5f0f<br \/>\n                                                    \u2514\u2500\u2500\u2192 Proactor \u6a21\u5f0f<\/p>\n<p>\u73b0\u5b9e\u4e16\u754c&#xff1a;Nginx \u7528 epoll &#043; \u5c11\u91cf worker \u8fdb\u7a0b&#xff0c;\u5355\u673a\u8f7b\u677e\u767e\u4e07\u5e76\u53d1\u3002Redis \u7528 epoll \u5b9e\u73b0\u5355\u7ebf\u7a0b\u5904\u7406\u6570\u4e07\u5ba2\u6237\u7aef\u3002\u4f60\u4eca\u5929\u5199\u7684 epoll_server \u5c31\u662f\u8fd9\u4e9b\u9876\u7ea7\u5f00\u6e90\u9879\u76ee\u7684&#034;\u79cd\u5b50&#034;\u3002<\/p>\n<hr \/>\n<h3 id=\"\u5e38\u89c1\u95ee\u9898\u901f\u67e5\u8868day-24-25\">\u5e38\u89c1\u95ee\u9898\u901f\u67e5\u8868&#xff08;Day 24-25&#xff09;<\/h3>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">\u95ee\u9898\u73b0\u8c61<\/td>\n<td style=\"background-color:#dddddd\">\u539f\u56e0<\/td>\n<td style=\"background-color:#dddddd\">\u89e3\u51b3\u529e\u6cd5<\/td>\n<\/tr>\n<tr>\n<td>fork_server\u00a0\u8fd0\u884c\u4e00\u6bb5\u65f6\u95f4\u540e\u00a0ps aux\u00a0\u51fa\u73b0\u5927\u91cf\u00a0&lt;defunct&gt;<\/td>\n<td>\u5b50\u8fdb\u7a0b\u9000\u51fa\u540e\u7236\u8fdb\u7a0b\u6ca1\u56de\u6536&#xff08;\u50f5\u5c38\u8fdb\u7a0b&#xff09;<\/td>\n<td>\u52a0\u4e0a\u00a0signal(SIGCHLD, sigchld_handler)\u00a0\u548c\u00a0waitpid\u00a0\u5faa\u73af<\/td>\n<\/tr>\n<tr>\n<td>thread_server\u00a0\u7f16\u8bd1\u62a5\u00a0undefined reference to pthread_create<\/td>\n<td>\u6ca1\u94fe\u63a5 pthread \u5e93<\/td>\n<td>\u7f16\u8bd1\u547d\u4ee4\u52a0\u4e0a\u00a0-lpthread&#xff1a;gcc thread_server.c -o thread_server -lpthread<\/td>\n<\/tr>\n<tr>\n<td>\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u884c\u4e3a\u602a\u5f02&#xff0c;\u4e24\u4e2a\u5ba2\u6237\u7aef\u6536\u5230\u7684\u5185\u5bb9\u4e92\u76f8\u4e32<\/td>\n<td>\u53c2\u6570\u4f20\u9012\u7528\u4e86\u6808\u53d8\u91cf\u5730\u5740<\/td>\n<td>\u5fc5\u987b\u00a0malloc\u00a0\u5728\u5806\u4e0a\u5206\u914d&#xff0c;\u7ebf\u7a0b\u5185\u00a0free<\/td>\n<\/tr>\n<tr>\n<td>select\u00a0\u670d\u52a1\u5668\u6700\u591a\u53ea\u80fd\u8fde 1024 \u4e2a\u5ba2\u6237\u7aef<\/td>\n<td>FD_SETSIZE \u786c\u9650\u5236<\/td>\n<td>\u6362\u6210 poll \u6216 epoll<\/td>\n<\/tr>\n<tr>\n<td>epoll_wait\u00a0\u8fd4\u56de\u4e86\u4e8b\u4ef6&#xff0c;\u4f46\u00a0read\u00a0\u65f6\u6ca1\u6709\u6570\u636e<\/td>\n<td>\u53ef\u80fd\u662f ET \u6a21\u5f0f\u4e0b\u6ca1\u5faa\u73af\u8bfb\u5230\u5e95<\/td>\n<td>ET \u6a21\u5f0f\u5fc5\u987b\u5faa\u73af\u00a0read\u00a0\u76f4\u5230\u00a0errno &#061;&#061; EAGAIN<\/td>\n<\/tr>\n<tr>\n<td>epoll_ctl\u00a0\u62a5\u00a0Bad file descriptor<\/td>\n<td>fd \u5df2\u7ecf\u88ab close \u4f46\u6ca1\u4ece epoll \u5220\u9664<\/td>\n<td>\u5173\u95ed fd \u524d\u5148\u00a0epoll_ctl(&#8230;, EPOLL_CTL_DEL, fd, NULL)<\/td>\n<\/tr>\n<tr>\n<td>bind: Address already in use<\/td>\n<td>\u4e0a\u6b21\u670d\u52a1\u5668\u9000\u51fa\u540e\u7aef\u53e3\u8fd8\u5904\u4e8e TIME_WAIT \u72b6\u6001<\/td>\n<td>\u52a0\u4e0a\u00a0setsockopt(&#8230;, SO_REUSEADDR, &#8230;)<\/td>\n<\/tr>\n<tr>\n<td>ET \u6a21\u5f0f\u670d\u52a1\u5668\u8dd1\u7740\u8dd1\u7740\u4e0d\u52a8\u4e86<\/td>\n<td>fd \u6ca1\u8bbe\u975e\u963b\u585e&#xff0c;read \u963b\u585e\u5728\u67d0\u4e2a\u5ba2\u6237\u7aef\u4e0a<\/td>\n<td>\u6240\u6709 ET \u6a21\u5f0f\u4e0b\u7684 fd \u5fc5\u987b\u5148\u00a0fcntl(fd, F_SETFL, O_NONBLOCK)<\/td>\n<\/tr>\n<tr>\n<td>\u5ba2\u6237\u7aef\u65ad\u5f00\u540e\u670d\u52a1\u5668 CPU \u98d9\u5347<\/td>\n<td>\u6ca1\u6709\u6b63\u786e\u5904\u7406 read \u8fd4\u56de 0 \u7684\u60c5\u51b5&#xff0c;\u7a7a\u8f6c\u5faa\u73af<\/td>\n<td>\u68c0\u67e5\u00a0read\u00a0\u8fd4\u56de\u503c&#xff1a;&lt;&#061;0\u00a0\u65f6\u5173\u95ed fd \u5e76\u4ece\u76d1\u63a7\u5217\u8868\u79fb\u9664<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3 id=\"\u77e5\u8bc6\u70b9\u5173\u7cfb\u56fe\">\u77e5\u8bc6\u70b9\u5173\u7cfb\u56fe<\/h3>\n<p>                    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n                    \u2502         \u7f51\u7edc\u7f16\u7a0b\u57fa\u7840&#xff08;Day 22-23&#xff09;   \u2502<br \/>\n                    \u2502   socket \/ bind \/ listen \/ accept   \u2502<br \/>\n                    \u2502          TCP Echo \u670d\u52a1\u5668            \u2502<br \/>\n                    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<br \/>\n                                   \u2502<br \/>\n                    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n                    \u2502         \u5e76\u53d1\u670d\u52a1\u5668&#xff08;Day 24-25&#xff09;     \u2502<br \/>\n                    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<br \/>\n                           \u2502              \u2502<br \/>\n              \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2510    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n              \u2502  \u591a\u8fdb\u7a0b Fork  \u2502    \u2502   \u591a\u7ebf\u7a0b Pthread     \u2502<br \/>\n              \u2502  \u6bcf\u5ba2\u6237&#061;1\u8fdb\u7a0b \u2502    \u2502   \u6bcf\u5ba2\u6237&#061;1\u7ebf\u7a0b       \u2502<br \/>\n              \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<br \/>\n                     \u2502                    \u2502<br \/>\n                     \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<br \/>\n                               \u2502   \u90fd\u65e0\u6cd5\u9ad8\u6548\u652f\u6491\u6d77\u91cf\u8fde\u63a5<br \/>\n                               \u2502<br \/>\n                    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n                    \u2502         IO \u590d\u7528\u6280\u672f                \u2502<br \/>\n                    \u2502                                    \u2502<br \/>\n                    \u2502  select \u2500\u2500\u2192 poll \u2500\u2500\u2192 epoll&#xff08;\u738b\u8005&#xff09; \u2502<br \/>\n                    \u2502  1983       1997       2002        \u2502<br \/>\n                    \u2502  O(n)       O(n)       O(1)        \u2502<br \/>\n                    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<br \/>\n                               \u2502<br \/>\n                    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n                    \u2502        Reactor \/ Proactor \u6a21\u5f0f      \u2502<br \/>\n                    \u2502        &#xff08;\u8fdb\u9636&#xff1a;\u6846\u67b6\u8bbe\u8ba1\u6a21\u5f0f&#xff09;       \u2502<br \/>\n                    \u2502                                     \u2502<br \/>\n                    \u2502  Nginx\u3001Redis\u3001Netty\u3001libuv \u7684\u57fa\u77f3  \u2502<br \/>\n                    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<\/p>\n<hr \/>\n<h3 id=\"\u4f5c\u4e1aday-24-25-\u5de9\u56fa\">&#x1f4cc; \u4f5c\u4e1a&#xff08;Day 24-25 \u5de9\u56fa&#xff09;<\/h3>\n<li>\u57fa\u7840&#xff1a;\u5206\u522b\u7f16\u8bd1\u8fd0\u884c\u591a\u8fdb\u7a0b\u548c\u591a\u7ebf\u7a0b\u670d\u52a1\u5668&#xff0c;\u7528\u00a0telnet\u00a0\u5f00 3 \u4e2a\u5ba2\u6237\u7aef\u540c\u65f6\u8fde\u63a5&#xff0c;\u7528\u00a0ps\u00a0\u89c2\u5bdf\u8fdb\u7a0b\/\u7ebf\u7a0b\u6570\u91cf\u7684\u53d8\u5316\u3002<\/li>\n<li>\u5b9e\u9a8c&#xff1a;\u5728\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u4e2d&#xff0c;\u6545\u610f\u4e0d\u5173\u95ed\u00a0server_fd\u00a0\u6216\u6545\u610f\u4e0d\u8bbe\u7f6e\u7ebf\u7a0b\u5206\u79bb&#xff0c;\u7528\u00a0htop\u00a0\u6216\u00a0ps\u00a0\u89c2\u5bdf\u8d44\u6e90\u6cc4\u6f0f\u73b0\u8c61\u3002<\/li>\n<li>\u52a8\u624b&#xff1a;\u628a\u4efb\u610f\u4e00\u4e2a\u670d\u52a1\u5668\u7684\u5ba2\u6237\u7aef\u5957\u63a5\u5b57\u7528\u00a0fcntl\u00a0\u8bbe\u4e3a\u975e\u963b\u585e&#xff0c;\u7136\u540e\u5728\u00a0read\u00a0\u65f6\u52a0\u5165\u5bf9\u00a0EAGAIN\u00a0\u7684\u5224\u65ad&#xff0c;\u4f53\u4f1a\u975e\u963b\u585e\u8bfb\u7684\u884c\u4e3a\u3002<\/li>\n<li>\u5bf9\u6bd4&#xff1a;\u5206\u522b\u7f16\u8bd1\u8fd0\u884c select\u3001poll\u3001epoll \u4e09\u4e2a\u670d\u52a1\u5668&#xff0c;\u7528\u7b2c\u516d\u90e8\u5206\u7684 Python \u811a\u672c\u521b\u5efa 500 \u4e2a\u8fde\u63a5&#xff0c;\u7528\u00a0top\u00a0\u5bf9\u6bd4\u4e09\u8005\u7684 CPU \u5360\u7528\u3002<\/li>\n<li>\u6311\u6218 A&#xff1a;\u5728\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u4e2d\u5f15\u5165\u4e00\u4e2a\u5168\u5c40\u8ba1\u6570\u5668\u00a0int online_clients&#xff0c;\u6bcf\u8fde\u63a5\u4e00\u4e2a\u5ba2\u6237\u7aef &#043;1&#xff0c;\u65ad\u5f00\u65f6 -1\u3002\u7528\u4e92\u65a5\u9501\u00a0pthread_mutex_t\u00a0\u4fdd\u62a4\u5b83&#xff0c;\u5e76\u8ba9\u4e3b\u7ebf\u7a0b\u6bcf\u79d2\u6253\u5370\u4e00\u6b21\u5f53\u524d\u5728\u7ebf\u4eba\u6570\u3002<\/li>\n<li>\u6311\u6218 B&#xff1a;\u628a epoll \u670d\u52a1\u5668\u4ece LT \u6a21\u5f0f\u6539\u4e3a ET \u6a21\u5f0f&#xff0c;\u786e\u4fdd\u6240\u6709 fd \u8bbe\u4e3a\u975e\u963b\u585e&#xff0c;\u5e76\u5faa\u73af\u00a0read\u00a0\u5230\u00a0EAGAIN\u3002\u7528\u5927\u91cf\u5ba2\u6237\u7aef\u538b\u6d4b&#xff0c;\u5bf9\u6bd4 LT \u548c ET \u6a21\u5f0f\u4e0b\u00a0epoll_wait\u00a0\u7684\u8fd4\u56de\u6b21\u6570\u3002<\/li>\n<li>\u601d\u8003\u9898&#xff1a;\u4e3a\u4ec0\u4e48 epoll \u9700\u8981\u4f7f\u7528\u7ea2\u9ed1\u6811\u6765\u5b58\u50a8 fd&#xff1f;\u7528\u54c8\u5e0c\u8868&#xff08;\u6570\u7ec4&#043;\u94fe\u8868&#xff09;\u884c\u4e0d\u884c&#xff1f;\u4ece\u589e\u5220\u6539\u67e5\u3001\u8303\u56f4\u67e5\u8be2\u3001\u5185\u5b58\u5360\u7528\u4e09\u4e2a\u89d2\u5ea6\u5206\u6790\u3002<\/li>\n<hr \/>\n<p>\u5e76\u53d1\u4e4b\u8def\u6f2b\u6f2b&#xff0c;\u4ece\u00a0fork\u00a0\u5230\u00a0pthread\u00a0\u518d\u5230\u00a0epoll&#xff0c;\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86 Linux \u7f51\u7edc\u7f16\u7a0b\u7684&#034;\u4e09\u628a\u94a5\u5319&#034;\u3002\u4eca\u5929\u5199\u7684\u00a0epoll_server.c&#xff0c;\u5c31\u662f Nginx \u548c Redis \u8bde\u751f\u7684\u7b2c\u4e00\u884c\u4ee3\u7801\u3002\u6211\u4eec\u4e0b\u671f\u9879\u76ee\u89c1&#xff01;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5355\u8fde\u63a5\u7684 Echo \u670d\u52a1\u5668\u5c31\u50cf\u4e00\u5bb6\u53ea\u6709\u4e00\u4e2a\u670d\u52a1\u5458\u7684\u9910\u9986\u2014\u2014\u5ba2\u4eba\u6765\u4e86\u5f97\u6392\u961f\u7b49\u3002\u4eca\u5929\u6211\u4eec\u7ed9\u9910\u5385\u62db\u6ee1\\&#8221;\u670d\u52a1\u5458\\&#8221;&#xff08;\u8fdb\u7a0b\/\u7ebf\u7a0b&#xff09;&#xff0c;\u518d\u7ed9\u6bcf\u4e2a\u4eba\u914d\u4e0a\u4e00\u90e8\\&#8221;\u667a\u80fd\u5bf9\u8bb2\u673a\\&#8221;&#xff08;epoll&#xff09;&#xff0c;\u8ba9\u5ba2\u4eba\u4eec\u968f\u5230\u968f\u5403&#xff0c;\u540c\u65f6\u8fd8\u80fd\u76d1\u63a7\u540e\u53a8\u3001\u6536\u94f6\u53f0\u3001\u5916\u5356\u7a97\u53e3&#xff0c;\u771f\u6b63\u5b9e\u73b0\u591a\u9762\u624b\u3002\u524d\u8a00\u5728 Day 22-23 \u4e2d&#xff0c;\u6211\u4eec\u5b9e\u73b0\u4e86\u4e00\u4e2a\u6807\u51c6\u7684 TCP Echo \u670d\u52a1\u5668\u3002\u4f46\u5b83\u6709\u4e00\u4e2a\u81f4\u547d\u7684\u7f3a\u9677&#xff1a;<\/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":[10187,174,58,43],"topic":[],"class_list":["post-91168","post","type-post","status-publish","format-standard","hentry","category-server","tag-codingplan","tag-c","tag-linux","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Day 24-25 \u5e76\u53d1\u670d\u52a1\u5668 + IO\u590d\u7528\uff1a\u8ba9\u4f60\u7684\u7a0b\u5e8f\u540c\u65f6\u670d\u52a1\u6210\u5343\u4e0a\u4e07\u4e2a\u201c\u5ba2\u4eba\u201c - \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\/91168.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Day 24-25 \u5e76\u53d1\u670d\u52a1\u5668 + IO\u590d\u7528\uff1a\u8ba9\u4f60\u7684\u7a0b\u5e8f\u540c\u65f6\u670d\u52a1\u6210\u5343\u4e0a\u4e07\u4e2a\u201c\u5ba2\u4eba\u201c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u5355\u8fde\u63a5\u7684 Echo \u670d\u52a1\u5668\u5c31\u50cf\u4e00\u5bb6\u53ea\u6709\u4e00\u4e2a\u670d\u52a1\u5458\u7684\u9910\u9986\u2014\u2014\u5ba2\u4eba\u6765\u4e86\u5f97\u6392\u961f\u7b49\u3002\u4eca\u5929\u6211\u4eec\u7ed9\u9910\u5385\u62db\u6ee1&quot;\u670d\u52a1\u5458&quot;&#xff08;\u8fdb\u7a0b\/\u7ebf\u7a0b&#xff09;&#xff0c;\u518d\u7ed9\u6bcf\u4e2a\u4eba\u914d\u4e0a\u4e00\u90e8&quot;\u667a\u80fd\u5bf9\u8bb2\u673a&quot;&#xff08;epoll&#xff09;&#xff0c;\u8ba9\u5ba2\u4eba\u4eec\u968f\u5230\u968f\u5403&#xff0c;\u540c\u65f6\u8fd8\u80fd\u76d1\u63a7\u540e\u53a8\u3001\u6536\u94f6\u53f0\u3001\u5916\u5356\u7a97\u53e3&#xff0c;\u771f\u6b63\u5b9e\u73b0\u591a\u9762\u624b\u3002\u524d\u8a00\u5728 Day 22-23 \u4e2d&#xff0c;\u6211\u4eec\u5b9e\u73b0\u4e86\u4e00\u4e2a\u6807\u51c6\u7684 TCP Echo \u670d\u52a1\u5668\u3002\u4f46\u5b83\u6709\u4e00\u4e2a\u81f4\u547d\u7684\u7f3a\u9677&#xff1a;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/91168.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-06T23:24:35+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"19 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/91168.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/91168.html\",\"name\":\"Day 24-25 \u5e76\u53d1\u670d\u52a1\u5668 + IO\u590d\u7528\uff1a\u8ba9\u4f60\u7684\u7a0b\u5e8f\u540c\u65f6\u670d\u52a1\u6210\u5343\u4e0a\u4e07\u4e2a\u201c\u5ba2\u4eba\u201c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-08-06T23:24:35+00:00\",\"dateModified\":\"2026-08-06T23:24:35+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/91168.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/91168.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/91168.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Day 24-25 \u5e76\u53d1\u670d\u52a1\u5668 + IO\u590d\u7528\uff1a\u8ba9\u4f60\u7684\u7a0b\u5e8f\u540c\u65f6\u670d\u52a1\u6210\u5343\u4e0a\u4e07\u4e2a\u201c\u5ba2\u4eba\u201c\"}]},{\"@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":"Day 24-25 \u5e76\u53d1\u670d\u52a1\u5668 + IO\u590d\u7528\uff1a\u8ba9\u4f60\u7684\u7a0b\u5e8f\u540c\u65f6\u670d\u52a1\u6210\u5343\u4e0a\u4e07\u4e2a\u201c\u5ba2\u4eba\u201c - \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\/91168.html","og_locale":"zh_CN","og_type":"article","og_title":"Day 24-25 \u5e76\u53d1\u670d\u52a1\u5668 + IO\u590d\u7528\uff1a\u8ba9\u4f60\u7684\u7a0b\u5e8f\u540c\u65f6\u670d\u52a1\u6210\u5343\u4e0a\u4e07\u4e2a\u201c\u5ba2\u4eba\u201c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u5355\u8fde\u63a5\u7684 Echo \u670d\u52a1\u5668\u5c31\u50cf\u4e00\u5bb6\u53ea\u6709\u4e00\u4e2a\u670d\u52a1\u5458\u7684\u9910\u9986\u2014\u2014\u5ba2\u4eba\u6765\u4e86\u5f97\u6392\u961f\u7b49\u3002\u4eca\u5929\u6211\u4eec\u7ed9\u9910\u5385\u62db\u6ee1\"\u670d\u52a1\u5458\"&#xff08;\u8fdb\u7a0b\/\u7ebf\u7a0b&#xff09;&#xff0c;\u518d\u7ed9\u6bcf\u4e2a\u4eba\u914d\u4e0a\u4e00\u90e8\"\u667a\u80fd\u5bf9\u8bb2\u673a\"&#xff08;epoll&#xff09;&#xff0c;\u8ba9\u5ba2\u4eba\u4eec\u968f\u5230\u968f\u5403&#xff0c;\u540c\u65f6\u8fd8\u80fd\u76d1\u63a7\u540e\u53a8\u3001\u6536\u94f6\u53f0\u3001\u5916\u5356\u7a97\u53e3&#xff0c;\u771f\u6b63\u5b9e\u73b0\u591a\u9762\u624b\u3002\u524d\u8a00\u5728 Day 22-23 \u4e2d&#xff0c;\u6211\u4eec\u5b9e\u73b0\u4e86\u4e00\u4e2a\u6807\u51c6\u7684 TCP Echo \u670d\u52a1\u5668\u3002\u4f46\u5b83\u6709\u4e00\u4e2a\u81f4\u547d\u7684\u7f3a\u9677&#xff1a;","og_url":"https:\/\/www.wsisp.com\/helps\/91168.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-08-06T23:24:35+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"19 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/91168.html","url":"https:\/\/www.wsisp.com\/helps\/91168.html","name":"Day 24-25 \u5e76\u53d1\u670d\u52a1\u5668 + IO\u590d\u7528\uff1a\u8ba9\u4f60\u7684\u7a0b\u5e8f\u540c\u65f6\u670d\u52a1\u6210\u5343\u4e0a\u4e07\u4e2a\u201c\u5ba2\u4eba\u201c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-08-06T23:24:35+00:00","dateModified":"2026-08-06T23:24:35+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/91168.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/91168.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/91168.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Day 24-25 \u5e76\u53d1\u670d\u52a1\u5668 + IO\u590d\u7528\uff1a\u8ba9\u4f60\u7684\u7a0b\u5e8f\u540c\u65f6\u670d\u52a1\u6210\u5343\u4e0a\u4e07\u4e2a\u201c\u5ba2\u4eba\u201c"}]},{"@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\/91168","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=91168"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/91168\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=91168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=91168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=91168"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=91168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}