{"id":85913,"date":"2026-07-28T00:08:31","date_gmt":"2026-07-27T16:08:31","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/85913.html"},"modified":"2026-07-28T00:08:31","modified_gmt":"2026-07-27T16:08:31","slug":"linux-tcp%e5%a4%9a%e8%bf%9b%e7%a8%8b%e5%b9%b6%e5%8f%91%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/85913.html","title":{"rendered":"linux\u2014\u2014TCP\u591a\u8fdb\u7a0b\u5e76\u53d1\u670d\u52a1\u5668"},"content":{"rendered":"<p>\u591a\u7ebf\u7a0b&#xff1a;\u6765\u4e00\u4e2a\u5ba2\u6237&#xff0c;\u5f00\u4e00\u4e2a\u7ebf\u7a0b<\/p>\n<p>\u591a\u8fdb\u7a0b&#xff1a;\u6765\u4e00\u4e2a\u5ba2\u6237&#xff0c;\u5f00\u4e00\u4e2a\u5b50\u8fdb\u7a0b<\/p>\n<p>\u670d\u52a1\u5668\u7aef<\/p>\n<p>#include&lt;stdio.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;          \/* See NOTES *\/<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include&lt;stdlib.h&gt;<br \/>\n#include &lt;strings.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include&lt;string.h&gt;<br \/>\n#include &lt;pthread.h&gt;<br \/>\n#include&lt;signal.h&gt;<br \/>\n#include &lt;sys\/wait.h&gt;<\/p>\n<p>#define QUIT_STR &#034;QUIT&#034;<br \/>\n#define BUFSIZE 1024<br \/>\n#define BACKLOG 5<br \/>\n#define SERV_IP 5001<br \/>\n#define SERV_IP_ADDR &#034;192.168.88.129&#034;<\/p>\n<p>void child_data_handle(int signum)<br \/>\n{<br \/>\nif(SIGCHLD &#061;&#061; signum)<br \/>\n{<br \/>\nwaitpid(-1,NULL,WNOHANG);<br \/>\n}<br \/>\n}<br \/>\nvoid* client_data_handle(void* arg);<br \/>\nint main()<br \/>\n{<br \/>\nint fd &#061; -1;<br \/>\nsignal(SIGCHLD,child_data_handle);<br \/>\nstruct sockaddr_in sin;<br \/>\n\/\/1.socket<br \/>\nfd &#061; socket(AF_INET,SOCK_STREAM,0);<br \/>\nif(fd&lt;0)<br \/>\n{<br \/>\nperror(&#034;socket&#034;);<br \/>\nexit(1);<br \/>\n}<\/p>\n<p>bzero(&amp;sin,sizeof(sin));<\/p>\n<p>sin.sin_family &#061; AF_INET;<br \/>\nsin.sin_port &#061; htons(SERV_IP);<br \/>\n\/\/sin.sin_addr.s_addr &#061; inet_addr(SERV_IP_ADDR);<br \/>\nsin.sin_addr.s_addr &#061; INADDR_ANY;<br \/>\n\/*if(inet_pion(AF_INET,SERV_IP_ADDR,(void *)sin.sin_addr.s_addr) !&#061; 1)<br \/>\n  {<br \/>\n  perror(&#034;inet_pton&#034;);<br \/>\n  exit(1);<br \/>\n  }<br \/>\n  *\/<br \/>\n\/\/2.bind<br \/>\nif(bind(fd,(struct sockaddr *)&amp;sin,sizeof(sin)) &lt;0)<br \/>\n{<br \/>\nperror(&#034;bind&#034;);<br \/>\nexit(0);<br \/>\n}<br \/>\n\/\/3.listen<br \/>\nif(listen(fd,BACKLOG) &lt; 0)<br \/>\n{<br \/>\nperror(&#034;listen&#034;);<br \/>\nexit(1);<br \/>\n}<\/p>\n<p>\/\/4.accept<br \/>\npid_t pid;<br \/>\nint newfd &#061; -1;<br \/>\nstruct sockaddr_in cin;<br \/>\nsocklen_t addrlen &#061; sizeof(cin);<br \/>\nwhile(1)<br \/>\n{<br \/>\nnewfd &#061; accept(fd,(struct sockaddr *)&amp;cin,&amp;addrlen);<br \/>\nif(newfd &lt; 0)<br \/>\n{<br \/>\nperror(&#034;accept&#034;);<br \/>\nbreak;<br \/>\n}<br \/>\npid &#061; fork();<br \/>\nif(pid &lt; 0)<br \/>\n{<br \/>\nperror(&#034;fork&#034;);<br \/>\nbreak;<br \/>\n}<br \/>\nif(pid &#061;&#061; 0)<br \/>\n{<br \/>\nchar ipv4_addr[16];<br \/>\nif(!inet_ntop(AF_INET,(void *)&amp;cin.sin_addr,ipv4_addr,sizeof(cin)))<br \/>\n{<br \/>\nperror(&#034;inet_ntop&#034;);<br \/>\nexit(1);<br \/>\n}<br \/>\nprintf(&#034;Client:(%s,%d) is connect\\\\n&#034;,ipv4_addr,ntohs(cin.sin_port));<\/p>\n<p>client_data_handle(&amp;newfd);<br \/>\nclose(fd);<br \/>\n}<br \/>\nif(pid &gt; 0)<br \/>\n{<br \/>\nclose(newfd);<br \/>\n}<\/p>\n<p>}<br \/>\nclose(fd);<\/p>\n<p>return 0;<br \/>\n}<\/p>\n<p>void* client_data_handle(void* arg)<br \/>\n{<br \/>\nint newfd &#061; *(int *)arg;<\/p>\n<p>char buf[BUFSIZE];<br \/>\nint ret &#061; -1;<br \/>\nprintf(&#034;client handle process:newfd &#061; %d\\\\n&#034;,newfd);<\/p>\n<p>while(1)<br \/>\n{<br \/>\ndo<br \/>\n{<br \/>\nbzero(buf,BUFSIZE);<br \/>\nret &#061; read(newfd,buf,BUFSIZE-1);<br \/>\n}while(ret &lt; 1);<br \/>\nif(ret &lt; 0)<br \/>\n{<br \/>\nexit(1);<br \/>\n}<br \/>\nif(!ret)<br \/>\n{<br \/>\nbreak;<br \/>\n}<br \/>\nprintf(&#034;receive data:%s\\\\n&#034;,buf);<\/p>\n<p>if(!strncasecmp(buf,QUIT_STR,strlen(QUIT_STR)))<br \/>\n{<br \/>\nprintf(&#034;Client is exiting!\\\\n&#034;);<br \/>\nbreak;<br \/>\n}<br \/>\n}<\/p>\n<p>close(newfd);<br \/>\nreturn NULL;<br \/>\n}<\/p>\n<p>\u5176\u5b9e\u4e0e\u591a\u7ebf\u7a0b\u7684\u53d8\u5316\u5c31\u5728\u6700\u540eaccept\u90a3\u90e8\u5206\u7684\u5185\u5bb9\u91cc<\/p>\n<p>\u7236\u8fdb\u7a0b &#061; \u53ea\u8d1f\u8d23\u7b49\u5ba2\u6237\u7aef&#xff08;accept&#xff09;<\/p>\n<p>\u5b50\u8fdb\u7a0b &#061; \u53ea\u8d1f\u8d23\u670d\u52a1\u5ba2\u6237\u7aef&#xff08;read\/write&#xff09;<\/p>\n<p>fork () \u4e00\u5288\u4e3a\u4e8c&#xff0c;\u7236\u5b50\u5404\u5e72\u5404\u7684&#xff01;<\/p>\n<p>\u2460\u53d8\u91cf\u5b9a\u4e49<\/p>\n<p>pid_t pid;             \/\/ \u8fdb\u7a0b\u53f7&#xff0c;\u7528\u6765\u5224\u65ad\u7236\u5b50<br \/>\nint newfd &#061; -1;        \/\/ \u5ba2\u6237\u7aef\u8fde\u63a5\u7684\u5957\u63a5\u5b57<br \/>\nstruct sockaddr_in cin;\/\/ \u7528\u6765\u5b58\u5ba2\u6237\u7aefIP\u3001\u7aef\u53e3<br \/>\nsocklen_t addrlen &#061; sizeof(cin);<\/p>\n<p>\u2461\u6b7b\u5faa\u73af&#xff1a;\u7236\u8fdb\u7a0b\u6c38\u8fdc\u7b49\u5ba2\u6237\u7aef<\/p>\n<p>while(1)  {<br \/>\n    \/\/ \u7236\u8fdb\u7a0b\u963b\u585e\u5728\u8fd9\u91cc&#xff0c;\u76f4\u5230\u5ba2\u6237\u7aef\u6765\u8fde\u63a5<br \/>\n    newfd &#061; accept(fd, (struct sockaddr *)&amp;cin, &amp;addrlen);<\/p>\n<p>\u2462\u5ba2\u6237\u7aef\u6765\u4e86 \u2192 \u521b\u5efa\u5b50\u8fdb\u7a0b<\/p>\n<p>pid &#061; fork();  \/\/ &#x1f525; \u8fdb\u7a0b\u5206\u88c2&#xff1a;\u4e00\u7236\u4e00\u5b50<\/p>\n<p>fork () \u4f5c\u7528&#xff1a;<\/p>\n<p>\u628a\u5f53\u524d\u8fdb\u7a0b\u590d\u5236\u4e00\u4efd&#xff0c;\u4e00\u6a21\u4e00\u6837\u3002\u8c03\u7528\u4e00\u6b21&#xff0c;\u8fd4\u56de\u4e24\u4e2a\u503c&#xff1a;<\/p>\n<ul>\n<li>\u8fd4\u56de\u503c &gt; 0 \u2192 \u7236\u8fdb\u7a0b<\/li>\n<li>\u8fd4\u56de\u503c &#061; 0 \u2192 \u5b50\u8fdb\u7a0b<\/li>\n<\/ul>\n<p>\u2463\u7236\u5b50\u5206\u652f1&#xff1a;\u5b50\u8fdb\u7a0b&#xff08;pid &#061;&#061; 0&#xff09;<\/p>\n<p>if(pid &#061;&#061; 0)  \/\/ &#x1f525; \u5b50\u8fdb\u7a0b\u8fd0\u884c\u8fd9\u91cc<br \/>\n{<br \/>\n    \/\/ \u6253\u5370\u5ba2\u6237\u7aefIP\u548c\u7aef\u53e3<br \/>\n    inet_ntop(&#8230;);<br \/>\n    printf(&#034;Client:(%s,%d) is connect\\\\n&#034;,&#8230;);<\/p>\n<p>    \/\/ &#x1f525; \u8c03\u7528\u51fd\u6570&#xff0c;\u4e13\u95e8\u670d\u52a1\u5ba2\u6237\u7aef<br \/>\n    client_data_handle(&amp;newfd);<\/p>\n<p>    close(fd);   \/\/ \u5b50\u8fdb\u7a0b\u5173\u95ed\u76d1\u542c\u5957\u63a5\u5b57<br \/>\n}<\/p>\n<p>\u5b50\u8fdb\u7a0b\u505a\u4ec0\u4e48&#xff1f;<\/p>\n<li>\u6253\u5370\u8c01\u8fde\u8fdb\u6765\u4e86<\/li>\n<li>\u8c03\u7528 client_data_handle \u5904\u7406\u8bfb\u5199<\/li>\n<li>\u5173\u95ed\u4e0d\u9700\u8981\u7684\u76d1\u542c fd<\/li>\n<li>\u51fd\u6570\u7ed3\u675f\u540e&#xff0c;\u5b50\u8fdb\u7a0b\u9000\u51fa<\/li>\n<p>\u2464\u7236\u5b50\u8fdb\u7a0b\u5206\u652f2&#xff1a;\u7236\u8fdb\u7a0b&#xff08;pid &gt; 0&#xff09;<\/p>\n<p>if(pid &gt; 0)  \/\/ &#x1f525; \u7236\u8fdb\u7a0b\u8fd0\u884c\u8fd9\u91cc<br \/>\n{<br \/>\n    close(newfd);  \/\/ \u7236\u8fdb\u7a0b\u5173\u95ed\u901a\u4fe1fd<br \/>\n}<\/p>\n<p>\u7236\u8fdb\u7a0b\u505a\u4ec0\u4e48&#xff1f;<\/p>\n<li>\u5173\u95ed\u4e0d\u9700\u8981\u7684\u901a\u4fe1 newfd<\/li>\n<li>\u56de\u5230 while (1) \u5faa\u73af&#xff0c;\u7ee7\u7eed accept \u7b49\u4e0b\u4e00\u4e2a\u5ba2\u6237\u7aef<\/li>\n<p>\u4e3a\u4ec0\u4e48\u8981\u5173\u95ed\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff1f;<\/p>\n<p>\u5b50\u8fdb\u7a0b\u91cc&#xff1a;<\/p>\n<p>close(fd);<\/p>\n<p>fd \u662f\u76d1\u542c\u5957\u63a5\u5b57\u5b50\u8fdb\u7a0b\u53ea\u8d1f\u8d23\u804a\u5929&#xff0c;\u4e0d\u7b49\u65b0\u8fde\u63a5 \u2192 \u5173\u6389<\/p>\n<p>\u7236\u8fdb\u7a0b\u91cc&#xff1a;<\/p>\n<p>close(newfd);<\/p>\n<p>newfd \u662f\u901a\u4fe1\u5957\u63a5\u5b57\u7236\u8fdb\u7a0b\u53ea\u8d1f\u8d23\u7b49\u8fde\u63a5&#xff0c;\u4e0d\u804a\u5929 \u2192 \u5173\u6389<\/p>\n<p>\u4e00\u53e5\u8bdd&#xff1a;<\/p>\n<p>\u5404\u7528\u5404\u7684&#xff0c;\u4e0d\u7528\u5c31\u5173&#xff0c;\u9632\u6b62\u8d44\u6e90\u6cc4\u6f0f&#xff01;<\/p>\n<p>\u5b8c\u6574\u6d41\u7a0b<\/p>\n<li>\u7236\u8fdb\u7a0b accept \u963b\u585e\u7b49\u5ba2\u6237\u7aef<\/li>\n<li>\u5ba2\u6237\u7aef\u8fde\u8fdb\u6765<\/li>\n<li>fork () \u2192 \u5206\u88c2\u51fa\u5b50\u8fdb\u7a0b<\/li>\n<li>\u7236\u8fdb\u7a0b\u5173\u95ed newfd \u2192 \u56de\u53bb\u7ee7\u7eed\u7b49\u65b0\u5ba2\u6237<\/li>\n<li>\u5b50\u8fdb\u7a0b\u5173\u95ed fd \u2192 \u8c03\u7528\u51fd\u6570\u5904\u7406\u804a\u5929<\/li>\n<li>\u5ba2\u6237\u7aef\u53d1\u6d88\u606f \u2192 \u5b50\u8fdb\u7a0b read \u5e76\u6253\u5370<\/li>\n<li>\u5ba2\u6237\u7aef\u9000\u51fa \u2192 \u5b50\u8fdb\u7a0b\u9000\u51fa<\/li>\n<li>\u7cfb\u7edf\u7ed9\u7236\u8fdb\u7a0b\u53d1 SIGCHLD<\/li>\n<li>\u4fe1\u53f7\u51fd\u6570 waitpid \u56de\u6536\u5b50\u8fdb\u7a0b&#xff08;\u6e05\u7406\u50f5\u5c38&#xff09;<\/li>\n<p>\u5ba2\u6237\u7aef\u8981\u662f\u65ad\u5f00\u8fde\u63a5&#xff0c;\u5b50\u8fdb\u7a0b\u5c31\u6b7b\u4e86&#xff0c;\u4f46\u662f\u7236\u8fdb\u7a0b\u4e00\u76f4\u5728\u6b7b\u5faa\u73af&#xff0c;\u65e0\u6cd5\u56de\u6536\u5b50\u8fdb\u7a0b&#xff0c;\u5b50\u8fdb\u7a0b\u5c31\u4f1a\u6210\u4e3a\u50f5\u5c38\u8fdb\u7a0b&#xff0c;\u8fc7\u591a\u7684\u50f5\u5c38\u8fdb\u7a0b\u4f1a\u5927\u91cf\u5360\u7528\u8d44\u6e90&#xff0c;\u6240\u4ee5\u6211\u95e8\u8fd8\u8981\u5199\u4e2a\u51fd\u6570\u4e13\u95e8\u56de\u6536\u50f5\u5c38\u8fdb\u7a0b<\/p>\n<p>\/\/ \u4fe1\u53f7\u5904\u7406\u51fd\u6570&#xff1a;\u56de\u6536\u5b50\u8fdb\u7a0b<br \/>\nvoid child_data_handle(int signum)<br \/>\n{<br \/>\n    \/\/ \u5224\u65ad\u6536\u5230\u7684\u4fe1\u53f7\u662f\u4e0d\u662f SIGCHLD<br \/>\n    if(SIGCHLD &#061;&#061; signum)<br \/>\n    {<br \/>\n        \/\/ \u975e\u963b\u585e\u65b9\u5f0f\u56de\u6536\u6240\u6709\u9000\u51fa\u7684\u5b50\u8fdb\u7a0b<br \/>\n        waitpid(-1, NULL, WNOHANG);<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u6ce8\u518c\u4fe1\u53f7\u5904\u7406&#xff1a;\u5f53\u5b50\u8fdb\u7a0b\u9000\u51fa\u65f6&#xff0c;\u5185\u6838\u81ea\u52a8\u8c03\u7528\u4e0a\u9762\u7684\u51fd\u6570<br \/>\nsignal(SIGCHLD, child_data_handle);<\/p>\n<p>\u5728 Linux \u4e2d&#xff1a;<\/p>\n<li>\u5b50\u8fdb\u7a0b\u9000\u51fa\u65f6&#xff0c;\u5185\u6838\u4e0d\u4f1a\u76f4\u63a5\u9500\u6bc1\u5b83&#xff0c;\u800c\u662f\u4fdd\u7559\u8fdb\u7a0b\u4fe1\u606f&#xff08;PID\u3001\u9000\u51fa\u72b6\u6001&#xff09;&#xff0c;\u53d8\u6210\u50f5\u5c38\u8fdb\u7a0b\u3002<\/li>\n<li>\u7236\u8fdb\u7a0b\u5fc5\u987b\u8c03\u7528 wait() \/ waitpid() \u8bfb\u53d6\u5b50\u8fdb\u7a0b\u9000\u51fa\u72b6\u6001&#xff0c;\u5185\u6838\u624d\u4f1a\u5f7b\u5e95\u5220\u9664\u5b50\u8fdb\u7a0b\u3002<\/li>\n<li>\u5982\u679c\u7236\u8fdb\u7a0b\u4e0d\u56de\u6536&#xff0c;\u50f5\u5c38\u8fdb\u7a0b\u4f1a\u4e00\u76f4\u5360\u7528\u7cfb\u7edf PID \u8d44\u6e90\u3002<\/li>\n<li>\u5b50\u8fdb\u7a0b\u9000\u51fa\u65f6&#xff0c;\u5185\u6838\u4f1a\u5411\u7236\u8fdb\u7a0b\u53d1\u9001 SIGCHLD \u4fe1\u53f7\u3002<\/li>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c31\u662f&#xff1a;\u76d1\u542c\u5b50\u8fdb\u7a0b\u9000\u51fa\u4fe1\u53f7 \u2192 \u81ea\u52a8\u56de\u6536 \u2192 \u6d88\u706d\u50f5\u5c38\u8fdb\u7a0b\u3002<\/p>\n<p>\u2460\u4fe1\u53f7\u5904\u7406\u51fd\u6570&#xff1a;child_data_handle<\/p>\n<p>void child_data_handle(int signum)<\/p>\n<ul>\n<li>\u8fd9\u662f\u81ea\u5b9a\u4e49\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570&#xff0c;\u5f53\u5185\u6838\u7ed9\u7236\u8fdb\u7a0b\u53d1\u4fe1\u53f7\u65f6&#xff0c;\u7236\u8fdb\u7a0b\u4f1a\u81ea\u52a8\u6267\u884c\u8fd9\u4e2a\u51fd\u6570\u3002<\/li>\n<li>\u53c2\u6570 signum&#xff1a;\u5185\u6838\u4f20\u8fc7\u6765\u7684\u4fe1\u53f7\u7f16\u53f7&#xff08;\u544a\u8bc9\u6211\u4eec\u6536\u5230\u4e86\u54ea\u4e2a\u4fe1\u53f7&#xff09;\u3002<\/li>\n<\/ul>\n<p>\u2461\u5224\u65ad\u4fe1\u53f7\u7c7b\u578b<\/p>\n<p>if(SIGCHLD &#061;&#061; signum)<\/p>\n<ul>\n<li>SIGCHLD&#xff1a;\u5b50\u8fdb\u7a0b\u9000\u51fa \/ \u505c\u6b62 \/ \u7ee7\u7eed\u65f6&#xff0c;\u5185\u6838\u53d1\u9001\u7ed9\u7236\u8fdb\u7a0b\u7684\u56fa\u5b9a\u4fe1\u53f7&#xff08;\u7f16\u53f7 17&#xff09;\u3002<\/li>\n<li>\u4f5c\u7528&#xff1a;\u5b89\u5168\u6821\u9a8c&#xff0c;\u786e\u4fdd\u6211\u4eec\u53ea\u5904\u7406\u5b50\u8fdb\u7a0b\u9000\u51fa\u4fe1\u53f7&#xff0c;\u4e0d\u5904\u7406\u5176\u4ed6\u4fe1\u53f7\u3002<\/li>\n<\/ul>\n<p>\u2462\u6838\u5fc3\u56de\u6536\u51fd\u6570&#xff1a;waitpid(-1, NULL, WNOHANG)<\/p>\n<p>\u8fd9\u662f\u6574\u4e2a\u4ee3\u7801\u7684\u7075\u9b42&#xff0c;3 \u4e2a\u53c2\u6570\u5fc5\u987b\u8bb2\u6e05\u695a&#xff1a;<\/p>\n<p style=\"background-color:transparent\">waitpid(pid_t pid, int *status, int options)<\/p>\n<table>\n<tr>\u53c2\u6570\u6211\u4eec\u7684\u53d6\u503c\u542b\u4e49<\/tr>\n<tbody>\n<tr>\n<td>pid<\/td>\n<td>-1<\/td>\n<td>\u56de\u6536\u4efb\u610f\u5b50\u8fdb\u7a0b&#xff08;\u6240\u6709\u9000\u51fa\u7684\u5b50\u8fdb\u7a0b&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>status<\/td>\n<td>NULL<\/td>\n<td>\u4e0d\u5173\u5fc3\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u72b6\u6001&#xff08;\u4e0d\u9700\u8981\u4fdd\u5b58&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>options<\/td>\n<td>WNOHANG<\/td>\n<td>\u975e\u963b\u585e\u6a21\u5f0f&#xff1a;\u5982\u679c\u6ca1\u6709\u5b50\u8fdb\u7a0b\u9000\u51fa&#xff0c;\u51fd\u6570\u7acb\u523b\u8fd4\u56de&#xff0c;\u4e0d\u5361\u4f4f\u7236\u8fdb\u7a0b<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5173\u952e\u7279\u6027&#xff1a;<\/p>\n<li>\u975e\u963b\u585e&#xff08;WNOHANG&#xff09;\n<ul>\n<li>\u7236\u8fdb\u7a0b\u8be5\u5e72\u561b\u5e72\u561b&#xff0c;\u4e0d\u4f1a\u56e0\u4e3a\u7b49\u5b50\u8fdb\u7a0b\u800c\u5361\u4f4f\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u81ea\u52a8\u56de\u6536\n<ul>\n<li>\u53ea\u8981\u6709\u5b50\u8fdb\u7a0b\u9000\u51fa&#xff0c;\u5185\u6838\u53d1\u4fe1\u53f7 \u2192 \u8c03\u7528\u51fd\u6570 \u2192 \u7acb\u523b\u56de\u6536\u3002<\/li>\n<\/ul>\n<\/li>\n<p>\u2463\u6ce8\u518c\u4fe1\u53f7\u5904\u7406&#xff1a;signal(SIGCHLD, child_data_handle)<\/p>\n<p>signal(SIGCHLD, child_data_handle);<\/p>\n<ul>\n<li>\u4f5c\u7528&#xff1a;\u544a\u8bc9\u5185\u6838&#xff1a;\n<p>\u201c\u5f53\u4f60\u6536\u5230 SIGCHLD&#xff08;\u5b50\u8fdb\u7a0b\u9000\u51fa&#xff09;\u4fe1\u53f7\u65f6&#xff0c;\u8bf7\u81ea\u52a8\u8c03\u7528 child_data_handle \u8fd9\u4e2a\u51fd\u6570\u5904\u7406\u3002\u201d<\/p>\n<\/li>\n<li>\u8fd9\u884c\u4ee3\u7801\u5fc5\u987b\u5199\u5728\u7236\u8fdb\u7a0b\u4e2d&#xff0c;\u4e00\u822c\u653e\u5728\u7a0b\u5e8f\u5f00\u5934\u3002<\/li>\n<\/ul>\n<p style=\"background-color:transparent\">1. \u4e3a\u4ec0\u4e48\u4e0d\u7528 wait()&#xff0c;\u8981\u7528 waitpid()&#xff1f;<\/p>\n<ul>\n<li>wait() \u662f\u963b\u585e\u7684&#xff0c;\u7236\u8fdb\u7a0b\u4f1a\u5361\u4f4f\u3002<\/li>\n<li>waitpid(&#8230;, WNOHANG) \u662f\u975e\u963b\u585e\u7684&#xff0c;\u7236\u8fdb\u7a0b\u4e0d\u5361\u987f\u3002<\/li>\n<\/ul>\n<p>2. \u4e3a\u4ec0\u4e48\u8981\u5224\u65ad if(SIGCHLD &#061;&#061; signum)&#xff1f;<\/p>\n<ul>\n<li>\u4e00\u4e2a\u51fd\u6570\u53ef\u80fd\u5904\u7406\u591a\u4e2a\u4fe1\u53f7&#xff0c;\u52a0\u4e0a\u5224\u65ad\u66f4\u5b89\u5168&#xff0c;\u907f\u514d\u8bef\u5904\u7406\u3002<\/li>\n<\/ul>\n<p>3. \u8fd9\u6bb5\u4ee3\u7801\u80fd\u56de\u6536\u591a\u4e2a\u5b50\u8fdb\u7a0b\u5417&#xff1f;<\/p>\n<ul>\n<li>\u80fd&#xff01;waitpid(-1, &#8230;) \u4f1a\u56de\u6536\u6240\u6709\u5df2\u7ecf\u9000\u51fa\u7684\u5b50\u8fdb\u7a0b\u3002<\/li>\n<\/ul>\n<p>\u6267\u884c\u7ed3\u679c<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"759\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260727160829-6a67827decfd2.png\" width=\"1474\" \/><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u591a\u7ebf\u7a0b&#xff1a;\u6765\u4e00\u4e2a\u5ba2\u6237&#xff0c;\u5f00\u4e00\u4e2a\u7ebf\u7a0b\u591a\u8fdb\u7a0b&#xff1a;\u6765\u4e00\u4e2a\u5ba2\u6237&#xff0c;\u5f00\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u670d\u52a1\u5668\u7aef#include<br \/>\n#include           \/* See NOTES *\/<br \/>\n#include<br \/>\n#include<br \/>\n#include<br \/>\n#include<br \/>\n#include<br \/>\n#inc<\/p>\n","protected":false},"author":2,"featured_media":85912,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,205,43],"topic":[],"class_list":["post-85913","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-linux","tag-tcp-ip","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>linux\u2014\u2014TCP\u591a\u8fdb\u7a0b\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/85913.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"linux\u2014\u2014TCP\u591a\u8fdb\u7a0b\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u591a\u7ebf\u7a0b&#xff1a;\u6765\u4e00\u4e2a\u5ba2\u6237&#xff0c;\u5f00\u4e00\u4e2a\u7ebf\u7a0b\u591a\u8fdb\u7a0b&#xff1a;\u6765\u4e00\u4e2a\u5ba2\u6237&#xff0c;\u5f00\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u670d\u52a1\u5668\u7aef#include #include      \/* See NOTES *\/ #include #include #include #include #include #inc\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/85913.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-27T16:08:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260727160829-6a67827decfd2.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/85913.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/85913.html\",\"name\":\"linux\u2014\u2014TCP\u591a\u8fdb\u7a0b\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-07-27T16:08:31+00:00\",\"dateModified\":\"2026-07-27T16:08:31+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/85913.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/85913.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/85913.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"linux\u2014\u2014TCP\u591a\u8fdb\u7a0b\u5e76\u53d1\u670d\u52a1\u5668\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"linux\u2014\u2014TCP\u591a\u8fdb\u7a0b\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/85913.html","og_locale":"zh_CN","og_type":"article","og_title":"linux\u2014\u2014TCP\u591a\u8fdb\u7a0b\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u591a\u7ebf\u7a0b&#xff1a;\u6765\u4e00\u4e2a\u5ba2\u6237&#xff0c;\u5f00\u4e00\u4e2a\u7ebf\u7a0b\u591a\u8fdb\u7a0b&#xff1a;\u6765\u4e00\u4e2a\u5ba2\u6237&#xff0c;\u5f00\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u670d\u52a1\u5668\u7aef#include #include      \/* See NOTES *\/ #include #include #include #include #include #inc","og_url":"https:\/\/www.wsisp.com\/helps\/85913.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-07-27T16:08:31+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260727160829-6a67827decfd2.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/85913.html","url":"https:\/\/www.wsisp.com\/helps\/85913.html","name":"linux\u2014\u2014TCP\u591a\u8fdb\u7a0b\u5e76\u53d1\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-07-27T16:08:31+00:00","dateModified":"2026-07-27T16:08:31+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/85913.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/85913.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/85913.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"linux\u2014\u2014TCP\u591a\u8fdb\u7a0b\u5e76\u53d1\u670d\u52a1\u5668"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/85913","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=85913"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/85913\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/85912"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=85913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=85913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=85913"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=85913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}