{"id":118745,"date":"2026-10-28T02:56:35","date_gmt":"2026-10-27T18:56:35","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/118745.html"},"modified":"2026-10-28T02:56:35","modified_gmt":"2026-10-27T18:56:35","slug":"linux-%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0%e5%8d%81%e4%b9%9d%ef%bc%89%e7%b3%bb%e7%bb%9f%e7%bc%96%e7%a8%8b%ef%bc%9a%e6%b7%b1%e5%ba%a6%e6%8b%86%e8%a7%a3-wait-waitpid-%e4%b8%8e-exec-%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/118745.html","title":{"rendered":"Linux \u5b66\u4e60\u7b14\u8bb0(\u5341\u4e5d\uff09\u7cfb\u7edf\u7f16\u7a0b\uff1a\u6df1\u5ea6\u62c6\u89e3 wait\/waitpid \u4e0e exec \u51fd\u6570\u65cf"},"content":{"rendered":"<h3>\u4e00\u3001 \u8fdb\u7a0b\u57fa\u7840&#xff1a;\u4ece PCB \u5230\u5185\u5b58\u5206\u5e03<\/h3>\n<h4>1. \u4ec0\u4e48\u662f\u8fdb\u7a0b&#xff1f;<\/h4>\n<p>\u8fdb\u7a0b\u662f\u7a0b\u5e8f\u6267\u884c\u7684\u8fc7\u7a0b\u3002\u5b83\u4e0d\u4ec5\u662f\u786c\u76d8\u4e0a\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6&#xff08;\u4ee3\u7801&#xff09;&#xff0c;\u8fd8\u9700\u8981 CPU \u8c03\u5ea6\u3001\u5185\u5b58\u8d44\u6e90\u3001\u6587\u4ef6\u7cfb\u7edf\u7b49\u914d\u5408\u3002<br \/>\nLinux \u5185\u6838\u901a\u8fc7 PCB&#xff08;\u8fdb\u7a0b\u63a7\u5236\u5757&#xff0c;task_struct \u7ed3\u6784\u4f53&#xff09; \u6765\u7ba1\u7406\u8fdb\u7a0b\u3002PCB \u4e2d\u8bb0\u5f55\u4e86&#xff1a;PID\u3001\u7236\u8fdb\u7a0b PID\u3001\u5f53\u524d\u5de5\u4f5c\u8def\u5f84\u3001\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u5217\u8868\u3001\u4fe1\u53f7\u5904\u7406\u3001\u8d44\u6e90\u4e0a\u9650\u7b49\u6838\u5fc3\u4fe1\u606f\u3002<\/p>\n<h4>2. \u8fdb\u7a0b\u7684\u5185\u5b58\u5206\u5e03<\/h4>\n<p>\u5f53\u6211\u4eec\u8fd0\u884c\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u65f6&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u4e3a\u5176\u5206\u914d 0~3G \u7684\u7528\u6237\u7a7a\u95f4&#xff08;\u5185\u6838\u7a7a\u95f4\u5360 3~4G&#xff09;&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u4ee3\u7801\u6bb5 (Code)&#xff1a;\u53ea\u8bfb&#xff0c;\u5b58\u653e\u673a\u5668\u6307\u4ee4\u3002<\/p>\n<\/li>\n<li>\n<p>\u6570\u636e\u6bb5 (Data\/BSS)&#xff1a;\u5b58\u653e\u5168\u5c40\u53d8\u91cf\u3001\u9759\u6001\u53d8\u91cf\u3002<\/p>\n<\/li>\n<li>\n<p>\u5806\u533a (Heap)&#xff1a;\u7531\u7a0b\u5e8f\u5458 malloc\/free \u7ba1\u7406&#xff0c;\u5411\u9ad8\u5730\u5740\u751f\u957f\u3002<\/p>\n<\/li>\n<li>\n<p>\u5171\u4eab\u5e93\u6620\u5c04\u533a&#xff1a;\u5b58\u653e C \u5e93\u51fd\u6570\u7b49\u52a8\u6001\u94fe\u63a5\u5e93\u3002<\/p>\n<\/li>\n<li>\n<p>\u6808\u533a (Stack)&#xff1a;\u5b58\u653e\u5c40\u90e8\u53d8\u91cf&#xff0c;\u9ed8\u8ba4 8MB&#xff0c;\u5411\u4f4e\u5730\u5740\u751f\u957f\u3002<\/p>\n<\/li>\n<\/ul>\n<h4>3. \u8fdb\u7a0b\u7684\u72b6\u6001\u5207\u6362\u4e0e 8 \u79cd\u9000\u51fa\u65b9\u5f0f<\/h4>\n<p>\u7ecf\u5178\u7684\u8fdb\u7a0b\u72b6\u6001\u5305\u62ec&#xff1a;\u5c31\u7eea\u6001\u3001\u8fd0\u884c\u6001\u3001\u963b\u585e\u6001\u3002\u5728 Linux \u5185\u6838\u4e2d&#xff0c;\u8fd8\u6709 \u50f5\u5c38\u6001 (Zombie) \u548c \u6682\u505c\u6001 (Stopped)\u3002<\/p>\n<p>\u8fdb\u7a0b\u9000\u51fa\u7684 8 \u79cd\u60c5\u51b5&#xff1a;<\/p>\n<li>\n<p>main \u51fd\u6570\u4e2d return\u3002<\/p>\n<\/li>\n<li>\n<p>\u8c03\u7528 exit()&#xff08;C \u5e93\u51fd\u6570&#xff0c;\u4f1a\u5237\u65b0 I\/O \u7f13\u51b2\u533a&#xff0c;\u5173\u95ed\u6d41&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u8c03\u7528 _exit() \u6216 _Exit()&#xff08;\u7cfb\u7edf\u8c03\u7528&#xff0c;\u76f4\u63a5\u9000\u51fa&#xff0c;\u4e0d\u5237\u65b0\u7f13\u51b2\u533a&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e3b\u7ebf\u7a0b\u9000\u51fa\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e3b\u7ebf\u7a0b\u8c03\u7528 pthread_exit\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f02\u5e38\u7ec8\u6b62&#xff1a;\u8c03\u7528 abort()\u3002<\/p>\n<\/li>\n<li>\n<p>\u63a5\u6536\u5230\u81f4\u547d\u4fe1\u53f7&#xff08;\u5982 kill -9&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u6700\u540e\u4e00\u4e2a\u7ebf\u7a0b\u88ab pthread_cancel\u3002<\/p>\n<\/li>\n<hr \/>\n<h3>\u4e8c\u3001 \u8fdb\u7a0b\u7684\u521b\u5efa&#xff1a;fork() \u9b54\u6cd5\u4e0e\u5751\u70b9<\/h3>\n<p>c<\/p>\n<p>#include &lt;unistd.h&gt;<br \/>\npid_t fork(void);<\/p>\n<p>fork() \u4f1a\u514b\u9686\u51fa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u3002\u5b83\u6709\u4e00\u4e2a\u795e\u5947\u7684\u7279\u6027&#xff1a;\u8c03\u7528\u4e00\u6b21&#xff0c;\u8fd4\u56de\u4e24\u6b21\u3002<\/p>\n<ul>\n<li>\n<p>\u5728\u7236\u8fdb\u7a0b\u4e2d\u8fd4\u56de\u5b50\u8fdb\u7a0b\u7684 PID&#xff08;&gt; 0&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728\u5b50\u8fdb\u7a0b\u4e2d\u8fd4\u56de 0\u3002<\/p>\n<\/li>\n<li>\n<p>\u5931\u8d25\u8fd4\u56de -1\u3002<\/p>\n<\/li>\n<\/ul>\n<h4>1. \u5e95\u5c42\u673a\u5236&#xff1a;\u5199\u65f6\u590d\u5236 (Copy-on-Write)<\/h4>\n<p>fork \u521a\u521b\u5efa\u65f6&#xff0c;\u7236\u5b50\u8fdb\u7a0b\u7684\u7528\u6237\u7a7a\u95f4\u662f\u5b8c\u5168\u5171\u4eab\u7684&#xff08;\u53ea\u8bfb&#xff09;\u3002\u4e00\u65e6\u7236\u8fdb\u7a0b\u6216\u5b50\u8fdb\u7a0b\u5c1d\u8bd5\u4fee\u6539\u5185\u5b58\u6570\u636e&#xff0c;\u5185\u6838\u4f1a\u89e6\u53d1\u7f3a\u9875\u5f02\u5e38&#xff0c;\u6b64\u65f6\u624d\u4f1a\u4e3a\u4fee\u6539\u65b9\u771f\u6b63\u5206\u914d\u7269\u7406\u5185\u5b58\u9875\u3002\u8fd9\u6781\u5927\u63d0\u5347\u4e86 fork \u7684\u6548\u7387\u3002<\/p>\n<p>\u4ee3\u7801\u5b9e\u6218&#xff1a;\u9a8c\u8bc1\u5199\u65f6\u590d\u5236<\/p>\n<p>c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>int a &#061; 10;<br \/>\nint main() {<br \/>\n    pid_t pid &#061; fork();<br \/>\n    if (pid &gt; 0) {<br \/>\n        sleep(3);<br \/>\n        printf(&#034;\u7236\u8fdb\u7a0b: a &#061; %d\\\\n&#034;, a); \/\/ \u4f9d\u7136\u662f 10<br \/>\n    } else if (pid &#061;&#061; 0) {<br \/>\n        a &#061; 50; \/\/ \u89e6\u53d1\u5199\u65f6\u590d\u5236&#xff0c;\u5b50\u8fdb\u7a0b\u5f00\u8f9f\u65b0\u5185\u5b58\u9875<br \/>\n        printf(&#034;\u5b50\u8fdb\u7a0b: a &#061; %d\\\\n&#034;, a);<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>2. \u81f4\u547d\u5751\u70b9\u4e0e\u9690\u85cf\u5927\u5751<\/h4>\n<ul>\n<li>\n<p>Fork \u70b8\u5f39&#xff1a;\u5982\u679c\u5728 for \u5faa\u73af\u91cc fork&#xff0c;\u5b50\u8fdb\u7a0b\u521b\u5efa\u540e\u5fc5\u987b exit() \u6216 break\u3002\u5426\u5219\u5b50\u8fdb\u7a0b\u4e5f\u4f1a\u7ee7\u7eed\u5faa\u73af\u53bb fork&#xff0c;\u5bfc\u81f4\u8fdb\u7a0b\u6570\u5448\u6307\u6570\u7ea7\u7206\u70b8\u3002<\/p>\n<\/li>\n<li>\n<p>\u6587\u4ef6\u5171\u4eab\u5751&#xff1a;\u5982\u679c\u5728 fork \u4e4b\u524d\u6253\u5f00\u4e86\u6587\u4ef6&#xff0c;\u7236\u5b50\u8fdb\u7a0b\u4f1a\u5171\u4eab\u540c\u4e00\u4e2a\u6587\u4ef6\u8868\u9879&#xff08;\u5305\u62ec\u6587\u4ef6\u504f\u79fb\u91cf&#xff09;\u3002\u4e00\u65b9\u8bfb\u53d6\u540e&#xff0c;\u53e6\u4e00\u65b9\u7684\u504f\u79fb\u91cf\u4e5f\u4f1a\u8ddf\u7740\u53d8\u3002<\/p>\n<\/li>\n<li>\n<p>\u7f13\u51b2\u533a\u91cd\u590d\u6253\u5370\u5751&#xff1a;\u5982\u679c\u5728 fork \u4e4b\u524d printf \u4e14\u6ca1\u52a0 \\\\n&#xff08;\u6216\u8005\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u4e86\u6587\u4ef6&#xff09;&#xff0c;\u6570\u636e\u4f1a\u6ede\u7559\u5728\u7236\u8fdb\u7a0b\u7684\u6807\u51c6 I\/O \u7f13\u51b2\u533a\u91cc\u3002fork \u65f6\u5b50\u8fdb\u7a0b\u4f1a\u590d\u5236\u8fd9\u4e2a\u7f13\u51b2\u533a&#xff0c;\u6700\u7ec8\u5bfc\u81f4\u7236\u5b50\u8fdb\u7a0b\u5404\u6253\u5370\u4e00\u6b21&#xff0c;\u6570\u636e\u7ffb\u500d\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h3>\u4e09\u3001 \u8fdb\u7a0b\u56de\u6536&#xff1a;wait() \u4e0e waitpid() \u6df1\u5ea6\u5256\u6790<\/h3>\n<p>\u5b50\u8fdb\u7a0b\u9000\u51fa\u540e&#xff0c;\u7528\u6237\u7a7a\u95f4\u88ab\u91ca\u653e&#xff0c;\u4f46\u5185\u6838\u91cc\u7684 PCB \u6ca1\u6709\u88ab\u9500\u6bc1&#xff0c;\u53d8\u6210\u50f5\u5c38\u8fdb\u7a0b\u3002\u7236\u8fdb\u7a0b\u5fc5\u987b\u5bf9\u5176\u8fdb\u884c\u56de\u6536\u3002<\/p>\n<h4>1. wait() \u51fd\u6570\u5e95\u5c42\u539f\u7406\u4e0e\u72b6\u6001\u4f4d\u56fe<\/h4>\n<p>c<\/p>\n<p>#include &lt;sys\/wait.h&gt;<br \/>\npid_t wait(int *wstatus);<\/p>\n<p>&#xff08;1&#xff09;\u72b6\u6001\u5b57 wstatus \u7684\u4e8c\u8fdb\u5236\u5965\u79d8<br \/>\nwstatus \u662f\u4e00\u4e2a 4 \u5b57\u8282\u7684 int&#xff0c;\u4f46 Linux \u53ea\u4f7f\u7528\u4e86\u5b83\u7684\u4f4e 16 \u4f4d\u3002<\/p>\n<ul>\n<li>\n<p>\u9ad8 8 \u4f4d&#xff1a;\u5b58\u653e\u6b63\u5e38\u9000\u51fa\u65f6\u7684\u9000\u51fa\u7801&#xff08;\u5982 exit(20) \u4e2d\u7684 20&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f4e 8 \u4f4d&#xff1a;\u5982\u679c\u88ab\u4fe1\u53f7\u6740\u6b7b\u7684&#xff0c;\u4f4e 7 \u4f4d\u5b58\u653e\u4fe1\u53f7\u7f16\u53f7&#xff0c;\u6700\u4f4e\u4f4d\u662f core dump \u6807\u5fd7&#xff08;\u8868\u793a\u662f\u5426\u751f\u6210\u4e86\u6838\u5fc3\u8f6c\u50a8\u6587\u4ef6&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u5343\u4e07\u4e0d\u80fd\u76f4\u63a5\u5224\u65ad wstatus &#061;&#061; 20&#xff01;\u56e0\u4e3a\u9000\u51fa\u7801\u5728\u9ad8\u4f4d&#xff0c;\u5b9e\u9645\u6570\u503c\u53ef\u80fd\u662f 20 &lt;&lt; 8 &#061; 5120\u3002\u5fc5\u987b\u4f7f\u7528\u7cfb\u7edf\u5b8f&#xff1a;<\/p>\n<ul>\n<li>\n<p>WIFEXITED(wstatus)&#xff1a;\u5224\u65ad\u662f\u5426\u6b63\u5e38\u9000\u51fa\u3002<\/p>\n<\/li>\n<li>\n<p>WEXITSTATUS(wstatus)&#xff1a;\u83b7\u53d6\u6b63\u5e38\u9000\u51fa\u7684\u9000\u51fa\u7801\u3002<\/p>\n<\/li>\n<li>\n<p>WIFSIGNALED(wstatus)&#xff1a;\u5224\u65ad\u662f\u5426\u88ab\u4fe1\u53f7\u5f02\u5e38\u7ec8\u6b62\u3002<\/p>\n<\/li>\n<li>\n<p>WTERMSIG(wstatus)&#xff1a;\u83b7\u53d6\u7ec8\u6b62\u4fe1\u53f7\u7f16\u53f7\u3002<\/p>\n<\/li>\n<li>\n<p>WCOREDUMP(wstatus)&#xff1a;\u5224\u65ad\u662f\u5426\u751f\u6210\u4e86 core \u6587\u4ef6\u3002<\/p>\n<\/li>\n<\/ul>\n<p>&#xff08;2&#xff09;\u88ab\u4fe1\u53f7\u4e2d\u65ad\u7684\u5751&#xff08;EINTR&#xff09;<br \/>\n\u7236\u8fdb\u7a0b\u5728 wait \u963b\u585e\u65f6&#xff0c;\u5982\u679c\u6536\u5230\u5982 SIGALRM \u7b49\u4e2d\u65ad\u4fe1\u53f7&#xff0c;wait \u4f1a\u63d0\u524d\u8fd4\u56de -1&#xff0c;\u4e14 errno &#061; EINTR\u3002\u6b64\u65f6\u5b50\u8fdb\u7a0b\u672a\u7ed3\u675f&#xff0c;\u5fc5\u987b\u91cd\u65b0\u8c03\u7528\u3002<\/p>\n<p>c<\/p>\n<p>int status;<br \/>\npid_t ret;<br \/>\ndo {<br \/>\n    ret &#061; wait(&amp;status);<br \/>\n} while (ret &#061;&#061; -1 &amp;&amp; errno &#061;&#061; EINTR); \/\/ \u88ab\u4e2d\u65ad\u5c31\u91cd\u8bd5<\/p>\n<p>\u5b9e\u6218&#xff1a;\u963b\u585e\u7b49\u5f85\u4e0e\u72b6\u6001\u89e3\u6790<\/p>\n<p>c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;sys\/wait.h&gt;<\/p>\n<p>void print_wait_status(int status) {<br \/>\n    if (WIFEXITED(status)) {<br \/>\n        printf(&#034;\u5b50\u8fdb\u7a0b\u6b63\u5e38\u9000\u51fa&#xff0c;\u9000\u51fa\u7801: %d\\\\n&#034;, WEXITSTATUS(status));<br \/>\n    } else if (WIFSIGNALED(status)) {<br \/>\n        printf(&#034;\u5b50\u8fdb\u7a0b\u88ab\u4fe1\u53f7\u6740\u6b7b&#xff0c;\u4fe1\u53f7\u7f16\u53f7: %d\\\\n&#034;, WTERMSIG(status));<br \/>\n        if (WCOREDUMP(status)) printf(&#034;  [\u6ce8\u610f] \u8fdb\u7a0b\u6b7b\u524d\u751f\u6210\u4e86 core \u6587\u4ef6\\\\n&#034;);<br \/>\n    }<br \/>\n}<\/p>\n<hr \/>\n<h4>2. waitpid() \u9ad8\u7ea7\u63a7\u5236\u4e0e\u4f5c\u4e1a\u63a7\u5236<\/h4>\n<p>c<\/p>\n<p>pid_t waitpid(pid_t pid, int *wstatus, int options);<\/p>\n<p>waitpid \u662f wait \u7684\u8d85\u96c6&#xff0c;\u652f\u6301\u7cbe\u51c6\u5b9a\u4f4d\u548c\u975e\u963b\u585e\u3002<\/p>\n<p>&#xff08;1&#xff09;\u53c2\u6570 pid&#xff1a;<\/p>\n<ul>\n<li>\n<p>pid &gt; 0&#xff1a;\u7cbe\u51c6\u7b49\u5f85\u6307\u5b9a\u7684\u5b50\u8fdb\u7a0b\u3002<\/p>\n<\/li>\n<li>\n<p>pid &#061;&#061; -1&#xff1a;\u7b49\u5f85\u4efb\u610f\u5b50\u8fdb\u7a0b&#xff08;\u7b49\u4ef7\u4e8e wait&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>pid &#061;&#061; 0&#xff1a;\u7b49\u5f85\u540c\u8fdb\u7a0b\u7ec4\u7684\u4efb\u610f\u5b50\u8fdb\u7a0b\u3002<\/p>\n<\/li>\n<li>\n<p>pid &lt; -1&#xff1a;\u7b49\u5f85\u8fdb\u7a0b\u7ec4 ID \u7b49\u4e8e |pid| \u7684\u4efb\u610f\u5b50\u8fdb\u7a0b\u3002<\/p>\n<\/li>\n<\/ul>\n<p>&#xff08;2&#xff09;\u53c2\u6570 options \u4e0e\u8fd4\u56de\u503c&#xff1a;<\/p>\n<ul>\n<li>\n<p>0&#xff1a;\u9ed8\u8ba4\u963b\u585e\u3002<\/p>\n<\/li>\n<li>\n<p>WNOHANG&#xff1a;\u975e\u963b\u585e\u6a21\u5f0f\u3002\u5b50\u8fdb\u7a0b\u8fd8\u5728\u8dd1\u5219\u7acb\u5373\u8fd4\u56de 0&#xff0c;\u7edd\u4e0d\u963b\u585e\u7236\u8fdb\u7a0b\u3002<\/p>\n<\/li>\n<li>\n<p>WUNTRACED&#xff1a;\u5982\u679c\u5b50\u8fdb\u7a0b\u88ab\u6682\u505c&#xff08;\u5982 SIGSTOP&#xff09;&#xff0c;\u4e5f\u7acb\u5373\u8fd4\u56de\u72b6\u6001\u3002<\/p>\n<\/li>\n<li>\n<p>\u8fd4\u56de\u503c&#xff1a;\u6210\u529f &gt;0&#xff0c;\u672a\u7ed3\u675f 0&#xff08;\u4ec5\u975e\u963b\u585e&#xff09;&#xff0c;\u9519\u8bef -1\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u5b9e\u6218 1&#xff1a;\u975e\u963b\u585e\u8f6e\u8be2\u4e0e\u9632 CPU \u7a7a\u8f6c<\/p>\n<p>c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/wait.h&gt;<\/p>\n<p>int main() {<br \/>\n    pid_t ret[5] &#061; {0};<br \/>\n    for (int i &#061; 0; i &lt; 5; i&#043;&#043;) {<br \/>\n        ret[i] &#061; fork();<br \/>\n        if (ret[i] &#061;&#061; 0) {<br \/>\n            sleep(rand() % 5 &#043; 1); \/\/ \u968f\u673a\u7761 1-5 \u79d2<br \/>\n            exit(0);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u975e\u963b\u585e\u8f6e\u8be2\u56de\u6536\u7b2c\u4e8c\u4e2a\u5b50\u8fdb\u7a0b<br \/>\n    while (1) {<br \/>\n        pid_t recycle_pid &#061; waitpid(ret[1], NULL, WNOHANG);<br \/>\n        if (recycle_pid &#061;&#061; ret[1]) {<br \/>\n            printf(&#034;\u6210\u529f\u56de\u6536\u7b2c\u4e8c\u5b50\u8fdb\u7a0b wait pid :%d\\\\n&#034;, recycle_pid);<br \/>\n            break;<br \/>\n        } else if (0 &#061;&#061; recycle_pid) {<br \/>\n            usleep(100000); \/\/ \u9632 CPU \u7a7a\u8f6c&#xff0c;\u7761 100ms \u518d\u8bd5<br \/>\n        } else {<br \/>\n            break;<br \/>\n        }<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u5b9e\u6218 2&#xff1a;\u5229\u7528 WUNTRACED \u5b9e\u73b0\u4f5c\u4e1a\u63a7\u5236&#xff08;Job Control&#xff09;<br \/>\n\u5f53\u4f60\u5728\u7ec8\u7aef\u6309\u4e0b Ctrl&#043;Z&#xff0c;\u5185\u6838\u5411\u8fdb\u7a0b\u53d1\u9001 SIGTSTP \u4fe1\u53f7&#xff0c;\u8fdb\u7a0b\u8fdb\u5165 T (Stopped) \u72b6\u6001\u3002\u7236\u8fdb\u7a0b\u4f7f\u7528 WUNTRACED \u5c31\u80fd\u6355\u6349\u5230\u8fd9\u4e2a\u6682\u505c\u4e8b\u4ef6\u3002<\/p>\n<p>c<\/p>\n<p>\/\/ \u5b50\u8fdb\u7a0b\u6b7b\u5faa\u73af&#xff0c;\u7236\u8fdb\u7a0b 2\u79d2\u540e\u53d1 SIGSTOP \u6682\u505c\u5b83&#xff0c;\u518d\u53d1 SIGCONT \u6062\u590d<br \/>\nif (fork() &#061;&#061; 0) { while(1) sleep(1); }<br \/>\nsleep(2); kill(pid, SIGSTOP);<br \/>\n\/\/ \u6355\u6349\u6682\u505c<br \/>\nint status;<br \/>\nwaitpid(pid, &amp;status, WUNTRACED);<br \/>\nif (WIFSTOPPED(status)) printf(&#034;\u6355\u6349\u5230\u6682\u505c\u4fe1\u53f7:%d\\\\n&#034;, WSTOPSIG(status));<\/p>\n<p>\u5b9e\u6218 3&#xff1a;\u7ec8\u6781\u7ec4\u5408 SIGCHLD \u4fe1\u53f7 &#043; \u5f02\u6b65\u56de\u6536<br \/>\n\u4e0d\u8981\u8ba9\u7236\u8fdb\u7a0b\u50bb\u50bb\u963b\u585e\u5728 wait \u91cc\u3002\u5b50\u8fdb\u7a0b\u72b6\u6001\u6539\u53d8\u65f6\u4f1a\u7ed9\u7236\u8fdb\u7a0b\u53d1 SIGCHLD \u4fe1\u53f7&#xff0c;\u5728\u5904\u7406\u51fd\u6570\u91cc\u56de\u6536&#xff1a;<\/p>\n<p>c<\/p>\n<p>void sigchld_handler(int sig) {<br \/>\n    int status;<br \/>\n    pid_t pid;<br \/>\n    \/\/ \u5fc5\u987b\u7528 while &#043; WNOHANG \u56de\u6536\u6240\u6709\u6b7b\u53bb\u7684\u5b50\u8fdb\u7a0b&#xff0c;\u9632\u6b62\u4fe1\u53f7\u4e22\u5931\u5bfc\u81f4\u50f5\u5c38<br \/>\n    while ((pid &#061; waitpid(-1, &amp;status, WNOHANG)) &gt; 0) {<br \/>\n        printf(&#034;[\u5f02\u6b65] \u56de\u6536\u5b50\u8fdb\u7a0b PID: %d\\\\n&#034;, pid);<br \/>\n    }<br \/>\n}<br \/>\n\/\/ \u5728 main \u4e2d\u6ce8\u518c&#xff1a;<br \/>\n\/\/ struct sigaction sa; sa.sa_handler &#061; sigchld_handler; sa.sa_flags &#061; SA_RESTART;<br \/>\n\/\/ sigaction(SIGCHLD, &amp;sa, NULL);<\/p>\n<hr \/>\n<h3>\u56db\u3001 \u8fdb\u7a0b\u66ff\u6362&#xff1a;exec \u51fd\u6570\u65cf\u4e0e\u5185\u5b58\u593a\u820d<\/h3>\n<p>\u5982\u679c\u8bf4 fork \u662f\u201c\u751f\u5a03\u201d&#xff0c;exec \u5c31\u662f\u201c\u593a\u820d\u201d\u3002\u8c03\u7528 exec \u540e&#xff0c;\u5f53\u524d\u8fdb\u7a0b\u7684\u4ee3\u7801\u6bb5\u3001\u6570\u636e\u6bb5\u3001\u5806\u6808\u4f1a\u88ab\u65b0\u7a0b\u5e8f\u5b8c\u5168\u8986\u76d6&#xff0c;PID \u4fdd\u6301\u4e0d\u53d8\u3002\u4e00\u65e6\u6210\u529f&#xff0c;exec \u540e\u9762\u7684\u539f\u4ee3\u7801\u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c\u3002<\/p>\n<h4>1. \u5185\u5b58\u5e03\u5c40\u7684\u53d8\u8fc1\u4e0e\u4fdd\u7559<\/h4>\n<p>exec \u6267\u884c\u6210\u529f\u65f6&#xff0c;\u5185\u6838\u4f1a\u91cd\u5efa\u8fdb\u7a0b\u7684\u5185\u5b58\u3002\u4f46\u4ee5\u4e0b\u5c5e\u6027\u88ab\u4fdd\u7559&#xff1a;<\/p>\n<ul>\n<li>\n<p>PID\u3001PPID\u3001\u8fdb\u7a0b\u7ec4 ID\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002<\/p>\n<\/li>\n<li>\n<p>\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;fd&#xff09;&#xff08;\u9664\u975e\u8bbe\u7f6e\u4e86 FD_CLOEXEC&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u4fe1\u53f7\u63a9\u7801&#xff08;\u4f46\u81ea\u5b9a\u4e49\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u91cd\u7f6e\u4e3a\u9ed8\u8ba4&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<h4>2. \u51fd\u6570\u65cf\u4e0e\u547d\u540d\u89c4\u5219<\/h4>\n<table>\n<tr>\u540e\u7f00\u542b\u4e49\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>l (list)<\/td>\n<td>\u5217\u8868<\/td>\n<td>\u53c2\u6570\u9010\u4e2a\u5217\u51fa&#xff0c;\u5fc5\u987b\u4ee5 NULL \u7ed3\u5c3e\u3002<\/td>\n<\/tr>\n<tr>\n<td>v (vector)<\/td>\n<td>\u6570\u7ec4<\/td>\n<td>\u53c2\u6570\u6253\u5305\u8fdb char *argv[]\u3002<\/td>\n<\/tr>\n<tr>\n<td>p (PATH)<\/td>\n<td>\u8def\u5f84<\/td>\n<td>\u81ea\u52a8\u53bb $PATH \u4e2d\u67e5\u627e&#xff0c;\u65e0\u9700\u7edd\u5bf9\u8def\u5f84\u3002<\/td>\n<\/tr>\n<tr>\n<td>e (env)<\/td>\n<td>\u73af\u5883\u53d8\u91cf<\/td>\n<td>\u663e\u5f0f\u4f20\u5165\u81ea\u5b9a\u4e49\u73af\u5883\u53d8\u91cf\u6570\u7ec4&#xff0c;\u800c\u4e0d\u662f\u7ee7\u627f\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4ee3\u7801\u5b9e\u6218\u5bf9\u6bd4<\/p>\n<p>c<\/p>\n<p>\/\/ 1. execl: \u5168\u8def\u5f84 &#043; \u5217\u4e3e<br \/>\nexecl(&#034;\/bin\/ls&#034;, &#034;ls&#034;, &#034;-a&#034;, &#034;-l&#034;, NULL);<\/p>\n<p>\/\/ 2. execlp: PATH \u67e5\u627e &#043; \u5217\u4e3e<br \/>\nexeclp(&#034;ls&#034;, &#034;ls&#034;, &#034;-a&#034;, &#034;-l&#034;, NULL);<\/p>\n<p>\/\/ 3. execvp: PATH \u67e5\u627e &#043; \u6570\u7ec4&#xff08;\u6700\u5e38\u7528&#xff09;<br \/>\nchar *arg[] &#061; {&#034;ls&#034;, &#034;-a&#034;, &#034;-l&#034;, NULL};<br \/>\nexecvp(&#034;ls&#034;, arg);<\/p>\n<p>\/\/ 4. execle: \u5217\u8868\u4f20\u53c2 &#043; \u81ea\u5b9a\u4e49\u73af\u5883\u53d8\u91cf<br \/>\nchar *my_env[] &#061; {&#034;PATH&#061;\/bin:\/usr\/bin&#034;, &#034;MY_VAR&#061;hello&#034;, NULL};<br \/>\nexecle(&#034;\/usr\/bin\/env&#034;, &#034;env&#034;, NULL, my_env);<\/p>\n<h4>3. \u5fc5\u987b\u8b66\u60d5\u7684\u593a\u820d\u5751\u70b9<\/h4>\n<ul>\n<li>\n<p>\u5751\u70b9 1&#xff1a;\u6210\u529f\u4e0d\u8fd4\u56de&#xff0c;\u5931\u8d25\u5fc5\u987b\u5904\u7406<br \/>\n\u5982\u679c\u5931\u8d25\u8fd4\u56de -1&#xff0c;\u5982\u679c\u4e0d\u5904\u7406&#xff0c;\u5b50\u8fdb\u7a0b\u4f1a\u7ee7\u7eed\u6267\u884c\u7236\u8fdb\u7a0b\u539f\u672c\u7684\u4ee3\u7801\u903b\u8f91\u3002<\/p>\n<p>c<\/p>\n<p>if (fork() &#061;&#061; 0) {<br \/>\n    execlp(&#034;firefox&#034;, &#034;firefox&#034;, NULL);<br \/>\n    perror(&#034;\u542f\u52a8\u5931\u8d25&#034;); \/\/ \u5931\u8d25\u624d\u4f1a\u8d70\u5230\u8fd9<br \/>\n    exit(1);           \/\/ \u5fc5\u987b\u9000\u51fa<br \/>\n}\n<\/li>\n<li>\n<p>\u5751\u70b9 2&#xff1a;C\u6807\u51c6\u5e93\u7f13\u51b2\u533a\u7684\u4e22\u6570\u636e\u95ee\u9898<br \/>\n\u5982\u679c\u5728 exec \u4e4b\u524d\u6709 printf&#xff0c;\u4e14\u6570\u636e\u6ede\u7559\u5728\u7f13\u51b2\u533a&#xff08;\u88ab\u91cd\u5b9a\u5411\u5230\u6587\u4ef6\u65f6&#xff09;\u3002exec \u628a\u5185\u5b58\u6e05\u7a7a&#xff0c;\u6570\u636e\u76f4\u63a5\u4e22\u5931\u3002<br \/>\n\u89e3\u51b3&#xff1a;\u5728\u8c03\u7528 exec \u4e4b\u524d&#xff0c;\u663e\u5f0f\u5237\u65b0\u7f13\u51b2\u533a&#xff1a;fflush(stdout);<\/p>\n<\/li>\n<li>\n<p>\u5751\u70b9 3&#xff1a;FD \u6cc4\u6f0f\u4e0e FD_CLOEXEC<br \/>\n\u5982\u679c\u4e0d\u5173\u95ed\u4e0d\u76f8\u5173\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;exec \u540e\u4f1a\u6cc4\u6f0f\u7ed9\u65b0\u7a0b\u5e8f\u3002\u5728 open \u65f6\u52a0 O_CLOEXEC \u6807\u5fd7&#xff0c;\u5185\u6838\u4f1a\u81ea\u52a8\u5728 exec \u65f6\u5e2e\u4f60\u5173\u95ed\u8be5 FD\u3002<\/p>\n<\/li>\n<li>\n<p>\u5751\u70b9 4&#xff1a;\u4e3a\u4ec0\u4e48\u4e0d\u80fd\u7528 exec \u6267\u884c cd&#xff1f;<br \/>\ncd \u662f Shell \u5185\u7f6e\u547d\u4ee4\u3002\u4e14\u5728\u5b50\u8fdb\u7a0b\u4e2d cd \u4e0d\u5f71\u54cd\u7236\u8fdb\u7a0b\u8def\u5f84\u3002\u6539\u53d8\u76ee\u5f55\u5fc5\u987b\u7528\u7236\u8fdb\u7a0b\u8c03\u7528 chdir()\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h3>\u4e94\u3001 \u7ec8\u6781\u5c01\u88c5&#xff1a;system \u51fd\u6570\u4e0e\u8fd4\u56de\u503c\u89e3\u5305<\/h3>\n<p>system \u5c01\u88c5\u4e86 fork &#043; exec(\/bin\/sh -c) &#043; waitpid&#xff0c;\u5b83\u4f1a\u542f\u52a8\u5b50\u8fdb\u7a0b\u8c03\u7528 Shell \u6267\u884c\u5b57\u7b26\u4e32\u3002<\/p>\n<p>c<\/p>\n<p>int ret &#061; system(&#034;ls -l no_such_file.txt&#034;);<\/p>\n<p>\u8fd4\u56de\u503c\u89e3\u5305&#xff08;\u975e\u5e38\u91cd\u8981&#xff09;&#xff1a;<br \/>\n\u5982\u679c Shell \u627e\u4e0d\u5230\u547d\u4ee4&#xff0c;\u9000\u51fa\u7801\u901a\u5e38\u662f 127&#xff1b;\u5982\u679c\u80fd\u627e\u5230\u4f46\u65e0\u6743\u9650\u6267\u884c&#xff0c;\u901a\u5e38\u662f 126\u3002<\/p>\n<p>c<\/p>\n<p>if (ret &#061;&#061; -1) {<br \/>\n    perror(&#034;system \u521b\u5efa\u5b50\u8fdb\u7a0b\u5931\u8d25&#034;);<br \/>\n} else if (WIFEXITED(ret)) {<br \/>\n    int exit_status &#061; WEXITSTATUS(ret);<br \/>\n    if (exit_status &#061;&#061; 127) printf(&#034;\u7cfb\u7edf\u627e\u4e0d\u5230\u6307\u5b9a\u7684\u547d\u4ee4\\\\n&#034;);<br \/>\n    else if (exit_status &#061;&#061; 0) printf(&#034;\u547d\u4ee4\u6267\u884c\u6210\u529f\\\\n&#034;);<br \/>\n    else printf(&#034;\u547d\u4ee4\u5f02\u5e38\u9000\u51fa&#xff0c;\u7801:%d\\\\n&#034;, exit_status);<br \/>\n}<\/p>\n<p>\u7f3a\u70b9&#xff1a;\u7f3a\u4e4f\u7cbe\u7ec6\u63a7\u5236\u3002\u5982\u679c\u662f\u5199\u9ad8\u7ea7\u7a0b\u5e8f&#xff0c;\u5efa\u8bae\u8001\u8001\u5b9e\u5b9e\u7528 fork &#043; execvp\u3002<\/p>\n<hr \/>\n<h3>\u516d\u3001 \u7ec8\u6781\u5b9e\u6218&#xff1a;\u624b\u5199\u4e00\u4e2a\u8ff7\u4f60 Shell<\/h3>\n<p>\u7ed3\u5408\u6211\u4eec\u5b66\u8fc7\u7684\u6240\u6709\u77e5\u8bc6&#xff1a;fork \u751f\u5a03&#xff0c;execvp \u66ff\u6362&#xff0c;waitpid \u6536\u5c38&#xff0c;\u518d\u52a0\u4e0a\u5185\u7f6e\u547d\u4ee4\u62e6\u622a\u3002<\/p>\n<p>c<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;sys\/wait.h&gt;<\/p>\n<p>#define MAX_ARGS 64<\/p>\n<p>int main() {<br \/>\n    char line[1024];<br \/>\n    char *args[MAX_ARGS];<\/p>\n<p>    while (1) {<br \/>\n        printf(&#034;mini-shell&gt; &#034;);<br \/>\n        if (fgets(line, sizeof(line), stdin) &#061;&#061; NULL) break;<br \/>\n        line[strcspn(line, &#034;\\\\n&#034;)] &#061; 0; \/\/ \u53bb\u9664\u6362\u884c<\/p>\n<p>        if (strlen(line) &#061;&#061; 0) continue;<\/p>\n<p>        \/\/ \u89e3\u6790\u53c2\u6570<br \/>\n        int argc &#061; 0;<br \/>\n        char *token &#061; strtok(line, &#034; &#034;);<br \/>\n        while (token !&#061; NULL &amp;&amp; argc &lt; MAX_ARGS &#8211; 1) {<br \/>\n            args[argc&#043;&#043;] &#061; token;<br \/>\n            token &#061; strtok(NULL, &#034; &#034;);<br \/>\n        }<br \/>\n        args[argc] &#061; NULL;<\/p>\n<p>        \/\/ \u5904\u7406\u5185\u7f6e\u547d\u4ee4 (Built-in)<br \/>\n        if (strcmp(args[0], &#034;exit&#034;) &#061;&#061; 0) break;<br \/>\n        if (strcmp(args[0], &#034;cd&#034;) &#061;&#061; 0) {<br \/>\n            \/\/ \u5fc5\u987b\u7531 Shell \u7236\u8fdb\u7a0b\u4eb2\u81ea\u5207\u6362<br \/>\n            if (args[1] &#061;&#061; NULL) chdir(getenv(&#034;HOME&#034;));<br \/>\n            else if (chdir(args[1]) !&#061; 0) perror(&#034;cd \u5931\u8d25&#034;);<br \/>\n            continue; \/\/ \u76f4\u63a5\u8fdb\u5165\u4e0b\u4e00\u8f6e\u5faa\u73af&#xff0c;\u4e0d\u7528 fork<br \/>\n        }<\/p>\n<p>        \/\/ \u5916\u90e8\u547d\u4ee4&#xff1a;fork &#043; exec<br \/>\n        pid_t pid &#061; fork();<br \/>\n        if (pid &#061;&#061; 0) {<br \/>\n            \/\/ \u5b50\u8fdb\u7a0b&#xff1a;\u66ff\u6362\u4e3a\u5916\u90e8\u7a0b\u5e8f<br \/>\n            execvp(args[0], args);<br \/>\n            perror(&#034;\u6267\u884c\u5931\u8d25&#034;);<br \/>\n            exit(1); \/\/ exec \u5931\u8d25\u5fc5\u987b\u9000\u51fa<br \/>\n        } else if (pid &gt; 0) {<br \/>\n            \/\/ \u7236\u8fdb\u7a0b&#xff1a;\u7b49\u5f85\u5b50\u8fdb\u7a0b\u7ed3\u675f<br \/>\n            waitpid(pid, NULL, 0);<br \/>\n        } else {<br \/>\n            perror(&#034;fork&#034;);<br \/>\n        }<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<hr \/>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u8fdb\u7a0b\u63a7\u5236\u662f Linux \u9ad8\u7ea7\u5f00\u53d1\u7684\u57fa\u77f3\u3002\u7406\u89e3\u4e86\u72b6\u6001\u5b57\u7684 16 \u4f4d\u4f4d\u56fe&#xff0c;\u4f60\u5c31\u80fd\u770b\u900f\u8fdb\u7a0b\u7684\u201c\u6b7b\u56e0\u62a5\u544a\u201d&#xff1b;\u638c\u63e1\u4e86 WNOHANG \u4e0e SIGCHLD&#xff0c;\u4f60\u5c31\u638c\u63e1\u4e86\u5f02\u6b65\u5e76\u53d1\u63a7\u5236\u7684\u94a5\u5319&#xff1b;\u5403\u900f\u4e86 exec \u4e0e system&#xff0c;\u4f60\u5c31\u771f\u6b63\u7406\u89e3\u4e86 Shell \u751a\u81f3\u5bb9\u5668\u7684\u5e95\u5c42\u8fd0\u4f5c\u65b9\u5f0f\u3002\u8bf7\u52a1\u5fc5\u5c06\u4ee5\u4e0a\u4ee3\u7801\u5728\u4f60\u7684 Linux \u73af\u5883\u91cc\u4eb2\u624b\u8dd1\u4e00\u904d&#xff0c;\u5e76\u7ed3\u5408 ps aux \u89c2\u5bdf\u72b6\u6001\u5207\u6362&#xff01;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001 \u8fdb\u7a0b\u57fa\u7840&#xff1a;\u4ece PCB \u5230\u5185\u5b58\u5206\u5e031. \u4ec0\u4e48\u662f\u8fdb\u7a0b&#xff1f;\u8fdb\u7a0b\u662f\u7a0b\u5e8f\u6267\u884c\u7684\u8fc7\u7a0b\u3002\u5b83\u4e0d\u4ec5\u662f\u786c\u76d8\u4e0a\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6&#xff08;\u4ee3\u7801&#xff09;&#xff0c;\u8fd8\u9700\u8981 CPU \u8c03\u5ea6\u3001\u5185\u5b58\u8d44\u6e90\u3001\u6587\u4ef6\u7cfb\u7edf\u7b49\u914d\u5408\u3002<br \/>\nLinux \u5185\u6838\u901a\u8fc7 PCB&#xff08;\u8fdb\u7a0b\u63a7\u5236\u5757&#xff0c;task_struct \u7ed3\u6784\u4f53&#xff09; \u6765\u7ba1\u7406\u8fdb\u7a0b\u3002PCB \u4e2d\u8bb0\u5f55\u4e86&#xff1a;PID\u3001\u7236\u8fdb\u7a0b PID\u3001\u5f53\u524d\u5de5\u4f5c\u8def\u5f84\u3001\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u5217\u8868\u3001\u4fe1\u53f7\u5904\u7406\u3001\u8d44\u6e90\u4e0a\u9650\u7b49\u6838\u5fc3<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,58,43],"topic":[],"class_list":["post-118745","post","type-post","status-publish","format-standard","hentry","category-server","tag-java","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>Linux \u5b66\u4e60\u7b14\u8bb0(\u5341\u4e5d\uff09\u7cfb\u7edf\u7f16\u7a0b\uff1a\u6df1\u5ea6\u62c6\u89e3 wait\/waitpid \u4e0e exec \u51fd\u6570\u65cf - \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\/118745.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linux \u5b66\u4e60\u7b14\u8bb0(\u5341\u4e5d\uff09\u7cfb\u7edf\u7f16\u7a0b\uff1a\u6df1\u5ea6\u62c6\u89e3 wait\/waitpid \u4e0e exec \u51fd\u6570\u65cf - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001 \u8fdb\u7a0b\u57fa\u7840&#xff1a;\u4ece PCB \u5230\u5185\u5b58\u5206\u5e031. \u4ec0\u4e48\u662f\u8fdb\u7a0b&#xff1f;\u8fdb\u7a0b\u662f\u7a0b\u5e8f\u6267\u884c\u7684\u8fc7\u7a0b\u3002\u5b83\u4e0d\u4ec5\u662f\u786c\u76d8\u4e0a\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6&#xff08;\u4ee3\u7801&#xff09;&#xff0c;\u8fd8\u9700\u8981 CPU \u8c03\u5ea6\u3001\u5185\u5b58\u8d44\u6e90\u3001\u6587\u4ef6\u7cfb\u7edf\u7b49\u914d\u5408\u3002 Linux \u5185\u6838\u901a\u8fc7 PCB&#xff08;\u8fdb\u7a0b\u63a7\u5236\u5757&#xff0c;task_struct \u7ed3\u6784\u4f53&#xff09; \u6765\u7ba1\u7406\u8fdb\u7a0b\u3002PCB \u4e2d\u8bb0\u5f55\u4e86&#xff1a;PID\u3001\u7236\u8fdb\u7a0b PID\u3001\u5f53\u524d\u5de5\u4f5c\u8def\u5f84\u3001\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u5217\u8868\u3001\u4fe1\u53f7\u5904\u7406\u3001\u8d44\u6e90\u4e0a\u9650\u7b49\u6838\u5fc3\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/118745.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-27T18:56: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=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/118745.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/118745.html\",\"name\":\"Linux \u5b66\u4e60\u7b14\u8bb0(\u5341\u4e5d\uff09\u7cfb\u7edf\u7f16\u7a0b\uff1a\u6df1\u5ea6\u62c6\u89e3 wait\/waitpid \u4e0e exec \u51fd\u6570\u65cf - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-27T18:56:35+00:00\",\"dateModified\":\"2026-10-27T18:56:35+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/118745.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/118745.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/118745.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux \u5b66\u4e60\u7b14\u8bb0(\u5341\u4e5d\uff09\u7cfb\u7edf\u7f16\u7a0b\uff1a\u6df1\u5ea6\u62c6\u89e3 wait\/waitpid \u4e0e exec \u51fd\u6570\u65cf\"}]},{\"@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 \u5b66\u4e60\u7b14\u8bb0(\u5341\u4e5d\uff09\u7cfb\u7edf\u7f16\u7a0b\uff1a\u6df1\u5ea6\u62c6\u89e3 wait\/waitpid \u4e0e exec \u51fd\u6570\u65cf - \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\/118745.html","og_locale":"zh_CN","og_type":"article","og_title":"Linux \u5b66\u4e60\u7b14\u8bb0(\u5341\u4e5d\uff09\u7cfb\u7edf\u7f16\u7a0b\uff1a\u6df1\u5ea6\u62c6\u89e3 wait\/waitpid \u4e0e exec \u51fd\u6570\u65cf - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001 \u8fdb\u7a0b\u57fa\u7840&#xff1a;\u4ece PCB \u5230\u5185\u5b58\u5206\u5e031. \u4ec0\u4e48\u662f\u8fdb\u7a0b&#xff1f;\u8fdb\u7a0b\u662f\u7a0b\u5e8f\u6267\u884c\u7684\u8fc7\u7a0b\u3002\u5b83\u4e0d\u4ec5\u662f\u786c\u76d8\u4e0a\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6&#xff08;\u4ee3\u7801&#xff09;&#xff0c;\u8fd8\u9700\u8981 CPU \u8c03\u5ea6\u3001\u5185\u5b58\u8d44\u6e90\u3001\u6587\u4ef6\u7cfb\u7edf\u7b49\u914d\u5408\u3002 Linux \u5185\u6838\u901a\u8fc7 PCB&#xff08;\u8fdb\u7a0b\u63a7\u5236\u5757&#xff0c;task_struct \u7ed3\u6784\u4f53&#xff09; \u6765\u7ba1\u7406\u8fdb\u7a0b\u3002PCB \u4e2d\u8bb0\u5f55\u4e86&#xff1a;PID\u3001\u7236\u8fdb\u7a0b PID\u3001\u5f53\u524d\u5de5\u4f5c\u8def\u5f84\u3001\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u5217\u8868\u3001\u4fe1\u53f7\u5904\u7406\u3001\u8d44\u6e90\u4e0a\u9650\u7b49\u6838\u5fc3","og_url":"https:\/\/www.wsisp.com\/helps\/118745.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-27T18:56:35+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/118745.html","url":"https:\/\/www.wsisp.com\/helps\/118745.html","name":"Linux \u5b66\u4e60\u7b14\u8bb0(\u5341\u4e5d\uff09\u7cfb\u7edf\u7f16\u7a0b\uff1a\u6df1\u5ea6\u62c6\u89e3 wait\/waitpid \u4e0e exec \u51fd\u6570\u65cf - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-27T18:56:35+00:00","dateModified":"2026-10-27T18:56:35+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/118745.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/118745.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/118745.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Linux \u5b66\u4e60\u7b14\u8bb0(\u5341\u4e5d\uff09\u7cfb\u7edf\u7f16\u7a0b\uff1a\u6df1\u5ea6\u62c6\u89e3 wait\/waitpid \u4e0e exec \u51fd\u6570\u65cf"}]},{"@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\/118745","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=118745"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/118745\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=118745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=118745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=118745"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=118745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}