{"id":52046,"date":"2024-12-03T13:01:12","date_gmt":"2024-12-03T05:01:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52046\/"},"modified":"2024-12-03T13:01:12","modified_gmt":"2024-12-03T05:01:12","slug":"linux%e5%83%b5%e5%b0%b8%e8%bf%9b%e7%a8%8b%e6%80%8e%e4%b9%88%e9%81%bf%e5%85%8d","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52046\/","title":{"rendered":"linux\u50f5\u5c38\u8fdb\u7a0b\u600e\u4e48\u907f\u514d"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>linux\u50f5\u5c38\u8fdb\u7a0b\u600e\u4e48\u907f\u514d<\/h1>\n<p>\u600e\u4e48\u5165\u95e8\u6587\u7ae0\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u7c73\u4e91\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300alinux\u50f5\u5c38\u8fdb\u7a0b\u600e\u4e48\u907f\u514d\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<\/p>\n<blockquote>\n<p>linux\u50f5\u5c38\u8fdb\u7a0b\u662f\u4e00\u4e2a\u65e9\u5df2\u6b7b\u4ea1\u7684\u8fdb\u7a0b\uff0c\u4f46\u662f\u5728\u8fdb\u7a0b\u8868\u4e2d\u4ecd\u5360\u4e86\u4e00\u4e2a\u4f4d\u7f6e\uff1b\u5982\u679c\u5b50\u8fdb\u7a0b\u6b7b\u4ea1\u65f6\u7236\u8fdb\u7a0b\u6ca1\u6709wait()\uff0c\u901a\u5e38\u7528ps\u53ef\u4ee5\u770b\u5230\u5b83\u88ab\u663e\u793a\u4e3a\u201c  \u201d\uff0c\u8fd9\u6837\u5c31\u4ea7\u751f\u4e86\u50f5\u5c38\u8fdb\u7a0b\uff1b\u5982\u679c\u5927\u91cf\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b\uff0c\u90a3\u4e48\u5c06\u56e0\u4e3a\u6ca1\u6709\u53ef\u7528\u7684\u8fdb\u7a0b\u53f7\u800c\u5bfc\u81f4\u7cfb\u7edf\u4e0d\u80fd\u4ea7\u751f\u65b0\u7684\u8fdb\u7a0b\uff0c\u6240\u4ee5\u8981\u907f\u514d\u6709\u50f5\u5c38\u8fdb\u7a0b\u3002 <\/p>\n<\/blockquote>\n<h3>\u4e00\u3001\u4ec0\u4e48\u662f\u50f5\u5c38\u8fdb\u7a0b<\/h3>\n<p>\u5728UNIX \u7cfb\u7edf\u4e2d\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u7ed3\u675f\u4e86\uff0c\u4f46\u662f\u4ed6\u7684\u7236\u8fdb\u7a0b\u6ca1\u6709\u7b49\u5f85(\u8c03\u7528wait \/ waitpid)\u4ed6\uff0c\u90a3\u4e48\u4ed6\u5c06\u53d8\u6210\u4e00\u4e2a\u50f5\u5c38\u8fdb\u7a0b\u3002\u5f53\u7528ps\u547d\u4ee4\u89c2\u5bdf\u8fdb\u7a0b\u7684\u6267\u884c\u72b6\u6001\u65f6\uff0c\u770b\u5230\u8fd9\u4e9b\u8fdb\u7a0b\u7684\u72b6\u6001\u680f\u4e3adefunct\u3002\u50f5\u5c38\u8fdb\u7a0b\u662f\u4e00\u4e2a\u65e9\u5df2\u6b7b\u4ea1\u7684\u8fdb\u7a0b\uff0c\u4f46\u5728\u8fdb\u7a0b\u8868\uff08processs table\uff09\u4e2d\u4ecd\u5360\u4e86\u4e00\u4e2a\u4f4d\u7f6e\uff08slot\uff09\u3002<\/p>\n<p>\u4f46\u662f\u5982\u679c\u8be5\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b\u5df2\u7ecf\u5148\u7ed3\u675f\u4e86\uff0c\u90a3\u4e48\u8be5\u8fdb\u7a0b\u5c31\u4e0d\u4f1a\u53d8\u6210\u50f5\u5c38\u8fdb\u7a0b\u3002\u56e0\u4e3a\u6bcf\u4e2a\u8fdb\u7a0b\u7ed3\u675f\u7684\u65f6\u5019,\u7cfb\u7edf\u90fd\u4f1a\u626b\u63cf\u5f53\u524d\u7cfb\u7edf\u4e2d\u6240\u8fd0\u884c\u7684\u6240\u6709\u8fdb\u7a0b\uff0c\u770b\u770b\u6709\u6ca1\u6709\u54ea\u4e2a\u8fdb\u7a0b\u662f\u521a\u521a\u7ed3\u675f\u7684\u8fd9\u4e2a\u8fdb\u7a0b\u7684\u5b50\u8fdb\u7a0b\uff0c\u5982\u679c\u662f\u7684\u8bdd\uff0c\u5c31\u7531Init\u8fdb\u7a0b\u6765\u63a5\u7ba1\u4ed6\uff0c\u6210\u4e3a\u4ed6\u7684\u7236\u8fdb\u7a0b\uff0c\u4ece\u800c\u4fdd\u8bc1\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u4f1a\u6709\u4e00\u4e2a\u7236\u8fdb\u7a0b\u3002\u800cInit\u8fdb\u7a0b\u4f1a\u81ea\u52a8wait\u5176\u5b50\u8fdb\u7a0b,\u56e0\u6b64\u88abInit\u63a5\u7ba1\u7684\u6240\u6709\u8fdb\u7a0b\u90fd\u4e0d\u4f1a\u53d8\u6210\u50f5\u5c38\u8fdb\u7a0b\u3002<\/p>\n<h3>\u4e8c\u3001UNIX\u4e0b\u8fdb\u7a0b\u7684\u8fd0\u4f5c\u65b9\u5f0f<\/h3>\n<p>\u6bcf\u4e2aUnix\u8fdb\u7a0b\u5728\u8fdb\u7a0b\u8868\u91cc\u90fd\u6709\u4e00\u4e2a\u8fdb\u5165\u70b9\uff08entry\uff09\uff0c<strong>\u6838\u5fc3\u8fdb\u7a0b<\/strong>\u6267\u884c\u8be5\u8fdb\u7a0b\u65f6\u4f7f\u7528\u5230\u7684\u4e00\u5207\u4fe1\u606f\u90fd\u5b58\u50a8\u5728\u8fdb\u5165\u70b9\u3002\u5f53\u7528 ps \u547d\u4ee4\u5bdf\u770b\u7cfb\u7edf\u4e2d\u7684\u8fdb\u7a0b\u4fe1\u606f\u65f6\uff0c\u770b\u5230\u7684\u5c31\u662f\u8fdb\u7a0b\u8868\u4e2d\u7684\u76f8\u5173\u6570\u636e\u3002\u5f53\u4ee5fork()\u7cfb\u7edf\u8c03\u7528\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\u540e\uff0c\u6838\u5fc3\u8fdb\u7a0b\u5c31\u4f1a\u5728\u8fdb\u7a0b\u8868\u4e2d\u7ed9\u8fd9\u4e2a\u65b0\u8fdb\u7a0b\u5206\u914d\u4e00\u4e2a\u8fdb\u5165\u70b9\uff0c\u7136\u540e\u5c06\u76f8\u5173\u4fe1\u606f\u5b58\u50a8\u5728\u8be5\u8fdb\u5165\u70b9\u6240\u5bf9\u5e94\u7684\u8fdb\u7a0b\u8868\u5185\u3002\u8fd9\u4e9b\u4fe1\u606f\u4e2d\u6709\u4e00\u9879\u662f\u5176\u7236\u8fdb\u7a0b\u7684\u8bc6\u522b\u7801\u3002<\/p>\n<p>\u5b50\u8fdb\u7a0b\u7684\u7ed3\u675f\u548c\u7236\u8fdb\u7a0b\u7684\u8fd0\u884c\u662f\u4e00\u4e2a\u5f02\u6b65\u8fc7\u7a0b\uff0c\u5373\u7236\u8fdb\u7a0b\u6c38\u8fdc\u65e0\u6cd5\u9884\u6d4b\u5b50\u8fdb\u7a0b\u5230\u5e95\u4ec0\u4e48\u65f6\u5019\u7ed3\u675f\u3002\u90a3\u4e48\u4f1a\u4e0d\u4f1a\u56e0\u4e3a\u7236\u8fdb\u7a0b\u592a\u5fd9\u6765\u4e0d\u53ca wait \u5b50\u8fdb\u7a0b\uff0c\u6216\u8005\u8bf4\u4e0d\u77e5\u9053\u5b50\u8fdb\u7a0b\u4ec0\u4e48\u65f6\u5019\u7ed3\u675f\uff0c\u800c\u4e22\u5931\u5b50\u8fdb\u7a0b\u7ed3\u675f\u65f6\u7684\u72b6\u6001\u4fe1\u606f\u5462\uff1f<\/p>\n<p>\u4e0d\u4f1a\u3002\u56e0\u4e3aUNIX\u63d0\u4f9b\u4e86\u4e00\u79cd\u673a\u5236\u53ef\u4ee5\u4fdd\u8bc1\uff0c\u53ea\u8981\u7236\u8fdb\u7a0b\u60f3\u77e5\u9053\u5b50\u8fdb\u7a0b\u7ed3\u675f\u65f6\u7684\u72b6\u6001\u4fe1\u606f\uff0c\u5c31\u53ef\u4ee5\u5f97\u5230\u3002\u8fd9\u79cd\u673a\u5236\u5c31\u662f\uff1a\u5f53\u5b50\u8fdb\u7a0b\u8d70\u5b8c\u4e86\u81ea\u5df1\u7684\u751f\u547d\u5468\u671f\u540e\uff0c\u5b83\u4f1a\u6267\u884cexit()\u7cfb\u7edf\u8c03\u7528\uff0c\u5185\u6838\u91ca\u653e\u8be5\u8fdb\u7a0b\u6240\u6709\u7684\u8d44\u6e90\uff0c\u5305\u62ec\u6253\u5f00\u7684\u6587\u4ef6\uff0c\u5360\u7528\u7684\u5185\u5b58\u7b49\u3002\u4f46\u662f\u4ecd\u7136\u4e3a\u5176\u4fdd\u7559\u4e00\u5b9a\u7684\u4fe1\u606f\uff08\u5305\u62ec\u8fdb\u7a0b\u53f7the process ID\uff0c\u9000\u51fa\u7801exit code\uff0c\u9000\u51fa\u72b6\u6001the terminationstatus of the process\uff0c\u8fd0\u884c\u65f6\u95f4the amount of CPU time taken by the process\u7b49\uff09\uff0c\u8fd9\u4e9b\u6570\u636e\u4f1a\u4e00\u76f4\u4fdd\u7559\u5230\u7cfb\u7edf\u5c06\u5b83\u4f20\u9012\u7ed9\u5b83\u7684\u7236\u8fdb\u7a0b\u4e3a\u6b62\uff0c\u76f4\u5230\u7236\u8fdb\u7a0b\u901a\u8fc7wait \/ waitpid\u6765\u53d6\u65f6\u624d\u91ca\u653e\u3002<\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u4e00\u4e2a\u8fdb\u7a0b\u6b7b\u4ea1\u65f6\uff0c\u5b83\u5e76\u4e0d\u662f\u5b8c\u5168\u7684\u6d88\u5931\u4e86\u3002\u8fdb\u7a0b\u7ec8\u6b62\uff0c\u5b83\u4e0d\u518d\u8fd0\u884c\uff0c\u4f46\u662f\u8fd8\u6709\u4e00\u4e9b\u6b8b\u7559\u7684\u6570\u636e\u7b49\u5f85\u7236\u8fdb\u7a0b\u6536\u56de\u3002\u5f53\u7236\u8fdb\u7a0b fork() \u4e00\u4e2a\u5b50\u8fdb\u7a0b\u540e\uff0c\u5b83\u5fc5\u987b\u7528 wait() \uff08\u6216\u8005 waitpid()\uff09\u7b49\u5f85\u5b50\u8fdb\u7a0b\u9000\u51fa\u3002\u6b63\u662f\u8fd9\u4e2a wait() \u52a8\u4f5c\u6765\u8ba9\u5b50\u8fdb\u7a0b\u7684\u6b8b\u7559\u6570\u636e\u6d88\u5931\u3002<\/p>\n<h3>\u4e09\u3001\u50f5\u5c38\u8fdb\u7a0b\u7684\u5371\u5bb3<\/h3>\n<p>\u5982\u679c\u7236\u8fdb\u7a0b\u4e0d\u8c03\u7528wait \/ waitpid\u7684\u8bdd\uff0c\u90a3\u4e48\u4fdd\u7559\u7684\u90a3\u6bb5\u4fe1\u606f\u5c31\u4e0d\u4f1a\u91ca\u653e\uff0c\u5176\u8fdb\u7a0b\u53f7\u5c31\u4f1a\u4e00\u76f4\u88ab\u5360\u7528\uff0c\u4f46\u662f\u7cfb\u7edf\u7684\u8fdb\u7a0b\u8868\u5bb9\u91cf\u662f\u6709\u9650\u7684\uff0c\u6240\u80fd\u4f7f\u7528\u7684\u8fdb\u7a0b\u53f7\u4e5f\u662f\u6709\u9650\u7684\uff0c\u5982\u679c\u5927\u91cf\u7684\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b,\u5c06\u56e0\u4e3a\u6ca1\u6709\u53ef\u7528\u7684\u8fdb\u7a0b\u53f7\u800c\u5bfc\u81f4\u7cfb\u7edf\u4e0d\u80fd\u4ea7\u751f\u65b0\u7684\u8fdb\u7a0b\u3002<\/p>\n<p>\u6240\u4ee5\uff0cdefunct\u8fdb\u7a0b\u4e0d\u4ec5\u5360\u7528\u7cfb\u7edf\u7684\u5185\u5b58\u8d44\u6e90\uff0c\u5f71\u54cd\u7cfb\u7edf\u7684\u6027\u80fd\uff0c\u800c\u4e14\u5982\u679c\u5176\u6570\u76ee\u592a\u591a\uff0c\u8fd8\u4f1a\u5bfc\u81f4\u7cfb\u7edf\u762b\u75ea\u3002\u800c\u4e14\uff0c\u7531\u4e8e\u8c03\u5ea6\u7a0b\u5e8f\u65e0\u6cd5\u9009\u4e2dDefunct \u8fdb\u7a0b\uff0c\u6240\u4ee5\u4e0d\u80fd\u7528kill\u547d\u4ee4\u5220\u9664Defunct \u8fdb\u7a0b\uff0c\u60df\u4e00\u7684\u65b9\u6cd5\u53ea\u6709\u91cd\u542f\u7cfb\u7edf\u3002<\/p>\n<h3>\u56db\u3001\u50f5\u5c38\u8fdb\u7a0b\u7684\u4ea7\u751f<\/h3>\n<p>\u5982\u679c\u5b50\u8fdb\u7a0b\u6b7b\u4ea1\u65f6\u7236\u8fdb\u7a0b\u6ca1\u6709 wait()\uff0c\u901a\u5e38\u7528 ps \u53ef\u4ee5\u770b\u5230\u5b83\u88ab\u663e\u793a\u4e3a\u201c&lt;defunct&gt;\u201d\uff0c\u8fd9\u6837\u5c31\u4ea7\u751f\u4e86\u50f5\u5c38\u8fdb\u7a0b\u3002\u5b83\u5c06\u6c38\u8fdc\u4fdd\u6301\u8fd9\u6837\u76f4\u5230\u7236\u8fdb\u7a0b wait()\u3002<\/p>\n<p>\u7531\u6b64\u53ef\u89c1\uff0cdefunct\u8fdb\u7a0b\u7684\u51fa\u73b0\u65f6\u95f4\u662f\u5728\u5b50\u8fdb\u7a0b\u7ec8\u6b62\u540e\uff0c\u4f46\u662f\u7236\u8fdb\u7a0b\u5c1a\u672a\u8bfb\u53d6\u8fd9\u4e9b\u6570\u636e\u4e4b\u524d\u3002\u5229\u7528\u8fd9\u4e00\u70b9\u6211\u4eec\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u7a0b\u5e8f\u5efa\u7acb\u4e00\u4e2adefunct \u8fdb\u7a0b\uff1a<\/p>\n<pre>#include &lt;stdio.h&gt; \r\n \r\n#include&lt;sys\/types.h&gt; \r\n \r\nmain() &nbsp;\r\n{ &nbsp;\r\n \r\n if(!fork()) &nbsp;\r\n &nbsp; &nbsp;{ &nbsp;\r\n \r\n &nbsp; &nbsp; &nbsp; &nbsp;printf(\u201cchild pid=%d\\n\u201d, getpid()); &nbsp;\r\n \r\n &nbsp; &nbsp; &nbsp; &nbsp;exit(0); &nbsp;\r\n \r\n &nbsp; &nbsp;} &nbsp;\r\n \r\n &nbsp; &nbsp;sleep(20); &nbsp;\r\n \r\n &nbsp; &nbsp;printf(\u201cparent pid=%d \\n\u201d, getpid()); &nbsp;\r\n \r\n &nbsp; &nbsp;exit(0); &nbsp;\r\n \r\n}\r\n<\/pre>\n<p>\u5f53\u4e0a\u8ff0\u7a0b\u5e8f\u4ee5\u540e\u53f0\u7684\u65b9\u5f0f\u6267\u884c\u65f6\uff0c\u7b2c17\u884c\u5f3a\u8feb\u7a0b\u5e8f\u7761\u772020\u79d2\uff0c\u8ba9\u7528\u6237\u6709\u65f6\u95f4\u8f93\u5165ps -e\u6307\u4ee4\uff0c\u89c2\u5bdf\u8fdb\u7a0b\u7684\u72b6\u6001\uff0c\u6211\u4eec\u770b\u5230\u8fdb\u7a0b\u8868\u4e2d\u51fa\u73b0\u4e86defunct\u8fdb\u7a0b\u3002\u5f53\u7236\u8fdb\u7a0b\u6267\u884c\u7ec8\u6b62\u540e\uff0c\u518d\u7528ps -e\u547d\u4ee4\u89c2\u5bdf\u65f6\uff0c\u6211\u4eec\u4f1a\u53d1\u73b0defunct\u8fdb\u7a0b\u4e5f\u968f\u4e4b\u6d88\u5931\u3002\u8fd9\u662f\u56e0\u4e3a\u7236\u8fdb\u7a0b\u7ec8\u6b62\u540e\uff0cinit \u8fdb\u7a0b\u4f1a\u63a5\u7ba1\u7236\u8fdb\u7a0b\u7559\u4e0b\u7684\u8fd9\u4e9b\u201c<strong>\u5b64\u513f\u8fdb\u7a0b<\/strong>\u201d\uff08orphan process\uff09\uff0c\u800c\u8fd9\u4e9b\u201c\u5b64\u513f\u8fdb\u7a0b\u201d\u6267\u884c\u5b8c\u540e\uff0c\u5b83\u5728\u8fdb\u7a0b\u8868\u4e2d\u7684\u8fdb\u5165\u70b9\u5c06\u88ab\u5220\u9664\u3002\u5982\u679c\u4e00\u4e2a\u7a0b\u5e8f\u8bbe\u8ba1\u4e0a\u6709\u7f3a\u9677\uff0c\u5c31\u53ef\u80fd\u5bfc\u81f4\u67d0\u4e2a\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b\u4e00\u76f4\u5904\u4e8e\u7761\u7720\u72b6\u6001\u6216\u662f\u9677\u5165\u6b7b\u5faa\u73af\uff0c\u7236\u8fdb\u7a0b\u6ca1\u6709wait\u5b50\u8fdb\u7a0b\uff0c\u4e5f\u6ca1\u6709\u7ec8\u6b62\u4ee5\u4f7fInit\u63a5\u7ba1\uff0c\u8be5\u5b50\u8fdb\u7a0b\u6267\u884c\u7ed3\u675f\u540e\u5c31\u53d8\u6210\u4e86defunct\u8fdb\u7a0b\uff0c\u8fd9\u4e2adefunct \u8fdb\u7a0b\u53ef\u80fd\u4f1a\u4e00\u76f4\u7559\u5728\u7cfb\u7edf\u4e2d\u76f4\u5230\u7cfb\u7edf\u91cd\u65b0\u542f\u52a8\u3002<\/p>\n<p>\u5728\u770b\u4e00\u4e2a\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b\u7684\u4f8b\u5b50\u3002<\/p>\n<p>\u5b50\u8fdb\u7a0b\u8981\u6267\u884c\u7684\u7a0b\u5e8ftest_prog<\/p>\n<pre>\/\/test.c \r\n#include &lt;stdio.h&gt; int main() &nbsp;{ &nbsp;\r\n int i = 0; &nbsp;\r\n for (i = 0 ; i &lt; 10; i++) &nbsp;\r\n &nbsp; &nbsp; &nbsp; &nbsp;{ &nbsp;\r\n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf (\"child time %d\\n\", i+1); &nbsp;\r\n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sleep (1); &nbsp;\r\n &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp;\r\n return 0; &nbsp;}\r\n<\/pre>\n<p>\u7236\u8fdb\u7a0bfather\u7684\u4ee3\u7801father.c<\/p>\n<pre>#include&nbsp;&lt;stdio.h&gt;&nbsp;\n#include&nbsp;&lt;unistd.h&gt;&nbsp;\n#include&nbsp;&lt;sys\/types.h&gt;&nbsp;\n#include&nbsp;&lt;sys\/wait.h&gt;&nbsp;\nint&nbsp;main()&nbsp;&nbsp;\n{&nbsp;&nbsp;\n&nbsp;int&nbsp;pid&nbsp;=&nbsp;fork&nbsp;();&nbsp;&nbsp;\n&nbsp;if&nbsp;(pid&nbsp;==&nbsp;0)&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system&nbsp;(\".\/test_prog\");&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_exit&nbsp;(0);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}else&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\n&nbsp;int&nbsp;i&nbsp;=&nbsp;0;&nbsp;&nbsp;\n&nbsp;\/*&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;status&nbsp;=&nbsp;0;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(!waitpid(pid,&nbsp;&amp;status,&nbsp;WNOHANG))&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf&nbsp;(\"father&nbsp;waiting%d\\n\",&nbsp;++i);&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep&nbsp;(1);&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}*\/&nbsp;\n&nbsp;while&nbsp;(1)&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf&nbsp;(\"father&nbsp;waiting&nbsp;over%d\\n\",&nbsp;++i);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep&nbsp;(1);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n&nbsp;return&nbsp;0;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n&nbsp;\n}<\/pre>\n<p>\u6267\u884c.\/father,\u5f53\u5b50\u8fdb\u7a0b\u9000\u51fa\u540e\uff0c\u7531\u4e8e\u7236\u8fdb\u7a0b\u6ca1\u6709\u5bf9\u5b83\u7684\u9000\u51fa\u8fdb\u884c\u5173\u6ce8\uff0c\u4f1a\u51fa\u73b0\u50f5\u5c38\u8fdb\u7a0b<\/p>\n<pre>20786 pts\/0 &nbsp; &nbsp;00:00:00 father &nbsp;\r\n20787 pts\/0 &nbsp; &nbsp;00:00:00 father &lt;defunct&gt;\r\n<\/pre>\n<p>\u603b\u7ed3\uff1a\u5b50\u8fdb\u7a0b\u6210\u4e3a defunct \u76f4\u5230\u7236\u8fdb\u7a0b wait()\uff0c\u9664\u975e\u7236\u8fdb\u7a0b\u5ffd\u7565\u4e86 SIGCLD \u3002\u66f4\u8fdb\u4e00\u6b65\uff0c\u7236\u8fdb\u7a0b\u6ca1\u6709 wait() \u5c31\u6d88\u4ea1\uff08\u4ecd\u5047\u8bbe\u7236\u8fdb\u7a0b\u6ca1\u6709\u5ffd\u7565 SIGCLD \uff09\u7684\u5b50\u8fdb\u7a0b\uff08\u6d3b\u52a8\u7684\u6216\u8005 defunct\uff09\u6210\u4e3a init \u7684\u5b50\u8fdb\u7a0b\uff0cinit \u7740\u624b\u5904\u7406\u5b83\u4eec\u3002<\/p>\n<h3>\u4e94\u3001\u5982\u4f55\u907f\u514d\u50f5\u5c38\u8fdb\u7a0b<\/h3>\n<p>1\u3001\u7236\u8fdb\u7a0b\u901a\u8fc7wait\u548cwaitpid\u7b49\u51fd\u6570\u7b49\u5f85\u5b50\u8fdb\u7a0b\u7ed3\u675f\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u7236\u8fdb\u7a0b\u6302\u8d77\u3002<\/p>\n<p>\u5728\u4e0a\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u6211\u4eec\u7565\u4f5c\u4fee\u6539\uff0c\u5728\u7b2c8\u884csleep()\u7cfb\u7edf\u8c03\u7528\u524d\u6267\u884cwait()\u6216waitpid()\u7cfb\u7edf\u8c03\u7528\uff0c\u5219\u5b50\u8fdb\u7a0b\u5728\u7ec8\u6b62\u540e\u4f1a\u7acb\u5373\u628a\u5b83\u5728\u8fdb\u7a0b\u8868\u4e2d\u7684\u6570\u636e\u8fd4\u56de\u7ed9\u7236\u8fdb\u7a0b\uff0c\u6b64\u65f6\u7cfb\u7edf\u4f1a\u7acb\u5373\u5220\u9664\u8be5\u8fdb\u5165\u70b9\u3002\u5728\u8fd9\u79cd\u60c5\u5f62\u4e0b\u5c31\u4e0d\u4f1a\u4ea7\u751fdefunct\u8fdb\u7a0b\u3002<\/p>\n<p>2. \u5982\u679c\u7236\u8fdb\u7a0b\u5f88\u5fd9\uff0c\u90a3\u4e48\u53ef\u4ee5\u7528signal\u51fd\u6570\u4e3aSIGCHLD\u5b89\u88c5handler\u3002\u5728\u5b50\u8fdb\u7a0b\u7ed3\u675f\u540e\uff0c\u7236\u8fdb\u7a0b\u4f1a\u6536\u5230\u8be5\u4fe1\u53f7\uff0c\u53ef\u4ee5\u5728handler\u4e2d\u8c03\u7528wait\u56de\u6536\u3002<\/p>\n<p>3. \u5982\u679c\u7236\u8fdb\u7a0b\u4e0d\u5173\u5fc3\u5b50\u8fdb\u7a0b\u4ec0\u4e48\u65f6\u5019\u7ed3\u675f\uff0c\u90a3\u4e48\u53ef\u4ee5\u7528<strong>signal(SIGCLD, SIG_IGN)\u6216signal\uff08SIGCHLD, SIG_IGN\uff09<\/strong>\u901a\u77e5\u5185\u6838\uff0c\u81ea\u5df1\u5bf9\u5b50\u8fdb\u7a0b\u7684\u7ed3\u675f\u4e0d\u611f\u5174\u8da3\uff0c\u90a3\u4e48\u5b50\u8fdb\u7a0b\u7ed3\u675f\u540e\uff0c\u5185\u6838\u4f1a\u56de\u6536\uff0c\u5e76\u4e0d\u518d\u7ed9\u7236\u8fdb\u7a0b\u53d1\u9001\u4fe1\u53f7<\/p>\n<p>4. fork\u4e24\u6b21\uff0c\u7236\u8fdb\u7a0bfork\u4e00\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u7136\u540e\u7ee7\u7eed\u5de5\u4f5c\uff0c\u5b50\u8fdb\u7a0bfork\u4e00\u4e2a\u5b59\u8fdb\u7a0b\u540e\u9000\u51fa\uff0c\u90a3\u4e48\u5b59\u8fdb\u7a0b\u88abinit\u63a5\u7ba1\uff0c\u5b59\u8fdb\u7a0b\u7ed3\u675f\u540e\uff0cinit\u4f1a\u56de\u6536\u3002\u4e0d\u8fc7\u5b50\u8fdb\u7a0b\u7684\u56de\u6536\u8fd8\u8981\u81ea\u5df1\u505a\u3002 \u4e0b\u9762\u5c31\u662fStevens\u7ed9\u7684\u91c7\u7528\u4e24\u6b21folk\u907f\u514d\u50f5\u5c38\u8fdb\u7a0b\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>#include \"apue.h\" #include &lt;sys\/wait.h&gt; &nbsp;int main(void) &nbsp;...{ &nbsp;\r\n &nbsp; &nbsp; pid_t &nbsp; &nbsp;pid; &nbsp;\r\n \r\n if ((pid = fork()) &lt; 0) ...{ &nbsp;\r\n &nbsp; &nbsp; &nbsp; &nbsp; err_sys(\"fork error\"); &nbsp;\r\n &nbsp; &nbsp; } else if (pid == 0) ...{ &nbsp; &nbsp; \/**\/\/* first child *\/ \r\n if ((pid = fork()) &lt; 0) &nbsp;\r\n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; err_sys(\"fork error\"); &nbsp;\r\n else if (pid &gt; 0) &nbsp;\r\n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit(0); &nbsp; &nbsp;\/**\/\/* parent from second fork == first child *\/ \r\n \/**\/\/* \r\n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* We're the second child; our parent becomes init as soon \r\n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* as our real parent calls exit() in the statement above. \r\n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Here's where we'd continue executing, knowing that when \r\n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* we're done, init will reap our status. \r\n &nbsp; &nbsp; &nbsp; &nbsp; *\/ \r\n &nbsp; &nbsp; &nbsp; &nbsp; sleep(2); &nbsp;\r\n &nbsp; &nbsp; &nbsp; &nbsp; printf(\"second child, parent pid = %d \", getppid()); &nbsp;\r\n &nbsp; &nbsp; &nbsp; &nbsp; exit(0); &nbsp;\r\n &nbsp; &nbsp; } &nbsp;\r\n \r\n if (waitpid(pid, NULL, 0) != pid) &nbsp;\/**\/\/* wait for first child *\/ \r\n &nbsp; &nbsp; &nbsp; &nbsp; err_sys(\"waitpid error\"); &nbsp;\r\n \r\n \/**\/\/* \r\n &nbsp; &nbsp; &nbsp;* We're the parent (the original process); we continue executing, \r\n &nbsp; &nbsp; &nbsp;* knowing that we're not the parent of the second child. \r\n &nbsp; &nbsp; *\/ \r\n &nbsp; &nbsp; exit(0); &nbsp;}\r\n<\/pre>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300alinux\u50f5\u5c38\u8fdb\u7a0b\u600e\u4e48\u907f\u514d\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8eLinux\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>linux\u50f5\u5c38\u8fdb\u7a0b\u600e\u4e48\u907f\u514d \u600e\u4e48\u5165\u95e8\u6587\u7ae0\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u7c73\u4e91\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300alinux\u50f5\u5c38\u8fdb\u7a0b\u600e\u4e48\u907f\u514d\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b linux\u50f5\u5c38\u8fdb\u7a0b\u662f\u4e00\u4e2a\u65e9\u5df2\u6b7b\u4ea1\u7684\u8fdb\u7a0b\uff0c\u4f46\u662f\u5728\u8fdb\u7a0b\u8868\u4e2d\u4ecd\u5360\u4e86\u4e00\u4e2a\u4f4d\u7f6e\uff1b\u5982\u679c\u5b50\u8fdb\u7a0b\u6b7b\u4ea1\u65f6\u7236\u8fdb\u7a0b\u6ca1\u6709wait()\uff0c\u901a\u5e38\u7528ps\u53ef\u4ee5\u770b\u5230\u5b83\u88ab\u663e\u793a\u4e3a\u201c \u201d\uff0c\u8fd9\u6837\u5c31\u4ea7\u751f\u4e86\u50f5\u5c38\u8fdb\u7a0b\uff1b\u5982\u679c\u5927\u91cf\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b\uff0c\u90a3\u4e48\u5c06\u56e0\u4e3a\u6ca1\u6709\u53ef\u7528\u7684\u8fdb\u7a0b\u53f7\u800c\u5bfc\u81f4\u7cfb\u7edf\u4e0d\u80fd\u4ea7\u751f\u65b0\u7684\u8fdb\u7a0b\uff0c\u6240\u4ee5\u8981\u907f\u514d\u6709\u50f5\u5c38\u8fdb\u7a0b\u3002 \u4e00\u3001\u4ec0\u4e48\u662f\u50f5\u5c38\u8fdb\u7a0b \u5728UNIX \u7cfb\u7edf\u4e2d\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u7ed3\u675f\u4e86\uff0c\u4f46\u662f\u4ed6\u7684\u7236\u8fdb\u7a0b\u6ca1\u6709\u7b49\u5f85(\u8c03\u7528wait \/ waitpid)\u4ed6\uff0c\u90a3\u4e48\u4ed6\u5c06\u53d8\u6210\u4e00\u4e2a\u50f5\u5c38\u8fdb\u7a0b\u3002\u5f53\u7528ps\u547d\u4ee4\u89c2\u5bdf\u8fdb\u7a0b\u7684\u6267\u884c\u72b6\u6001\u65f6\uff0c\u770b\u5230\u8fd9\u4e9b\u8fdb\u7a0b\u7684\u72b6\u6001\u680f\u4e3adefunct\u3002\u50f5\u5c38\u8fdb\u7a0b\u662f\u4e00\u4e2a\u65e9\u5df2\u6b7b\u4ea1\u7684\u8fdb\u7a0b\uff0c\u4f46\u5728\u8fdb\u7a0b\u8868\uff08processs table\uff09\u4e2d\u4ecd\u5360\u4e86\u4e00\u4e2a\u4f4d\u7f6e\uff08slot\uff09\u3002 \u4f46\u662f\u5982\u679c\u8be5\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b\u5df2\u7ecf\u5148\u7ed3\u675f\u4e86\uff0c\u90a3\u4e48\u8be5\u8fdb\u7a0b\u5c31\u4e0d\u4f1a\u53d8\u6210\u50f5\u5c38\u8fdb\u7a0b\u3002\u56e0\u4e3a\u6bcf\u4e2a\u8fdb\u7a0b\u7ed3\u675f\u7684\u65f6\u5019,\u7cfb\u7edf\u90fd\u4f1a\u626b\u63cf\u5f53\u524d\u7cfb\u7edf\u4e2d\u6240\u8fd0\u884c\u7684\u6240\u6709\u8fdb\u7a0b\uff0c\u770b\u770b\u6709\u6ca1\u6709\u54ea\u4e2a\u8fdb\u7a0b\u662f\u521a\u521a\u7ed3\u675f\u7684\u8fd9\u4e2a\u8fdb\u7a0b\u7684\u5b50\u8fdb\u7a0b\uff0c\u5982\u679c\u662f\u7684\u8bdd\uff0c\u5c31\u7531Init\u8fdb\u7a0b\u6765\u63a5\u7ba1\u4ed6\uff0c\u6210\u4e3a\u4ed6\u7684\u7236\u8fdb\u7a0b\uff0c\u4ece\u800c\u4fdd\u8bc1\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u4f1a\u6709\u4e00\u4e2a\u7236\u8fdb\u7a0b\u3002\u800cInit\u8fdb\u7a0b\u4f1a\u81ea\u52a8wait\u5176\u5b50\u8fdb\u7a0b,\u56e0\u6b64\u88abInit\u63a5\u7ba1\u7684\u6240\u6709\u8fdb\u7a0b\u90fd\u4e0d\u4f1a\u53d8\u6210\u50f5\u5c38\u8fdb\u7a0b\u3002 \u4e8c\u3001UNIX\u4e0b\u8fdb\u7a0b\u7684\u8fd0\u4f5c\u65b9\u5f0f \u6bcf\u4e2aUnix\u8fdb\u7a0b\u5728\u8fdb\u7a0b\u8868\u91cc\u90fd\u6709\u4e00\u4e2a\u8fdb\u5165\u70b9\uff08entry\uff09\uff0c\u6838\u5fc3\u8fdb\u7a0b\u6267\u884c\u8be5\u8fdb\u7a0b\u65f6\u4f7f\u7528\u5230\u7684\u4e00\u5207\u4fe1\u606f\u90fd\u5b58\u50a8\u5728\u8fdb\u5165\u70b9\u3002\u5f53\u7528 ps \u547d\u4ee4\u5bdf\u770b\u7cfb\u7edf\u4e2d\u7684\u8fdb\u7a0b\u4fe1\u606f\u65f6\uff0c\u770b\u5230\u7684\u5c31\u662f\u8fdb\u7a0b\u8868\u4e2d\u7684\u76f8\u5173\u6570\u636e\u3002\u5f53\u4ee5fork()\u7cfb\u7edf\u8c03\u7528\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\u540e\uff0c\u6838\u5fc3\u8fdb\u7a0b\u5c31\u4f1a\u5728\u8fdb\u7a0b\u8868\u4e2d\u7ed9\u8fd9\u4e2a\u65b0\u8fdb\u7a0b\u5206\u914d\u4e00\u4e2a\u8fdb\u5165\u70b9\uff0c\u7136\u540e\u5c06\u76f8\u5173\u4fe1\u606f\u5b58\u50a8\u5728\u8be5\u8fdb\u5165\u70b9\u6240\u5bf9\u5e94\u7684\u8fdb\u7a0b\u8868\u5185\u3002\u8fd9\u4e9b\u4fe1\u606f\u4e2d\u6709\u4e00\u9879\u662f\u5176\u7236\u8fdb\u7a0b\u7684\u8bc6\u522b\u7801\u3002 \u5b50\u8fdb\u7a0b\u7684\u7ed3\u675f\u548c\u7236\u8fdb\u7a0b\u7684\u8fd0\u884c\u662f\u4e00\u4e2a\u5f02\u6b65\u8fc7\u7a0b\uff0c\u5373\u7236\u8fdb\u7a0b\u6c38\u8fdc\u65e0\u6cd5\u9884\u6d4b\u5b50\u8fdb\u7a0b\u5230\u5e95\u4ec0\u4e48\u65f6\u5019\u7ed3\u675f\u3002\u90a3\u4e48\u4f1a\u4e0d\u4f1a\u56e0\u4e3a\u7236\u8fdb\u7a0b\u592a\u5fd9\u6765\u4e0d\u53ca wait \u5b50\u8fdb\u7a0b\uff0c\u6216\u8005\u8bf4\u4e0d\u77e5\u9053\u5b50\u8fdb\u7a0b\u4ec0\u4e48\u65f6\u5019\u7ed3\u675f\uff0c\u800c\u4e22\u5931\u5b50\u8fdb\u7a0b\u7ed3\u675f\u65f6\u7684\u72b6\u6001\u4fe1\u606f\u5462\uff1f \u4e0d\u4f1a\u3002\u56e0\u4e3aUNIX\u63d0\u4f9b\u4e86\u4e00\u79cd\u673a\u5236\u53ef\u4ee5\u4fdd\u8bc1\uff0c\u53ea\u8981\u7236\u8fdb\u7a0b\u60f3\u77e5\u9053\u5b50\u8fdb\u7a0b\u7ed3\u675f\u65f6\u7684\u72b6\u6001\u4fe1\u606f\uff0c\u5c31\u53ef\u4ee5\u5f97\u5230\u3002\u8fd9\u79cd\u673a\u5236\u5c31\u662f\uff1a\u5f53\u5b50\u8fdb\u7a0b\u8d70\u5b8c\u4e86\u81ea\u5df1\u7684\u751f\u547d\u5468\u671f\u540e\uff0c\u5b83\u4f1a\u6267\u884cexit()\u7cfb\u7edf\u8c03\u7528\uff0c\u5185\u6838\u91ca\u653e\u8be5\u8fdb\u7a0b\u6240\u6709\u7684\u8d44\u6e90\uff0c\u5305\u62ec\u6253\u5f00\u7684\u6587\u4ef6\uff0c\u5360\u7528\u7684\u5185\u5b58\u7b49\u3002\u4f46\u662f\u4ecd\u7136\u4e3a\u5176\u4fdd\u7559\u4e00\u5b9a\u7684\u4fe1\u606f\uff08\u5305\u62ec\u8fdb\u7a0b\u53f7the process ID\uff0c\u9000\u51fa\u7801exit code\uff0c\u9000\u51fa\u72b6\u6001the terminationstatus of the process\uff0c\u8fd0\u884c\u65f6\u95f4the amount of CPU time taken by the process\u7b49\uff09\uff0c\u8fd9\u4e9b\u6570\u636e\u4f1a\u4e00\u76f4\u4fdd\u7559\u5230\u7cfb\u7edf\u5c06\u5b83\u4f20\u9012\u7ed9\u5b83\u7684\u7236\u8fdb\u7a0b\u4e3a\u6b62\uff0c\u76f4\u5230\u7236\u8fdb\u7a0b\u901a\u8fc7wait \/ waitpid\u6765\u53d6\u65f6\u624d\u91ca\u653e\u3002 \u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u4e00\u4e2a\u8fdb\u7a0b\u6b7b\u4ea1\u65f6\uff0c\u5b83\u5e76\u4e0d\u662f\u5b8c\u5168\u7684\u6d88\u5931\u4e86\u3002\u8fdb\u7a0b\u7ec8\u6b62\uff0c\u5b83\u4e0d\u518d\u8fd0\u884c\uff0c\u4f46\u662f\u8fd8\u6709\u4e00\u4e9b\u6b8b\u7559\u7684\u6570\u636e\u7b49\u5f85\u7236\u8fdb\u7a0b\u6536\u56de\u3002\u5f53\u7236\u8fdb\u7a0b fork() \u4e00\u4e2a\u5b50\u8fdb\u7a0b\u540e\uff0c\u5b83\u5fc5\u987b\u7528 wait() \uff08\u6216\u8005 waitpid()\uff09\u7b49\u5f85\u5b50\u8fdb\u7a0b\u9000\u51fa\u3002\u6b63\u662f\u8fd9\u4e2a wait() \u52a8\u4f5c\u6765\u8ba9\u5b50\u8fdb\u7a0b\u7684\u6b8b\u7559\u6570\u636e\u6d88\u5931\u3002 \u4e09\u3001\u50f5\u5c38\u8fdb\u7a0b\u7684\u5371\u5bb3 \u5982\u679c\u7236\u8fdb\u7a0b\u4e0d\u8c03\u7528wait \/ waitpid\u7684\u8bdd\uff0c\u90a3\u4e48\u4fdd\u7559\u7684\u90a3\u6bb5\u4fe1\u606f\u5c31\u4e0d\u4f1a\u91ca\u653e\uff0c\u5176\u8fdb\u7a0b\u53f7\u5c31\u4f1a\u4e00\u76f4\u88ab\u5360\u7528\uff0c\u4f46\u662f\u7cfb\u7edf\u7684\u8fdb\u7a0b\u8868\u5bb9\u91cf\u662f\u6709\u9650\u7684\uff0c\u6240\u80fd\u4f7f\u7528\u7684\u8fdb\u7a0b\u53f7\u4e5f\u662f\u6709\u9650\u7684\uff0c\u5982\u679c\u5927\u91cf\u7684\u4ea7\u751f\u50f5\u5c38\u8fdb\u7a0b,\u5c06\u56e0\u4e3a\u6ca1\u6709\u53ef\u7528\u7684\u8fdb\u7a0b\u53f7\u800c\u5bfc\u81f4\u7cfb\u7edf\u4e0d\u80fd\u4ea7\u751f\u65b0\u7684\u8fdb\u7a0b\u3002 \u6240\u4ee5\uff0cdefunct\u8fdb\u7a0b\u4e0d\u4ec5\u5360\u7528\u7cfb\u7edf\u7684\u5185\u5b58\u8d44\u6e90\uff0c\u5f71\u54cd\u7cfb\u7edf\u7684\u6027\u80fd\uff0c\u800c\u4e14\u5982\u679c\u5176\u6570\u76ee\u592a\u591a\uff0c\u8fd8\u4f1a\u5bfc\u81f4\u7cfb\u7edf\u762b\u75ea\u3002\u800c\u4e14\uff0c\u7531\u4e8e\u8c03\u5ea6\u7a0b\u5e8f\u65e0\u6cd5\u9009\u4e2dDefunct \u8fdb\u7a0b\uff0c\u6240\u4ee5\u4e0d\u80fd\u7528kill\u547d\u4ee4\u5220\u9664Defunct \u8fdb\u7a0b\uff0c\u60df\u4e00\u7684\u65b9\u6cd5\u53ea\u6709\u91cd\u542f\u7cfb\u7edf\u3002 \u56db\u3001\u50f5\u5c38\u8fdb\u7a0b\u7684\u4ea7\u751f \u5982\u679c\u5b50\u8fdb\u7a0b\u6b7b\u4ea1\u65f6\u7236\u8fdb\u7a0b\u6ca1\u6709 wait()\uff0c\u901a\u5e38\u7528 ps [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-52046","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52046","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=52046"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52046\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}