{"id":13105,"date":"2025-04-18T20:13:13","date_gmt":"2025-04-18T12:13:13","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/13105.html"},"modified":"2025-04-18T20:13:13","modified_gmt":"2025-04-18T12:13:13","slug":"%e3%80%90linux%e7%af%87%e3%80%91%e8%bf%9b%e7%a8%8b%e6%8e%a7%e5%88%b6","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/13105.html","title":{"rendered":"\u3010Linux\u7bc7\u3011\u8fdb\u7a0b\u63a7\u5236"},"content":{"rendered":"<p>&#x1f4cc; \u4e2a\u4eba\u4e3b\u9875&#xff1a; \u5b59\u540c\u5b66_ &#x1f527; \u6587\u7ae0\u4e13\u680f&#xff1a;Liunx &#x1f4a1; \u5173\u6ce8\u6211&#xff0c;\u5206\u4eab\u7ecf\u9a8c&#xff0c;\u52a9\u4f60\u5c11\u8d70\u5f2f\u8def&#xff01; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418121309-680241d5166e0.webp\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h3>1. \u8fdb\u7a0b\u521b\u5efa<\/h3>\n<h4>1.1 fork\u51fd\u6570<\/h4>\n<p>\u5728linux\u4e2dfork\u51fd\u6570\u662f\u975e\u5e38\u91cd\u8981\u7684\u51fd\u6570&#xff0c;\u5b83\u4ece\u5df2\u5b58\u5728\u8fdb\u7a0b\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u8fdb\u7a0b\u3002\u65b0\u8fdb\u7a0b\u4e3a\u5b50\u8fdb\u7a0b&#xff0c;\u800c\u539f\u8fdb\u7a0b\u4e3a\u7236\u8fdb\u7a0b\u3002<\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;unistd.h&gt;<\/span><\/span><br \/>\npid_t <span class=\"token function\">fork<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">void<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n\u8fd4\u56de\u503c&#xff1a;\u2f83\u8fdb\u7a0b\u4e2d\u8fd4\u56de<span class=\"token number\">0<\/span>&#xff0c;\u2f57\u8fdb\u7a0b\u8fd4\u56de\u2f26\u8fdb\u7a0bid&#xff0c;\u51fa\u9519\u8fd4\u56de<span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><\/p>\n<p>\u8fdb\u7a0b\u8c03\u7528fork&#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\u5b50\u8fdb\u7a0b<\/li>\n<li>\u5c06\u7236\u8fdb\u7a0b\u90e8\u5206\u6570\u636e\u7ed3\u6784\u5185\u5bb9\u62f7\u8d1d\u81f3\u5b50\u8fdb\u7a0b<\/li>\n<li>\u6dfb\u52a0\u5b50\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 <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418121309-680241d548592.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> \u5f53\u4e00\u4e2a\u8fdb\u7a0b\u8c03\u7528fork\u4e4b\u540e&#xff0c;\u5c31\u6709\u4e24\u4e2a\u4e8c\u8fdb\u5236\u4ee3\u7801\u76f8\u540c\u7684\u8fdb\u7a0b\u3002\u800c\u4e14\u5b83\u4eec\u90fd\u8fd0\u884c\u5230\u76f8\u540c\u7684\u5730\u65b9\u3002\u4f46\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u5c06\u53ef\u4ee5\u5f00\u59cb\u5b83\u4eec\u81ea\u5df1\u7684\u65c5\u7a0b&#xff0c;\u770b\u5982\u4e0b\u7a0b\u5e8f\u3002<\/li>\n<\/ul>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n pid_t pid<span class=\"token punctuation\">;<\/span><br \/>\n <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;Before: pid is %d\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token function\">getpid<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span> <span class=\"token punctuation\">(<\/span>pid<span class=\"token operator\">&#061;<\/span><span class=\"token function\">fork<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span> <span class=\"token punctuation\">)<\/span><span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;fork()&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><span class=\"token function\">exit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;After:pid is %d, fork return %d\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token function\">getpid<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> pid<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n <span class=\"token function\">sleep<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n\u8fd0\u2f8f\u7ed3\u679c&#xff1a;<br \/>\n<span class=\"token punctuation\">[<\/span>root&#064;localhost linux<span class=\"token punctuation\">]<\/span># <span class=\"token punctuation\">.<\/span><span class=\"token operator\">\/<\/span>a<span class=\"token punctuation\">.<\/span>out<br \/>\nBefore<span class=\"token operator\">:<\/span> pid is <span class=\"token number\">43676<\/span><br \/>\nAfter<span class=\"token operator\">:<\/span>pid is <span class=\"token number\">43676<\/span><span class=\"token punctuation\">,<\/span> fork <span class=\"token keyword\">return<\/span> <span class=\"token number\">43677<\/span><br \/>\nAfter<span class=\"token operator\">:<\/span>pid is <span class=\"token number\">43677<\/span><span class=\"token punctuation\">,<\/span> fork <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><\/p>\n<p>\u8fd9\u91cc\u770b\u5230\u4e86\u4e09\u884c\u8f93\u51fa&#xff0c;\u4e00\u884cbefore&#xff0c;\u4e24\u884cafter\u3002\u8fdb\u7a0b43676\u5148\u6253\u5370before\u6d88\u606f&#xff0c;\u7136\u540e\u5b83\u6709\u6253\u5370after\u3002\u53e6\u4e00\u4e2aafter\u6d88\u606f\u670943677\u6253\u5370\u7684\u3002\u6ce8\u610f\u5230\u8fdb\u7a0b43677\u6ca1\u6709\u6253\u5370before&#xff0c;\u4e3a\u4ec0\u4e48\u5462&#xff1f;\u5982\u4e0b\u56fe\u6240\u793a <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418121309-680241d583b2a.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> \u6240\u4ee5&#xff0c;fork\u4e4b\u524d\u7236\u8fdb\u7a0b\u72ec\u7acb\u6267\u884c&#xff0c;fork\u4e4b\u540e&#xff0c;\u7236\u5b50\u4e24\u4e2a\u6267\u884c\u6d41\u5206\u522b\u6267\u884c\u3002 \u6ce8\u610f&#xff1a;fork\u4e4b\u540e&#xff0c;\u8c01\u5148\u6267\u884c\u5b8c\u5168\u7531\u8c03\u5ea6\u5668\u51b3\u5b9a\u3002<\/p>\n<h4>1.2 \u5199\u65f6\u62f7\u8d1d<\/h4>\n<p>\u901a\u5e38&#xff0c;\u7236\u5b50\u4ee3\u7801\u5171\u4eab&#xff0c;\u7236\u5b50\u518d\u4e0d\u5199\u5165\u65f6&#xff0c;\u6570\u636e\u4e5f\u662f\u5171\u4eab\u7684&#xff0c;\u5f53\u4efb\u610f\u4e00\u65b9\u8bd5\u56fe\u5199\u5165&#xff0c;\u4fbf\u4ee5\u5199\u65f6\u62f7\u5907\u7684\u65b9\u5f0f\u5404\u81ea\u4e00\u4efd\u526f\u672c\u3002\u5177\u4f53\u89c1\u4e0b\u56fe: <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418121309-680241d5b9c5a.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> \u4e0a\u56fe\u663e\u793a\u7236\u8fdb\u7a0b\u4ee3\u7801\u6bb5\u5728\u81ea\u5df1\u7684\u9875\u8868\u4e2d\u662f\u53ea\u8bfb\u7684&#xff0c;\u5305\u62ec\u6211\u4eec\u4ee5\u524d\u5b9a\u4e49\u7684\u5b57\u7b26\u5e38\u91cf\u533a&#xff0c;\u4ee3\u7801\u662f\u4e0d\u53ef\u5199\u7684&#xff0c;\u53ef\u662f\u6570\u636e\u6bb5\u4e3a\u4ec0\u4e48\u4e5f\u662f\u53ea\u8bfb\u7684&#xff1f;\u8d77\u59cb\u5728\u6211\u4eec\u7684\u7236\u8fdb\u7a0b\u8fd8\u6ca1\u521b\u5efa\u5b50\u8fdb\u7a0b\u524d&#xff0c;\u4ee3\u7801\u6bb5\u662f\u53ea\u8bfb\u7684\u6ca1\u95ee\u9898&#xff0c;\u4f46\u662f\u6570\u636e\u6bb5\u5bf9\u5e94\u7684\u6620\u5c04\u5173\u7cfb&#xff0c;\u53ef\u80fd\u6709\u4e00\u767e\u4e2a\u4e00\u5343\u4e2a\u6620\u5c04\u5730\u5740&#xff0c;\u8fd9\u4e9b\u6620\u5c04\u5730\u5740\u7684\u6743\u9650\u5b9e\u9645\u4e0a\u662f\u8bfb\u5199\u7684&#xff0c;\u4f46\u4e00\u65e6\u521b\u5efa\u4e86\u5b50\u8fdb\u7a0b&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u5c31\u4f1a\u628a\u6570\u636e\u6bb5\u7684\u6743\u9650\u4e5f\u6539\u6210\u53ea\u8bfb\u7684\u3002 \u7136\u540e\u540e\u9762\u7684\u7236\u5b50\u8fdb\u7a0b&#xff0c;\u6bd4\u5982\u8bf4\u5b50\u8fdb\u7a0b\u5c1d\u8bd5\u5bf9\u5b83\u7684\u6570\u636e\u8fdb\u884c\u5199\u5165&#xff0c;\u5f53\u5b83\u5199\u5165\u65f6&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u5c31\u4f1a\u53d1\u73b0\u4f60\u8981\u8bbf\u95ee\u7684\u6570\u636e&#xff0c;\u7b2c\u4e00&#xff0c;\u6570\u636e\u662f\u5408\u6cd5\u7684&#xff0c;\u56e0\u4e3a\u865a\u62df\u5730\u5740\u7269\u7406\u5730\u5740\u90fd\u6709&#xff0c;\u800c\u4e14\u5b83\u53d1\u73b0\u8bbf\u95ee\u7684\u533a\u57df\u662f\u6570\u636e\u6bb5&#xff0c;\u5982\u679c\u662f\u4ee3\u7801\u6bb5\u80af\u5b9a\u5728start_code,end_code\u8fd9\u4e2a\u533a\u95f4\u91cc\u9762&#xff0c;\u5982\u679c\u662f\u6570\u636e\u6bb5\u80af\u5b9a\u5728start_data,start_end\u8fd9\u4e2a\u533a\u95f4\u91cc\u9762&#xff0c;\u53d1\u73b0\u4f60\u662f\u6570\u636e\u6bb5&#xff0c;\u800c\u4e14\u9875\u8868\u7684\u6620\u5c04\u5173\u7cfb\u662f\u6b63\u786e\u7684&#xff0c;\u4f46\u662f\u53d1\u73b0\u6570\u636e\u6bb5\u600e\u4e48\u662f\u53ea\u8bfb\u7684&#xff0c;\u6240\u4ee5\u8fd9\u65f6\u5019\u64cd\u4f5c\u7cfb\u7edf\u5c31\u4f1a\u51fa\u9519&#xff0c;\u8fd9\u79cd\u51fa\u9519\u4e0d\u662f\u771f\u7684\u9519\u4e86&#xff0c;\u662f\u64cd\u4f5c\u7cfb\u7edf\u68c0\u6d4b\u5230\u4e00\u4e2a\u7528\u6237\u5bf9\u4e00\u4e2a\u53ea\u8bfb\u7684\u533a\u57df\u8fdb\u884c\u5199\u5165&#xff0c;\u4f46\u64cd\u4f5c\u7cfb\u7edf\u7ecf\u8fc7\u68c0\u67e5\u53d1\u73b0\u5b83\u662f\u6570\u636e\u6bb5&#xff0c;\u800c\u4e14\u662f\u5b50\u8fdb\u7a0b&#xff0c;\u8fd9\u65f6\u5019\u64cd\u4f5c\u7cfb\u7edf\u5c31\u4f1a\u89e6\u53d1\u5199\u65f6\u62f7\u8d1d\u3002\u5199\u65f6\u62f7\u8d1d\u662f\u901a\u8fc7\u8bbe\u7f6e\u9875\u8868\u7684\u6743\u9650&#xff0c;\u8ba9\u9875\u8868\u8ba9\u64cd\u4f5c\u7cfb\u7edf\u51fa\u9519\u7684\u884c\u4e3a\u3002\u8ba9\u64cd\u4f5c\u7cfb\u7edf\u77e5\u9053\u6211\u4eec\u6b63\u5728\u8bbf\u95ee\u4e00\u4e2a\u53ea\u8bfb\u7684\u533a\u57df&#xff0c;\u8fdb\u800c\u5728\u9519\u8bef\u7684\u9a71\u4f7f\u4e4b\u4e0b\u8ba9\u64cd\u4f5c\u7cfb\u7edf\u5b8c\u6210\u5bf9\u5e94\u7684\u5199\u65f6\u62f7\u8d1d\u8fd9\u6837\u7684\u4efb\u52a1\u3002<\/p>\n<p>\u56e0\u4e3a\u6709\u5199\u65f6\u62f7\u8d1d\u6280\u672f\u7684\u5b58\u5728,\u6240\u4ee5\u7236\u5b50\u8fdb\u7a0b\u5f97\u4ee5\u5f7b\u5e95\u5206\u79bb\u79bb&#xff01;\u5b8c\u6210\u4e86\u8fdb\u7a0b\u72ec\u7acb\u6027\u7684\u6280\u672f\u4fdd\u8bc1! \u5199\u65f6\u62f7\u8d1d,\u662f\u4e00\u79cd\u5ef6\u65f6\u7533\u8bf7\u6280\u672f,\u53ef\u4ee5\u63d0\u9ad8\u6574\u673a\u5185\u5b58\u7684\u4f7f\u7528\u7387<\/p>\n<p>\u4e3a\u4ec0\u4e48\u8981\u5199\u65f6\u62f7\u8d1d&#xff1f;<\/p>\n<li>\u51cf\u5c11\u5b50\u8fdb\u7a0b\u7684\u521b\u5efa\u65f6\u95f4<\/li>\n<li>\u51cf\u5c11\u5185\u5b58\u6d6a\u8d39<\/li>\n<h4>1.3 fork\u8c03\u7528\u5931\u8d25\u7684\u539f\u56e0<\/h4>\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<h3>2. \u8fdb\u7a0b\u7ec8\u6b62<\/h3>\n<p>\u8fdb\u7a0b\u7ec8\u6b62\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<h4>2.1 \u8fdb\u7a0b\u9000\u51fa\u573a\u666f<\/h4>\n<li>\u4ee3\u7801\u8fd0\u884c\u5b8c\u6bd5&#xff0c;\u7ed3\u679c\u6b63\u786e<\/li>\n<li>\u4ee3\u7801\u8fd0\u884c\u5b8c\u6bd5&#xff0c;\u7ed3\u679c\u4e0d\u6b63\u786e<\/li>\n<li>\u4ee3\u7801\u5f02\u5e38\u7ec8\u6b62&#xff08;\u9000\u51fa\u7801\u65e0\u610f\u4e49&#xff09;<\/li>\n<h4>2.2 \u8fdb\u7a0b\u5e38\u89c1\u9000\u51fa\u65b9\u5f0f<\/h4>\n<p>\u6b63\u5e38\u7ec8\u6b62&#xff08;\u53ef\u4ee5\u901a\u8fc7 echo $? \u67e5\u770b\u6700\u8fd1\u8fdb\u7a0b\u7684\u9000\u51fa\u7801&#xff09;&#xff1a;<\/p>\n<li>\u4ecemain\u8fd4\u56de&#xff08;main\u51fd\u6570\u7ed3\u675f\u8868\u793a\u8fdb\u7a0b\u7ed3\u675f&#xff0c;\u5176\u4ed6\u51fd\u6570\u53ea\u8868\u793a\u81ea\u5df1\u51fd\u6570\u8c03\u7528\u5b8c\u6210&#xff09;<\/li>\n<li>\u8c03\u7528exit(status)(\u4efb\u4f55\u5730\u65b9\u8c03\u7528exit\u8868\u793a\u8fdb\u7a0b\u7ed3\u675f&#xff0c;\u5e76\u8fd4\u56de\u7ed9\u7236\u8fdb\u7a0bbash\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u7801)<\/li>\n<li>_exit:\u7ec8\u6b62\u4e00\u4e2a\u8c03\u7528\u8fdb\u7a0b&#xff08;\u76f8\u5f53\u4e8e\u8c01\u8c03\u7528\u5b83&#xff0c;\u5b83\u628a\u8c01\u201c\u5f04\u6b7b\u201d&#xff09;<\/li>\n<h5>2.2.1 exit\u51fd\u6570<\/h5>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;unistd.h&gt;<\/span><\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">exit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> status<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>exit\u6700\u540e\u4e5f\u4f1a\u8c03\u7528_exit,\u4f46\u5728\u8c03\u7528_exit\u4e4b\u524d&#xff0c;\u8fd8\u505a\u4e86\u5176\u4ed6\u5de5\u4f5c&#xff1a;<\/p>\n<li>\u6267\u884c\u7528\u6237\u901a\u8fc7atexit\u6216on_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\u5165<\/li>\n<li>\u8c03\u7528_exit <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418121309-680241d5f3364.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/li>\n<h5>2.2.2 _exit\u51fd\u6570<\/h5>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;unistd.h&gt;<\/span><\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">_exit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> status<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n\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\u7236\u8fdb\u7a0b\u6240\u7528\u3002\u6240\u4ee5_exit(-1)\u65f6&#xff0c;\u5728\u7ec8\u7aef\u6267\u884c$?\u53d1\u73b0\u8fd4\u56de\u503c\u662f255\u3002<\/p>\n<h5>2.2.3 exit\u548c_exit\u7684\u533a\u522b&#xff1a;<\/h5>\n<p>exit\u662fc\u8bed\u8a00\u63d0\u4f9b\u7684&#xff0c;_exit\u662f\u7cfb\u7edf\u63d0\u4f9b\u7684 \u8fdb\u7a0b\u5982\u679cexit\u9000\u51fa\u7684\u65f6\u5019&#xff0c;exit()\u4f1a\u8fdb\u884c\u7f13\u51b2\u533a\u7684\u5237\u65b0 \u8fdb\u7a0b\u5982\u679cexit\u9000\u51fa\u7684\u65f6\u5019&#xff0c;_exit\u4e0d\u4f1a\u8fdb\u884c\u7f13\u51b2\u533a\u7684\u5237\u65b0<\/p>\n<p>\u5e93\u51fd\u6570\u548c\u7cfb\u7edf\u8c03\u7528\u662f\u4e0a\u4e0b\u5c42\u7684\u5173\u7cfb&#xff0c;\u5e93\u51fd\u6570\u6ca1\u6709\u8fdb\u7a0b\u7ec8\u6b62\u80fd\u529b&#xff0c;\u53ea\u80fd\u8c03\u7528\u7cfb\u7edf\u8c03\u7528&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u7ed9\u5b83\u63d0\u4f9b\u7684\u8fdb\u7a0b\u7ec8\u6b62\u7684\u63a5\u53e3\u5b83\u624d\u80fd\u7ec8\u6b62\u8fdb\u7a0b&#xff0c;\u6240\u4ee5exit\u7684\u5e95\u5c42\u5c01\u88c5\u4e86_exit,\u6240\u4ee5\u6211\u4eec\u4e4b\u524d\u8c08\u8bba\u7684\u7f13\u51b2\u533a\u4e00\u5b9a\u4e0d\u5728\u64cd\u4f5c\u7cfb\u7edf\u7684\u5185\u90e8&#xff0c;\u800c\u662f\u5e93\u7f13\u51b2\u533a&#xff08;c\u8bed\u8a00\u63d0\u4f9b\u7684\u7f13\u51b2\u533a&#xff09;<\/p>\n<p>\u5f02\u5e38\u9000\u51fa&#xff1a;<\/p>\n<ul>\n<li>ctrl &#043; c&#xff0c;\u4fe1\u53f7\u7ec8\u6b62<\/li>\n<\/ul>\n<h5>2.2.4 \u9000\u51fa\u7801<\/h5>\n<p>\u9000\u51fa\u7801\u5728Linux\u4e2d\u901a\u5e38\u7528\u6765\u8868\u793a\u547d\u4ee4\u6267\u884c\u540e\u7684\u7ed3\u679c&#xff0c;0\u8868\u793a\u6210\u529f&#xff0c;\u975e0\u8868\u793a\u4e0d\u540c\u7684\u9519\u8bef\u7c7b\u578b Linux Shell \u4e2d\u7684\u4e3b\u8981\u9000\u51fa\u7801&#xff1a;<\/p>\n<table>\n<tr>\u9000\u51fa\u7801\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>0<\/td>\n<td>\u6210\u529f&#xff08;\u547d\u4ee4\u6b63\u5e38\u6267\u884c&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>\u4e00\u822c\u6027\u9519\u8bef&#xff08;\u5982\u53c2\u6570\u9519\u8bef\u3001\u6587\u4ef6\u672a\u627e\u5230\u3001\u6743\u9650\u4e0d\u8db3\u7b49&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>Shell \u5185\u7f6e\u547d\u4ee4\u8bef\u7528&#xff08;\u5982\u8bed\u6cd5\u9519\u8bef\u3001\u672a\u627e\u5230\u547d\u4ee4\u7b49&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>126<\/td>\n<td>\u6743\u9650\u95ee\u9898&#xff08;\u547d\u4ee4\u4e0d\u53ef\u6267\u884c&#xff0c;\u5982\u7f3a\u5c11\u6267\u884c\u6743\u9650&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>127<\/td>\n<td>\u547d\u4ee4\u672a\u627e\u5230&#xff08;Shell \u627e\u4e0d\u5230\u6307\u5b9a\u547d\u4ee4&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>130<\/td>\n<td>\u8fdb\u7a0b\u88ab Ctrl&#043;C \u7ec8\u6b62&#xff08;SIGINT \u4fe1\u53f7&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>141<\/td>\n<td>\u8fdb\u7a0b\u88ab SIGHUP \u4fe1\u53f7\u7ec8\u6b62&#xff08;\u5982\u7ec8\u7aef\u5173\u95ed&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>3. \u8fdb\u7a0b\u7b49\u5f85<\/h3>\n<h4>3.1 \u8fdb\u7a0b\u7b49\u5f85\u5fc5\u8981\u6027<\/h4>\n<ul>\n<li>\u4e4b\u524d\u8bb2\u8fc7&#xff0c;\u5b50\u8fdb\u7a0b\u9000\u51fa&#xff0c;\u7236\u8fdb\u7a0b\u5982\u679c\u4e0d\u7ba1\u4e0d\u987e&#xff0c;\u5c31\u53ef\u80fd\u9020\u6210\u50f5\u5c38\u8fdb\u7a0b\u7684\u95ee\u9898&#xff0c;\u8fdb\u800c\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\u3002<\/li>\n<li>\u8fdb\u7a0b\u4e00\u65e6\u53d8\u6210\u50f5\u5c38\u72b6\u6001&#xff0c;\u90a3\u5c31\u5200\u67aa\u4e0d\u5165&#xff0c;\u201c\u6740\u4eba\u4e0d\u7728\u773c\u201d\u7684kill-9\u4e5f\u65e0\u80fd\u4e3a\u529b&#xff0c;\u56e0\u4e3a\u8c01\u4e5f\u6ca1\u6709\u529e\u6cd5\u6740\u6b7b\u4e00\u4e2a\u5df2\u7ecf\u6b7b\u53bb\u7684\u8fdb\u7a0b\u3002<\/li>\n<li>\u6700\u540e&#xff0c;\u7236\u8fdb\u7a0b\u6d3e\u7ed9\u5b50\u8fdb\u7a0b\u7684\u4efb\u52a1\u5b8c\u6210\u7684\u5982\u4f55&#xff0c;\u6211\u4eec\u9700\u8981\u77e5\u9053\u3002\u5982&#xff0c;\u5b50\u8fdb\u7a0b\u8fd0\u884c\u5b8c\u6210&#xff0c;\u7ed3\u679c\u5bf9\u8fd8\u662f\u4e0d\u5bf9&#xff0c;\u6216\u8005\u662f\u5426\u6b63\u5e38\u9000\u51fa\u3002<\/li>\n<li>\u7236\u8fdb\u7a0b\u901a\u8fc7\u8fdb\u7a0b\u7b49\u5f85\u7684\u65b9\u5f0f&#xff0c;\u56de\u6536\u5b50\u8fdb\u7a0b\u8d44\u6e90&#xff0c;\u83b7\u53d6\u5b50\u8fdb\u7a0b\u9000\u51fa\u4fe1\u606f<\/li>\n<\/ul>\n<h4>3.2 \u8fdb\u7a0b\u7b49\u5f85\u7684\u65b9\u6cd5<\/h4>\n<h5>3.2.1 wait\u65b9\u6cd5<\/h5>\n<p>\u5982\u679c\u7b49\u5f85\u5b50\u8fdb\u7a0b&#xff0c;\u5b50\u8fdb\u7a0b\u6ca1\u6709\u9000\u51fa&#xff0c;\u7236\u8fdb\u7a0b\u5c31\u4f1a\u963b\u585e\u5728wait\u8c03\u7528\u5904&#xff08;\u76f8\u5f53\u4e8escanf&#xff09; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418121310-680241d6391e4.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418121310-680241d65fb70.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span><span class=\"token string\">&lt;sys\/types.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span><span class=\"token string\">&lt;sys\/wait.h&gt;<\/span><\/span><br \/>\npid_t <span class=\"token function\">wait<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span><span class=\"token operator\">*<\/span> status<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n\u8fd4\u56de\u503c&#xff1a;<br \/>\n \u6210\u529f\u8fd4\u56de\u88ab\u7b49\u5f85\u8fdb\u7a0bpid&#xff0c;\u5931\u8d25\u8fd4\u56de<span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span>\u3002<br \/>\n\u53c2\u6570&#xff1a;<br \/>\n \u8f93\u51fa\u578b\u53c2\u6570&#xff0c;\u83b7\u53d6\u5b50\u8fdb\u7a0b\u9000\u51fa\u72b6\u6001<span class=\"token punctuation\">,<\/span>\u4e0d\u5173\u2f3c\u5219\u53ef\u4ee5\u8bbe\u7f6e\u6210\u4e3a<span class=\"token constant\">NULL<\/span><\/p>\n<h5>3.2.2 waitpid\u65b9\u6cd5<\/h5>\n<p>pid_ t <span class=\"token function\">waitpid<\/span><span class=\"token punctuation\">(<\/span>pid_t pid<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> <span class=\"token operator\">*<\/span>status<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> options<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n\u8fd4\u56de\u503c&#xff1a;<br \/>\n \u5f53\u6b63\u5e38\u8fd4\u56de\u7684\u65f6\u5019waitpid\u8fd4\u56de\u6536\u96c6\u5230\u7684\u5b50\u8fdb\u7a0b\u7684\u8fdb\u7a0bID&#xff1b;<br \/>\n \u5982\u679c\u8bbe\u7f6e\u4e86\u9009\u9879WNOHANG<span class=\"token punctuation\">,<\/span>\u2f7d\u8c03\u2f64\u4e2dwaitpid\u53d1\u73b0\u6ca1\u6709\u5df2\u9000\u51fa\u7684\u5b50\u8fdb\u7a0b\u53ef\u6536\u96c6<span class=\"token punctuation\">,<\/span>\u5219\u8fd4\u56de<span class=\"token number\">0<\/span>&#xff1b;<br \/>\n \u5982\u679c\u8c03\u2f64\u4e2d\u51fa\u9519<span class=\"token punctuation\">,<\/span>\u5219\u8fd4\u56de<span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span>\u8fd9\u65f6errno\u4f1a\u88ab\u8bbe\u7f6e\u6210\u76f8\u5e94\u7684\u503c\u4ee5\u6307\u2f70\u9519\u8bef\u6240\u5728&#xff1b;<br \/>\n\u53c2\u6570&#xff1a;<br \/>\n pid&#xff1a;<br \/>\n Pid <span class=\"token operator\">&#061;<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span>\u7b49\u5f85\u4efb\u2f00\u4e2a\u5b50\u8fdb\u7a0b\u3002\u4e0ewait\u7b49\u6548\u3002<br \/>\n Pid <span class=\"token operator\">&gt;<\/span> <span class=\"token number\">0.<\/span>\u7b49\u5f85\u5176\u8fdb\u7a0bID\u4e0epid\u76f8\u7b49\u7684\u2f26\u8fdb\u7a0b\u3002<br \/>\n status<span class=\"token operator\">:<\/span> \u8f93\u51fa\u578b\u53c2\u6570<br \/>\n <span class=\"token function\">WIFEXITED<\/span><span class=\"token punctuation\">(<\/span>status<span class=\"token punctuation\">)<\/span><span class=\"token operator\">:<\/span> \u82e5\u4e3a\u6b63\u5e38\u7ec8\u2f4c\u5b50\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;<br \/>\n <span class=\"token function\">WEXITSTATUS<\/span><span class=\"token punctuation\">(<\/span>status<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span>status<span class=\"token operator\">&gt;&gt;<\/span><span class=\"token number\">8<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&amp;<\/span><span class=\"token number\">0xFF<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">:<\/span> \u82e5WIFEXITED\u2fae\u96f6&#xff0c;\u63d0\u53d6\u5b50\u8fdb\u7a0b\u9000\u51fa\u7801\u3002&#xff08;\u67e5\u770b\u8fdb\u7a0b\u7684\u9000\u51fa\u7801&#xff09;<br \/>\n options<span class=\"token operator\">:<\/span>\u9ed8\u8ba4\u4e3a<span class=\"token number\">0<\/span>&#xff0c;\u8868\u2f70\u963b\u585e\u7b49\u5f85<br \/>\n WNOHANG<span class=\"token operator\">:<\/span> \u82e5pid\u6307\u5b9a\u7684\u5b50\u8fdb\u7a0b\u6ca1\u6709\u7ed3\u675f&#xff0c;\u5219<span class=\"token function\">waitpid<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>\u51fd\u6570\u8fd4\u56de<span class=\"token number\">0<\/span>&#xff0c;\u4e0d\u4e88\u4ee5\u7b49<br \/>\n\u5f85\u3002\u82e5\u6b63\u5e38\u7ed3\u675f&#xff0c;\u5219\u8fd4\u56de\u8be5\u5b50\u8fdb\u7a0b\u7684ID\u3002<\/p>\n<ul>\n<li>\u5982\u679c\u5b50\u8fdb\u7a0b\u5df2\u7ecf\u9000\u51fa&#xff0c;\u8c03\u7528wait\/waitpid\u65f6&#xff0c;wait\/waitpid\u4f1a\u7acb\u5373\u8fd4\u56de&#xff0c;\u5e76\u4e14\u91ca\u653e\u8d44\u6e90&#xff0c;\u83b7\u5f97\u5b50\u8fdb\u7a0b\u9000\u51fa\u4fe1\u606f\u3002<\/li>\n<li>\u5982\u679c\u5728\u4efb\u610f\u65f6\u523b\u8c03\u7528wait\/waitpid&#xff0c;\u5b50\u8fdb\u7a0b\u5b58\u5728\u4e14\u6b63\u5e38\u8fd0\u884c&#xff0c;\u5219\u8fdb\u7a0b\u53ef\u80fd\u963b\u585e\u3002<\/li>\n<li>\u5982\u679c\u4e0d\u5b58\u5728\u8be5\u5b50\u8fdb\u7a0b&#xff0c;\u5219\u7acb\u5373\u51fa\u9519\u8fd4\u56de\u3002 <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418121310-680241d6852d2.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/li>\n<\/ul>\n<h5>3.2.3 \u83b7\u53d6\u5b50\u8fdb\u7a0bstatus<\/h5>\n<ul>\n<li>wait\u548cwaitpid&#xff0c;\u90fd\u6709\u4e00\u4e2astatus\u53c2\u6570&#xff0c;\u8be5\u53c2\u6570\u662f\u4e00\u4e2a\u8f93\u51fa\u578b\u53c2\u6570&#xff0c;\u7531\u64cd\u4f5c\u7cfb\u7edf\u586b\u5145\u3002<\/li>\n<li>\u5982\u679c\u4f20\u9012NULL&#xff0c;\u8868\u793a\u4e0d\u5173\u5fc3\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u72b6\u6001\u4fe1\u606f\u3002<\/li>\n<li>\u5426\u5219&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u6839\u636e\u8be5\u53c2\u6570&#xff0c;\u5c06\u5b50\u8fdb\u7a0b\u7684\u9000\u51fa\u4fe1\u606f\u53cd\u9988\u7ed9\u7236\u8fdb\u7a0b\u3002<\/li>\n<li>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\u6bd4\u7279\u4f4d&#xff09;&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418121310-680241d6c92af.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/li>\n<\/ul>\n<h5>3.2.4 \u963b\u585e\u4e0e\u975e\u963b\u585e\u7b49\u5f85<\/h5>\n<ul>\n<li>\u8fdb\u7a0b\u7684\u963b\u585e\u7b49\u5f85\u65b9\u5f0f&#xff1a;<\/li>\n<\/ul>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;sys\/wait.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;stdio.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;stdlib.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;string.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;errno.h&gt;<\/span><\/span><br \/>\n<span class=\"token keyword\">int<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">void<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    pid_t pid<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span>pid <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">fork<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;fork&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token function\">exit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>pid <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token function\">sleep<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">20<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">exit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">10<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> st<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> ret <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">wait<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>st<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>ret <span class=\"token operator\">&gt;<\/span> <span class=\"token number\">0<\/span> <span class=\"token operator\">&amp;&amp;<\/span> <span class=\"token punctuation\">(<\/span>st <span class=\"token operator\">&amp;<\/span> <span class=\"token number\">0X7F<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token comment\">\/\/ \u6b63\u5e38\u9000\u51fa <\/span><br \/>\n            <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;child exit code:%d\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">(<\/span>st <span class=\"token operator\">&gt;&gt;<\/span> <span class=\"token number\">8<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&amp;<\/span> <span class=\"token number\">0XFF<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token keyword\">else<\/span> <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>ret <span class=\"token operator\">&gt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token comment\">\/\/ \u5f02\u5e38\u9000\u51fa <\/span><br \/>\n            <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;sig code : %d\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> st <span class=\"token operator\">&amp;<\/span> <span class=\"token number\">0X7F<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n\u6d4b\u8bd5\u7ed3\u679c&#xff1a;<br \/>\n# <span class=\"token punctuation\">.<\/span><span class=\"token operator\">\/<\/span>a<span class=\"token punctuation\">.<\/span>out #\u7b49<span class=\"token number\">20<\/span>\u79d2\u9000\u51fa<br \/>\nchild exit code <span class=\"token operator\">:<\/span> <span class=\"token number\">10<\/span><br \/>\n# <span class=\"token punctuation\">.<\/span><span class=\"token operator\">\/<\/span>a<span class=\"token punctuation\">.<\/span>out #\u5728\u5176\u4ed6\u7ec8\u7aefkill\u6389<br \/>\nsig code <span class=\"token operator\">:<\/span> <span class=\"token number\">9<\/span><\/p>\n<ul>\n<li>\u8fdb\u7a0b\u7684\u975e\u963b\u585e\u7b49\u5f85\u65b9\u5f0f&#xff1a;<\/li>\n<\/ul>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;stdio.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;stdlib.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;sys\/wait.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;unistd.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;vector&gt;<\/span><\/span><br \/>\n<span class=\"token keyword\">typedef<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>handler_t<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u51fd\u6570\u6307\u9488\u7c7b\u578b <\/span><br \/>\nstd<span class=\"token operator\">::<\/span>vector<span class=\"token operator\">&lt;<\/span>handler_t<span class=\"token operator\">&gt;<\/span> handlers<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u51fd\u6570\u6307\u9488\u6570\u7ec4 <\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">fun_one<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u8fd9\u662f\u2f00\u4e2a\u4e34\u65f6\u4efb\u52a11\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">fun_two<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u8fd9\u662f\u2f00\u4e2a\u4e34\u65f6\u4efb\u52a12\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">Load<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    handlers<span class=\"token punctuation\">.<\/span><span class=\"token function\">push_back<\/span><span class=\"token punctuation\">(<\/span>fun_one<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    handlers<span class=\"token punctuation\">.<\/span><span class=\"token function\">push_back<\/span><span class=\"token punctuation\">(<\/span>fun_two<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">handler<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>handlers<span class=\"token punctuation\">.<\/span><span class=\"token function\">empty<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token function\">Load<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">for<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">auto<\/span> iter <span class=\"token operator\">:<\/span> handlers<span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token function\">iter<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">int<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    pid_t pid<span class=\"token punctuation\">;<\/span><br \/>\n    pid <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">fork<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>pid <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;%s fork error\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> __FUNCTION__<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">else<\/span> <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>pid <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token comment\">\/\/ child<\/span><br \/>\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;child is run, pid is : %d\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token function\">getpid<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">sleep<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">5<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">exit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> status <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        pid_t ret <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">do<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            ret <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">waitpid<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>status<span class=\"token punctuation\">,<\/span> WNOHANG<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u2fae\u963b\u585e\u5f0f\u7b49\u5f85 <\/span><br \/>\n            <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>ret <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;child is running\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><br \/>\n            <span class=\"token function\">handler<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">while<\/span> <span class=\"token punctuation\">(<\/span>ret <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token function\">WIFEXITED<\/span><span class=\"token punctuation\">(<\/span>status<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&amp;&amp;<\/span> ret <span class=\"token operator\">&#061;&#061;<\/span> pid<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;wait child 5s success, child return code is :%d.\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n                <span class=\"token function\">WEXITSTATUS<\/span><span class=\"token punctuation\">(<\/span>status<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;wait child failed, return.\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token keyword\">return<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>4. \u8fdb\u7a0b\u66ff\u6362<\/h3>\n<p>\u6211\u4eec\u5148\u6765\u770b\u4e00\u6bb5\u4ee3\u7801&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418121310-680241d6f0125.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418121311-680241d7245f5.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> \u4e0a\u9762\u8fd9\u79cd\u73b0\u8c61\u5c31\u53eb\u505a\u7a0b\u5e8f\u66ff\u6362&#xff0c;\u4e5f\u5c31\u662f\u6211\u81ea\u5df1\u7684\u7a0b\u5e8f\u628a\u7cfb\u7edf\u5f53\u4e2d\u7684\u6307\u4ee4\u8dd1\u8d77\u6765\u4e86\u3002\u5728\u7a0b\u5e8f\u66ff\u6362\u7684\u65f6\u5019&#xff0c;\u5e76\u6ca1\u6709\u521b\u5efa\u65b0\u7684\u8fdb\u7a0b&#xff0c;\u53ea\u662f\u628a\u5f53\u524d\u8fdb\u7a0b\u7684\u4ee3\u7801\u548c\u6570\u636e\u7528\u65b0\u7684\u8fdb\u7a0b\u7684\u4ee3\u7801\u548c\u6570\u636e\u8986\u76d6\u5f0f\u7684\u8fdb\u884c\u66ff\u6362\u3002<\/p>\n<li>\u95ee\u9898\u4e00&#xff1a;\u201c\u4e3a\u4ec0\u4e48\u6211\u7684\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u675f\u4e86\u201d&#xff0c;\u8fd9\u6bb5\u8bdd\u6ca1\u6709\u5728\u663e\u793a\u5668\u4e0a\u6253\u5370\u51fa\u6765&#xff1f; \u7b54\u6848\u662f\u4e00\u65e6\u7a0b\u5e8f\u66ff\u6362\u6210\u529f\u5c31\u53bb\u6267\u884c\u65b0\u4ee3\u7801\u4e86&#xff0c;\u539f\u59cb\u4ee3\u7801\u7684\u540e\u534a\u90e8\u5206\u5df2\u7ecf\u4e0d\u5b58\u5728\u4e86<\/li>\n<ul>\n<li>\u6709\u6ca1\u6709\u529e\u6cd5\u8ba9\u540e\u9762\u7684\u4ee3\u7801\u80fd\u7ee7\u7eed\u6267\u884c&#xff1f; \u7b54\u6848\u662f\u6709\u7684&#xff0c;\u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b&#xff0c;\u8ba9\u5b50\u8fdb\u7a0b\u53bb\u505a\u66ff\u6362\u5de5\u4f5c&#xff0c;\u8ba9\u7236\u8fdb\u7a0b\u7ee7\u7eed\u6267\u884c\u540e\u9762\u7684\u4ee3\u7801\u3002 <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418121311-680241d73d02b.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> \u6548\u679c\u6f14\u793a&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418121311-680241d782458.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> &#x1f4cc;Tips:\u7a0b\u5e8f\u66ff\u6362\u4e5f\u80fd\u66ff\u6362\u6211\u4eec\u81ea\u5df1\u5199\u7684\u7a0b\u5e8f&#xff0c;\u5c31\u76f8\u5f53\u4e8e\u4e00\u79cd\u52a0\u8f7d\u5668&#xff0c;\u53ef\u4ee5\u52a0\u8f7d\u5404\u79cd\u7a0b\u5e8f&#xff0c;\u5305\u62ec\u7f16\u8bd1\u578b\u7684\u89e3\u91ca\u578b\u7684&#xff0c;\u7a0b\u5e8f\u66ff\u6362\u672c\u8d28\u4e0a\u4e0d\u4f1a\u521b\u5efa\u65b0\u7684\u8fdb\u7a0b \u4e3a\u4ec0\u4e48\u4e0d\u4f1a\u5f71\u54cd\u7236\u8fdb\u7a0b\u5462&#xff1f; a.\u8fdb\u7a0b\u5177\u6709\u72ec\u7acb\u6027 b.\u6570\u636e\u548c\u4ee3\u7801\u53d1\u751f\u5199\u65f6\u62f7\u8d1d<\/li>\n<\/ul>\n<li>execl\u7684\u8fd4\u56de\u503c <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418121311-680241d79d019.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> execl\u51fd\u6570\u53ea\u6709\u5931\u8d25\u8fd4\u56de\u503c&#xff0c;\u6ca1\u6709\u6210\u529f\u8fd4\u56de\u503c &#x1f4a6;\u7ed3\u8bba&#xff1a;exec*\u7cfb\u5217\u7684\u51fd\u6570&#xff0c;\u4e0d\u7528\u505a\u8fd4\u56de\u503c\u5224\u5b9a&#xff0c;\u53ea\u8981\u8fd4\u56de&#xff0c;\u5c31\u662f\u5931\u8d25<\/li>\n<h4>4.1 \u66ff\u6362\u539f\u7406<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418121311-680241d7c07c1.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h4>4.2 \u66ff\u6362\u51fd\u6570<\/h4>\n<li>\n<p>int execl(const char *path,const char *arg,&#8230;),\u7b2c\u4e00\u4e2a\u53c2\u6570\u8868\u793a\u7a0b\u5e8f&#043;\u8def\u5f84\u540d&#xff0c;\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6709\u4e2a\u53e3\u8bc0&#xff1a;\u547d\u4ee4\u884c\u600e\u4e48\u5199&#xff0c;\u6211\u4eec\u5c31\u600e\u4e48\u4f20&#xff08;\u5f53\u7136\u4f20-al\u4e5f\u662f\u53ef\u4ee5\u7684&#xff09;&#xff0c;\u800c\u628a\u53c2\u6570\u4e00\u4e2a\u4e00\u4e2a\u7684\u4f20\u8fdb\u6765\u6211\u4eec\u79f0\u4e4b\u4e3alist&#xff0c;\u7c7b\u4f3c\u4e8e\u4ee5\u94fe\u8868\u7684\u5f62\u5f0f\u4f20\u7ed9\u5b83&#xff0c;\u6240\u4ee5execl\u8fd9\u4e2al\u5c31\u662fllist\u7684\u610f\u601d&#xff0c;execl\u51fd\u6570\u7684\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u5fc5\u987b\u4ee5NULL\u7ed3\u5c3e&#xff0c;\u8868\u660e\u53c2\u6570\u4f20\u9012\u5b8c\u6210<img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418121312-680241d8117d5.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<\/li>\n<li>\n<p>int execlp(const char *file,cont char *arg,&#8230;),execlp\u5f53\u4e2d\u7684p\u8868\u793aPATH&#xff0c;\u6240\u4ee5\u7b2c\u4e00\u4e2a\u53c2\u6570\u53ea\u9700\u4f20\u8981\u6267\u884c\u7684\u7a0b\u5e8f\u540d\u5c31\u884c\u4e86&#xff0c;\u56e0\u4e3aexeclp\u4f1a\u81ea\u52a8\u7684\u5728\u73af\u5883\u53d8\u91cf&#xff08;PATH&#xff09;\u91cc\u67e5\u627e\u5bf9\u5e94\u7684\u547d\u4ee4&#xff0c;\u6240\u4ee5execlp\u4e00\u822c\u6267\u884c\u7cfb\u7edf\u7ea7\u7684\u547d\u4ee4\u3002\u540e\u9762\u53c2\u6570\u7684\u4f20\u9012\u548c\u4e0a\u9762\u7684\u76f8\u540c <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418121312-680241d83eaa5.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<\/li>\n<li>\n<p>int execv(const char *path,char *const argv[]),\u9996\u5148\u5b83\u6ca1\u6709\u5e26p\u6240\u4ee5\u5b83\u7684\u53c2\u6570\u662fpath,\u6240\u4ee5\u540c\u4e0a\u4e0a&#xff0c;\u8fd9\u91cc\u7684v\u5c31\u76f8\u5f53\u4e8evector&#xff0c;\u6240\u4ee5\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5c31\u4ee5\u6570\u7ec4\u7684\u5f62\u5f0f\u5448\u73b0\u4e86&#xff0c;\u6240\u4ee5\u5c31\u5fc5\u987b\u63d0\u4f9b\u4e00\u4e2a\u547d\u4ee4\u884c\u53c2\u6570\u8868&#xff0c;\u5c31\u662f\u6307\u9488\u6570\u7ec4&#xff0c;\u5c31\u662f\u628als -a -l\u6574\u4f53\u653e\u5728\u6570\u7ec4\u91cc&#xff0c;\u4e00\u6b21\u6027\u4f20\u9012&#xff0c;\u8fd9\u4e2a\u8868\u4e5f\u5fc5\u987b\u4ee5NULL\u7ed3\u5c3e\u3002\u6240\u4ee5\u6211\u4eec\u4ee5\u524d\u6267\u884c\u7684\u6240\u6709\u547d\u4ee4\u884c\u53c2\u6570\u90fd\u662f\u7236\u8fdb\u7a0b\u901a\u8fc7execv\u4f20\u7ed9\u5b50\u8fdb\u7a0b\u7684 <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418121312-680241d86cf42.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<\/li>\n<li>\n<p>int execvp(const char *path,char *const argv[]),\u6709p\u6240\u4ee5\u4e0d\u7528\u5e26\u8def\u5f84 <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418121312-680241d886e37.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<\/li>\n<li>\n<p>int execvpe(const *file,char *const argv[],char *const envp[])\u8fd9\u91cc\u7684v\u8868\u793a\u4ee5\u6570\u7ec4\u7684\u65b9\u5f0f\u4f20\u8fdb\u6765&#xff0c;p\u8868\u793a\u4e0d\u7528\u5e26\u8def\u5f84&#xff0c;e\u8868\u793a\u73af\u5883\u53d8\u91cf&#xff0c;\u5982\u679c\u975e\u8981\u4f20\u9012\u73af\u5883\u53d8\u91cf\u5217\u8868&#xff0c;\u8981\u6c42&#xff1a;\u88ab\u66ff\u6362\u7684\u5b50\u8fdb\u7a0b\u4f7f\u7528\u5168\u65b0\u7684Env\u5217\u8868&#xff08;\u81ea\u5df1\u5199\u7684&#xff09; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418121312-680241d8956e8.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> \u82e5\u8981\u4ee5\u65b0\u589e\u7684\u65b9\u5f0f\u4f20\u9012\u73af\u5883\u5217\u8868\u5462&#xff1f; \u2780putenv\u8868\u793a\u54ea\u4e2a\u8fdb\u7a0b\u8c03\u7528\u5b83&#xff0c;\u5c31\u5728\u8c01\u7684\u73af\u5883\u53d8\u91cf\u8868\u91cc\u65b0\u589e\u4e00\u4e2a\u73af\u5883\u53d8\u91cf&#xff08;B\u662fA\u7684\u5b50\u8fdb\u7a0b&#xff0c;C\u662fB\u7684\u5b50\u8fdb\u7a0b&#xff0c;\u5982\u679cB\u5728\u5b83\u7684\u73af\u5883\u5217\u8868\u91cc\u5bfc\u5165\u4e86\u4e00\u4e2a\u65b0\u7684\u73af\u5883\u53d8\u91cf&#xff0c;A\u7684\u73af\u5883\u5217\u8868\u91cc\u770b\u4e0d\u5230&#xff0c;\u800cC\u7684\u73af\u5883\u5217\u8868\u91cc\u80fd\u770b\u5230&#xff09; \u2781\u5982\u679c\u6211\u4eec\u5c31\u884c\u7528execvpe\u7684\u65b9\u5f0f\u5462&#xff1f;environ <img decoding=\"async\" src=\"2025-04-18rupvri4n32c.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> \u8868\u793a\u628a\u65b0\u589e\u7684\u73af\u5883\u53d8\u91cf\u6dfb\u52a0\u5230\u73af\u5883\u53d8\u91cf\u8868\u91cc\u9762\u53bb&#xff0c;\u7136\u540e\u628a\u73af\u5883\u53d8\u91cf\u8868\u7684\u8d77\u59cb\u5730\u5740\u4f20\u7ed9execvpe<\/p>\n<\/li>\n<li>\n<p>int execle(const *path,const *arg,&#8230;,char * const envp[]) <img decoding=\"async\" src=\"2025-04-183ff32iyoijr.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<\/li>\n<p>\u603b\u7ed3&#xff1a; \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):\u8868\u793a\u53c2\u6570\u91c7\u7528\u5217\u8868<\/li>\n<li>v(vector):\u53c2\u6570\u7528\u6570\u7ec4<\/li>\n<li>p(path):\u6709p\u81ea\u52a8\u641c\u7d22\u73af\u5883\u53d8\u91cfPATH<\/li>\n<li>e(env):\u8868\u793a\u81ea\u5df1\u7ef4\u62a4\u73af\u5883\u53d8\u91cf <img decoding=\"async\" src=\"2025-04-18wqgn1sdgqr3.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/li>\n<\/ul>\n<p>\u4e0a\u9762\u8fd9\u4e9b\u51fd\u6570\u90fd\u662f\u5bf9\u7cfb\u7edf\u8c03\u7528\u8fdb\u884c\u4e86\u8bed\u8a00\u578b\u7684\u5c01\u88c5&#xff0c;\u6700\u540e\u90fd\u8981\u8c03\u7528\u7cfb\u7edf\u8c03\u7528execve,\u4e3a\u4ec0\u4e48\u8981\u505a\u8bed\u8a00\u5c01\u88c5\u5462&#xff1f;\u56e0\u4e3a\u7a0b\u5e8f\u66ff\u6362\u65f6\u8981\u9762\u5bf9\u5404\u79cd\u5404\u6837\u4e0a\u5c42\u66ff\u6362\u7684\u573a\u666f\u3002\u6240\u4ee5execve\u5728man\u624b\u518c\u7b2c2\u8282 <img decoding=\"async\" src=\"2025-04-18ydtqtwpeglv.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> \u4e0b\u56feexec\u51fd\u6570\u7c07\u4e00\u4e2a\u5b8c\u6574\u7684\u4f8b\u5b50: <img decoding=\"async\" src=\"2025-04-18saptm00zwrx.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<hr \/>\n<p>&#x1f44d; \u5982\u679c\u5bf9\u4f60\u6709\u5e2e\u52a9&#xff0c;\u6b22\u8fce&#xff1a;<\/p>\n<ul>\n<li>\u70b9\u8d5e \u2b50\ufe0f<\/li>\n<li>\u6536\u85cf &#x1f4cc;<\/li>\n<li>\u5173\u6ce8 &#x1f514;<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.6k\u6b21\uff0c\u70b9\u8d5e96\u6b21\uff0c\u6536\u85cf86\u6b21\u3002\u672c\u7bc7\u6587\u7ae0\u4ecb\u7ecd\u4e86linux\u91cc\u9762\u7684\u8fdb\u7a0b\u521b\u5efa\uff0c\u8fdb\u7a0b\u7ec8\u6b62\uff0c\u8fdb\u7a0b\u7b49\u5f85\uff0c\u8fdb\u7a0b\u66ff\u6362\u7b49\u7ec6\u8282\u95ee\u9898<\/p>\n","protected":false},"author":2,"featured_media":13085,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,43,44],"topic":[],"class_list":["post-13105","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-linux","tag-43","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010Linux\u7bc7\u3011\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\/13105.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010Linux\u7bc7\u3011\u8fdb\u7a0b\u63a7\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.6k\u6b21\uff0c\u70b9\u8d5e96\u6b21\uff0c\u6536\u85cf86\u6b21\u3002\u672c\u7bc7\u6587\u7ae0\u4ecb\u7ecd\u4e86linux\u91cc\u9762\u7684\u8fdb\u7a0b\u521b\u5efa\uff0c\u8fdb\u7a0b\u7ec8\u6b62\uff0c\u8fdb\u7a0b\u7b49\u5f85\uff0c\u8fdb\u7a0b\u66ff\u6362\u7b49\u7ec6\u8282\u95ee\u9898\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/13105.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-18T12:13:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418121309-680241d5166e0.webp\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/13105.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/13105.html\",\"name\":\"\u3010Linux\u7bc7\u3011\u8fdb\u7a0b\u63a7\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T12:13:13+00:00\",\"dateModified\":\"2025-04-18T12:13:13+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/13105.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/13105.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/13105.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010Linux\u7bc7\u3011\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":"\u3010Linux\u7bc7\u3011\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\/13105.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010Linux\u7bc7\u3011\u8fdb\u7a0b\u63a7\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.6k\u6b21\uff0c\u70b9\u8d5e96\u6b21\uff0c\u6536\u85cf86\u6b21\u3002\u672c\u7bc7\u6587\u7ae0\u4ecb\u7ecd\u4e86linux\u91cc\u9762\u7684\u8fdb\u7a0b\u521b\u5efa\uff0c\u8fdb\u7a0b\u7ec8\u6b62\uff0c\u8fdb\u7a0b\u7b49\u5f85\uff0c\u8fdb\u7a0b\u66ff\u6362\u7b49\u7ec6\u8282\u95ee\u9898","og_url":"https:\/\/www.wsisp.com\/helps\/13105.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T12:13:13+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418121309-680241d5166e0.webp"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/13105.html","url":"https:\/\/www.wsisp.com\/helps\/13105.html","name":"\u3010Linux\u7bc7\u3011\u8fdb\u7a0b\u63a7\u5236 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T12:13:13+00:00","dateModified":"2025-04-18T12:13:13+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/13105.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/13105.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/13105.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010Linux\u7bc7\u3011\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\/13105","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=13105"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/13105\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/13085"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=13105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=13105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=13105"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=13105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}