{"id":37819,"date":"2025-05-17T17:46:09","date_gmt":"2025-05-17T09:46:09","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/37819.html"},"modified":"2025-05-17T17:46:09","modified_gmt":"2025-05-17T09:46:09","slug":"linux%e4%b9%8b%e8%bf%9b%e7%a8%8b%e6%8e%a7%e5%88%b6","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/37819.html","title":{"rendered":"Linux\u4e4b\u8fdb\u7a0b\u63a7\u5236"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"%E4%B8%80%E3%80%81%E8%BF%9B%E7%A8%8B%E5%88%9B%E5%BB%BA-toc\" style=\"margin-left:0px\">\u4e00\u3001\u8fdb\u7a0b\u521b\u5efa<\/p>\n<p id=\"1.1%E3%80%81fork%E5%87%BD%E6%95%B0%E5%88%9D%E5%A7%8B-toc\" style=\"margin-left:40px\">1.1\u3001fork\u51fd\u6570\u521d\u59cb<\/p>\n<p id=\"1.2%E3%80%81fork%E5%87%BD%E6%95%B0%E8%BF%94%E5%9B%9E%E5%80%BC-toc\" style=\"margin-left:40px\">1.2\u3001fork\u51fd\u6570\u8fd4\u56de\u503c<\/p>\n<p id=\"1.3%E3%80%81%E5%86%99%E5%AE%9E%E6%8B%B7%E8%B4%9D-toc\" style=\"margin-left:40px\">1.3\u3001\u5199\u5b9e\u62f7\u8d1d<\/p>\n<p id=\"1.4%E3%80%81fork%E5%B8%B8%E8%A7%84%E7%94%A8%E6%B3%95-toc\" style=\"margin-left:40px\">1.4\u3001fork\u5e38\u89c4\u7528\u6cd5<\/p>\n<p id=\"1.5%E3%80%81fork%E8%B0%83%E7%94%A8%E5%A4%B1%E8%B4%A5%E7%9A%84%E5%8E%9F%E5%9B%A0-toc\" style=\"margin-left:40px\">1.5\u3001fork\u8c03\u7528\u5931\u8d25\u7684\u539f\u56e0<\/p>\n<p id=\"%E4%BA%8C%E3%80%81%E8%BF%9B%E7%A8%8B%E7%BB%88%E6%AD%A2-toc\" style=\"margin-left:0px\">\u4e8c\u3001\u8fdb\u7a0b\u7ec8\u6b62<\/p>\n<p id=\"2.1%E3%80%81%E8%BF%9B%E7%A8%8B%E9%80%80%E5%87%BA%E5%9C%BA%E6%99%AF-toc\" style=\"margin-left:40px\">2.1\u3001\u8fdb\u7a0b\u9000\u51fa\u573a\u666f<\/p>\n<p id=\"2.2%E3%80%81%E8%BF%9B%E7%A8%8B%E5%B8%B8%E8%A7%81%E9%80%80%E5%87%BA%E6%96%B9%E6%B3%95-toc\" style=\"margin-left:40px\">2.2\u3001\u8fdb\u7a0b\u5e38\u89c1\u9000\u51fa\u65b9\u6cd5<\/p>\n<p id=\"2.2.1%E3%80%81%E9%80%80%E5%87%BA%E7%A0%81-toc\" style=\"margin-left:80px\">2.2.1\u3001\u9000\u51fa\u7801<\/p>\n<p id=\"2.3.2%E3%80%81_exit%E5%87%BD%E6%95%B0-toc\" style=\"margin-left:80px\">2.3.2\u3001_exit\u51fd\u6570<\/p>\n<p id=\"2.3.3%E3%80%81exit%E5%87%BD%E6%95%B0-toc\" style=\"margin-left:80px\">2.3.3\u3001exit\u51fd\u6570<\/p>\n<p id=\"2.3.4%E3%80%81return%E9%80%80%E5%87%BA-toc\" style=\"margin-left:80px\">2.3.4\u3001return\u9000\u51fa<\/p>\n<p id=\"%E4%B8%89%E3%80%81%E8%BF%9B%E7%A8%8B%E7%AD%89%E5%BE%85-toc\" style=\"margin-left:0px\">\u4e09\u3001\u8fdb\u7a0b\u7b49\u5f85<\/p>\n<p id=\"3.1%E3%80%81%E8%BF%9B%E7%A8%8B%E7%AD%89%E5%BE%85%E5%BF%85%E8%A6%81%E6%80%A7-toc\" style=\"margin-left:40px\">3.1\u3001\u8fdb\u7a0b\u7b49\u5f85\u5fc5\u8981\u6027<\/p>\n<p id=\"3.2%E3%80%81%E8%BF%9B%E7%A8%8B%E7%AD%89%E5%BE%85%E7%9A%84%E6%96%B9%E6%B3%95-toc\" style=\"margin-left:40px\">3.2\u3001\u8fdb\u7a0b\u7b49\u5f85\u7684\u65b9\u6cd5<\/p>\n<p id=\"3.2.1%E3%80%81wait%E6%96%B9%E6%B3%95-toc\" style=\"margin-left:80px\">3.2.1\u3001wait\u65b9\u6cd5<\/p>\n<p id=\"3.2.2%E3%80%81waitpid%E6%96%B9%E6%B3%95-toc\" style=\"margin-left:80px\">3.2.2\u3001waitpid\u65b9\u6cd5<\/p>\n<p id=\"3.2.3%E3%80%81%E8%8E%B7%E5%8F%96%E5%AD%90%E8%BF%9B%E7%A8%8Bstatus-toc\" style=\"margin-left:80px\">3.2.3\u3001\u83b7\u53d6\u5b50\u8fdb\u7a0bstatus<\/p>\n<p id=\"3.2.4%E3%80%81%E9%98%BB%E5%A1%9E%E4%B8%8E%E9%9D%9E%E9%98%BB%E5%A1%9E%E7%AD%89%E5%BE%85-toc\" style=\"margin-left:80px\">3.2.4\u3001\u963b\u585e\u4e0e\u975e\u963b\u585e\u7b49\u5f85<\/p>\n<p id=\"3.2.5%E3%80%81%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E9%80%9A%E8%BF%87%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8%E6%9D%A5%E8%8E%B7%E5%8F%96%E8%BF%9B%E7%A8%8B%E9%80%80%E5%87%BA%E7%A0%81-toc\" style=\"margin-left:80px\">3.2.5\u3001\u4e3a\u4ec0\u4e48\u8981\u901a\u8fc7\u7cfb\u7edf\u8c03\u7528\u6765\u83b7\u53d6\u8fdb\u7a0b\u9000\u51fa\u7801<\/p>\n<p id=\"%E5%9B%9B%E3%80%81%E8%BF%9B%E7%A8%8B%E7%A8%8B%E5%BA%8F%E6%9B%BF%E6%8D%A2-toc\" style=\"margin-left:0px\">\u56db\u3001\u8fdb\u7a0b\u7a0b\u5e8f\u66ff\u6362<\/p>\n<p id=\"4.1%E3%80%81%E6%9B%BF%E6%8D%A2%E5%8E%9F%E7%90%86-toc\" style=\"margin-left:40px\">4.1\u3001\u66ff\u6362\u539f\u7406<\/p>\n<p id=\"4.2%E3%80%81%E6%9B%BF%E6%8D%A2%E5%87%BD%E6%95%B0-toc\" style=\"margin-left:40px\">4.2\u3001\u66ff\u6362\u51fd\u6570<\/p>\n<p id=\"4.2.1%E3%80%81%E5%87%BD%E6%95%B0%E8%A7%A3%E9%87%8A-toc\" style=\"margin-left:80px\">4.2.1\u3001\u51fd\u6570\u89e3\u91ca<\/p>\n<p id=\"4.2.2%E3%80%81%E5%91%BD%E5%90%8D%E7%90%86%E8%A7%A3-toc\" style=\"margin-left:80px\">4.2.2\u3001\u547d\u540d\u7406\u89e3<\/p>\n<p id=\"%E4%BA%94%E3%80%81%E8%87%AA%E4%B8%BBShell%E5%91%BD%E4%BB%A4%E8%A1%8C%E8%A7%A3%E9%87%8A%E5%99%A8-toc\" style=\"margin-left:0px\">\u4e94\u3001\u81ea\u4e3bShell\u547d\u4ee4\u884c\u89e3\u91ca\u5668<\/p>\n<p id=\"5.1%E3%80%81%E7%9B%AE%E6%A0%87-toc\" style=\"margin-left:40px\">5.1\u3001\u76ee\u6807<\/p>\n<p id=\"5.2%E3%80%81%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86-toc\" style=\"margin-left:40px\">5.2\u3001\u5b9e\u73b0\u539f\u7406<\/p>\n<p id=\"5.3%E3%80%81%E6%BA%90%E7%A0%81-toc\" style=\"margin-left:40px\">5.3\u3001\u6e90\u7801<\/p>\n<p id=\"5.4%E3%80%81%E6%80%BB%E7%BB%93-toc\" style=\"margin-left:40px\">5.4\u3001\u603b\u7ed3<\/p>\n<hr id=\"hr-toc\" \/>\n<h2 id=\"%E4%B8%80%E3%80%81%E8%BF%9B%E7%A8%8B%E5%88%9B%E5%BB%BA\" style=\"background-color:transparent\">\u4e00\u3001\u8fdb\u7a0b\u521b\u5efa<\/h2>\n<h3 id=\"1.1%E3%80%81fork%E5%87%BD%E6%95%B0%E5%88%9D%E5%A7%8B\">1.1\u3001fork\u51fd\u6570\u521d\u59cb<\/h3>\n<p>\u5728linux\u4e2dfork\u51fd\u6570\u662f\u2fae\u5e38\u91cd\u8981\u7684\u51fd\u6570&#xff0c;\u5b83\u4ece\u5df2\u5b58\u5728\u8fdb\u7a0b\u4e2d\u521b\u5efa\u2f00\u4e2a\u65b0\u8fdb\u7a0b\u3002\u65b0\u8fdb\u7a0b\u4e3a\u2f26\u8fdb\u7a0b&#xff0c;\u2f7d\u539f\u8fdb\u7a0b\u4e3a\u2f57\u8fdb\u7a0b\u3002<\/p>\n<p><span style=\"color:#6eaad7\">#<\/span><span style=\"color:#956fe7\">include<\/span><span style=\"color:#1c7331\">&lt;unistd.h&gt;<\/span><\/p>\n<p><span style=\"color:#ad720d\">pid_t\u00a0<\/span> <span style=\"color:#6eaad7\">fork<\/span>(<span style=\"color:#ad720d\">void<\/span>);<\/p>\n<p>\u8fd4\u56de\u503c&#xff1a;\u5b50\u8fdb\u7a0b\u4e2d\u8fd4\u56de0&#xff0c;\u2f57\u8fdb\u7a0b\u8fd4\u56de\u2f26\u8fdb\u7a0bid&#xff0c;\u51fa\u9519\u8fd4\u56de-1<\/p>\n<p>\u8fdb\u7a0b\u8c03\u2f64fork&#xff0c;\u5f53\u63a7\u5236\u8f6c\u79fb\u5230\u5185\u6838\u4e2d\u7684fork\u4ee3\u7801\u540e&#xff0c;\u5185\u6838\u505a&#xff1a;<\/p>\n<ul>\n<li>\u5206\u914d\u65b0\u7684\u5185\u5b58\u5757\u548c\u5185\u6838\u6570\u636e\u7ed3\u6784\u7ed9\u2f26\u8fdb\u7a0b<\/li>\n<li>\u5c06\u2f57\u8fdb\u7a0b\u90e8\u5206\u6570\u636e\u7ed3\u6784\u5185\u5bb9\u62f7\u2ec9\u2f84\u2f26\u8fdb\u7a0b<\/li>\n<li>\u6dfb\u52a0\u2f26\u8fdb\u7a0b\u5230\u7cfb\u7edf\u8fdb\u7a0b\u5217\u8868\u5f53\u4e2d<\/li>\n<li>fork\u8fd4\u56de&#xff0c;\u5f00\u59cb\u8c03\u5ea6\u5668\u8c03\u5ea6<\/li>\n<\/ul>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"481\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517094605-68285add2631c.png\" width=\"1120\" \/><\/p>\n<p>\u5f53\u2f00\u4e2a\u8fdb\u7a0b\u8c03\u2f64fork\u4e4b\u540e&#xff0c;\u5c31\u6709\u4e24\u4e2a\u2f06\u8fdb\u5236\u4ee3\u7801\u76f8\u540c\u7684\u8fdb\u7a0b\u3002\u2f7d\u4e14\u5b83\u4eec\u90fd\u8fd0\u2f8f\u5230\u76f8\u540c\u7684\u5730\u2f45\u3002\u4f46\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u5c06\u53ef\u4ee5\u5f00\u59cb\u5b83\u4eec\u2f83\u2f30\u7684\u65c5\u7a0b&#xff0c;\u5982\u4e0b\u56fe&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"419\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517094605-68285add6115c.png\" width=\"1143\" \/><\/p>\n<p>\u6240\u4ee5&#xff0c;fork\u4e4b\u524d\u2f57\u8fdb\u7a0b\u72ec\u2f74\u6267\u2f8f&#xff0c;fork\u4e4b\u540e&#xff0c;\u2f57\u2f26\u4e24\u4e2a\u6267\u2f8f\u6d41\u5206\u522b\u6267\u2f8f\u3002\u6ce8\u610f&#xff0c;fork\u4e4b\u540e&#xff0c;\u8c01\u5148\u6267\u2f8f\u5b8c\u5168\u7531\u8c03\u5ea6\u5668\u51b3\u5b9a\u3002<\/p>\n<h3 id=\"1.2%E3%80%81fork%E5%87%BD%E6%95%B0%E8%BF%94%E5%9B%9E%E5%80%BC\">1.2\u3001fork\u51fd\u6570\u8fd4\u56de\u503c<\/h3>\n<ul>\n<li>\u2f26\u8fdb\u7a0b\u8fd4\u56de0\u3002<\/li>\n<li>\u2f57\u8fdb\u7a0b\u8fd4\u56de\u7684\u662f\u2f26\u8fdb\u7a0b\u7684pid\u3002<\/li>\n<\/ul>\n<h3 id=\"1.3%E3%80%81%E5%86%99%E5%AE%9E%E6%8B%B7%E8%B4%9D\">1.3\u3001\u5199\u5b9e\u62f7\u8d1d<\/h3>\n<p>\u901a\u5e38&#xff0c;\u2f57\u2f26\u4ee3\u7801\u5171\u4eab&#xff0c;\u2f57\u2f26\u5728\u4e0d\u5199\u2f0a\u65f6&#xff0c;\u6570\u636e\u4e5f\u662f\u5171\u4eab\u7684&#xff0c;\u5f53\u4efb\u610f\u2f00\u2f45\u8bd5\u56fe\u5199\u2f0a&#xff0c;\u4fbf\u4ee5\u5199\u65f6\u62f7\u2ec9\u7684\u2f45\u5f0f\u5404\u2f83\u2f00\u4efd\u526f\u672c\u3002\u5177\u4f53\u2ec5\u4e0b\u56fe:<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"435\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517094605-68285add9480e.png\" width=\"1166\" \/><\/p>\n<p>\u8be6\u7ec6\u89e3\u91ca&#xff1a;\u7236\u8fdb\u7a0b\u521b\u5efa\u5b50\u8fdb\u7a0b\u540e&#xff0c;\u66f4\u65b0\u9875\u8868\u4e2d\u8868\u793a\u6570\u636e\u8bbf\u95ee\u6743\u9650\u7684\u6807\u5fd7\u4f4d\u4e3a\u53ea\u8bfb\u5c5e\u6027&#xff0c;\u5f53\u7236\u5b50\u8fdb\u7a0b\u4e2d\u7684\u67d0\u4e00\u65b9\u5199\u5165\u6570\u636e\u65f6&#xff0c;\u89e6\u53d1\u7cfb\u7edf\u9519\u8bef&#xff0c;\u8fdb\u800c\u5f15\u53d1\u7f3a\u9875\u4e2d\u65ad&#xff0c;\u7136\u540e\u7cfb\u7edf\u5c31\u4f1a\u8fdb\u884c\u4e00\u7cfb\u5217\u8f83\u4e3a\u590d\u6742\u7684\u68c0\u6d4b&#xff0c;\u5f53\u5224\u5b9a\u9700\u8981\u53d1\u751f\u5199\u5b9e\u62f7\u8d1d\u540e&#xff0c;\u5c31\u4f1a\u5f00\u59cb\u7533\u8bf7\u5185\u5b58&#xff0c;\u53d1\u751f\u62f7\u8d1d&#xff0c;\u4fee\u6539\u7236\u5b50\u9875\u8868\u5bf9\u5e94\u6570\u636e\u7684\u6743\u9650\u4e3a\u53ef\u8bfb\u53ef\u5199&#xff0c;\u7136\u540e\u6062\u590d\u6267\u884c\u3002<\/p>\n<p>\u610f\u4e49&#xff1a;\u56e0\u4e3a\u6709\u5199\u65f6\u62f7\u2ec9\u6280\u672f\u7684\u5b58\u5728,\u6240\u4ee5\u2f57\u2f26\u8fdb\u7a0b\u5f97\u4ee5\u5f7b\u5e95\u5206\u79bb\u79bb&#xff01;\u5b8c\u6210\u4e86\u8fdb\u7a0b\u72ec\u2f74\u6027\u7684\u6280\u672f\u4fdd\u8bc1!<\/p>\n<p>\u5199\u65f6\u62f7\u2ec9&#xff0c;\u662f\u2f00\u79cd\u5ef6\u65f6\u7533\u8bf7\u6280\u672f&#xff0c;\u53ef\u4ee5\u63d0\u2fbc\u6574\u673a\u5185\u5b58\u7684\u4f7f\u2f64\u7387\u3002<\/p>\n<h3 id=\"1.4%E3%80%81fork%E5%B8%B8%E8%A7%84%E7%94%A8%E6%B3%95\">1.4\u3001fork\u5e38\u89c4\u7528\u6cd5<\/h3>\n<ul>\n<li>\u2f00\u4e2a\u2f57\u8fdb\u7a0b\u5e0c\u671b\u590d\u5236\u2f83\u2f30&#xff0c;\u4f7f\u2f57\u2f26\u8fdb\u7a0b\u540c\u65f6\u6267\u2f8f\u4e0d\u540c\u7684\u4ee3\u7801\u6bb5\u3002\u4f8b\u5982&#xff0c;\u2f57\u8fdb\u7a0b\u7b49\u5f85\u5ba2\u6237\u7aef\u8bf7\u6c42&#xff0c; \u2f63\u6210\u2f26\u8fdb\u7a0b\u6765\u5904\u7406\u8bf7\u6c42\u3002<\/li>\n<li>\u2f00\u4e2a\u8fdb\u7a0b\u8981\u6267\u2f8f\u2f00\u4e2a\u4e0d\u540c\u7684\u7a0b\u5e8f\u3002\u4f8b\u5982\u2f26\u8fdb\u7a0b\u4ecefork\u8fd4\u56de\u540e&#xff0c;\u8c03\u2f64exec\u51fd\u6570\u3002<\/li>\n<\/ul>\n<h3 id=\"1.5%E3%80%81fork%E8%B0%83%E7%94%A8%E5%A4%B1%E8%B4%A5%E7%9A%84%E5%8E%9F%E5%9B%A0\">1.5\u3001fork\u8c03\u7528\u5931\u8d25\u7684\u539f\u56e0<\/h3>\n<ul>\n<li>\u7cfb\u7edf\u4e2d\u6709\u592a\u591a\u7684\u8fdb\u7a0b<\/li>\n<li>\u5b9e\u9645\u7528\u6237\u7684\u8fdb\u7a0b\u6570\u8d85\u8fc7\u4e86\u9650\u5236<\/li>\n<\/ul>\n<h2 id=\"%E4%BA%8C%E3%80%81%E8%BF%9B%E7%A8%8B%E7%BB%88%E6%AD%A2\">\u4e8c\u3001\u8fdb\u7a0b\u7ec8\u6b62<\/h2>\n<p>\u8fdb\u7a0b\u7ec8\u2f4c\u7684\u672c\u8d28\u662f\u91ca\u653e\u7cfb\u7edf\u8d44\u6e90&#xff0c;\u5c31\u662f\u91ca\u653e\u8fdb\u7a0b\u7533\u8bf7\u7684\u76f8\u5173\u5185\u6838\u6570\u636e\u7ed3\u6784\u548c\u5bf9\u5e94\u7684\u6570\u636e\u548c\u4ee3\u7801\u3002<\/p>\n<h3 id=\"2.1%E3%80%81%E8%BF%9B%E7%A8%8B%E9%80%80%E5%87%BA%E5%9C%BA%E6%99%AF\">2.1\u3001\u8fdb\u7a0b\u9000\u51fa\u573a\u666f<\/h3>\n<ul>\n<li>\u4ee3\u7801\u8fd0\u2f8f\u5b8c\u6bd5&#xff0c;\u7ed3\u679c\u6b63\u786e<\/li>\n<li>\u4ee3\u7801\u8fd0\u2f8f\u5b8c\u6bd5&#xff0c;\u7ed3\u679c\u4e0d\u6b63\u786e<\/li>\n<li>\u4ee3\u7801\u5f02\u5e38\u7ec8\u2f4c\n<ul>\n<li>\u5f53\u4ee3\u7801\u662f\u5f02\u5e38\u7ec8\u6b62\u65f6&#xff0c;\u5176\u5b9e\u662fOS\u63d0\u524d\u53d1\u73b0\u7684\u4ee3\u7801\u7684\u95ee\u9898&#xff0c;\u7136\u540e\u53d1\u9001\u5bf9\u5e94\u7684\u4fe1\u53f7\u7ec8\u6b62\u4e86\u8be5\u8fdb\u7a0b\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u6240\u6709\u4fe1\u53f7&#xff1a;<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"441\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517094605-68285addcb85f.png\" width=\"1243\" \/><\/p>\n<h3 id=\"2.2%E3%80%81%E8%BF%9B%E7%A8%8B%E5%B8%B8%E8%A7%81%E9%80%80%E5%87%BA%E6%96%B9%E6%B3%95\">2.2\u3001\u8fdb\u7a0b\u5e38\u89c1\u9000\u51fa\u65b9\u6cd5<\/h3>\n<p>\u6b63\u5e38\u7ec8\u2f4c&#xff08;\u53ef\u4ee5\u901a\u8fc7 echo $? \u67e5\u770b\u8fdb\u7a0b\u9000\u51fa\u7801&#xff09;&#xff1a;<\/p>\n<ul>\n<li>\u4ecemain\u8fd4\u56de<\/li>\n<li>\u8c03\u2f64exit<\/li>\n<li>_exit<\/li>\n<\/ul>\n<p>\u5f02\u5e38\u9000\u51fa&#xff1a;<\/p>\n<ul>\n<li>ctrl &#043; c&#xff0c;\u4fe1\u53f7\u7ec8\u2f4c<\/li>\n<\/ul>\n<h4 id=\"2.2.1%E3%80%81%E9%80%80%E5%87%BA%E7%A0%81\">2.2.1\u3001\u9000\u51fa\u7801<\/h4>\n<p>\u9000\u51fa\u7801&#xff08;\u9000\u51fa\u72b6\u6001&#xff09;\u53ef\u4ee5\u544a\u8bc9\u6211\u4eec\u6700\u540e\u2f00\u6b21\u6267\u2f8f\u7684\u547d\u4ee4\u7684\u72b6\u6001\u3002\u5728\u547d\u4ee4\u7ed3\u675f\u4ee5\u540e&#xff0c;\u6211\u4eec\u53ef\u4ee5\u77e5\u9053\u547d\u4ee4\u662f\u6210\u529f\u5b8c\u6210\u7684\u8fd8\u662f\u4ee5\u9519\u8bef\u7ed3\u675f\u7684\u3002\u5176\u57fa\u672c\u601d\u60f3\u662f&#xff0c;\u7a0b\u5e8f\u8fd4\u56de\u9000\u51fa\u4ee3\u7801 0 \u65f6\u8868\u2f70\u6267\u2f8f\u6210\u529f&#xff0c;\u6ca1\u6709\u95ee\u9898\u3002 \u4ee3\u7801 1 \u6216 0 \u4ee5\u5916\u7684\u4efb\u4f55\u4ee3\u7801\u90fd\u88ab\u89c6\u4e3a\u4e0d\u6210\u529f\u3002\u901a\u8fc7\u9519\u8bef\u7801&#xff0c;\u6211\u4eec\u53ef\u4ee5\u8ba9\u7236\u8fdb\u7a0b\u6216\u7cfb\u7edf\u66f4\u597d\u7684\u4e86\u89e3\u5f53\u524d\u8fdb\u7a0b\u662f\u6210\u529f\u4e86\u8fd8\u662f\u5931\u8d25\u4e86&#xff0c;\u5982\u679c\u5931\u8d25\u4e86\u662f\u56e0\u4e3a\u4ec0\u4e48\u5931\u8d25\u7684\u3002<\/p>\n<p>Linux Shell \u4e2d\u7684\u4e3b\u8981\u9000\u51fa\u7801&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"505\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517094606-68285ade0d30d.png\" width=\"1045\" \/><\/p>\n<ul>\n<li>\u9000\u51fa\u7801 0 \u8868\u2f70\u547d\u4ee4\u6267\u2f8f\u2f46\u8bef&#xff0c;\u8fd9\u662f\u5b8c\u6210\u547d\u4ee4\u7684\u7406\u60f3\u72b6\u6001\u3002<\/li>\n<li>\u9000\u51fa\u7801 1 \u6211\u4eec\u4e5f\u53ef\u4ee5\u5c06\u5176\u89e3\u91ca\u4e3a \u201c\u4e0d\u88ab\u5141\u8bb8\u7684\u64cd\u4f5c\u201d\u3002\u4f8b\u5982\u5728\u6ca1\u6709 sudo \u6743\u9650\u7684\u60c5\u51b5\u4e0b\u4f7f\u2f64 yum&#xff1b;\u518d\u4f8b\u5982\u9664\u4ee5 0 \u7b49\u64cd\u4f5c\u4e5f\u4f1a\u8fd4\u56de\u9519\u8bef\u7801 1 &#xff0c;\u5bf9\u5e94\u7684\u547d\u4ee4\u4e3a let a&#061;1\/0 \u3002<\/li>\n<li>130 &#xff08; SIGINT \u6216 Ctrl\u00a0C &#xff09;\u548c 143 &#xff08; SIGTERM &#xff09;\u7b49\u7ec8\u2f4c\u4fe1\u53f7\u662f\u2fae\u5e38\u5178\u578b\u7684&#xff0c;\u5b83\u4eec\u5c5e\u4e8e 128&#043;n \u4fe1\u53f7&#xff0c;\u5176\u4e2d n \u4ee3\u8868\u7ec8\u2f4c\u7801\u3002<\/li>\n<li>\u53ef\u4ee5\u4f7f\u2f64strerror\u51fd\u6570\u6765\u83b7\u53d6\u9000\u51fa\u7801\u5bf9\u5e94\u7684\u63cf\u8ff0\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b\u4ee3\u7801&#xff1a;<\/p>\n<p>  1 #include&lt;iostream&gt;<br \/>\n  2 #include&lt;string&gt;<br \/>\n  3 #include&lt;cstdio&gt;<br \/>\n  4 #include&lt;string.h&gt;<br \/>\n  5 #include&lt;error.h&gt;<br \/>\n  6<br \/>\n  7 int main()<br \/>\n  8 {<br \/>\n  9   printf(&#034;before: errno: %d, errstring: %s\\\\n&#034;,errno,strerror(errno));<br \/>\n 10<br \/>\n 11   FILE *fp &#061; fopen(&#034;log.txt&#034;,&#034;r&#034;);<br \/>\n 12   if(fp &#061;&#061; nullptr)<br \/>\n 13   {<br \/>\n 14     printf(&#034;after: errno: %d, errstring: %s\\\\n&#034;,errno,strerror(errno));<br \/>\n 15     return errno;<br \/>\n 16   }<br \/>\n 17<br \/>\n 18   return 0;<br \/>\n 19 } <\/p>\n<p>\u6548\u679c&#xff1a;<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"150\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517094606-68285ade47e1a.png\" width=\"942\" \/><\/p>\n<p>\u89e3\u91ca&#xff1a;C\u8bed\u8a00\u4e2d\u63d0\u4f9b\u4e86\u53ef\u4ee5\u67e5\u770b\u5f53\u524d\u9519\u8bef\u7801\u7684\u5168\u5c40\u53d8\u91cferrno&#xff0c;\u53ea\u8981\u5305\u542berrno.h\u5934\u6587\u4ef6\u5c31\u53ef\u4ee5\u4f7f\u7528&#xff0c;\u8fd8\u63d0\u4f9b\u4e86\u5c06\u9519\u8bef\u7801\u8f6c\u6362\u4e3a\u5bf9\u5e94\u9519\u8bef\u4fe1\u606f\u7684strerror\u51fd\u6570\u3002<\/p>\n<h4 id=\"2.3.2%E3%80%81_exit%E5%87%BD%E6%95%B0\">2.3.2\u3001_exit\u51fd\u6570<\/h4>\n<p><span style=\"color:#6eaad7\">#<\/span><span style=\"color:#956fe7\">include<\/span><span style=\"color:#1c7331\">&lt;unistd.h&gt;<\/span><\/p>\n<p><span style=\"color:#ad720d\">void <\/span>_exit(<span style=\"color:#ad720d\">int<\/span> status);<\/p>\n<p>\u53c2\u6570&#xff1a;status \u5b9a\u4e49\u4e86\u8fdb\u7a0b\u7684\u7ec8\u2f4c\u72b6\u6001&#xff0c;\u2f57\u8fdb\u7a0b\u901a\u8fc7wait\u6765\u83b7\u53d6\u8be5\u503c<\/p>\n<p>\u8bf4\u660e&#xff1a;\u867d\u7136status\u662fint&#xff0c;\u4f46\u662f\u4ec5\u6709\u4f4e8\u4f4d\u53ef\u4ee5\u88ab\u2f57\u8fdb\u7a0b\u6240\u2f64\u3002\u6240\u4ee5_exit(-1)\u65f6&#xff0c;\u5728\u7ec8\u7aef\u6267\u2f8f$?\u53d1\u73b0 \u8fd4\u56de\u503c\u662f255\u3002<\/p>\n<p>\u793a\u4f8b\u4ee3\u7801\u4e00&#xff1a;<\/p>\n<p>  1 #include&lt;iostream&gt;<br \/>\n  2 #include&lt;string&gt;<br \/>\n  3 #include&lt;cstdio&gt;<br \/>\n  4 #include&lt;string.h&gt;<br \/>\n  5 #include&lt;unistd.h&gt;<br \/>\n  6<br \/>\n  7 void func()<br \/>\n  8 {<br \/>\n  9   std::cout&lt;&lt;&#034;hello,bit&#034;&lt;&lt;std::endl;<br \/>\n 10<br \/>\n 11   _exit(10);<br \/>\n 12 }<br \/>\n 13<br \/>\n 14 int main()<br \/>\n 15 {<br \/>\n 16   func();<br \/>\n 17   std::cout&lt;&lt;&#034;\u8fdb\u7a0b\u7ed3\u675f\u4e86&#034;&lt;&lt;std::endl;<br \/>\n 18<br \/>\n 19   return 0;<br \/>\n 20 }     <\/p>\n<p>\u6548\u679c&#xff1a;<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"154\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517094606-68285ade5a2bb.png\" width=\"824\" \/><\/p>\n<p>\u89e3\u91ca&#xff1a;\u53ef\u4ee5\u770b\u5230 &#034;\u8fdb\u7a0b\u7ed3\u675f\u4e86&#034;\u8fd9\u53e5\u8bdd\u5e76\u6ca1\u6709\u6253\u5370&#xff0c;\u6240\u4ee5\u5f97\u5230\u7ed3\u8bba\u00a0_exit \u65b9\u6cd5\u7684\u4f5c\u7528\u662f\u7ed3\u675f\u6574\u4e2a\u8fdb\u7a0b\u3002<\/p>\n<p>\u793a\u4f8b\u4ee3\u7801\u4e8c&#xff1a;<\/p>\n<p>#include&lt;stdio.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include&lt;stdlib.h&gt;<\/p>\n<p>int main()<br \/>\n{<br \/>\n    printf(&#034;\u8fdb\u7a0b\u8fd0\u884c\u7ed3\u675f!\\\\n&#034;);<br \/>\n    sleep(2);<br \/>\n    _exit(20);<\/p>\n<p>    return 0;<br \/>\n} <\/p>\n<p>\u6548\u679c&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"128\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517094606-68285ade6d7bd.png\" width=\"804\" \/><\/p>\n<p>\u89e3\u91ca&#xff1a;\u4ece\u4e0a\u9762\u4ee3\u7801\u53ef\u4ee5\u5f97\u5230\u7ed3\u8bba&#xff0c;_exit \u7ed3\u675f\u8fdb\u7a0b\u65f6&#xff0c;\u5982\u679c\u7f13\u51b2\u533a\u5185\u6709\u6570\u636e&#xff0c;_exit \u4e0d\u4f1a\u5237\u65b0\u3002<\/p>\n<h4 id=\"2.3.3%E3%80%81exit%E5%87%BD%E6%95%B0\">2.3.3\u3001exit\u51fd\u6570<\/h4>\n<p><span style=\"color:#6eaad7\">#<\/span><span style=\"color:#956fe7\">include<\/span><span style=\"color:#1c7331\">&lt;unistd.h&gt;<\/span><\/p>\n<p><span style=\"color:#ad720d\">void\u00a0\u00a0<\/span>exit(<span style=\"color:#ad720d\">int<\/span> status);<\/p>\n<p>exit\u6700\u540e\u4e5f\u4f1a\u8c03\u2f64_exit&#xff0c;\u4f46\u5728\u8c03\u2f64_exit\u4e4b\u524d&#xff0c;\u8fd8\u505a\u4e86\u5176\u4ed6\u2f2f\u4f5c&#xff1a;<\/p>\n<li>\u6267\u2f8f\u7528\u6237\u901a\u8fc7 atexit \u6216 on_exit \u5b9a\u4e49\u7684\u6e05\u7406\u51fd\u6570\u3002<\/li>\n<li>\u5173\u95ed\u6240\u6709\u6253\u5f00\u7684\u6d41&#xff0c;\u6240\u6709\u7684\u7f13\u5b58\u6570\u636e\u5747\u88ab\u5199\u2f0a\u3002<\/li>\n<li>\u8c03\u2f64_exit\u3002<\/li>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"475\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517094606-68285ade7e82f.png\" width=\"1074\" \/><\/p>\n<p>\u793a\u4f8b\u4ee3\u7801\u4e00&#xff1a;<\/p>\n<p>  1 #include&lt;iostream&gt;<br \/>\n  2 #include&lt;string&gt;<br \/>\n  3 #include&lt;cstdio&gt;<br \/>\n  4 #include&lt;string.h&gt;<br \/>\n  5 #include&lt;unistd.h&gt;<br \/>\n  6<br \/>\n  7 void func()<br \/>\n  8 {<br \/>\n  9   std::cout&lt;&lt;&#034;hello,bit&#034;&lt;&lt;std::endl;<br \/>\n 10<br \/>\n 11   exit(10);<br \/>\n 12 }<br \/>\n 13<br \/>\n 14 int main()<br \/>\n 15 {<br \/>\n 16   func();<br \/>\n 17   std::cout&lt;&lt;&#034;\u8fdb\u7a0b\u7ed3\u675f\u4e86&#034;&lt;&lt;std::endl;<br \/>\n 18<br \/>\n 19   return 0;<br \/>\n 20 }     <\/p>\n<p>\u6548\u679c&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"140\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517094606-68285adeb5d8f.png\" width=\"840\" \/><\/p>\n<p>\u89e3\u91ca&#xff1a;\u53ef\u4ee5\u770b\u5230 &#034;\u8fdb\u7a0b\u7ed3\u675f\u4e86&#034;\u8fd9\u53e5\u8bdd\u5e76\u6ca1\u6709\u6253\u5370&#xff0c;\u6240\u4ee5\u5f97\u5230\u7ed3\u8bba\u00a0exit \u65b9\u6cd5\u7684\u4f5c\u7528\u662f\u7ed3\u675f\u6574\u4e2a\u8fdb\u7a0b\u3002<\/p>\n<p>\u793a\u4f8b\u4ee3\u7801\u4e8c&#xff1a;<\/p>\n<p>#include&lt;stdio.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include&lt;stdlib.h&gt;<\/p>\n<p>int main()<br \/>\n{<br \/>\n    printf(&#034;\u8fdb\u7a0b\u8fd0\u884c\u7ed3\u675f!\\\\n&#034;);<br \/>\n    sleep(2);<br \/>\n    exit(20);<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p>\u6548\u679c&#xff1a;<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"127\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517094606-68285adec7281.png\" width=\"841\" \/><\/p>\n<p>\u89e3\u91ca&#xff1a;\u6211\u4eec\u53ef\u4ee5\u770b\u5230 &#034;\u8fdb\u7a0b\u7ed3\u675f\u4e86&#034;\u8fd9\u53e5\u8bdd\u6253\u5370\u51fa\u6765\u4e86&#xff0c;\u8fd9\u91cc\u4ee3\u7801\u6211\u4eec\u6ca1\u6709\u901a\u8fc7 &#039;\\\\n&#039; \u5f3a\u5236\u5237\u65b0\u7f13\u51b2\u533a&#xff0c;\u6240\u4ee5\u8bf4\u660e exit \u65b9\u6cd5\u7ed3\u675f\u8fdb\u7a0b\u524d\u4f1a\u4e3b\u52a8\u5e2e\u6211\u4eec\u5237\u65b0\u7f13\u51b2\u533a\u3002<\/p>\n<h4 id=\"2.3.4%E3%80%81return%E9%80%80%E5%87%BA\">2.3.4\u3001return\u9000\u51fa<\/h4>\n<p>\u793a\u4f8b\u4ee3\u7801&#xff1a;<\/p>\n<p>  1 #include&lt;iostream&gt;<br \/>\n  2 #include&lt;string&gt;<br \/>\n  3 #include&lt;cstdio&gt;<br \/>\n  4 #include&lt;string.h&gt;<br \/>\n  5 #include&lt;unistd.h&gt;<br \/>\n  6<br \/>\n  7 int func()<br \/>\n  8 {<br \/>\n  9   std::cout&lt;&lt;&#034;hello,bit&#034;&lt;&lt;std::endl;<br \/>\n 10<br \/>\n 11   return 10;<br \/>\n 12 }<br \/>\n 13<br \/>\n 14 int main()<br \/>\n 15 {<br \/>\n 16   func();<br \/>\n 17   std::cout&lt;&lt;&#034;\u8fdb\u7a0b\u7ed3\u675f\u4e86&#034;&lt;&lt;std::endl;<br \/>\n 18<br \/>\n 19   return 0;<br \/>\n 20 }     <\/p>\n<p>\u6548\u679c&#xff1a;<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"181\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517094606-68285aded7ed6.png\" width=\"866\" \/><\/p>\n<p>\u89e3\u91ca&#xff1a;return\u662f\u2f00\u79cd\u66f4\u5e38\u2ec5\u7684\u9000\u51fa\u8fdb\u7a0b\u2f45\u6cd5\u3002\u6267\u2f8freturn num\u7b49\u540c\u4e8e\u6267\u2f8fexit(n)&#xff0c;\u56e0\u4e3a\u8c03\u2f64main\u7684\u8fd0\u2f8f\u65f6\u51fd\u6570\u4f1a\u5c06main\u7684\u8fd4\u56de\u503c\u5f53\u505a exit \u7684\u53c2\u6570\u3002\u53e6\u5916&#xff0c;\u53ea\u6709main\u51fd\u6570\u6267\u884c return \u8bed\u53e5\u624d\u4f1a\u7ed3\u675f\u8fdb\u7a0b&#xff0c;\u5176\u4ed6\u51fd\u6570\u6267\u884c return \u8bed\u53e5\u53ea\u662f\u7ed3\u675f\u5f53\u524d\u51fd\u6570\u800c\u5df2\u3002<\/p>\n<h2 id=\"%E4%B8%89%E3%80%81%E8%BF%9B%E7%A8%8B%E7%AD%89%E5%BE%85\">\u4e09\u3001\u8fdb\u7a0b\u7b49\u5f85<\/h2>\n<h3 id=\"3.1%E3%80%81%E8%BF%9B%E7%A8%8B%E7%AD%89%E5%BE%85%E5%BF%85%E8%A6%81%E6%80%A7\">3.1\u3001\u8fdb\u7a0b\u7b49\u5f85\u5fc5\u8981\u6027<\/h3>\n<ul>\n<li>\u2f26\u8fdb\u7a0b\u9000\u51fa&#xff0c;\u2f57\u8fdb\u7a0b\u5982\u679c\u4e0d\u7ba1\u4e0d\u987e&#xff0c;\u5c31\u53ef\u80fd\u9020\u6210\u2018\u50f5\u2f2b\u8fdb\u7a0b\u2019\u7684\u95ee\u9898&#xff0c;\u8fdb\u2f7d\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\u3002<\/li>\n<li>\u53e6\u5916&#xff0c;\u8fdb\u7a0b\u2f00\u65e6\u53d8\u6210\u50f5\u2f2b\u72b6\u6001&#xff0c;\u90a3\u5c31\u2f11\u67aa\u4e0d\u2f0a&#xff0c;\u201c\u6740\u2f08\u4e0d\u7728\u773c\u201d\u7684kill -9 \u4e5f\u2f46\u80fd\u4e3a\u2f12&#xff0c;\u56e0\u4e3a\u8c01\u4e5f\u6ca1\u6709\u529e\u6cd5\u6740\u6b7b\u2f00\u4e2a\u5df2\u7ecf\u6b7b\u53bb\u7684\u8fdb\u7a0b\u3002<\/li>\n<li>\u6700\u540e&#xff0c;\u2f57\u8fdb\u7a0b\u6d3e\u7ed9\u2f26\u8fdb\u7a0b\u7684\u4efb\u52a1\u5b8c\u6210\u7684\u5982\u4f55&#xff0c;\u6211\u4eec\u662f\u9700\u8981\u77e5\u9053\u7684\u3002\u5982&#xff0c;\u2f26\u8fdb\u7a0b\u8fd0\u2f8f\u5b8c\u6210&#xff0c;\u7ed3\u679c\u5bf9\u8fd8\u662f\u4e0d\u5bf9&#xff0c;\u6216\u8005\u662f\u5426\u6b63\u5e38\u9000\u51fa\u3002<\/li>\n<li>\u2f57\u8fdb\u7a0b\u901a\u8fc7\u8fdb\u7a0b\u7b49\u5f85\u7684\u2f45\u5f0f&#xff0c;\u56de\u6536\u2f26\u8fdb\u7a0b\u8d44\u6e90&#xff0c;\u83b7\u53d6\u2f26\u8fdb\u7a0b\u9000\u51fa\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<h3 id=\"3.2%E3%80%81%E8%BF%9B%E7%A8%8B%E7%AD%89%E5%BE%85%E7%9A%84%E6%96%B9%E6%B3%95\">3.2\u3001\u8fdb\u7a0b\u7b49\u5f85\u7684\u65b9\u6cd5<\/h3>\n<h4 id=\"3.2.1%E3%80%81wait%E6%96%B9%E6%B3%95\">3.2.1\u3001wait\u65b9\u6cd5<\/h4>\n<p><span style=\"color:#6eaad7\">#<\/span><span style=\"color:#956fe7\">include<\/span><span style=\"color:#1c7331\">&lt;sys\/types.h&gt;<\/span><\/p>\n<p><span style=\"color:#6eaad7\">#<\/span><span style=\"color:#956fe7\">include<\/span><span style=\"color:#1c7331\">&lt;sys\/wait.h&gt;<\/span><\/p>\n<p><span style=\"color:#ad720d\">pid_t<\/span><span style=\"color:#6eaad7\"> wait<\/span>(<span style=\"color:#ad720d\">int<\/span>* status);<\/p>\n<\/p>\n<p>\u8fd4\u56de\u503c&#xff1a;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6210\u529f\u8fd4\u56de\u88ab\u7b49\u5f85\u8fdb\u7a0bpid&#xff0c;\u5931\u8d25\u8fd4\u56de-1\u3002<\/p>\n<p>\u53c2\u6570&#xff1a;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u8f93\u51fa\u578b\u53c2\u6570&#xff0c;\u83b7\u53d6\u2f26\u8fdb\u7a0b\u9000\u51fa\u72b6\u6001,\u4e0d\u5173\u2f3c\u5219\u53ef\u4ee5\u8bbe\u7f6e\u6210\u4e3aNULL\u3002<\/p>\n<p>\u793a\u4f8b\u4ee3\u7801&#xff1a;<\/p>\n<p>  1 #include&lt;stdio.h&gt;<br \/>\n  2 #include&lt;unistd.h&gt;<br \/>\n  3 #include&lt;errno.h&gt;<br \/>\n  4 #include&lt;string.h&gt;<br \/>\n  5 #include&lt;stdlib.h&gt;<br \/>\n  6 #include&lt;sys\/wait.h&gt;<br \/>\n  7<br \/>\n  8 int main()<br \/>\n  9 {<br \/>\n 10   pid_t id &#061; fork();<br \/>\n 11   if(id &lt; 0)<br \/>\n 12   {<br \/>\n 13     printf(&#034;errno: %d,errstring: %s\\\\n&#034;,errno,strerror(errno));<br \/>\n 14     return errno;<br \/>\n 15   }<br \/>\n 16   else if(id &#061;&#061; 0)<br \/>\n 17   {<br \/>\n 18     int cnt &#061; 3;<br \/>\n 19     while(cnt)<br \/>\n 20     {<br \/>\n 21       printf(&#034;\u5b50\u8fdb\u7a0b\u8fd0\u884c\u4e2d&#xff0c;pid&#xff1a;%d\\\\n&#034;,getpid());<br \/>\n 22       cnt&#8211;;<br \/>\n 23       sleep(1);<br \/>\n 24     }<br \/>\n 25     exit(123);<br \/>\n 26   }<br \/>\n 27   else<br \/>\n 28   {<br \/>\n 29     sleep(5);<br \/>\n 30     pid_t rid &#061; wait(nullptr);<br \/>\n 31     if(rid &gt; 0)<br \/>\n 32     {<br \/>\n 33       printf(&#034;wait sub process success,rid: %d\\\\n&#034;,rid);<br \/>\n 34     }<br \/>\n 35     else<br \/>\n 36     {<br \/>\n 37       perror(&#034;wait fail:&#034;);<br \/>\n 38     }<br \/>\n 39     while(true)<br \/>\n 40     {<br \/>\n 41       printf(&#034;\u6211\u662f\u7236\u8fdb\u7a0b,pid: %d\\\\n&#034;,getpid());<br \/>\n 42       sleep(1);<br \/>\n 43     }<br \/>\n 44   }<br \/>\n 45<br \/>\n 46   return 0;<br \/>\n 47 }<\/p>\n<p>\u6548\u679c&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"269\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517094606-68285adeeb981.png\" width=\"958\" \/><\/p>\n<h4 id=\"3.2.2%E3%80%81waitpid%E6%96%B9%E6%B3%95\">3.2.2\u3001waitpid\u65b9\u6cd5<\/h4>\n<p>pid_ t <span style=\"color:#6eaad7\">waitpid<\/span>(<span style=\"color:#ad720d\">pid_t<\/span> pid, <span style=\"color:#ad720d\">int<\/span> *status,<span style=\"color:#ad720d\"> int <\/span>options);<\/p>\n<p>\u8fd4\u56de\u503c&#xff1a;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5f53\u6b63\u5e38\u8fd4\u56de\u7684\u65f6\u5019waitpid\u8fd4\u56de\u6536\u96c6\u5230\u7684\u2f26\u8fdb\u7a0b\u7684\u8fdb\u7a0bID&#xff1b;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5982\u679c\u8bbe\u7f6e\u4e86\u9009\u9879WNOHANG,\u2f7d\u8c03\u2f64\u4e2dwaitpid\u53d1\u73b0\u6ca1\u6709\u5df2\u9000\u51fa\u7684\u2f26\u8fdb\u7a0b\u53ef\u6536\u96c6,\u5219\u8fd4\u56de0&#xff1b;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5982\u679c\u8c03\u2f64\u4e2d\u51fa\u9519,\u5219\u8fd4\u56de-1,\u8fd9\u65f6errno\u4f1a\u88ab\u8bbe\u7f6e\u6210\u76f8\u5e94\u7684\u503c\u4ee5\u6307\u2f70\u9519\u8bef\u6240\u5728&#xff1b;<\/p>\n<p>\u53c2\u6570&#xff1a;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0pid&#xff1a;<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 pid&#061;-1,\u7b49\u5f85\u4efb\u2f00\u4e2a\u2f26\u8fdb\u7a0b\u3002\u4e0ewait\u7b49\u6548\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 pid&gt;0.\u7b49\u5f85\u5176\u8fdb\u7a0bID\u4e0epid\u76f8\u7b49\u7684\u2f26\u8fdb\u7a0b\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0status: \u8f93\u51fa\u578b\u53c2\u6570<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0WIFEXITED(status): \u82e5\u4e3a\u6b63\u5e38\u7ec8\u2f4c\u2f26\u8fdb\u7a0b\u8fd4\u56de\u7684\u72b6\u6001&#xff0c;\u5219\u4e3a\u771f\u3002&#xff08;\u67e5\u770b\u8fdb\u7a0b\u662f\u5426\u662f\u6b63\u5e38\u9000\u51fa&#xff09;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0WEXITSTATUS(status): \u82e5WIFEXITED\u2fae\u96f6&#xff0c;\u63d0\u53d6\u2f26\u8fdb\u7a0b\u9000\u51fa\u7801\u3002&#xff08;\u67e5\u770b\u8fdb\u7a0b\u7684\u9000\u51fa\u7801&#xff09;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0options:\u9ed8\u8ba4\u4e3a0&#xff0c;\u8868\u2f70\u963b\u585e\u7b49\u5f85\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0WNOHANG: \u82e5pid\u6307\u5b9a\u7684\u2f26\u8fdb\u7a0b\u6ca1\u6709\u7ed3\u675f&#xff0c;\u5219waitpid()\u51fd\u6570\u8fd4\u56de0&#xff0c;\u4e0d\u4e88\u4ee5\u7b49\u5f85\u3002\u82e5\u6b63\u5e38\u7ed3\u675f&#xff0c;\u5219\u8fd4\u56de\u8be5\u2f26\u8fdb\u7a0b\u7684ID\u3002<\/p>\n<ul>\n<li>\u5982\u679c\u2f26\u8fdb\u7a0b\u5df2\u7ecf\u9000\u51fa&#xff0c;\u8c03\u2f64wait\/waitpid\u65f6&#xff0c;wait\/waitpid\u4f1a\u2f74\u5373\u8fd4\u56de&#xff0c;\u5e76\u4e14\u91ca\u653e\u8d44\u6e90&#xff0c;\u83b7\u5f97\u2f26\u8fdb\u7a0b\u9000\u51fa\u4fe1\u606f\u3002<\/li>\n<li>\u5982\u679c\u5728\u4efb\u610f\u65f6\u523b\u8c03\u2f64wait\/waitpid&#xff0c;\u2f26\u8fdb\u7a0b\u5b58\u5728\u4e14\u6b63\u5e38\u8fd0\u2f8f&#xff0c;\u5219\u8fdb\u7a0b\u53ef\u80fd\u963b\u585e\u3002<\/li>\n<li>\u5982\u679c\u4e0d\u5b58\u5728\u8be5\u2f26\u8fdb\u7a0b&#xff0c;\u5219\u2f74\u5373\u51fa\u9519\u8fd4\u56de\u3002<\/li>\n<\/ul>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"583\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517094607-68285adf0ff48.png\" width=\"1170\" \/><\/p>\n<p>\u793a\u4f8b\u4ee3\u7801&#xff1a;<\/p>\n<p>  1 #include&lt;stdio.h&gt;<br \/>\n  2 #include&lt;unistd.h&gt;<br \/>\n  3 #include&lt;errno.h&gt;<br \/>\n  4 #include&lt;string.h&gt;<br \/>\n  5 #include&lt;stdlib.h&gt;<br \/>\n  6 #include&lt;sys\/wait.h&gt;<br \/>\n  7<br \/>\n  8 int main()<br \/>\n  9 {<br \/>\n 10   pid_t id &#061; fork();<br \/>\n 11   if(id &lt; 0)<br \/>\n 12   {<br \/>\n 13     printf(&#034;errno: %d,errstring: %s\\\\n&#034;,errno,strerror(errno));<br \/>\n 14     return errno;<br \/>\n 15   }<br \/>\n 16   else if(id &#061;&#061; 0)<br \/>\n 17   {<br \/>\n 18     int cnt &#061; 3;<br \/>\n 19     while(cnt)<br \/>\n 20     {<br \/>\n 21       printf(&#034;\u5b50\u8fdb\u7a0b\u8fd0\u884c\u4e2d&#xff0c;pid&#xff1a;%d\\\\n&#034;,getpid());<br \/>\n 22       cnt&#8211;;<br \/>\n 23       sleep(1);<br \/>\n 24     }<br \/>\n 25     exit(123);<br \/>\n 26   }<br \/>\n 27   else<br \/>\n 28   {<br \/>\n 29     sleep(5);<br \/>\n 30     pid_t rid &#061; waitpid(id,nullptr,0);<br \/>\n 31     if(rid &gt; 0)<br \/>\n 32     {<br \/>\n 33       printf(&#034;wait sub process success,rid: %d\\\\n&#034;,rid);<br \/>\n 34     }<br \/>\n 35     else<br \/>\n 36     {<br \/>\n 37       perror(&#034;wait fail:&#034;);<br \/>\n 38     }<br \/>\n 39     while(true)<br \/>\n 40     {<br \/>\n 41       printf(&#034;\u6211\u662f\u7236\u8fdb\u7a0b,pid: %d\\\\n&#034;,getpid());<br \/>\n 42       sleep(1);<br \/>\n 43     }<br \/>\n 44   }<br \/>\n 45<br \/>\n 46   return 0;<br \/>\n 47 } <\/p>\n<p>\u6548\u679c&#xff1a;<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"262\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517094607-68285adf50ac6.png\" width=\"1079\" \/><\/p>\n<h4 id=\"3.2.3%E3%80%81%E8%8E%B7%E5%8F%96%E5%AD%90%E8%BF%9B%E7%A8%8Bstatus\">3.2.3\u3001\u83b7\u53d6\u5b50\u8fdb\u7a0bstatus<\/h4>\n<p>wait\u548cwaitpid&#xff0c;\u90fd\u6709\u2f00\u4e2astatus\u53c2\u6570&#xff0c;\u8be5\u53c2\u6570\u662f\u2f00\u4e2a\u8f93\u51fa\u578b\u53c2\u6570&#xff0c;\u7531\u64cd\u4f5c\u7cfb\u7edf\u586b\u5145\u3002<\/p>\n<p>\u5982\u679c\u4f20\u9012NULL&#xff0c;\u8868\u2f70\u4e0d\u5173\u2f3c\u2f26\u8fdb\u7a0b\u7684\u9000\u51fa\u72b6\u6001\u4fe1\u606f\u3002<\/p>\n<p>\u5426\u5219&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u6839\u636e\u8be5\u53c2\u6570&#xff0c;\u5c06\u2f26\u8fdb\u7a0b\u7684\u9000\u51fa\u4fe1\u606f\u53cd\u9988\u7ed9\u2f57\u8fdb\u7a0b\u3002<\/p>\n<p>status\u4e0d\u80fd\u7b80\u5355\u7684\u5f53\u4f5c\u6574\u5f62\u6765\u770b\u5f85&#xff0c;\u53ef\u4ee5\u5f53\u4f5c\u4f4d\u56fe\u6765\u770b\u5f85&#xff0c;\u5177\u4f53\u7ec6\u8282\u5982\u4e0b\u56fe&#xff08;\u53ea\u7814\u7a76status\u4f4e16 \u2f50\u7279\u4f4d&#xff09;&#xff1a;<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"267\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517094607-68285adf739fe.png\" width=\"1079\" \/><\/p>\n<p>\u5728 status \u7684\u4f4e\u5341\u516d\u4f4d\u4e2d&#xff0c;\u6b21\u4f4e\u516b\u4f4d\u662f\u9000\u51fa\u7801&#xff0c;\u6b21\u4f4e\u4e03\u4f4d\u662f\u9000\u51fa\u4fe1\u53f7\u7684\u503c\u3002<\/p>\n<p>\u793a\u4f8b\u4ee3\u7801&#xff1a;<\/p>\n<p>  1 #include&lt;stdio.h&gt;<br \/>\n  2 #include&lt;unistd.h&gt;<br \/>\n  3 #include&lt;errno.h&gt;<br \/>\n  4 #include&lt;string.h&gt;<br \/>\n  5 #include&lt;stdlib.h&gt;<br \/>\n  6 #include&lt;sys\/wait.h&gt;<br \/>\n  7<br \/>\n  8 int main()<br \/>\n  9 {<br \/>\n 10   pid_t id &#061; fork();<br \/>\n 11   if(id &lt; 0)<br \/>\n 12   {<br \/>\n 13     printf(&#034;errno: %d,errstring: %s\\\\n&#034;,errno,strerror(errno));<br \/>\n 14     return errno;<br \/>\n 15   }<br \/>\n 16   else if(id &#061;&#061; 0)<br \/>\n 17   {<br \/>\n 18     int cnt &#061; 3;<br \/>\n 19     while(cnt)<br \/>\n 20     {<br \/>\n 21       printf(&#034;\u5b50\u8fdb\u7a0b\u8fd0\u884c\u4e2d&#xff0c;pid&#xff1a;%d\\\\n&#034;,getpid());<br \/>\n 22       cnt&#8211;;<br \/>\n 23       sleep(1);<br \/>\n 24     }<br \/>\n 25     exit(123);<br \/>\n 26   }<br \/>\n 27   else<br \/>\n 28   {<br \/>\n 29     sleep(5);<br \/>\n 30<br \/>\n 31     int status &#061; 0;<br \/>\n 32     pid_t rid &#061; waitpid(id, &amp;status, 0);<br \/>\n 33<br \/>\n 34     if(rid &gt; 0)<br \/>\n 35     {<br \/>\n 36       if(WIFEXITED(status))<br \/>\n 37       {<br \/>\n 38         \/\/\u4e24\u79cd\u83b7\u53d6\u9000\u51fa\u7801\u7684\u65b9\u6cd5&#xff0c;\u7b2c\u4e00\u79cd\u8fd8\u53ef\u4ee5\u83b7\u53d6\u9000\u51fa\u4fe1\u53f7<br \/>\n 39         printf(&#034;wait sub process success,rid: %d, status code: %d, exit signal: %d\\\\n&#034;, rid, (status&gt;&gt;8)&amp;0xFF, status&amp;0x7F);<br \/>\n 40<br \/>\n 41         printf(&#034;wait sub process success,rid: %d, status code: %d\\\\n&#034;, rid, WEXITSTATUS(status));<br \/>\n 42       }<br \/>\n 43       else<br \/>\n 44       {<br \/>\n 45         printf(&#034;child process quit error!\\\\n&#034;);<br \/>\n 46       }<br \/>\n 47     }<br \/>\n 48     else<br \/>\n 49     {<br \/>\n 50       perror(&#034;wait fail:&#034;);<br \/>\n 51     }<br \/>\n 52<br \/>\n 53     while(true)<br \/>\n 54     {<br \/>\n 55       printf(&#034;\u6211\u662f\u7236\u8fdb\u7a0b,pid: %d\\\\n&#034;,getpid());<br \/>\n 56       sleep(1);<br \/>\n 57     }<br \/>\n 58   }<br \/>\n 59<br \/>\n 60   return 0;<br \/>\n 61 }<\/p>\n<p>\u6548\u679c&#xff1a;<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"327\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517094607-68285adf974a9.png\" width=\"1063\" \/><\/p>\n<h4 id=\"3.2.4%E3%80%81%E9%98%BB%E5%A1%9E%E4%B8%8E%E9%9D%9E%E9%98%BB%E5%A1%9E%E7%AD%89%E5%BE%85\">3.2.4\u3001\u963b\u585e\u4e0e\u975e\u963b\u585e\u7b49\u5f85<\/h4>\n<ul>\n<li>\u8fdb\u7a0b\u7684\u963b\u585e\u7b49\u5f85\u2f45\u5f0f&#xff1a;<\/li>\n<\/ul>\n<p>\u793a\u4f8b\u4ee3\u7801&#xff1a;<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;cstdio&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/wait.h&gt;<br \/>\n#include &lt;functional&gt;<br \/>\n#include &#034;task.h&#034;<\/p>\n<p>enum{<br \/>\n    OK &#061; 0,<br \/>\n    OPEN_FILE_ERROR,<br \/>\n};<\/p>\n<p>const std::string gsep &#061; &#034; &#034;;<br \/>\nstd::vector&lt;int&gt; data;<\/p>\n<p>int SaveBegin()<br \/>\n{<br \/>\n    std::string name &#061; std::to_string(time(nullptr));<br \/>\n    name &#043;&#061; &#034;.backup&#034;;<br \/>\n    FILE *fp &#061; fopen(name.c_str(), &#034;w&#034;);<br \/>\n    if(fp &#061;&#061; nullptr) return OPEN_FILE_ERROR;<\/p>\n<p>    std::string dataStr;<br \/>\n    for (auto d : data)<br \/>\n    {<br \/>\n        dataStr &#043;&#061; std::to_string(d);<br \/>\n        dataStr &#043;&#061; gsep;<br \/>\n    }<br \/>\n    fputs(dataStr.c_str(), fp);<br \/>\n    fclose(fp);<br \/>\n    return OK;<br \/>\n}<\/p>\n<p>void Save()<br \/>\n{<br \/>\n    pid_t id &#061; fork();<br \/>\n    if(id &#061;&#061; 0) \/\/ \u5b50\u8fdb\u7a0b<br \/>\n    {<br \/>\n        int code &#061; SaveBegin();<br \/>\n        exit(code);<br \/>\n    }<br \/>\n    int status &#061; 0;<br \/>\n    pid_t rid &#061; waitpid(id, &amp;status, 0);<br \/>\n    if(rid &gt; 0)<br \/>\n    {<br \/>\n        int code &#061; WEXITSTATUS(status);<br \/>\n        if(code &#061;&#061; 0) printf(&#034;\u5907\u4efd\u6210\u529f, exit code : %d\\\\n&#034;, code);<br \/>\n        else printf(&#034;\u5907\u4efd\u5931\u8d25, exit code : %d\\\\n&#034;, code);<br \/>\n    }<br \/>\n    else<br \/>\n    {<br \/>\n        perror(&#034;waitpid&#034;);<br \/>\n    }<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\n    int cnt &#061; 1;<br \/>\n    while(true)<br \/>\n    {<br \/>\n        data.push_back(cnt&#043;&#043;);<br \/>\n        sleep(1);<\/p>\n<p>        if(cnt % 10 &#061;&#061; 0)<br \/>\n        {<br \/>\n            Save();<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<ul>\n<li>\u8fdb\u7a0b\u7684\u975e\u963b\u585e\u7b49\u5f85\u65b9\u5f0f&#xff1a;<\/li>\n<\/ul>\n<p>\u5982\u679c\u6211\u4eec\u60f3\u8ba9\u7236\u8fdb\u7a0b\u5728\u7b49\u5f85\u5b50\u8fdb\u7a0b\u7684\u540c\u65f6\u8fd8\u53ef\u4ee5\u505a\u81ea\u5df1\u7684\u4e8b&#xff0c;\u4e0d\u88ab\u963b\u585e\u4f4f&#xff0c;\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5c06waitpid\u65b9\u6cd5\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u4f20\u5165WNOHANG&#xff0c;\u5373\u53ef\u8fdb\u5165\u975e\u963b\u585e\u7b49\u5f85\u6a21\u5f0f\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u503c &gt; 0 \u65f6&#xff0c;\u7b49\u5f85\u6210\u529f&#xff0c;\u8fd4\u56de\u503c\u4e3a\u76ee\u6807\u5b50\u8fdb\u7a0b\u7684pid&#xff1b;\u5f53\u8fd4\u56de\u503c &#061;&#061; 0 \u65f6&#xff0c;\u7b49\u5f85\u6210\u529f&#xff0c;\u4f46\u662f\u76ee\u6807\u5b50\u8fdb\u7a0b\u6ca1\u6709\u9000\u51fa&#xff0c;\u8fd4\u56de\u503c&lt; 0 \u65f6&#xff0c;\u7b49\u5f85\u5931\u8d25\u3002<\/p>\n<p>\u793a\u4f8b\u4ee3\u7801&#xff1a;<\/p>\n<p>task.h:<\/p>\n<p>#pragma once<br \/>\n#include &lt;iostream&gt;<\/p>\n<p>void PrintLog();<br \/>\nvoid Download();<br \/>\nvoid Backup();<\/p>\n<p>task.cc:<\/p>\n<p>#include &#034;task.h&#034;<\/p>\n<p>void PrintLog()<br \/>\n{<br \/>\n    std::cout &lt;&lt; &#034;Print log task&#034; &lt;&lt; std::endl;<br \/>\n}<\/p>\n<p>void Download()<br \/>\n{<br \/>\n    std::cout &lt;&lt; &#034;DownLoad task&#034; &lt;&lt; std::endl;<br \/>\n}<\/p>\n<p>void Backup()<br \/>\n{<br \/>\n    std::cout &lt;&lt; &#034;BackUp task&#034; &lt;&lt; std::endl;<br \/>\n}<\/p>\n<p>proc.cc<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;cstdio&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/wait.h&gt;<br \/>\n#include &lt;functional&gt;<br \/>\n#include &#034;task.h&#034;<\/p>\n<p>typedef std::function&lt;void()&gt; task_t;<\/p>\n<p>void LoadTask(std::vector&lt;task_t&gt; &amp;tasks)<br \/>\n{<br \/>\n    tasks.push_back(PrintLog);<br \/>\n    tasks.push_back(Download);<br \/>\n    tasks.push_back(Backup);<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\n    std::vector&lt;task_t&gt; tasks;<br \/>\n    LoadTask(tasks);<\/p>\n<p>    pid_t id &#061; fork();<br \/>\n    if(id &#061;&#061; 0)<br \/>\n    {<br \/>\n        \/\/ child<br \/>\n        while(true)<br \/>\n        {<br \/>\n            printf(&#034;\u6211\u662f\u5b50\u8fdb\u7a0b, pid : %d\\\\n&#034;, getpid());<br \/>\n            sleep(1);<br \/>\n        }<br \/>\n        exit(0);<br \/>\n    }<\/p>\n<p>    \/\/ father<br \/>\n    while(true)<br \/>\n    {<br \/>\n        sleep(1);<br \/>\n        pid_t rid &#061; waitpid(id, nullptr, WNOHANG);<br \/>\n        if(rid &gt; 0)<br \/>\n        {<br \/>\n            printf(&#034;\u7b49\u5f85\u5b50\u8fdb\u7a0b%d \u6210\u529f\\\\n&#034;, rid);<br \/>\n            break;<br \/>\n        }<br \/>\n        else if(rid &lt; 0)<br \/>\n        {<br \/>\n            printf(&#034;\u7b49\u5f85\u5b50\u8fdb\u7a0b\u5931\u8d25\\\\n&#034;);<br \/>\n            break;<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            printf(&#034;\u5b50\u8fdb\u7a0b\u5c1a\u672a\u9000\u51fa\\\\n&#034;);<\/p>\n<p>            \/\/ \u505a\u81ea\u5df1\u7684\u4e8b\u60c5<br \/>\n            for(auto &amp;task : tasks)<br \/>\n            {<br \/>\n                task();<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>} <\/p>\n<p>Makefile:<\/p>\n<p>process:process.cc task.cc<br \/>\ng&#043;&#043; -o $&#064; $^ -std&#061;c&#043;&#043;11<\/p>\n<p>.PHONY:clean<br \/>\nclean:<br \/>\nrm -f process<\/p>\n<h4 id=\"3.2.5%E3%80%81%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E9%80%9A%E8%BF%87%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8%E6%9D%A5%E8%8E%B7%E5%8F%96%E8%BF%9B%E7%A8%8B%E9%80%80%E5%87%BA%E7%A0%81\">3.2.5\u3001\u4e3a\u4ec0\u4e48\u8981\u901a\u8fc7\u7cfb\u7edf\u8c03\u7528\u6765\u83b7\u53d6\u8fdb\u7a0b\u9000\u51fa\u7801<\/h4>\n<p>\u8fd9\u662f\u56e0\u4e3a\u5982\u679c\u6211\u4eec\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u6765\u83b7\u53d6&#xff0c;\u5b50\u8fdb\u7a0b\u548c\u7236\u8fdb\u7a0b\u7684\u6570\u636e\u5728\u4e0d\u4fee\u6539\u65f6\u662f\u5171\u4eab\u7684&#xff0c;\u4f46\u662f\u4e00\u65e6\u4fee\u6539\u5c31\u4f1a\u53d1\u751f\u5199\u5b9e\u62f7\u8d1d&#xff0c;\u8fd9\u5c31\u5bfc\u81f4\u7236\u8fdb\u7a0b\u62ff\u4e0d\u5230\u4fee\u6539\u540e\u7684\u503c&#xff0c;\u53ef\u5b50\u8fdb\u7a0b\u8981\u60f3\u901a\u8fc7\u53d8\u91cf\u5c06\u9000\u51fa\u7801\u4f20\u9012\u51fa\u53bb&#xff0c;\u5c31\u5fc5\u987b\u5bf9\u53d8\u91cf\u8fdb\u884c\u4fee\u6539&#xff0c;\u6240\u4ee5\u901a\u8fc7\u53d8\u91cf\u7684\u65b9\u5f0f\u884c\u4e0d\u901a&#xff0c;\u5c31\u53ea\u80fd\u901a\u8fc7\u7cfb\u7edf\u63d0\u4f9b\u7684\u63a5\u53e3\u4e86\u3002<\/p>\n<h2 id=\"%E5%9B%9B%E3%80%81%E8%BF%9B%E7%A8%8B%E7%A8%8B%E5%BA%8F%E6%9B%BF%E6%8D%A2\">\u56db\u3001\u8fdb\u7a0b\u7a0b\u5e8f\u66ff\u6362<\/h2>\n<p>fork() \u4e4b\u540e,\u2f57\u2f26\u5404\u2f83\u6267\u2f8f\u2f57\u8fdb\u7a0b\u4ee3\u7801\u7684\u2f00\u90e8\u5206\u5982\u679c\u2f26\u8fdb\u7a0b\u5c31\u60f3\u6267\u2f8f\u2f00\u4e2a\u5168\u65b0\u7684\u7a0b\u5e8f\u5462&#xff1f;\u8fdb\u7a0b\u7684\u7a0b\u5e8f \u66ff\u6362\u6765\u5b8c\u6210\u8fd9\u4e2a\u529f\u80fd&#xff01;\u7a0b\u5e8f\u66ff\u6362\u662f\u901a\u8fc7\u7279\u5b9a\u7684\u63a5\u2f1d&#xff0c;\u52a0\u8f7d\u78c1\u76d8\u4e0a\u7684\u2f00\u4e2a\u5168\u65b0\u7684\u7a0b\u5e8f(\u4ee3\u7801\u548c\u6570\u636e)&#xff0c;\u52a0\u8f7d\u5230\u8c03\u2f64\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\u4e2d&#xff01;<\/p>\n<h3 id=\"4.1%E3%80%81%E6%9B%BF%E6%8D%A2%E5%8E%9F%E7%90%86\">4.1\u3001\u66ff\u6362\u539f\u7406<\/h3>\n<p>\u2f64fork\u521b\u5efa\u2f26\u8fdb\u7a0b\u540e\u6267\u2f8f\u7684\u662f\u548c\u2f57\u8fdb\u7a0b\u76f8\u540c\u7684\u7a0b\u5e8f(\u4f46\u6709\u53ef\u80fd\u6267\u2f8f\u4e0d\u540c\u7684\u4ee3\u7801\u5206\u2f40),\u2f26\u8fdb\u7a0b\u5f80\u5f80\u8981\u8c03\u2f64\u2f00\u79cdexec\u51fd\u6570\u4ee5\u6267\u2f8f\u53e6\u2f00\u4e2a\u7a0b\u5e8f\u3002\u5f53\u8fdb\u7a0b\u8c03\u2f64\u2f00\u79cdexec\u51fd\u6570\u65f6,\u8be5\u8fdb\u7a0b\u7684\u7528\u6237\u7a7a\u95f4\u4ee3\u7801\u548c\u6570\u636e\u5b8c\u5168\u88ab\u65b0\u7a0b\u5e8f\u66ff\u6362,\u4ece\u65b0\u7a0b\u5e8f\u7684\u542f\u52a8\u4f8b\u7a0b\u5f00\u59cb\u6267\u2f8f\u3002\u8c03\u2f64exec\u5e76\u4e0d\u521b\u5efa\u65b0\u8fdb\u7a0b,\u6240\u4ee5\u8c03\u2f64exec\u524d\u540e\u8be5\u8fdb\u7a0b\u7684id\u5e76\u672a\u6539\u53d8\u3002<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"472\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517094607-68285adfc0237.png\" width=\"1168\" \/><\/p>\n<h3 id=\"4.2%E3%80%81%E6%9B%BF%E6%8D%A2%E5%87%BD%E6%95%B0\">4.2\u3001\u66ff\u6362\u51fd\u6570<\/h3>\n<p>\u5176\u5b9e\u6709\u516d\u79cd\u4ee5exec\u5f00\u5934\u7684\u51fd\u6570,\u7edf\u79f0exec\u51fd\u6570:<\/p>\n<p><span style=\"color:#6eaad7\">#<\/span><span style=\"color:#956fe7\">include<\/span><span style=\"color:#1c7331\">&lt;unistd.h&gt;<\/span><\/p>\n<\/p>\n<p><span style=\"color:#ad720d\">int<\/span> <span style=\"color:#6eaad7\">execl<\/span>(<span style=\"color:#ad720d\">const char<\/span> *path,<span style=\"color:#ad720d\"> const char<\/span> *arg, &#8230;);<\/p>\n<p><span style=\"color:#ad720d\">int <\/span><span style=\"color:#6eaad7\">execlp<\/span>(<span style=\"color:#ad720d\">const char<\/span> *file, <span style=\"color:#ad720d\">const char<\/span> *arg, &#8230;);<\/p>\n<p><span style=\"color:#ad720d\">int<\/span> <span style=\"color:#6eaad7\">execle<\/span>(<span style=\"color:#ad720d\">const char <\/span>*path, <span style=\"color:#ad720d\">const char<\/span> *arg, &#8230;,<span style=\"color:#ad720d\">char<\/span> *<span style=\"color:#ad720d\">const<\/span> envp[]);<\/p>\n<p><span style=\"color:#ad720d\">int <\/span><span style=\"color:#6eaad7\">execv<\/span>(<span style=\"color:#ad720d\">const char <\/span>*path, <span style=\"color:#ad720d\">char<\/span> *<span style=\"color:#ad720d\">const<\/span> argv[]);<\/p>\n<p><span style=\"color:#ad720d\">int<\/span> <span style=\"color:#6eaad7\">execvp<\/span>(<span style=\"color:#ad720d\">const char <\/span>*file, <span style=\"color:#ad720d\">char <\/span>*<span style=\"color:#ad720d\">const <\/span>argv[]);<\/p>\n<p><span style=\"color:#ad720d\">int<\/span><span style=\"color:#6eaad7\"> execve<\/span>(<span style=\"color:#ad720d\">const char<\/span> *path, <span style=\"color:#ad720d\">char <\/span>*<span style=\"color:#ad720d\">const<\/span> argv[], <span style=\"color:#ad720d\">char<\/span> *<span style=\"color:#ad720d\">const <\/span>envp[]);<\/p>\n<h4 id=\"4.2.1%E3%80%81%E5%87%BD%E6%95%B0%E8%A7%A3%E9%87%8A\">4.2.1\u3001\u51fd\u6570\u89e3\u91ca<\/h4>\n<ul>\n<li>\u8fd9\u4e9b\u51fd\u6570\u5982\u679c\u8c03\u2f64\u6210\u529f\u5219\u52a0\u8f7d\u65b0\u7684\u7a0b\u5e8f\u4ece\u542f\u52a8\u4ee3\u7801\u5f00\u59cb\u6267\u2f8f,\u4e0d\u518d\u8fd4\u56de\u3002<\/li>\n<li>\u5982\u679c\u8c03\u2f64\u51fa\u9519\u5219\u8fd4\u56de-1\u3002<\/li>\n<li>\u6240\u4ee5exec\u51fd\u6570\u53ea\u6709\u51fa\u9519\u7684\u8fd4\u56de\u503c\u2f7d\u6ca1\u6709\u6210\u529f\u7684\u8fd4\u56de\u503c\u3002<\/li>\n<\/ul>\n<h4 id=\"4.2.2%E3%80%81%E5%91%BD%E5%90%8D%E7%90%86%E8%A7%A3\">4.2.2\u3001\u547d\u540d\u7406\u89e3<\/h4>\n<p>\u8fd9\u4e9b\u51fd\u6570\u539f\u578b\u770b\u8d77\u6765\u5f88\u5bb9\u6613\u6df7,\u4f46\u53ea\u8981\u638c\u63e1\u4e86\u89c4\u5f8b\u5c31\u5f88\u597d\u8bb0\u3002<\/p>\n<ul>\n<li>l(list)&#xff1a;\u8868\u2f70\u53c2\u6570\u91c7\u2f64\u5217\u8868\u3002<\/li>\n<li>v(vector)&#xff1a;\u53c2\u6570\u2f64\u6570\u7ec4\u3002<\/li>\n<li>p(path)&#xff1a;\u6709p\u2f83\u52a8\u641c\u7d22\u73af\u5883\u53d8\u91cfPATH\u3002<\/li>\n<li>e(env)&#xff1a;\u8868\u2f70\u2f83\u2f30\u7ef4\u62a4\u73af\u5883\u53d8\u91cf\u3002<\/li>\n<\/ul>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"337\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517094608-68285ae00309a.png\" width=\"1157\" \/><\/p>\n<p><span style=\"color:#0d0016\">exec\u8c03\u2f64\u4e3e\u4f8b\u5982\u4e0b:<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u4e3a\u4e86\u65b9\u4fbf\u6f14\u793a&#xff0c;\u6240\u6709exec\u8c03\u7528\u90fd\u653e\u5230\u4e86\u4e00\u4e2a\u7a0b\u5e8f\u4e2d\u4e86&#xff0c;\u5b9e\u9645\u4f7f\u7528\u65f6\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u6839\u636e\u9700\u8981\u6267\u884c\u67d0\u4e00\u4e2a\u5c31\u53ef\u4ee5\u3002<\/span><\/p>\n<p>myexec.cc:<\/p>\n<p>    1 #include &lt;iostream&gt;<br \/>\n    2 #include &lt;cstdio&gt;<br \/>\n    3 #include &lt;unistd.h&gt;<br \/>\n    4 #include &lt;sys\/types.h&gt;<br \/>\n    5 #include &lt;sys\/wait.h&gt;<br \/>\n    6<br \/>\n    7 const std::string myenv&#061;&#034;HELLO&#061;AAAAAAAAAAAAAAAAAAAA&#034;;<br \/>\n    8<br \/>\n    9 extern char **environ;<br \/>\n   10 int main()<br \/>\n   11 {<br \/>\n   12     \/\/\u5728\u539f\u6709\u73af\u5883\u53d8\u91cf\u4e0a\u589e\u52a0\u65b0\u7684<br \/>\n   13     putenv((char*)myenv.c_str());<br \/>\n   14<br \/>\n   15     pid_t id &#061; fork();<br \/>\n   16<br \/>\n   17     if(id &#061;&#061; 0)<br \/>\n   18     {<br \/>\n   19         char *const argv[] &#061; {<br \/>\n   20             (char*)&#034;other&#034;,<br \/>\n   21             nullptr<br \/>\n   22         };<br \/>\n   23<br \/>\n   24         \/\/(void)argv;<br \/>\nW&gt; 25         char *const env[] &#061; {<br \/>\n   26             (char*)&#034;HELLO&#061;bite&#034;,<br \/>\n   27             (char*)&#034;HELLO1&#061;bite1&#034;,<br \/>\n   28             (char*)&#034;HELLO2&#061;bite2&#034;,<br \/>\n   29             (char*)&#034;HELLO3&#061;bite3&#034;<br \/>\n   30         };<br \/>\n   31<br \/>\n   32         \/\/\u5141\u8bb8\u81ea\u5df1\u4f20\u5165\u73af\u5883\u53d8\u91cf<br \/>\n   33         \/\/ \u4f20\u5165\u7684\u4f1a\u8986\u76d6\u7ee7\u627f\u6765\u7684<br \/>\n   34         \/\/ \u8be5\u65b9\u6cd5\u4f1a\u4f7f\u7528\u4f20\u5165\u7684\u65b0\u7684\u73af\u5883\u53d8\u91cf<br \/>\n   35         \/\/ \u6211\u4eec\u53ef\u4ee5\u81ea\u5df1\u5199\u4e00\u4e2a\u5168\u65b0\u7684&#xff0c;\u4e5f\u53ef\u4ee5\u5728\u539f\u6709\u73af\u5883\u53d8\u91cf\u540e\u9762\u6dfb\u52a0\u65b0\u7684\u9879<br \/>\n   36         execvpe(&#034;.\/other&#034;, argv, environ);<br \/>\n   37<br \/>\n   38         \/\/\u53ef\u4ee5\u81ea\u52a8\u67e5\u627e\u73af\u5883\u53d8\u91cf&#xff0c;\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e0d\u9700\u8981\u6307\u660e\u8def\u5f84<br \/>\n   39         execvp(argv[0], argv);<br \/>\n   40<br \/>\n   41         \/\/\u548cexecl\u4e00\u6837&#xff0c;\u53ea\u662f\u7b2c\u4e8c\u4e2a\u53c2\u6570\u53d8\u6210\u4e86\u6570\u7ec4<br \/>\n   42         execv(&#034;\/usr\/bin\/ls&#034;, argv);<br \/>\n   43<br \/>\n   44         \/\/ execl\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u5fc5\u987b\u662fnullptr<br \/>\n   45         \/\/ \u7b2c\u4e00\u4e2a\u53c2\u6570\u4ee3\u8868\u8981\u6267\u884c\u8c01<br \/>\n   46         \/\/ \u540e\u9762\u7684\u53ef\u53d8\u53c2\u6570\u5217\u8868\u8868\u660e\u8981\u600e\u4e48\u6267\u884c<br \/>\n   47         execl(&#034;\/bin\/ls&#034;, &#034;ls&#034;, &#034;-l&#034;, &#034;&#8211;color&#034;, &#034;-a&#034;, nullptr);<br \/>\n   48         execl(&#034;.\/other&#034;, &#034;other&#034;, nullptr);<br \/>\n   49         execl(&#034;\/usr\/bin\/python&#034;, &#034;python&#034;, &#034;test.py&#034;, nullptr);<br \/>\n   50         execl(&#034;\/usr\/bin\/bash&#034;, &#034;bash&#034;, &#034;test.sh&#034;, nullptr);<br \/>\n   51<br \/>\n   52         \/\/\u81ea\u52a8\u67e5\u627e\u73af\u5883\u53d8\u91cf&#xff0c;\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e0d\u9700\u8981\u6307\u660e\u8def\u5f84<br \/>\n   53         execlp(&#034;ls&#034;, &#034;ls&#034;, &#034;&#8211;color&#034;, &#034;-aln&#034;, nullptr);<br \/>\n   54<br \/>\n   55         \/\/\u5982\u679c\u7a0b\u5e8f\u66ff\u6362\u5931\u8d25&#xff0c;\u7ed3\u675f\u5b50\u8fdb\u7a0b\u8fd4\u56de\u9519\u8bef\u7801 -&gt; 1<br \/>\n   56         exit(1);<br \/>\n   57     }<br \/>\n   58<br \/>\n   59     \/\/ father<br \/>\n   60     pid_t rid &#061; waitpid(id, nullptr, 0);<br \/>\n   61     if(rid &gt; 0)<br \/>\n   62     {<br \/>\n   63         printf(&#034;\u7b49\u5f85\u5b50\u8fdb\u7a0b\u6210\u529f!\\\\n&#034;);<br \/>\n   64     }<br \/>\n   65<br \/>\n   66     return 0;<br \/>\n   67 }<\/p>\n<p>other.c:<\/p>\n<p>#include &lt;stdio.h&gt;<\/p>\n<p>extern char**environ;<\/p>\n<p>int main()<br \/>\n{<br \/>\n    for(int i &#061; 0; environ[i]; i&#043;&#043;)<br \/>\n    {<br \/>\n        printf(&#034;evn[%d]: %s\\\\n&#034;, i, environ[i]);<br \/>\n    }<br \/>\n}<\/p>\n<p>test.py:<\/p>\n<p>#!\/usr\/bin\/python<\/p>\n<p>print (&#034;hello python&#034;)<\/p>\n<p>test.sh:<\/p>\n<p>#!\/bin\/bash<\/p>\n<p>echo &#034;hello shell&#034;<\/p>\n<p>Makefile:<\/p>\n<p>myexec:myexec.cc<br \/>\ng&#043;&#043; -o $&#064; $^ -std&#061;c&#043;&#043;11<\/p>\n<p>.PHONY:clean<br \/>\nclean:<br \/>\nrm -f myexec<\/p>\n<p>\u4e8b\u5b9e\u4e0a,\u53ea\u6709execve\u662f\u771f\u6b63\u7684\u7cfb\u7edf\u8c03\u2f64,\u5176\u5b83\u4e94\u4e2a\u51fd\u6570\u6700\u7ec8\u90fd\u8c03\u2f64execve,\u6240\u4ee5execve\u5728man\u2f3f\u518c\u7b2c2\u8282, \u5176\u5b83\u51fd\u6570\u5728man\u2f3f\u518c\u7b2c3\u8282\u3002\u8fd9\u4e9b\u51fd\u6570\u4e4b\u95f4\u7684\u5173\u7cfb\u5982\u4e0b\u56fe\u6240\u2f70\u3002\u4e0b\u56feexec\u51fd\u6570\u7c07\u2f00\u4e2a\u5b8c\u6574\u7684\u4f8b\u2f26&#xff1a;<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"551\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517094608-68285ae0314f7.png\" width=\"1091\" \/><\/p>\n<h2 id=\"%E4%BA%94%E3%80%81%E8%87%AA%E4%B8%BBShell%E5%91%BD%E4%BB%A4%E8%A1%8C%E8%A7%A3%E9%87%8A%E5%99%A8\" style=\"background-color:transparent\">\u4e94\u3001\u81ea\u4e3bShell\u547d\u4ee4\u884c\u89e3\u91ca\u5668<\/h2>\n<h3 id=\"5.1%E3%80%81%E7%9B%AE%E6%A0%87\" style=\"background-color:transparent\">5.1\u3001\u76ee\u6807<\/h3>\n<ul>\n<li>\u8981\u80fd\u5904\u7406\u666e\u901a\u547d\u4ee4\u3002<\/li>\n<li>\u8981\u80fd\u5904\u7406\u5185\u5efa\u547d\u4ee4\u3002<\/li>\n<li>\u8981\u80fd\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u5185\u5efa\u547d\u4ee4\/\u672c\u5730\u53d8\u91cf\/\u73af\u5883\u53d8\u91cf\u8fd9\u4e9b\u6982\u5ff5\u3002<\/li>\n<li>\u8981\u80fd\u5e2e\u52a9\u6211\u4eec\u7406\u89e3shell\u7684\u5141\u8bb8\u539f\u7406\u3002<\/li>\n<\/ul>\n<h3 id=\"5.2%E3%80%81%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86\" style=\"background-color:transparent\">5.2\u3001\u5b9e\u73b0\u539f\u7406<\/h3>\n<p>\u2f64\u4e0b\u56fe\u7684\u65f6\u95f4\u8f74\u6765\u8868\u2f70\u4e8b\u4ef6\u7684\u53d1\u2f63\u6b21\u5e8f\u3002\u5176\u4e2d\u65f6\u95f4\u4ece\u5de6\u5411\u53f3\u3002shell\u7531\u6807\u8bc6\u4e3ash\u7684\u2f45\u5757\u4ee3\u8868&#xff0c;\u5b83\u968f\u7740\u65f6\u95f4\u7684\u6d41\u901d\u4ece\u5de6\u5411\u53f3\u79fb\u52a8\u3002shell\u4ece\u7528\u6237\u8bfb\u2f0a\u5b57\u7b26\u4e32&#034;ls&#034;\u3002shell\u5efa\u2f74\u2f00\u4e2a\u65b0\u7684\u8fdb\u7a0b&#xff0c;\u7136\u540e\u5728\u90a3\u4e2a\u8fdb\u7a0b\u4e2d\u8fd0\u2f8fls\u7a0b\u5e8f\u5e76\u7b49\u5f85\u90a3\u4e2a\u8fdb\u7a0b\u7ed3\u675f\u3002<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"376\" src=\"2025-05-175rrmdsqazx5.png\" width=\"1146\" \/><\/p>\n<p>\u7136\u540eshell\u8bfb\u53d6\u65b0\u7684\u2f00\u2f8f\u8f93\u2f0a&#xff0c;\u5efa\u2f74\u2f00\u4e2a\u65b0\u7684\u8fdb\u7a0b&#xff0c;\u5728\u8fd9\u4e2a\u8fdb\u7a0b\u4e2d\u8fd0\u2f8f\u7a0b\u5e8f\u5e76\u7b49\u5f85\u8fd9\u4e2a\u8fdb\u7a0b\u7ed3\u675f\u3002\u00a0\u6240\u4ee5\u8981\u5199\u2f00\u4e2ashell&#xff0c;\u9700\u8981\u5faa\u73af\u4ee5\u4e0b\u8fc7\u7a0b:<\/p>\n<ul>\n<li>\u83b7\u53d6\u547d\u4ee4\u2f8f<\/li>\n<li>\u89e3\u6790\u547d\u4ee4\u2f8f<\/li>\n<li>\u5efa\u2f74\u2f00\u4e2a\u2f26\u8fdb\u7a0b&#xff08;fork&#xff09;<\/li>\n<li>\u66ff\u6362\u2f26\u8fdb\u7a0b&#xff08;execvp&#xff09;\u00a0 \u00a0 \u00a0 \u00a0\u00a0<\/li>\n<li>\u2f57\u8fdb\u7a0b\u7b49\u5f85\u2f26\u8fdb\u7a0b\u9000\u51fa&#xff08;wait&#xff09;<\/li>\n<\/ul>\n<p>\u6839\u636e\u8fd9\u4e9b\u601d\u8def&#xff0c;\u548c\u6211\u4eec\u524d\u2faf\u7684\u5b66\u7684\u6280\u672f&#xff0c;\u5c31\u53ef\u4ee5\u2f83\u2f30\u6765\u5b9e\u73b0\u2f00\u4e2ashell\u4e86\u3002<\/p>\n<h3 id=\"5.3%E3%80%81%E6%BA%90%E7%A0%81\" style=\"background-color:transparent\">5.3\u3001\u6e90\u7801<\/h3>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;cstdio&gt;<br \/>\n#include &lt;cstdlib&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/wait.h&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>const int basesize &#061; 1024;<br \/>\nconst int argvnum &#061; 64;<br \/>\nconst int envnum &#061; 64;<br \/>\n\/\/ \u5168\u5c40\u7684\u547d\u4ee4\u884c\u53c2\u6570\u8868<br \/>\nchar *gargv[argvnum];<br \/>\nint gargc &#061; 0;<\/p>\n<p>\/\/ \u5168\u5c40\u7684\u53d8\u91cf<br \/>\nint lastcode &#061; 0;<\/p>\n<p>\/\/ \u6211\u7684\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf<br \/>\nchar *genv[envnum];<\/p>\n<p>\/\/ \u5168\u5c40\u7684\u5f53\u524dshell\u5de5\u4f5c\u8def\u5f84<br \/>\nchar pwd[basesize];<br \/>\nchar pwdenv[basesize];<\/p>\n<p>string GetUserName()<br \/>\n{<br \/>\n    string name &#061; getenv(&#034;USER&#034;);<br \/>\n    return name.empty() ? &#034;None&#034; : name;<br \/>\n}<\/p>\n<p>string GetHostName()<br \/>\n{<br \/>\n    string hostname &#061; getenv(&#034;HOSTNAME&#034;);<br \/>\n    return hostname.empty() ? &#034;None&#034; : hostname;<br \/>\n}<\/p>\n<p>string GetPwd()<br \/>\n{<br \/>\n    if(nullptr &#061;&#061; getcwd(pwd, sizeof(pwd))) return &#034;None&#034;;<br \/>\n    snprintf(pwdenv, sizeof(pwdenv),&#034;PWD&#061;%s&#034;, pwd);<br \/>\n    putenv(pwdenv); \/\/ PWD&#061;XXX<br \/>\n    return pwd;<\/p>\n<p>    \/\/string pwd &#061; getenv(&#034;PWD&#034;);<br \/>\n    \/\/return pwd.empty() ? &#034;None&#034; : pwd;<br \/>\n}<\/p>\n<p>string LastDir()<br \/>\n{<br \/>\n    string curr &#061; GetPwd();<br \/>\n    if(curr &#061;&#061; &#034;\/&#034; || curr &#061;&#061; &#034;None&#034;) return curr;<br \/>\n    \/\/ \/home\/whb\/XXX<br \/>\n    size_t pos &#061; curr.rfind(&#034;\/&#034;);<br \/>\n    if(pos &#061;&#061; std::string::npos) return curr;<br \/>\n    return curr.substr(pos&#043;1);<br \/>\n}<\/p>\n<p>string MakeCommandLine()<br \/>\n{<br \/>\n    \/\/ [whb&#064;bite-alicloud myshell]$<br \/>\n    char command_line[basesize];<br \/>\n    snprintf(command_line, basesize, &#034;[%s&#064;%s %s]# &#034;,\\\\<br \/>\n            GetUserName().c_str(), GetHostName().c_str(), LastDir().c_str());<br \/>\n    return command_line;<br \/>\n}<\/p>\n<p>void PrintCommandLine() \/\/ 1. \u547d\u4ee4\u884c\u63d0\u793a\u7b26<br \/>\n{<br \/>\n    printf(&#034;%s&#034;, MakeCommandLine().c_str());<br \/>\n    fflush(stdout);<br \/>\n}<\/p>\n<p>bool GetCommandLine(char command_buffer[], int size)   \/\/ 2. \u83b7\u53d6\u7528\u6237\u547d\u4ee4<br \/>\n{<br \/>\n    \/\/ \u6211\u4eec\u8ba4\u4e3a&#xff1a;\u6211\u4eec\u8981\u5c06\u7528\u6237\u8f93\u5165\u7684\u547d\u4ee4\u884c&#xff0c;\u5f53\u505a\u4e00\u4e2a\u5b8c\u6574\u7684\u5b57\u7b26\u4e32<br \/>\n    \/\/ &#034;ls -a -l -n&#034;<br \/>\n    char *result &#061; fgets(command_buffer, size, stdin);<br \/>\n    if(!result)<br \/>\n    {<br \/>\n        return false;<br \/>\n    }<br \/>\n    command_buffer[strlen(command_buffer)-1] &#061; 0;<br \/>\n    if(strlen(command_buffer) &#061;&#061; 0) return false;<br \/>\n    return true;<br \/>\n}<\/p>\n<p>void ParseCommandLine(char command_buffer[], int len) \/\/ 3. \u5206\u6790\u547d\u4ee4<br \/>\n{<br \/>\n    (void)len;<br \/>\n    memset(gargv, 0, sizeof(gargv));<br \/>\n    gargc &#061; 0;<br \/>\n    \/\/ &#034;ls -a -l -n&#034;<br \/>\n    const char *sep &#061; &#034; &#034;;<br \/>\n    gargv[gargc&#043;&#043;] &#061; strtok(command_buffer, sep);<br \/>\n    \/\/ &#061;\u662f\u523b\u610f\u5199\u7684<br \/>\n    while((bool)(gargv[gargc&#043;&#043;] &#061; strtok(nullptr, sep)));<br \/>\n    gargc&#8211;;<br \/>\n}<\/p>\n<p>void debug()<br \/>\n{<br \/>\n    printf(&#034;argc: %d\\\\n&#034;, gargc);<br \/>\n    for(int i &#061; 0; gargv[i]; i&#043;&#043;)<br \/>\n    {<br \/>\n        printf(&#034;argv[%d]: %s\\\\n&#034;, i, gargv[i]);<br \/>\n    }<br \/>\n}<br \/>\n\/\/ \u5728shell\u4e2d<br \/>\n\/\/ \u6709\u4e9b\u547d\u4ee4&#xff0c;\u5fc5\u987b\u7531\u5b50\u8fdb\u7a0b\u6765\u6267\u884c<br \/>\n\/\/ \u6709\u4e9b\u547d\u4ee4&#xff0c;\u4e0d\u80fd\u7531\u5b50\u8fdb\u7a0b\u6267\u884c&#xff0c;\u8981\u7531shell\u81ea\u5df1\u6267\u884c &#8212; \u5185\u5efa\u547d\u4ee4 built command<br \/>\nbool ExecuteCommand()   \/\/ 4. \u6267\u884c\u547d\u4ee4<br \/>\n{<br \/>\n    \/\/ \u8ba9\u5b50\u8fdb\u7a0b\u8fdb\u884c\u6267\u884c<br \/>\n    pid_t id &#061; fork();<br \/>\n    if(id &lt; 0) return false;<br \/>\n    if(id &#061;&#061; 0)<br \/>\n    {<br \/>\n        \/\/\u5b50\u8fdb\u7a0b<br \/>\n        \/\/ 1. \u6267\u884c\u547d\u4ee4<br \/>\n        execvpe(gargv[0], gargv, genv);<br \/>\n        \/\/ 2. \u9000\u51fa<br \/>\n        exit(1);<br \/>\n    }<br \/>\n    int status &#061; 0;<br \/>\n    pid_t rid &#061; waitpid(id, &amp;status, 0);<br \/>\n    if(rid &gt; 0)<br \/>\n    {<br \/>\n        if(WIFEXITED(status))<br \/>\n        {<br \/>\n            lastcode &#061; WEXITSTATUS(status);<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            lastcode &#061; 100;<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<br \/>\n    return false;<br \/>\n}<\/p>\n<p>void AddEnv(const char *item)<br \/>\n{<br \/>\n    int index &#061; 0;<br \/>\n    while(genv[index])<br \/>\n    {<br \/>\n        index&#043;&#043;;<br \/>\n    }<\/p>\n<p>    genv[index] &#061; (char*)malloc(strlen(item)&#043;1);<br \/>\n    strncpy(genv[index], item, strlen(item)&#043;1);<br \/>\n    genv[&#043;&#043;index] &#061; nullptr;<br \/>\n}<br \/>\n\/\/ shell\u81ea\u5df1\u6267\u884c\u547d\u4ee4&#xff0c;\u672c\u8d28\u662fshell\u8c03\u7528\u81ea\u5df1\u7684\u51fd\u6570<br \/>\nbool CheckAndExecBuiltCommand()<br \/>\n{<br \/>\n    if(strcmp(gargv[0], &#034;cd&#034;) &#061;&#061; 0)<br \/>\n    {<br \/>\n        \/\/ \u5185\u5efa\u547d\u4ee4<br \/>\n        if(gargc &#061;&#061; 2)<br \/>\n        {<br \/>\n            chdir(gargv[1]);<br \/>\n            lastcode &#061; 0;<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            lastcode &#061; 1;<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<br \/>\n    else if(strcmp(gargv[0], &#034;export&#034;) &#061;&#061; 0)<br \/>\n    {<br \/>\n        \/\/ export\u4e5f\u662f\u5185\u5efa\u547d\u4ee4<br \/>\n        if(gargc &#061;&#061; 2)<br \/>\n        {<br \/>\n            AddEnv(gargv[1]);<br \/>\n            lastcode &#061; 0;<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            lastcode &#061; 2;<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<br \/>\n    else if(strcmp(gargv[0], &#034;env&#034;) &#061;&#061; 0)<br \/>\n    {<br \/>\n        for(int i &#061; 0; genv[i]; i&#043;&#043;)<br \/>\n        {<br \/>\n            printf(&#034;%s\\\\n&#034;, genv[i]);<br \/>\n        }<br \/>\n        lastcode &#061; 0;<br \/>\n        return true;<br \/>\n    }<br \/>\n    else if(strcmp(gargv[0], &#034;echo&#034;) &#061;&#061; 0)<br \/>\n    {<br \/>\n        if(gargc &#061;&#061; 2)<br \/>\n        {<br \/>\n            \/\/ echo $?<br \/>\n            \/\/ echo $PATH<br \/>\n            \/\/ echo hello<br \/>\n            if(gargv[1][0] &#061;&#061; &#039;$&#039;)<br \/>\n            {<br \/>\n                if(gargv[1][1] &#061;&#061; &#039;?&#039;)<br \/>\n                {<br \/>\n                    printf(&#034;%d\\\\n&#034;, lastcode);<br \/>\n                    lastcode &#061; 0;<br \/>\n                }<br \/>\n            }<br \/>\n            else<br \/>\n            {<br \/>\n                printf(&#034;%s\\\\n&#034;, gargv[1]);<br \/>\n                lastcode &#061; 0;<br \/>\n            }<br \/>\n        }<br \/>\n        else<br \/>\n        {<br \/>\n            lastcode &#061; 3;<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<br \/>\n    return false;<br \/>\n}<\/p>\n<p>\/\/ \u4f5c\u4e3a\u4e00\u4e2ashell&#xff0c;\u83b7\u53d6\u73af\u5883\u53d8\u91cf\u5e94\u8be5\u4ece\u7cfb\u7edf\u7684\u914d\u7f6e\u6765<br \/>\n\/\/ \u6211\u4eec\u4eca\u5929\u5c31\u76f4\u63a5\u4ece\u7236shell\u4e2d\u83b7\u53d6\u73af\u5883\u53d8\u91cf<br \/>\nvoid InitEnv()<br \/>\n{<br \/>\n    extern char **environ;<br \/>\n    int index &#061; 0;<br \/>\n    while(environ[index])<br \/>\n    {<br \/>\n        genv[index] &#061; (char*)malloc(strlen(environ[index])&#043;1);<br \/>\n        strncpy(genv[index], environ[index], strlen(environ[index])&#043;1);<br \/>\n        index&#043;&#043;;<br \/>\n    }<br \/>\n    genv[index] &#061; nullptr;<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\n    InitEnv();<br \/>\n    char command_buffer[basesize];<br \/>\n    while(true)<br \/>\n    {<br \/>\n        PrintCommandLine(); \/\/ 1. \u547d\u4ee4\u884c\u63d0\u793a\u7b26<br \/>\n        \/\/ command_buffer -&gt; output<br \/>\n        if( !GetCommandLine(command_buffer, basesize) )   \/\/ 2. \u83b7\u53d6\u7528\u6237\u547d\u4ee4<br \/>\n        {<br \/>\n            continue;<br \/>\n        }<br \/>\n        \/\/printf(&#034;%s\\\\n&#034;, command_buffer);<br \/>\n        \/\/ls<br \/>\n        \/\/&#034;ls -a -b -c -d&#034;-&gt;&#034;ls&#034; &#034;-a&#034; &#034;-b&#034; &#034;-c&#034; &#034;-d&#034;<br \/>\n        ParseCommandLine(command_buffer, strlen(command_buffer)); \/\/ 3. \u5206\u6790\u547d\u4ee4<\/p>\n<p>        if ( CheckAndExecBuiltCommand() )<br \/>\n        {<br \/>\n            continue;<br \/>\n        }<\/p>\n<p>        ExecuteCommand();   \/\/ 4. \u6267\u884c\u547d\u4ee4<br \/>\n    }<br \/>\n    return 0;<br \/>\n} <\/p>\n<h3 id=\"5.4%E3%80%81%E6%80%BB%E7%BB%93\" style=\"background-color:transparent\">5.4\u3001\u603b\u7ed3<\/h3>\n<p>exec\/exit\u5c31\u50cfcall\/return\u3002<\/p>\n<p>\u00a0\u2f00\u4e2aC\u7a0b\u5e8f\u6709\u5f88\u591a\u51fd\u6570\u7ec4\u6210\u3002\u2f00\u4e2a\u51fd\u6570\u53ef\u4ee5\u8c03\u2f64\u53e6\u5916\u2f00\u4e2a\u51fd\u6570&#xff0c;\u540c\u65f6\u4f20\u9012\u7ed9\u5b83\u2f00\u4e9b\u53c2\u6570\u3002\u88ab\u8c03\u2f64\u7684\u51fd\u6570\u6267\u2f8f\u2f00\u5b9a\u7684\u64cd\u4f5c&#xff0c;\u7136\u540e\u8fd4\u56de\u2f00\u4e2a\u503c\u3002\u6bcf\u4e2a\u51fd\u6570\u90fd\u6709\u4ed6\u7684\u5c40\u90e8\u53d8\u91cf&#xff0c;\u4e0d\u540c\u7684\u51fd\u6570\u901a\u8fc7call\/return\u7cfb\u7edf\u8fdb\u2f8f\u901a\u4fe1\u3002<\/p>\n<p>\u8fd9\u79cd\u901a\u8fc7\u53c2\u6570\u548c\u8fd4\u56de\u503c\u5728\u62e5\u6709\u79c1\u6709\u6570\u636e\u7684\u51fd\u6570\u95f4\u901a\u4fe1\u7684\u6a21\u5f0f\u662f\u7ed3\u6784\u5316\u7a0b\u5e8f\u8bbe\u8ba1\u7684\u57fa\u7840\u3002Linux\u2fce\u52b1\u5c06\u8fd9\u79cd\u5e94\u2f64\u4e8e\u7a0b\u5e8f\u4e4b\u5185\u7684\u6a21\u5f0f\u6269\u5c55\u5230\u7a0b\u5e8f\u4e4b\u95f4\u3002\u5982\u4e0b\u56fe<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"557\" src=\"2025-05-17jy1lc5kw4pi.png\" width=\"1065\" \/><\/p>\n<p>\u2f00\u4e2aC\u7a0b\u5e8f\u53ef\u4ee5fork\/exec\u53e6\u2f00\u4e2a\u7a0b\u5e8f&#xff0c;\u5e76\u4f20\u7ed9\u5b83\u2f00\u4e9b\u53c2\u6570\u3002\u8fd9\u4e2a\u88ab\u8c03\u2f64\u7684\u7a0b\u5e8f\u6267\u2f8f\u2f00\u5b9a\u7684\u64cd\u4f5c&#xff0c;\u7136\u540e\u901a\u8fc7exit(n)\u6765\u8fd4\u56de\u503c\u3002\u8c03\u2f64\u5b83\u7684\u8fdb\u7a0b\u53ef\u4ee5\u901a\u8fc7wait&#xff08;&amp;ret&#xff09;\u6765\u83b7\u53d6exit\u7684\u8fd4\u56de\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.1k\u6b21\uff0c\u70b9\u8d5e39\u6b21\uff0c\u6536\u85cf50\u6b21\u3002Linux\u4e4b\u8fdb\u7a0b\u63a7\u5236_linux\u8fdb\u7a0b\u7684\u521b\u5efa\u548c\u63a7\u5236<\/p>\n","protected":false},"author":2,"featured_media":37799,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,58,190,305,124],"topic":[],"class_list":["post-37819","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-linux","tag-190","tag-305","tag-124"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Linux\u4e4b\u8fdb\u7a0b\u63a7\u5236 - \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\/37819.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linux\u4e4b\u8fdb\u7a0b\u63a7\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.1k\u6b21\uff0c\u70b9\u8d5e39\u6b21\uff0c\u6536\u85cf50\u6b21\u3002Linux\u4e4b\u8fdb\u7a0b\u63a7\u5236_linux\u8fdb\u7a0b\u7684\u521b\u5efa\u548c\u63a7\u5236\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/37819.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-17T09:46:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517094605-68285add2631c.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=\"12 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/37819.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/37819.html\",\"name\":\"Linux\u4e4b\u8fdb\u7a0b\u63a7\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-17T09:46:09+00:00\",\"dateModified\":\"2025-05-17T09:46:09+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/37819.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/37819.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/37819.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux\u4e4b\u8fdb\u7a0b\u63a7\u5236\"}]},{\"@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\u4e4b\u8fdb\u7a0b\u63a7\u5236 - \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\/37819.html","og_locale":"zh_CN","og_type":"article","og_title":"Linux\u4e4b\u8fdb\u7a0b\u63a7\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.1k\u6b21\uff0c\u70b9\u8d5e39\u6b21\uff0c\u6536\u85cf50\u6b21\u3002Linux\u4e4b\u8fdb\u7a0b\u63a7\u5236_linux\u8fdb\u7a0b\u7684\u521b\u5efa\u548c\u63a7\u5236","og_url":"https:\/\/www.wsisp.com\/helps\/37819.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-17T09:46:09+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517094605-68285add2631c.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"12 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/37819.html","url":"https:\/\/www.wsisp.com\/helps\/37819.html","name":"Linux\u4e4b\u8fdb\u7a0b\u63a7\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-17T09:46:09+00:00","dateModified":"2025-05-17T09:46:09+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/37819.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/37819.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/37819.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Linux\u4e4b\u8fdb\u7a0b\u63a7\u5236"}]},{"@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\/37819","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=37819"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/37819\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/37799"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=37819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=37819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=37819"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=37819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}