{"id":83545,"date":"2026-07-26T01:40:51","date_gmt":"2026-07-25T17:40:51","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/83545.html"},"modified":"2026-07-26T01:40:51","modified_gmt":"2026-07-25T17:40:51","slug":"%e4%bb%8e0%e5%bc%80%e5%a7%8b%e7%9a%84%e6%93%8d%e4%bd%9c%e7%b3%bb%e7%bb%9f%ef%bc%884%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/83545.html","title":{"rendered":"\u4ece0\u5f00\u59cb\u7684\u64cd\u4f5c\u7cfb\u7edf\uff084\uff09"},"content":{"rendered":"<h2>\u3010\u64cd\u4f5c\u7cfb\u7edf\u3011\u7a0b\u5e8f\u4e0e\u8fdb\u7a0b&#xff1a;\u4ece\u72b6\u6001\u673a\u5230 fork\u3001execve \u4e0e\u8fdb\u7a0b\u751f\u547d\u5468\u671f<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260725174050-6a64f52202269.png\" alt=\"\u5934\u50cf\" \/><\/p>\n<p> <font color=\"#FF4D4F\" size=\"4\"><b>&#x1f525; \u661f\u6052\u968f\u98ce&#xff1a;<\/b><\/font><br \/>\n <font color=\"#FF4D4F\" size=\"4\"><b>\u4e2a\u4eba\u4e3b\u9875<\/b><\/font><\/p>\n<p> <font color=\"#66CCFF\" size=\"3\"><b>\u2744\ufe0f \u4e2a\u4eba\u4e13\u680f&#xff1a;<\/b><\/font><br \/>\n <font color=\"#66CCFF\" size=\"3\"><b>\u300a\u6307\u9488\u5408\u96c6\u300b<\/b><\/font><br \/>\n <font color=\"#333333\" size=\"3\"><b>&#xff5c;<\/b><\/font><br \/>\n <font color=\"#66CCFF\" size=\"3\"><b>\u300aC\u8bed\u8a00\u57fa\u7840\u300b<\/b><\/font><br \/>\n <font color=\"#333333\" size=\"3\"><b>&#xff5c;<\/b><\/font><br \/>\n <font color=\"#66CCFF\" size=\"3\"><b>\u300a\u6570\u636e\u7ed3\u6784\u300b<\/b><\/font><br \/>\n <font color=\"#333333\" size=\"3\"><b>&#xff5c;<\/b><\/font><br \/>\n <font color=\"#66CCFF\" size=\"3\"><b>\u300a\u673a\u5668\u5b66\u4e60\u5bfc\u8bba\u300b<\/b><\/font><br \/>\n <font color=\"#333333\" size=\"3\"><b>&#xff5c;<\/b><\/font><br \/>\n <font color=\"#66CCFF\" size=\"3\"><b>\u300a\u524d\u7aef\u57fa\u7840\u300b<\/b><\/font><br \/>\n <font color=\"#333333\" size=\"3\"><b>&#xff5c;<\/b><\/font><br \/>\n <font color=\"#66CCFF\" size=\"3\"><b>\u300apython\u57fa\u7840\u300b<\/b><\/font><br \/>\n <font color=\"#333333\" size=\"3\"><b>&#xff5c;<\/b><\/font><br \/>\n <font color=\"#66CCFF\" size=\"3\"><b>\u300aC&#043;&#043;\u4ece\u5165\u95e8\u5230\u5165\u571f\u300b<\/b><\/font> <\/p>\n<p> <font color=\"#F5B400\" size=\"5\"><b>\u2728 \u6570\u636e\u5373\u77e5\u8bc6&#xff0c;\u538b\u7f29\u5373\u667a\u80fd<\/b><\/font><\/p>\n<hr \/>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>\u3010\u64cd\u4f5c\u7cfb\u7edf\u3011\u7a0b\u5e8f\u4e0e\u8fdb\u7a0b&#xff1a;\u4ece\u72b6\u6001\u673a\u5230 fork\u3001execve \u4e0e\u8fdb\u7a0b\u751f\u547d\u5468\u671f<\/li>\n<li>\n<ul>\n<li>\u58f0\u660e<\/li>\n<li>\u4e00\u3001\u4ece\u865a\u62df\u5316\u5f00\u59cb&#xff1a;\u4e3a\u4ec0\u4e48\u6bcf\u4e2a\u7a0b\u5e8f\u90fd\u50cf\u72ec\u5360\u4e86\u4e00\u53f0\u8ba1\u7b97\u673a&#xff1f;<\/li>\n<li>\u4e8c\u3001\u7a0b\u5e8f\u548c\u8fdb\u7a0b\u5230\u5e95\u6709\u4ec0\u4e48\u533a\u522b&#xff1f;<\/li>\n<li>\n<ul>\n<li>1. \u4e00\u4e2a\u8fdb\u7a0b\u5305\u542b\u4ec0\u4e48&#xff1f;<\/li>\n<li>2. \u8fdb\u7a0b\u4e0d\u662f\u7a0b\u5e8f\u6587\u4ef6\u7684\u201c\u53e6\u4e00\u4e2a\u540d\u5b57\u201d<\/li>\n<\/ul>\n<\/li>\n<li>\u4e09\u3001\u8fdb\u7a0b\u4e3a\u4ec0\u4e48\u4f1a\u6709\u4e0d\u540c\u72b6\u6001&#xff1f;<\/li>\n<li>\u56db\u3001\u5728 Linux \u4e2d\u89c2\u5bdf\u4e00\u4e2a\u771f\u5b9e\u8fdb\u7a0b<\/li>\n<li>\u4e94\u3001UNIX \u5982\u4f55\u7ba1\u7406\u8fdb\u7a0b&#xff1a;\u590d\u5236\u3001\u91cd\u7f6e\u548c\u9500\u6bc1\u72b6\u6001\u673a<\/li>\n<li>\u516d\u3001fork&#xff1a;\u590d\u5236\u4e00\u4e2a\u6b63\u5728\u8fd0\u884c\u7684\u8fdb\u7a0b<\/li>\n<li>\n<ul>\n<li>1. \u201c\u590d\u5236\u6574\u4e2a\u5185\u5b58\u201d\u4f1a\u4e0d\u4f1a\u5f88\u6162&#xff1f;<\/li>\n<li>2. fork \u5230\u5e95\u590d\u5236\u548c\u7ee7\u627f\u4e86\u4ec0\u4e48&#xff1f;<\/li>\n<li>3. fork \u4e4b\u540e\u4e3a\u4ec0\u4e48\u4f1a\u5f62\u6210\u8fdb\u7a0b\u6811&#xff1f;<\/li>\n<li>4. \u4e3a\u4ec0\u4e48 fork \u4f1a\u5e26\u6765\u6307\u6570\u589e\u957f&#xff1f;<\/li>\n<li>5. fork \u7684\u5b9e\u9645\u7528\u9014<\/li>\n<\/ul>\n<\/li>\n<li>\u4e03\u3001\u4e00\u9053\u770b\u4f3c\u7b80\u5355&#xff0c;\u5374\u5f88\u5bb9\u6613\u7b54\u9519\u7684 fork \u9898<\/li>\n<li>\n<ul>\n<li>1. \u5173\u952e\u4e0d\u5728 fork&#xff0c;\u800c\u5728 stdio \u7f13\u51b2<\/li>\n<\/ul>\n<\/li>\n<li>\u516b\u3001execve&#xff1a;\u4e0d\u662f\u521b\u5efa\u8fdb\u7a0b&#xff0c;\u800c\u662f\u66ff\u6362\u5f53\u524d\u8fdb\u7a0b<\/li>\n<li>\n<ul>\n<li>1. execve \u7684\u4e09\u4e2a\u53c2\u6570<\/li>\n<li>\n<ul>\n<li>path&#xff1a;\u53ef\u6267\u884c\u6587\u4ef6\u8def\u5f84<\/li>\n<li>argv&#xff1a;\u547d\u4ee4\u884c\u53c2\u6570<\/li>\n<li>envp&#xff1a;\u73af\u5883\u53d8\u91cf<\/li>\n<\/ul>\n<\/li>\n<li>2. \u54ea\u4e9b\u72b6\u6001\u88ab\u66ff\u6362&#xff0c;\u54ea\u4e9b\u72b6\u6001\u4f1a\u4fdd\u7559&#xff1f;<\/li>\n<li>3. execve \u4e0e execl\u3001execvp \u6709\u4ec0\u4e48\u5173\u7cfb&#xff1f;<\/li>\n<li>4. PATH \u4e3a\u4ec0\u4e48\u4f1a\u5f71\u54cd\u7a0b\u5e8f\u6267\u884c&#xff1f;<\/li>\n<\/ul>\n<\/li>\n<li>\u4e5d\u3001wait \u548c waitpid&#xff1a;\u7236\u8fdb\u7a0b\u5982\u4f55\u7b49\u5f85\u5e76\u56de\u6536\u5b50\u8fdb\u7a0b&#xff1f;<\/li>\n<li>\u5341\u3001exit \u4e0e _exit&#xff1a;\u770b\u8d77\u6765\u76f8\u4f3c&#xff0c;\u884c\u4e3a\u5e76\u4e0d\u76f8\u540c<\/li>\n<li>\u5341\u4e00\u3001\u4e00\u4e2a\u5b8c\u6574\u7684 fork &#043; execve &#043; waitpid<\/li>\n<li>\u5341\u4e8c\u3001\u4e3a\u4ec0\u4e48 UNIX \u8981\u628a fork \u548c exec \u5206\u5f00&#xff1f;<\/li>\n<li>\u5341\u4e09\u3001\u7528 strace \u89c2\u5bdf\u8fdb\u7a0b\u751f\u547d\u5468\u671f<\/li>\n<li>\u5341\u56db\u3001\u7406\u89e3\u72b6\u6001\u673a<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>\u58f0\u660e<\/h3>\n<p>\u672c\u6587\u6839\u636e\u5357\u4eac\u5927\u5b66 JYY\u300a\u64cd\u4f5c\u7cfb\u7edf\u300b2026 \u6625\u5b63\u8bfe\u7a0b Lecture 5\u201c\u7a0b\u5e8f\u548c\u8fdb\u7a0b\u201d\u6574\u7406&#xff0c;\u5e76\u53c2\u8003 Operating Systems: Three Easy Pieces&#xff08;OSTEP&#xff09;\u7b2c 4\u30015 \u7ae0\u53ca Linux \u5b98\u65b9\u624b\u518c\u8fdb\u884c\u8865\u5145\u3002\u5404\u4f4d\u611f\u5174\u8da3\u7684\u8bdd\u53ef\u4ee5\u4e0a\u5b98\u7f51\u770b\u770b<\/p>\n<p>\u8bfe\u7a0b\u7f51\u7ad9 https:\/\/jyywiki.cn<\/p>\n<hr \/>\n<h3>\u4e00\u3001\u4ece\u865a\u62df\u5316\u5f00\u59cb&#xff1a;\u4e3a\u4ec0\u4e48\u6bcf\u4e2a\u7a0b\u5e8f\u90fd\u50cf\u72ec\u5360\u4e86\u4e00\u53f0\u8ba1\u7b97\u673a&#xff1f;<\/h3>\n<p>\u7ad9\u5728\u5e94\u7528\u7a0b\u5e8f\u7684\u89d2\u5ea6\u770b&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u53ef\u4ee5\u7406\u89e3\u6210\u201c\u5bf9\u8c61 &#043; API\u201d&#xff1a;<\/p>\n<ul>\n<li>\u6587\u4ef6\u662f\u5bf9\u8c61&#xff0c;\u53ef\u4ee5\u901a\u8fc7 open\u3001read\u3001write \u7b49\u63a5\u53e3\u8bbf\u95ee&#xff1b;<\/li>\n<li>\u8fdb\u7a0b\u662f\u5bf9\u8c61&#xff0c;\u53ef\u4ee5\u901a\u8fc7 fork\u3001execve\u3001waitpid\u3001_exit \u7b49\u63a5\u53e3\u7ba1\u7406&#xff1b;<\/li>\n<li>\u865a\u62df\u5185\u5b58\u3001\u7f51\u7edc\u5957\u63a5\u5b57\u4e5f\u90fd\u53ef\u4ee5\u7528\u7c7b\u4f3c\u7684\u65b9\u5f0f\u7406\u89e3\u3002<\/li>\n<\/ul>\n<p>\u7ad9\u5728\u786c\u4ef6\u7684\u89d2\u5ea6\u770b&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u672c\u8eab\u53c8\u53ea\u662f\u4e00\u4e2a\u7a0b\u5e8f\u3002\u5b83\u4ece\u673a\u5668\u542f\u52a8\u540e\u7684\u67d0\u4e2a\u5165\u53e3\u5f00\u59cb\u6267\u884c&#xff0c;\u7ba1\u7406 CPU\u3001\u5185\u5b58\u548c\u5404\u79cd\u5916\u90e8\u8bbe\u5907\u3002<\/p>\n<p>\u8fd9\u4e24\u79cd\u89c6\u89d2\u6700\u7ec8\u5728\u201c\u865a\u62df\u5316\u201d\u4e0a\u6c47\u5408&#xff1a;<\/p>\n<p>\u64cd\u4f5c\u7cfb\u7edf\u628a\u4e00\u53f0\u771f\u5b9e\u7684\u7269\u7406\u8ba1\u7b97\u673a&#xff0c;\u62bd\u8c61\u6210\u8bb8\u591a\u53f0\u5f7c\u6b64\u9694\u79bb\u7684\u201c\u865a\u62df\u8ba1\u7b97\u673a\u201d\u3002<\/p>\n<p>\u6211\u4eec\u660e\u660e\u53ea\u6709\u6709\u9650\u6570\u91cf\u7684 CPU \u6838\u5fc3&#xff0c;\u4f46\u6d4f\u89c8\u5668\u3001\u7f16\u8f91\u5668\u3001\u7ec8\u7aef\u3001\u97f3\u4e50\u64ad\u653e\u5668\u5374\u597d\u50cf\u90fd\u5728\u540c\u65f6\u8fd0\u884c\u3002\u539f\u56e0\u5e76\u4e0d\u662f\u6bcf\u4e2a\u7a0b\u5e8f\u771f\u7684\u62e5\u6709\u4e00\u9897 CPU&#xff0c;\u800c\u662f\u64cd\u4f5c\u7cfb\u7edf\u4e0d\u65ad\u8ba9\u4e0d\u540c\u8fdb\u7a0b\u8f6e\u6d41\u8fd0\u884c&#xff1a;<\/p>\n<p><span class=\"token keyword\">while<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    Process <span class=\"token operator\">*<\/span>p <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">choose_one_ready_process<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    p<span class=\"token operator\">-&gt;<\/span><span class=\"token function\">run_for_a_while<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token function\">handle_event_or_syscall<\/span><span class=\"token punctuation\">(<\/span>p<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5f53\u7136\u53ea\u662f\u6982\u5ff5\u6a21\u578b\u3002\u771f\u5b9e\u64cd\u4f5c\u7cfb\u7edf\u901a\u5e38\u4e0d\u4f1a\u9010\u6761\u89e3\u91ca\u5e94\u7528\u7a0b\u5e8f\u6307\u4ee4&#xff0c;\u800c\u662f\u8ba9\u7a0b\u5e8f\u76f4\u63a5\u5728 CPU \u4e0a\u6267\u884c&#xff0c;\u518d\u501f\u52a9\u65f6\u949f\u4e2d\u65ad\u3001\u5f02\u5e38\u3001\u7cfb\u7edf\u8c03\u7528\u548c\u4e0a\u4e0b\u6587\u5207\u6362\u91cd\u65b0\u83b7\u5f97\u63a7\u5236\u6743\u3002<\/p>\n<p>\u4f46\u8fd9\u4e2a\u770b\u4f3c\u7b80\u5355\u7684\u5faa\u73af&#xff0c;\u5df2\u7ecf\u63ed\u793a\u4e86 CPU \u865a\u62df\u5316\u7684\u6838\u5fc3&#xff1a;<\/p>\n<li>\u64cd\u4f5c\u7cfb\u7edf\u4fdd\u5b58\u591a\u4e2a\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001&#xff1b;<\/li>\n<li>\u6bcf\u6b21\u9009\u62e9\u4e00\u4e2a\u53ef\u4ee5\u8fd0\u884c\u7684\u7a0b\u5e8f&#xff1b;<\/li>\n<li>\u8ba9\u5b83\u6267\u884c\u4e00\u6bb5\u65f6\u95f4&#xff1b;<\/li>\n<li>\u4fdd\u5b58\u5b83\u7684\u65b0\u72b6\u6001&#xff0c;\u518d\u5207\u6362\u5230\u53e6\u4e00\u4e2a\u7a0b\u5e8f\u3002<\/li>\n<p>OSTEP \u5c06\u8fd9\u79cd\u65b9\u6cd5\u79f0\u4e3a time sharing&#xff08;\u65f6\u95f4\u5171\u4eab&#xff09;\u3002\u64cd\u4f5c\u7cfb\u7edf\u901a\u8fc7\u5728\u65f6\u95f4\u4e0a\u5207\u5206 CPU&#xff0c;\u8ba9\u591a\u4e2a\u8fdb\u7a0b\u90fd\u4ea7\u751f\u201c\u81ea\u5df1\u62e5\u6709 CPU\u201d\u7684\u9519\u89c9\u3002<\/p>\n<hr \/>\n<h3>\u4e8c\u3001\u7a0b\u5e8f\u548c\u8fdb\u7a0b\u5230\u5e95\u6709\u4ec0\u4e48\u533a\u522b&#xff1f;<\/h3>\n<p>\u8fd9\u662f\u672c\u8282\u8bfe\u6700\u5148\u9700\u8981\u8bf4\u6e05\u695a\u7684\u95ee\u9898\u3002<\/p>\n<p>\u5047\u8bbe\u78c1\u76d8\u4e2d\u6709\u4e0b\u9762\u8fd9\u6837\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6&#xff1a;<\/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><\/p>\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> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">while<\/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\">write<\/span><span class=\"token punctuation\">(<\/span>STDOUT_FILENO<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;Hello, World!\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">14<\/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><\/p>\n<p>\u5f53\u5b83\u9759\u9759\u5730\u8eba\u5728\u78c1\u76d8\u4e2d\u65f6&#xff0c;\u5b83\u662f\u4e00\u4e2a\u7a0b\u5e8f&#xff08;program&#xff09;&#xff1b;\u5f53\u64cd\u4f5c\u7cfb\u7edf\u628a\u5b83\u52a0\u8f7d\u5230\u5185\u5b58\u5e76\u5f00\u59cb\u6267\u884c\u540e&#xff0c;\u5b83\u5c31\u6210\u4e3a\u4e00\u4e2a\u8fdb\u7a0b&#xff08;process&#xff09;\u3002<\/p>\n<p>\u53ef\u4ee5\u4ece\u72b6\u6001\u673a\u7684\u89d2\u5ea6\u8fdb\u4e00\u6b65\u7406\u89e3&#xff1a;<\/p>\n<ul>\n<li>\u7a0b\u5e8f\u63cf\u8ff0\u4e86\u521d\u59cb\u72b6\u6001\u4ee5\u53ca\u6bcf\u4e00\u6b65\u5982\u4f55\u8fc1\u79fb&#xff1b;<\/li>\n<li>\u8fdb\u7a0b\u662f\u8fd9\u4e2a\u72b6\u6001\u673a\u6b63\u5728\u8fd0\u884c\u7684\u4e00\u4e2a\u5b9e\u4f8b&#xff1b;<\/li>\n<li>\u540c\u4e00\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u540c\u65f6\u8fd0\u884c\u591a\u6b21&#xff0c;\u4ece\u800c\u4ea7\u751f\u591a\u4e2a\u5f7c\u6b64\u72ec\u7acb\u7684\u8fdb\u7a0b\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982\u540c\u65f6\u6253\u5f00\u4e09\u4e2a\u7ec8\u7aef&#xff0c;\u5e76\u5728\u5176\u4e2d\u5206\u522b\u8fd0\u884c\u540c\u4e00\u4e2a\u7a0b\u5e8f&#xff0c;\u78c1\u76d8\u4e0a\u4ecd\u7136\u53ea\u6709\u4e00\u4efd\u53ef\u6267\u884c\u6587\u4ef6&#xff0c;\u4f46\u7cfb\u7edf\u91cc\u4f1a\u51fa\u73b0\u4e09\u4e2a\u8fdb\u7a0b\u3002\u5b83\u4eec\u62e5\u6709\u4e0d\u540c\u7684 PID&#xff0c;\u4e5f\u53ef\u80fd\u6267\u884c\u5230\u4e0d\u540c\u7684\u4f4d\u7f6e\u3001\u4fdd\u5b58\u4e0d\u540c\u7684\u6570\u636e\u3002<\/p>\n<h4>1. \u4e00\u4e2a\u8fdb\u7a0b\u5305\u542b\u4ec0\u4e48&#xff1f;<\/h4>\n<p>\u8fdb\u7a0b\u7edd\u4e0d\u53ea\u662f\u201c\u6b63\u5728\u6267\u884c\u7684\u4ee3\u7801\u201d\u3002\u6309\u7167 OSTEP \u7684\u63cf\u8ff0&#xff0c;\u4e00\u4e2a\u8fdb\u7a0b\u5728\u67d0\u4e2a\u65f6\u523b\u7684\u72b6\u6001\u81f3\u5c11\u5305\u62ec&#xff1a;<\/p>\n<table>\n<tr>\u7ec4\u6210\u90e8\u5206\u5178\u578b\u5185\u5bb9<\/tr>\n<tbody>\n<tr>\n<td>\u5730\u5740\u7a7a\u95f4<\/td>\n<td>\u4ee3\u7801\u6bb5\u3001\u5168\u5c40\u6570\u636e\u3001\u5806\u3001\u6808\u3001\u52a8\u6001\u5e93\u548c\u5185\u5b58\u6620\u5c04<\/td>\n<\/tr>\n<tr>\n<td>CPU \u4e0a\u4e0b\u6587<\/td>\n<td>\u901a\u7528\u5bc4\u5b58\u5668\u3001\u7a0b\u5e8f\u8ba1\u6570\u5668 PC\u3001\u6808\u6307\u9488 SP\u3001\u72b6\u6001\u5bc4\u5b58\u5668<\/td>\n<\/tr>\n<tr>\n<td>I\/O \u72b6\u6001<\/td>\n<td>\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3001\u5f53\u524d\u6587\u4ef6\u504f\u79fb\u3001\u7ba1\u9053\u3001\u5957\u63a5\u5b57<\/td>\n<\/tr>\n<tr>\n<td>\u8eab\u4efd\u4fe1\u606f<\/td>\n<td>PID\u3001PPID\u3001\u7528\u6237 ID\u3001\u7ec4 ID\u3001\u8fdb\u7a0b\u7ec4\u3001\u4f1a\u8bdd<\/td>\n<\/tr>\n<tr>\n<td>\u5185\u6838\u7ba1\u7406\u4fe1\u606f<\/td>\n<td>\u8c03\u5ea6\u72b6\u6001\u3001\u4f18\u5148\u7ea7\u3001\u4fe1\u53f7\u72b6\u6001\u3001\u8d44\u6e90\u4f7f\u7528\u91cf\u7b49<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u56e0\u6b64\u53ef\u4ee5\u5199\u51fa\u4e00\u4e2a\u66f4\u5b8c\u6574\u7684\u5173\u7cfb&#xff1a;<\/p>\n<p>\u8fdb\u7a0b &#061; \u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u72b6\u6001 &#043; \u64cd\u4f5c\u7cfb\u7edf\u7ef4\u62a4\u7684\u7ba1\u7406\u72b6\u6001\u3002<\/p>\n<p>\u64cd\u4f5c\u7cfb\u7edf\u901a\u5e38\u4f1a\u7528\u67d0\u79cd\u5185\u6838\u6570\u636e\u7ed3\u6784\u8bb0\u5f55\u8fd9\u4e9b\u4fe1\u606f\u3002\u6559\u6750\u4e2d\u7ecf\u5e38\u628a\u5b83\u6982\u62ec\u4e3a PCB&#xff08;Process Control Block&#xff0c;\u8fdb\u7a0b\u63a7\u5236\u5757&#xff09;\u3002\u4e0d\u540c\u5185\u6838\u7684\u5b9e\u9645\u7ed3\u6784\u5e76\u4e0d\u5b8c\u5168\u76f8\u540c&#xff0c;\u4f46\u601d\u60f3\u662f\u4e00\u81f4\u7684&#xff1a;\u60f3\u6682\u505c\u4e00\u4e2a\u8fdb\u7a0b\u5e76\u5728\u672a\u6765\u6062\u590d\u5b83&#xff0c;\u5c31\u5fc5\u987b\u4fdd\u5b58\u8db3\u591f\u591a\u7684\u73b0\u573a\u3002<\/p>\n<h4>2. \u8fdb\u7a0b\u4e0d\u662f\u7a0b\u5e8f\u6587\u4ef6\u7684\u201c\u53e6\u4e00\u4e2a\u540d\u5b57\u201d<\/h4>\n<p>\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u5bf9\u6bd4\u52a0\u6df1\u5370\u8c61&#xff1a;<\/p>\n<table>\n<tr>\u7a0b\u5e8f\u8fdb\u7a0b<\/tr>\n<tbody>\n<tr>\n<td>\u9759\u6001\u7684<\/td>\n<td>\u52a8\u6001\u7684<\/td>\n<\/tr>\n<tr>\n<td>\u901a\u5e38\u5b58\u653e\u5728\u78c1\u76d8\u4e2d<\/td>\n<td>\u6b63\u5728\u5185\u5b58\u548c CPU \u4e0a\u6f14\u5316<\/td>\n<\/tr>\n<tr>\n<td>\u63cf\u8ff0\u201c\u5e94\u8be5\u600e\u6837\u6267\u884c\u201d<\/td>\n<td>\u8868\u793a\u201c\u5df2\u7ecf\u6267\u884c\u5230\u4e86\u54ea\u91cc\u201d<\/td>\n<\/tr>\n<tr>\n<td>\u4e00\u4efd\u7a0b\u5e8f\u53ef\u4ee5\u957f\u671f\u5b58\u5728<\/td>\n<td>\u8fdb\u7a0b\u6709\u521b\u5efa\u3001\u8fd0\u884c\u548c\u9000\u51fa\u7684\u751f\u547d\u5468\u671f<\/td>\n<\/tr>\n<tr>\n<td>\u540c\u4e00\u7a0b\u5e8f\u901a\u5e38\u53ea\u6709\u4e00\u4efd\u4ee3\u7801\u6587\u4ef6<\/td>\n<td>\u540c\u4e00\u7a0b\u5e8f\u53ef\u4ee5\u5bf9\u5e94\u591a\u4e2a\u8fdb\u7a0b<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5982\u679c\u628a\u7a0b\u5e8f\u6bd4\u4f5c\u4e00\u4efd\u83dc\u8c31&#xff0c;\u90a3\u4e48\u8fdb\u7a0b\u4e0d\u662f\u83dc\u8c31\u672c\u8eab&#xff0c;\u800c\u662f\u201c\u67d0\u4f4d\u53a8\u5e08\u6309\u7167\u83dc\u8c31\u505a\u83dc\u7684\u6574\u4e2a\u73b0\u573a\u201d&#xff1a;\u505a\u5230\u54ea\u4e00\u6b65\u3001\u624b\u4e2d\u6709\u4ec0\u4e48\u6750\u6599\u3001\u7089\u706b\u662f\u5426\u6253\u5f00&#xff0c;\u8fd9\u4e9b\u90fd\u5c5e\u4e8e\u8fd0\u884c\u72b6\u6001\u3002<\/p>\n<hr \/>\n<h3>\u4e09\u3001\u8fdb\u7a0b\u4e3a\u4ec0\u4e48\u4f1a\u6709\u4e0d\u540c\u72b6\u6001&#xff1f;<\/h3>\n<p>\u591a\u4e2a\u8fdb\u7a0b\u5171\u4eab\u6709\u9650\u7684 CPU&#xff0c;\u4f46\u5e76\u4e0d\u662f\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u968f\u65f6\u80fd\u591f\u8fd0\u884c\u3002OSTEP \u4f7f\u7528\u4e09\u4e2a\u57fa\u672c\u72b6\u6001\u5e2e\u52a9\u6211\u4eec\u5efa\u7acb\u6a21\u578b&#xff1a;<\/p>\n<table>\n<tr>\u72b6\u6001\u542b\u4e49<\/tr>\n<tbody>\n<tr>\n<td>Running&#xff08;\u8fd0\u884c&#xff09;<\/td>\n<td>\u6b63\u5728\u67d0\u4e2a CPU \u6838\u5fc3\u4e0a\u6267\u884c<\/td>\n<\/tr>\n<tr>\n<td>Ready&#xff08;\u5c31\u7eea&#xff09;<\/td>\n<td>\u5df2\u7ecf\u5177\u5907\u8fd0\u884c\u6761\u4ef6&#xff0c;\u53ea\u662f\u5728\u7b49\u5f85 CPU<\/td>\n<\/tr>\n<tr>\n<td>Blocked&#xff08;\u963b\u585e&#xff09;<\/td>\n<td>\u6b63\u5728\u7b49\u5f85\u67d0\u4e2a\u4e8b\u4ef6&#xff0c;\u4f8b\u5982\u78c1\u76d8 I\/O\u3001\u7f51\u7edc\u6570\u636e\u6216\u9501<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5e38\u89c1\u7684\u72b6\u6001\u8fc1\u79fb\u5982\u4e0b&#xff1a;<\/p>\n<ul>\n<li>Ready \u2192 Running&#xff1a;\u8c03\u5ea6\u5668\u9009\u4e2d\u4e86\u8be5\u8fdb\u7a0b&#xff1b;<\/li>\n<li>Running \u2192 Ready&#xff1a;\u65f6\u95f4\u7247\u8017\u5c3d&#xff0c;\u6216\u8005\u8fdb\u7a0b\u88ab\u66f4\u9ad8\u4f18\u5148\u7ea7\u4efb\u52a1\u62a2\u5360&#xff1b;<\/li>\n<li>Running \u2192 Blocked&#xff1a;\u8fdb\u7a0b\u53d1\u8d77 I\/O&#xff0c;\u6682\u65f6\u65e0\u6cd5\u7ee7\u7eed&#xff1b;<\/li>\n<li>Blocked \u2192 Ready&#xff1a;\u7b49\u5f85\u7684\u4e8b\u4ef6\u5b8c\u6210&#xff0c;\u8fdb\u7a0b\u91cd\u65b0\u83b7\u5f97\u8fd0\u884c\u8d44\u683c\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u91cc\u6700\u5bb9\u6613\u6df7\u6dc6\u7684\u662f\u201c\u5c31\u7eea\u201d\u548c\u201c\u963b\u585e\u201d&#xff1a;<\/p>\n<ul>\n<li>\u5c31\u7eea\u8fdb\u7a0b\u4ec0\u4e48\u90fd\u4e0d\u7f3a&#xff0c;\u53ea\u7f3a CPU&#xff1b;<\/li>\n<li>\u963b\u585e\u8fdb\u7a0b\u5373\u4f7f\u7acb\u523b\u5206\u914d CPU&#xff0c;\u4e5f\u65e0\u6cd5\u7ee7\u7eed\u5b8c\u6210\u5f53\u524d\u5de5\u4f5c\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982&#xff0c;\u4e00\u4e2a\u8fdb\u7a0b\u8c03\u7528 read \u7b49\u5f85\u7f51\u7edc\u6570\u636e\u65f6&#xff0c;\u5b83\u53ef\u4ee5\u8fdb\u5165\u963b\u585e\u72b6\u6001\u3002\u64cd\u4f5c\u7cfb\u7edf\u4e0d\u4f1a\u8ba9 CPU \u966a\u7740\u5b83\u7a7a\u7b49&#xff0c;\u800c\u4f1a\u5207\u6362\u5230\u5176\u4ed6\u5c31\u7eea\u8fdb\u7a0b\u3002\u7b49\u7f51\u5361\u6536\u5230\u6570\u636e\u5e76\u4ea7\u751f\u4e2d\u65ad\u540e&#xff0c;\u5185\u6838\u518d\u628a\u5b83\u8f6c\u56de\u5c31\u7eea\u72b6\u6001\u3002<\/p>\n<p>\u771f\u5b9e\u7cfb\u7edf\u8fd8\u4f1a\u6709\u521b\u5efa\u4e2d\u3001\u505c\u6b62\u3001\u50f5\u5c38\u7b49\u66f4\u7ec6\u7684\u72b6\u6001&#xff0c;\u4f46 Running\u3001Ready\u3001Blocked \u5df2\u7ecf\u8db3\u4ee5\u89e3\u91ca\u8c03\u5ea6\u7684\u57fa\u672c\u903b\u8f91\u3002<\/p>\n<hr \/>\n<h3>\u56db\u3001\u5728 Linux \u4e2d\u89c2\u5bdf\u4e00\u4e2a\u771f\u5b9e\u8fdb\u7a0b<\/h3>\n<p>\u62bd\u8c61\u6700\u7ec8\u8981\u843d\u5230\u771f\u5b9e\u7cfb\u7edf\u4e0a\u3002\u5728 Linux \u4e2d&#xff0c;\u6700\u9002\u5408\u89c2\u5bdf\u8fdb\u7a0b\u7684\u5165\u53e3\u4e4b\u4e00\u662f procfs\u3002<\/p>\n<p>\/proc \u770b\u8d77\u6765\u50cf\u4e00\u4e2a\u666e\u901a\u76ee\u5f55&#xff0c;\u5b9e\u9645\u4e0a\u662f\u5185\u6838\u52a8\u6001\u63d0\u4f9b\u7684\u865a\u62df\u6587\u4ef6\u7cfb\u7edf\u3002\u5bf9\u4e8e PID \u4e3a 1234 \u7684\u8fdb\u7a0b&#xff0c;\/proc\/1234\/ \u4e2d\u4fdd\u5b58\u4e86\u5927\u91cf\u4e0e\u5b83\u76f8\u5173\u7684\u4fe1\u606f\u3002<\/p>\n<p>\u53ef\u4ee5\u5148\u5728\u7ec8\u7aef\u4e2d\u5b8c\u6210\u4e0b\u9762\u8fd9\u7ec4\u5b9e\u9a8c&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u5f53\u524d shell \u7684 PID<\/span><br \/>\n<span class=\"token builtin class-name\">echo<\/span> <span class=\"token variable\">$$<\/span><\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u8fdb\u7a0b\u7684\u53ef\u8bfb\u72b6\u6001\u4fe1\u606f<\/span><br \/>\n<span class=\"token function\">cat<\/span> \/proc\/<span class=\"token variable\">$$<\/span>\/status<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u5b83\u6b63\u5728\u6267\u884c\u7684\u7a0b\u5e8f<\/span><br \/>\nreadlink \/proc\/<span class=\"token variable\">$$<\/span>\/exe<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u547d\u4ee4\u884c\u53c2\u6570&#xff1b;\u5176\u4e2d\u539f\u672c\u4f7f\u7528 &#039;\\\\0&#039; \u5206\u9694<\/span><br \/>\n<span class=\"token function\">tr<\/span> <span class=\"token string\">&#039;\\\\0&#039;<\/span> <span class=\"token string\">&#039; &#039;<\/span> <span class=\"token operator\">&lt;<\/span> \/proc\/<span class=\"token variable\">$$<\/span>\/cmdline<br \/>\n<span class=\"token builtin class-name\">echo<\/span><\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u73af\u5883\u53d8\u91cf<\/span><br \/>\n<span class=\"token function\">tr<\/span> <span class=\"token string\">&#039;\\\\0&#039;<\/span> <span class=\"token string\">&#039;\\\\n&#039;<\/span> <span class=\"token operator\">&lt;<\/span> \/proc\/<span class=\"token variable\">$$<\/span>\/environ <span class=\"token operator\">|<\/span> <span class=\"token function\">head<\/span><\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26<\/span><br \/>\n<span class=\"token function\">ls<\/span> <span class=\"token parameter variable\">-l<\/span> \/proc\/<span class=\"token variable\">$$<\/span>\/fd<\/p>\n<p><span class=\"token comment\"># \u67e5\u770b\u5185\u5b58\u6620\u5c04<\/span><br \/>\n<span class=\"token function\">head<\/span> \/proc\/<span class=\"token variable\">$$<\/span>\/maps<\/p>\n<p>\u51e0\u4e2a\u5e38\u7528\u6587\u4ef6\u7684\u542b\u4e49\u5982\u4e0b&#xff1a;<\/p>\n<table>\n<tr>\u8def\u5f84\u5185\u5bb9<\/tr>\n<tbody>\n<tr>\n<td>\/proc\/[pid]\/status<\/td>\n<td>PID\u3001PPID\u3001\u5185\u5b58\u3001\u7ebf\u7a0b\u6570\u3001\u4fe1\u53f7\u7b49\u53ef\u8bfb\u4fe1\u606f<\/td>\n<\/tr>\n<tr>\n<td>\/proc\/[pid]\/stat<\/td>\n<td>\u9762\u5411\u7a0b\u5e8f\u89e3\u6790\u7684\u8fdb\u7a0b\u72b6\u6001<\/td>\n<\/tr>\n<tr>\n<td>\/proc\/[pid]\/cmdline<\/td>\n<td>\u542f\u52a8\u8be5\u7a0b\u5e8f\u65f6\u7684\u53c2\u6570<\/td>\n<\/tr>\n<tr>\n<td>\/proc\/[pid]\/environ<\/td>\n<td>\u73af\u5883\u53d8\u91cf<\/td>\n<\/tr>\n<tr>\n<td>\/proc\/[pid]\/fd\/<\/td>\n<td>\u5df2\u6253\u5f00\u6587\u4ef6\u63cf\u8ff0\u7b26\u5bf9\u5e94\u7684\u7b26\u53f7\u94fe\u63a5<\/td>\n<\/tr>\n<tr>\n<td>\/proc\/[pid]\/maps<\/td>\n<td>\u865a\u62df\u5730\u5740\u7a7a\u95f4\u4e2d\u7684\u5185\u5b58\u6620\u5c04<\/td>\n<\/tr>\n<tr>\n<td>\/proc\/[pid]\/exe<\/td>\n<td>\u5f53\u524d\u53ef\u6267\u884c\u6587\u4ef6<\/td>\n<\/tr>\n<tr>\n<td>\/proc\/[pid]\/cwd<\/td>\n<td>\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd8\u6709\u4e00\u4e2a\u5f88\u6709\u610f\u601d\u7684\u8def\u5f84&#xff1a;<\/p>\n<p><span class=\"token function\">cat<\/span> \/proc\/self\/status<\/p>\n<p>\/proc\/self \u4f1a\u81ea\u52a8\u6307\u5411\u201c\u6b63\u5728\u8bbf\u95ee \/proc \u7684\u90a3\u4e2a\u8fdb\u7a0b\u201d\u3002\u6240\u4ee5\u4e0a\u8ff0\u547d\u4ee4\u663e\u793a\u7684\u901a\u5e38\u662f cat \u81ea\u5df1&#xff0c;\u800c\u4e0d\u662f\u542f\u52a8 cat \u7684 shell\u3002<\/p>\n<p>\u9664\u4e86 procfs&#xff0c;\u7a0b\u5e8f\u8fd8\u53ef\u4ee5\u901a\u8fc7\u7cfb\u7edf\u8c03\u7528\u83b7\u53d6\u81ea\u5df1\u7684\u8eab\u4efd&#xff1a;<\/p>\n<p><span class=\"token function\">getpid<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>   <span class=\"token comment\">\/\/ \u5f53\u524d\u8fdb\u7a0b PID<\/span><br \/>\n<span class=\"token function\">getppid<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u7236\u8fdb\u7a0b PID<\/span><br \/>\n<span class=\"token function\">getpgrp<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u8fdb\u7a0b\u7ec4<\/span><br \/>\n<span class=\"token function\">getsid<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u4f1a\u8bdd ID<\/span><br \/>\n<span class=\"token function\">getuid<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>   <span class=\"token comment\">\/\/ \u771f\u5b9e\u7528\u6237 ID<\/span><br \/>\n<span class=\"token function\">geteuid<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u6709\u6548\u7528\u6237 ID<\/span><br \/>\n<span class=\"token function\">getgid<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>   <span class=\"token comment\">\/\/ \u771f\u5b9e\u7ec4 ID<\/span><br \/>\n<span class=\"token function\">getegid<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u6709\u6548\u7ec4 ID<\/span><\/p>\n<p>\u9700\u8981\u6ce8\u610f&#xff0c;\/proc \u662f Linux \u7279\u6709\u7684\u63a5\u53e3&#xff0c;\u5e76\u4e0d\u662f\u6240\u6709 UNIX \u7cfb\u7edf\u90fd\u63d0\u4f9b\u5b8c\u5168\u76f8\u540c\u7684\u76ee\u5f55\u548c\u5b57\u6bb5\u3002\u5199\u53ef\u79fb\u690d\u7a0b\u5e8f\u65f6&#xff0c;\u5e94\u4f18\u5148\u4f7f\u7528\u6807\u51c6 API&#xff1b;\u8c03\u8bd5\u548c\u63a2\u7d22 Linux \u65f6&#xff0c;\/proc \u5219\u662f\u4e00\u5ea7\u5b9d\u5e93\u3002<\/p>\n<hr \/>\n<h3>\u4e94\u3001UNIX \u5982\u4f55\u7ba1\u7406\u8fdb\u7a0b&#xff1a;\u590d\u5236\u3001\u91cd\u7f6e\u548c\u9500\u6bc1\u72b6\u6001\u673a<\/h3>\n<p>\u5982\u679c\u8ba9\u6211\u4eec\u81ea\u5df1\u8bbe\u8ba1\u8fdb\u7a0b API&#xff0c;\u6700\u76f4\u89c2\u7684\u65b9\u6848\u53ef\u80fd\u662f&#xff1a;<\/p>\n<p>spawn(path, argv)  \u521b\u5efa\u5e76\u8fd0\u884c\u4e00\u4e2a\u65b0\u7a0b\u5e8f<br \/>\nexit(status)       \u7ed3\u675f\u5f53\u524d\u8fdb\u7a0b<\/p>\n<p>Windows \u7684 CreateProcess \u5728\u63a5\u53e3\u5f62\u6001\u4e0a\u66f4\u63a5\u8fd1\u8fd9\u79cd\u601d\u8def\u3002<\/p>\n<p>UNIX \u5374\u9009\u62e9\u4e86\u4e00\u4e2a\u770b\u8d77\u6765\u6709\u4e9b\u5947\u602a\u7684\u7ec4\u5408&#xff1a;<\/p>\n<table>\n<tr>\u76ee\u6807UNIX \u63a5\u53e3\u72b6\u6001\u673a\u89c6\u89d2<\/tr>\n<tbody>\n<tr>\n<td>\u521b\u5efa<\/td>\n<td>fork()<\/td>\n<td>\u590d\u5236\u5f53\u524d\u72b6\u6001\u673a<\/td>\n<\/tr>\n<tr>\n<td>\u6267\u884c\u53e6\u4e00\u7a0b\u5e8f<\/td>\n<td>execve()<\/td>\n<td>\u7528\u65b0\u7a0b\u5e8f\u91cd\u7f6e\u5f53\u524d\u72b6\u6001\u673a<\/td>\n<\/tr>\n<tr>\n<td>\u7b49\u5f85\u5b50\u8fdb\u7a0b<\/td>\n<td>wait() \/ waitpid()<\/td>\n<td>\u7b49\u5f85\u5b50\u72b6\u6001\u673a\u53d1\u751f\u7279\u5b9a\u53d8\u5316\u5e76\u56de\u6536\u7ed3\u679c<\/td>\n<\/tr>\n<tr>\n<td>\u9000\u51fa<\/td>\n<td>_exit()<\/td>\n<td>\u9500\u6bc1\u5f53\u524d\u72b6\u6001\u673a\u5e76\u7559\u4e0b\u9000\u51fa\u72b6\u6001<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6240\u4ee5&#xff0c;UNIX \u4e2d\u201c\u542f\u52a8\u4e00\u4e2a\u65b0\u7a0b\u5e8f\u201d\u7684\u7ecf\u5178\u6d41\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u8c03\u7528&#xff0c;\u800c\u662f&#xff1a;<\/p>\n<p>fork &#043; execve<\/p>\n<p>\u7236\u8fdb\u7a0b\u5148\u590d\u5236\u51fa\u4e00\u4e2a\u5b50\u8fdb\u7a0b&#xff0c;\u518d\u7531\u5b50\u8fdb\u7a0b\u628a\u81ea\u5df1\u66ff\u6362\u6210\u76ee\u6807\u7a0b\u5e8f\u3002\u8fd9\u4e2a\u8bbe\u8ba1\u770b\u4f3c\u7ed5\u4e86\u4e00\u6b65&#xff0c;\u5374\u7ed9 shell \u7684\u91cd\u5b9a\u5411\u3001\u7ba1\u9053\u3001\u6743\u9650\u8c03\u6574\u548c\u6587\u4ef6\u63cf\u8ff0\u7b26\u5b89\u6392\u7559\u4e0b\u4e86\u975e\u5e38\u7075\u6d3b\u7684\u64cd\u4f5c\u7a7a\u95f4\u3002<\/p>\n<hr \/>\n<h3>\u516d\u3001fork&#xff1a;\u590d\u5236\u4e00\u4e2a\u6b63\u5728\u8fd0\u884c\u7684\u8fdb\u7a0b<\/h3>\n<p>fork \u7684\u51fd\u6570\u539f\u578b\u5f88\u7b80\u5355&#xff1a;<\/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><\/p>\n<p><span class=\"token class-name\">pid_t<\/span> <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><\/p>\n<p>\u5b83\u6700\u7279\u6b8a\u7684\u5730\u65b9\u5728\u4e8e&#xff1a;\u8c03\u7528\u4e00\u6b21&#xff0c;\u5374\u53ef\u80fd\u8fd4\u56de\u4e24\u6b21\u3002<\/p>\n<p>\u6210\u529f\u8c03\u7528\u540e&#xff0c;\u7236\u8fdb\u7a0b\u548c\u5b50\u8fdb\u7a0b\u90fd\u4f1a\u4ece fork() \u8fd4\u56de\u7684\u4f4d\u7f6e\u7ee7\u7eed\u6267\u884c&#xff0c;\u53ea\u662f\u8fd4\u56de\u503c\u4e0d\u540c&#xff1a;<\/p>\n<table>\n<tr>\u6240\u5728\u8fdb\u7a0bfork() \u8fd4\u56de\u503c<\/tr>\n<tbody>\n<tr>\n<td>\u7236\u8fdb\u7a0b<\/td>\n<td>\u5b50\u8fdb\u7a0b\u7684 PID&#xff0c;\u5927\u4e8e 0<\/td>\n<\/tr>\n<tr>\n<td>\u5b50\u8fdb\u7a0b<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>\u8c03\u7528\u5931\u8d25<\/td>\n<td>\u7236\u8fdb\u7a0b\u5f97\u5230 -1&#xff0c;\u4e14\u4e0d\u4f1a\u521b\u5efa\u5b50\u8fdb\u7a0b<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e0b\u9762\u662f\u6700\u57fa\u672c\u7684\u4f8b\u5b50&#xff1a;<\/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;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\/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 \/>\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><\/p>\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> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> value <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">100<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token class-name\">pid_t<\/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><\/p>\n<p>    <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\">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><br \/>\n        <span class=\"token keyword\">return<\/span> EXIT_FAILURE<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <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        value<span class=\"token operator\">&#043;&#043;<\/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] pid&#061;%ld, ppid&#061;%ld, value&#061;%d\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n               <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">long<\/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><span class=\"token keyword\">long<\/span><span class=\"token punctuation\">)<\/span><span class=\"token function\">getppid<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> value<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><\/p>\n<p>    value <span class=\"token operator\">&#043;&#061;<\/span> <span class=\"token number\">10<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[parent] pid&#061;%ld, child&#061;%ld, value&#061;%d\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n           <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">long<\/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><span class=\"token keyword\">long<\/span><span class=\"token punctuation\">)<\/span>pid<span class=\"token punctuation\">,<\/span> value<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token function\">waitpid<\/span><span class=\"token punctuation\">(<\/span>pid<span class=\"token punctuation\">,<\/span> <span class=\"token constant\">NULL<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/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><\/p>\n<p>\u7f16\u8bd1\u5e76\u8fd0\u884c&#xff1a;<\/p>\n<p>gcc <span class=\"token parameter variable\">-Wall<\/span> <span class=\"token parameter variable\">-Wextra<\/span> <span class=\"token parameter variable\">-O2<\/span> fork_basic.c <span class=\"token parameter variable\">-o<\/span> fork_basic<br \/>\n.\/fork_basic<\/p>\n<p>\u4f60\u53ef\u80fd\u770b\u5230\u7236\u8fdb\u7a0b\u5148\u8f93\u51fa&#xff0c;\u4e5f\u53ef\u80fd\u770b\u5230\u5b50\u8fdb\u7a0b\u5148\u8f93\u51fa\u3002\u4e24\u8005\u90fd\u6b63\u786e&#xff0c;\u56e0\u4e3a fork \u4e4b\u540e\u8c01\u5148\u88ab\u8c03\u5ea6\u5e76\u4e0d\u786e\u5b9a\u3002<\/p>\n<p>\u4f46 value \u4e00\u5b9a\u5206\u522b\u662f&#xff1a;<\/p>\n<ul>\n<li>\u5b50\u8fdb\u7a0b\u4e2d\u7684 101&#xff1b;<\/li>\n<li>\u7236\u8fdb\u7a0b\u4e2d\u7684 110\u3002<\/li>\n<\/ul>\n<p>\u539f\u56e0\u662f\u7236\u5b50\u8fdb\u7a0b\u62e5\u6709\u5f7c\u6b64\u72ec\u7acb\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\u3002fork \u65f6\u4e8c\u8005\u5185\u5bb9\u76f8\u540c&#xff0c;\u4e4b\u540e\u4e00\u65b9\u5bf9\u5185\u5b58\u7684\u4fee\u6539\u4e0d\u4f1a\u6539\u53d8\u53e6\u4e00\u65b9\u3002<\/p>\n<h4>1. \u201c\u590d\u5236\u6574\u4e2a\u5185\u5b58\u201d\u4f1a\u4e0d\u4f1a\u5f88\u6162&#xff1f;<\/h4>\n<p>\u4ece\u8bed\u4e49\u4e0a\u770b&#xff0c;fork \u50cf\u662f\u590d\u5236\u4e86\u7236\u8fdb\u7a0b\u7684\u6574\u4e2a\u5730\u5740\u7a7a\u95f4&#xff1b;\u4f46\u73b0\u4ee3 Linux \u901a\u5e38\u4f7f\u7528 Copy-on-Write&#xff08;\u5199\u65f6\u590d\u5236&#xff0c;COW&#xff09;&#xff1a;<\/p>\n<li>fork \u521a\u5b8c\u6210\u65f6&#xff0c;\u7236\u5b50\u8fdb\u7a0b\u7684\u9875\u8868\u53ef\u4ee5\u6682\u65f6\u6307\u5411\u76f8\u540c\u7684\u7269\u7406\u9875&#xff1b;<\/li>\n<li>\u8fd9\u4e9b\u9875\u9762\u4f1a\u88ab\u4fdd\u62a4\u8d77\u6765&#xff1b;<\/li>\n<li>\u5f53\u67d0\u4e00\u65b9\u5c1d\u8bd5\u5199\u5165\u65f6&#xff0c;CPU \u89e6\u53d1\u7f3a\u9875\u5f02\u5e38&#xff1b;<\/li>\n<li>\u5185\u6838\u518d\u4e3a\u5373\u5c06\u5199\u5165\u7684\u4e00\u65b9\u590d\u5236\u5bf9\u5e94\u9875\u9762\u3002<\/li>\n<p>\u56e0\u6b64&#xff0c;fork \u7684\u4e3b\u8981\u521d\u59cb\u5f00\u9500\u901a\u5e38\u662f\u590d\u5236\u9875\u8868\u3001\u521b\u5efa\u5185\u6838\u4efb\u52a1\u7ed3\u6784\u7b49&#xff0c;\u800c\u4e0d\u662f\u7acb\u523b\u590d\u5236\u7236\u8fdb\u7a0b\u6bcf\u4e00\u4e2a\u7269\u7406\u5185\u5b58\u9875\u3002<\/p>\n<p>\u8fd9\u4e5f\u89e3\u91ca\u4e86\u4e00\u4e2a\u7ecf\u5178\u7528\u6cd5&#xff1a;\u7a0b\u5e8f\u5148\u5b8c\u6210\u8017\u65f6\u7684\u5171\u4eab\u6570\u636e\u521d\u59cb\u5316&#xff0c;\u518d fork \u51fa\u591a\u4e2a\u5de5\u4f5c\u8fdb\u7a0b\u3002\u53ea\u8bfb\u6570\u636e\u53ef\u4ee5\u7ee7\u7eed\u5171\u4eab\u7269\u7406\u9875&#xff0c;\u771f\u6b63\u53d1\u751f\u4fee\u6539\u7684\u90e8\u5206\u624d\u9700\u8981\u590d\u5236\u3002Android \u7684 Zygote \u8fdb\u7a0b\u4e5f\u5229\u7528\u4e86\u201c\u5148\u9884\u52a0\u8f7d&#xff0c;\u518d\u6d3e\u751f\u5e94\u7528\u8fdb\u7a0b\u201d\u7684\u601d\u8def\u3002<\/p>\n<p>\u4e0d\u8fc7 COW \u5e76\u4e0d\u7b49\u4e8e\u201c\u6ca1\u6709\u5f00\u9500\u201d\u3002\u7236\u8fdb\u7a0b\u9875\u8868\u5f88\u5927\u3001\u5b50\u8fdb\u7a0b\u5927\u91cf\u5199\u5185\u5b58&#xff0c;\u6216\u8005\u9891\u7e41\u521b\u5efa\u8fdb\u7a0b\u65f6&#xff0c;\u6210\u672c\u4ecd\u7136\u53ef\u80fd\u5f88\u9ad8\u3002<\/p>\n<h4>2. fork \u5230\u5e95\u590d\u5236\u548c\u7ee7\u627f\u4e86\u4ec0\u4e48&#xff1f;<\/h4>\n<p>\u8fd9\u5c31\u8981\u8bb2\u5230&#xff1a;<\/p>\n<p>\u7236\u5b50\u8fdb\u7a0b\u5728 fork \u8fd4\u56de\u7684\u77ac\u95f4\u770b\u8d77\u6765\u51e0\u4e4e\u4e00\u6837&#xff0c;\u4f46\u5b83\u4eec\u4e0d\u662f\u540c\u4e00\u4e2a\u8fdb\u7a0b\u3002<\/p>\n<p>\u51e0\u4e2a\u6700\u91cd\u8981\u7684\u7ec6\u8282\u662f&#xff1a;<\/p>\n<ul>\n<li>\u5b50\u8fdb\u7a0b\u6709\u81ea\u5df1\u7684 PID&#xff1b;<\/li>\n<li>\u5b50\u8fdb\u7a0b\u7684 PPID \u6307\u5411\u7236\u8fdb\u7a0b&#xff1b;<\/li>\n<li>\u7236\u5b50\u8fdb\u7a0b\u62e5\u6709\u72ec\u7acb\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4&#xff1b;<\/li>\n<li>\u5b50\u8fdb\u7a0b\u7ee7\u627f\u7236\u8fdb\u7a0b\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u526f\u672c&#xff1b;<\/li>\n<li>\u8fd9\u4e9b\u6587\u4ef6\u63cf\u8ff0\u7b26\u901a\u5e38\u6307\u5411\u76f8\u540c\u7684\u5185\u6838 open file description&#xff0c;\u56e0\u6b64\u53ef\u80fd\u5171\u4eab\u6587\u4ef6\u504f\u79fb&#xff1b;<\/li>\n<li>\u67d0\u4e9b\u4fe1\u53f7\u3001\u9501\u3001\u8ba1\u65f6\u5668\u7b49\u72b6\u6001\u6709\u7279\u6b8a\u7ee7\u627f\u89c4\u5219&#xff0c;\u4e0d\u80fd\u7b80\u5355\u7406\u89e3\u6210\u201c\u6240\u6709\u4e1c\u897f\u9010\u4f4d\u590d\u5236\u201d\u3002<\/li>\n<\/ul>\n<p>\u6700\u540e\u4e00\u70b9\u975e\u5e38\u91cd\u8981\u3002\u8bfe\u5802\u4e2d\u7684\u201c\u590d\u5236\u72b6\u6001\u673a\u201d\u662f\u7406\u89e3\u8bed\u4e49\u7684\u597d\u6a21\u578b&#xff0c;\u4f46\u771f\u6b63\u5199\u7cfb\u7edf\u7a0b\u5e8f\u65f6&#xff0c;\u4ecd\u7136\u9700\u8981\u67e5\u770b fork(2) \u624b\u518c\u786e\u8ba4\u5177\u4f53\u8d44\u6e90\u7684\u7ee7\u627f\u884c\u4e3a\u3002<\/p>\n<h4>3. fork \u4e4b\u540e\u4e3a\u4ec0\u4e48\u4f1a\u5f62\u6210\u8fdb\u7a0b\u6811&#xff1f;<\/h4>\n<p>\u6bcf\u6b21 fork \u90fd\u4f1a\u5efa\u7acb\u7236\u5b50\u5173\u7cfb&#xff0c;\u4e8e\u662f\u6574\u4e2a\u7cfb\u7edf\u4e2d\u7684\u8fdb\u7a0b\u81ea\u7136\u5f62\u6210\u4e00\u68f5\u6811\u3002<\/p>\n<p>\u5047\u8bbe\u5173\u7cfb\u4e3a&#xff1a;<\/p>\n<p>A \u521b\u5efa B&#xff0c;B \u521b\u5efa C<\/p>\n<p>\u5982\u679c B \u5148\u9000\u51fa&#xff0c;C \u7684\u7236\u8fdb\u7a0b\u5e76\u4e0d\u4e00\u5b9a\u7b80\u5355\u5730\u53d8\u6210 A\u3002\u5728 Linux \u4e2d&#xff0c;\u4ecd\u5728\u8fd0\u884c\u7684\u5b64\u513f\u8fdb\u7a0b\u4f1a\u88ab\u91cd\u65b0\u6258\u7ba1\u7ed9\u6700\u8fd1\u7684 subreaper&#xff1b;\u5982\u679c\u4e0d\u5b58\u5728\u5408\u9002\u7684 subreaper&#xff0c;\u5219\u901a\u5e38\u7531\u6240\u5728 PID \u547d\u540d\u7a7a\u95f4\u4e2d\u7684 init \u8fdb\u7a0b\u63a5\u7ba1\u3002<\/p>\n<p>\u8fd9\u5c31\u662f\u5b64\u513f\u8fdb\u7a0b&#xff08;orphan process&#xff09; \u95ee\u9898&#xff1a;\u7236\u8fdb\u7a0b\u5148\u9000\u51fa&#xff0c;\u4f46\u5b50\u8fdb\u7a0b\u4ecd\u5728\u8fd0\u884c\u3002<\/p>\n<p>\u53e6\u4e00\u4e2a\u5bb9\u6613\u6df7\u6dc6\u7684\u6982\u5ff5\u662f\u50f5\u5c38\u8fdb\u7a0b&#xff08;zombie process&#xff09;&#xff1a;<\/p>\n<ul>\n<li>\u5b50\u8fdb\u7a0b\u5df2\u7ecf\u7ed3\u675f\u8fd0\u884c&#xff1b;<\/li>\n<li>\u5185\u6838\u4ecd\u9700\u4fdd\u7559\u5b83\u7684 PID\u3001\u9000\u51fa\u72b6\u6001\u548c\u5c11\u91cf\u7edf\u8ba1\u4fe1\u606f&#xff1b;<\/li>\n<li>\u7236\u8fdb\u7a0b\u5c1a\u672a\u8c03\u7528 wait \u6216 waitpid \u53d6\u8d70\u8fd9\u4e9b\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<p>\u5b64\u513f\u8fdb\u7a0b\u8fd8\u6d3b\u7740&#xff0c;\u50f5\u5c38\u8fdb\u7a0b\u5df2\u7ecf\u6b7b\u4e86\u3002\u4e24\u8005\u5b8c\u5168\u4e0d\u662f\u4e00\u56de\u4e8b\u3002<\/p>\n<h4>4. \u4e3a\u4ec0\u4e48 fork \u4f1a\u5e26\u6765\u6307\u6570\u589e\u957f&#xff1f;<\/h4>\n<p>\u770b\u4e0b\u9762\u7684\u5faa\u73af&#xff1a;<\/p>\n<p><span class=\"token keyword\">for<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> i <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span> i <span class=\"token operator\">&lt;<\/span> n<span class=\"token punctuation\">;<\/span> i<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <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 punctuation\">}<\/span><\/p>\n<p>\u5982\u679c\u6bcf\u6b21 fork \u90fd\u6210\u529f&#xff0c;\u800c\u4e14\u6240\u6709\u65b0\u8fdb\u7a0b\u90fd\u4f1a\u7ee7\u7eed\u6267\u884c\u5269\u4f59\u5faa\u73af&#xff0c;\u90a3\u4e48\u8fdb\u7a0b\u6570\u91cf\u4f1a\u8fd9\u6837\u589e\u957f&#xff1a;<\/p>\n<p>1 \u2192 2 \u2192 4 \u2192 8 \u2192 &#8230; \u2192 2^n<\/p>\n<p>\u8fde\u7eed\u6267\u884c n \u6b21\u65e0\u6761\u4ef6 fork \u540e&#xff1a;<\/p>\n<ul>\n<li>\u6700\u7ec8\u8fdb\u7a0b\u603b\u6570\u662f 2^n&#xff1b;<\/li>\n<li>\u65b0\u521b\u5efa\u7684\u8fdb\u7a0b\u6570\u662f 2^n &#8211; 1\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u4e5f\u662f fork bomb \u5371\u9669\u7684\u6839\u6e90&#xff1a;\u5b83\u4f1a\u5feb\u901f\u8017\u5c3d PID\u3001\u8fdb\u7a0b\u6570\u9650\u5236\u3001\u5185\u5b58\u548c\u8c03\u5ea6\u8d44\u6e90\u3002\u73b0\u4ee3 Linux \u53ef\u4ee5\u4f7f\u7528 RLIMIT_NPROC\u3001cgroup \u7684 pids.max \u7b49\u673a\u5236\u9650\u5236\u8fdb\u7a0b\u6570\u91cf&#xff0c;\u4f46 fork bomb \u4f9d\u7136\u53ef\u80fd\u4e25\u91cd\u5f71\u54cd\u7cfb\u7edf\u3002<\/p>\n<p>\u4e0d\u8981\u5728\u771f\u5b9e\u73af\u5883\u4e2d\u5c1d\u8bd5 fork bomb\u3002\u7406\u89e3\u6307\u6570\u589e\u957f\u5373\u53ef&#xff0c;\u6ca1\u6709\u5fc5\u8981\u7528\u673a\u5668\u7a33\u5b9a\u6027\u505a\u5b9e\u9a8c\u3002<\/p>\n<h4>5. fork \u7684\u5b9e\u9645\u7528\u9014<\/h4>\n<p>\u5c3d\u7ba1\u63a5\u53e3\u770b\u8d77\u6765\u53e4\u602a&#xff0c;fork \u5177\u6709\u5f88\u5f3a\u7684\u8868\u8fbe\u80fd\u529b&#xff1a;<\/p>\n<ul>\n<li>shell \u521b\u5efa\u5b50\u8fdb\u7a0b\u6267\u884c\u547d\u4ee4&#xff1b;<\/li>\n<li>Web \u670d\u52a1\u5668\u6216\u5b88\u62a4\u8fdb\u7a0b\u6d3e\u751f\u5de5\u4f5c\u8fdb\u7a0b&#xff1b;<\/li>\n<li>\u5b8c\u6210\u516c\u5171\u6570\u636e\u9884\u5904\u7406\u540e\u521b\u5efa\u591a\u4e2a\u53ea\u8bfb\u5de5\u4f5c\u8fdb\u7a0b&#xff1b;<\/li>\n<li>\u4e3a\u641c\u7d22\u7b97\u6cd5\u7684\u4e0d\u540c\u5206\u652f\u4fdd\u5b58\u72b6\u6001\u5feb\u7167&#xff1b;<\/li>\n<li>\u901a\u8fc7\u8fdb\u7a0b\u8fb9\u754c\u5b9e\u73b0\u6545\u969c\u9694\u79bb&#xff1b;<\/li>\n<\/ul>\n<p>\u5f53\u7136&#xff0c;\u5e76\u884c\u7a0b\u5e8f\u4e0d\u5e94\u770b\u5230\u5206\u652f\u5c31\u65e0\u6761\u4ef6 fork\u3002\u8fdb\u7a0b\u521b\u5efa\u3001\u8c03\u5ea6\u548c\u901a\u4fe1\u90fd\u6709\u6210\u672c&#xff0c;\u4efb\u52a1\u89c4\u6a21\u8fc7\u5c0f\u65f6&#xff0c;\u5f00\u9500\u53ef\u80fd\u5927\u4e8e\u5e76\u884c\u6536\u76ca\u3002<\/p>\n<hr \/>\n<h3>\u4e03\u3001\u4e00\u9053\u770b\u4f3c\u7b80\u5355&#xff0c;\u5374\u5f88\u5bb9\u6613\u7b54\u9519\u7684 fork \u9898<\/h3>\n<p>\u8bfe\u5802\u4e2d\u7ed9\u51fa\u4e86\u4e0b\u9762\u7684\u7a0b\u5e8f&#xff1a;<\/p>\n<p><span class=\"token keyword\">for<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> i <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span> i <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">2<\/span><span class=\"token punctuation\">;<\/span> i<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <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 function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;Hello\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u5148\u6682\u65f6\u5ffd\u7565\u7f13\u51b2&#xff0c;\u6570\u4e00\u6570 printf \u88ab\u6267\u884c\u591a\u5c11\u6b21&#xff1a;<\/p>\n<ul>\n<li>\u7b2c 1 \u8f6e fork \u540e\u6709 2 \u4e2a\u8fdb\u7a0b&#xff0c;\u56e0\u6b64\u8f93\u51fa 2 \u6b21&#xff1b;<\/li>\n<li>\u7b2c 2 \u8f6e\u5f00\u59cb\u65f6\u6709 2 \u4e2a\u8fdb\u7a0b&#xff0c;\u6bcf\u4e2a\u518d\u590d\u5236\u4e00\u6b21&#xff0c;\u4e8e\u662f\u6709 4 \u4e2a\u8fdb\u7a0b&#xff0c;\u8f93\u51fa 4 \u6b21&#xff1b;<\/li>\n<li>\u603b\u8ba1 2 &#043; 4 &#061; 6 \u6b21\u3002<\/li>\n<\/ul>\n<p>\u4e00\u822c\u5730&#xff0c;\u5982\u679c\u5faa\u73af\u6267\u884c n \u8f6e&#xff0c;\u5e76\u5728\u6bcf\u8f6e fork \u540e\u8f93\u51fa\u4e00\u6b21&#xff0c;\u8f93\u51fa\u8bed\u53e5\u7684\u6267\u884c\u6b21\u6570\u662f&#xff1a;<\/p>\n<p>2 &#043; 4 &#043; &#8230; &#043; 2^n &#061; 2^(n&#043;1) &#8211; 2<\/p>\n<p>\u4f46\u5b9e\u9a8c\u65f6\u53ef\u80fd\u51fa\u73b0\u4e00\u4e2a\u53cd\u5e38\u73b0\u8c61&#xff1a;<\/p>\n<p>.\/a.out<br \/>\n.\/a.out <span class=\"token operator\">|<\/span> <span class=\"token function\">wc<\/span> <span class=\"token parameter variable\">-l<\/span><\/p>\n<p>\u524d\u8005\u901a\u5e38\u770b\u5230 6 \u884c&#xff0c;\u800c\u540e\u8005\u53ef\u80fd\u7edf\u8ba1\u51fa 8 \u884c\u3002\u4e3a\u4ec0\u4e48&#xff1f;<\/p>\n<h4>1. \u5173\u952e\u4e0d\u5728 fork&#xff0c;\u800c\u5728 stdio \u7f13\u51b2<\/h4>\n<p>\u8fde\u63a5\u7ec8\u7aef\u65f6&#xff0c;stdout \u901a\u5e38\u91c7\u7528\u884c\u7f13\u51b2&#xff0c;\u9047\u5230\u6362\u884c\u7b26\u5c31\u4f1a\u5237\u65b0\u3002\u56e0\u6b64\u7b2c\u4e00\u8f6e\u4ea7\u751f\u7684\u4e24\u884c\u6587\u672c\u5728\u7b2c\u4e8c\u8f6e fork \u524d\u5927\u6982\u7387\u5df2\u7ecf\u901a\u8fc7 write \u4ea4\u7ed9\u5185\u6838&#xff0c;\u4e0d\u4f1a\u518d\u6b21\u590d\u5236\u3002<\/p>\n<p>\u8fde\u63a5\u7ba1\u9053\u65f6&#xff0c;stdout \u901a\u5e38\u91c7\u7528\u5168\u7f13\u51b2\u3002\u7b2c\u4e00\u8f6e\u7684\u4e24\u4e2a printf(&#034;Hello\\\\n&#034;) \u53ea\u662f\u628a\u6587\u672c\u653e\u8fdb\u5404\u81ea\u8fdb\u7a0b\u7684\u7528\u6237\u6001\u7f13\u51b2\u533a&#xff0c;\u8fd8\u6ca1\u6709\u771f\u6b63\u6267\u884c write\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u7b2c\u4e8c\u8f6e fork \u590d\u5236\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4&#xff0c;\u4e5f\u5c31\u590d\u5236\u4e86\u7f13\u51b2\u533a&#xff1a;<\/p>\n<li>\u7b2c\u4e00\u8f6e\u540e\u6709 2 \u4e2a\u8fdb\u7a0b&#xff0c;\u6bcf\u4e2a\u7f13\u51b2\u533a\u4e2d\u6709\u4e00\u884c Hello&#xff1b;<\/li>\n<li>\u7b2c\u4e8c\u8f6e fork \u540e\u6709 4 \u4e2a\u8fdb\u7a0b&#xff0c;\u6bcf\u4e2a\u90fd\u5e26\u7740\u88ab\u590d\u5236\u7684\u4e00\u884c&#xff1b;<\/li>\n<li>4 \u4e2a\u8fdb\u7a0b\u53c8\u5404\u81ea\u8ffd\u52a0\u4e00\u884c&#xff1b;<\/li>\n<li>\u6b63\u5e38\u9000\u51fa\u65f6&#xff0c;\u6bcf\u4e2a\u8fdb\u7a0b\u5237\u65b0\u4e24\u884c&#xff0c;\u5171\u5f97\u5230 4 \u00d7 2 &#061; 8 \u884c\u3002<\/li>\n<p>\u6240\u4ee5&#xff0c;\u771f\u6b63\u88ab\u590d\u5236\u7684\u4e0d\u662f\u5df2\u7ecf\u5199\u5165\u7ec8\u7aef\u6216\u7ba1\u9053\u7684\u6570\u636e&#xff0c;\u800c\u662f\u8fd8\u7559\u5728\u7528\u6237\u7a7a\u95f4\u5185\u5b58\u4e2d\u7684 stdio \u7f13\u51b2\u533a\u3002<\/p>\n<p>\u53ef\u4ee5\u7528\u4e0b\u9762\u51e0\u79cd\u65b9\u6cd5\u9a8c\u8bc1&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ \u65b9\u6cd5\u4e00&#xff1a;\u5173\u95ed stdout \u7f13\u51b2<\/span><br \/>\n<span class=\"token function\">setbuf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token constant\">stdout<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token constant\">NULL<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u65b9\u6cd5\u4e8c&#xff1a;\u5728 fork \u524d\u4e3b\u52a8\u5237\u65b0<\/span><br \/>\n<span class=\"token function\">fflush<\/span><span class=\"token punctuation\">(<\/span><span class=\"token constant\">stdout<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u65b9\u6cd5\u4e09&#xff1a;\u5bf9\u6bd4\u5e95\u5c42 write \u7cfb\u7edf\u8c03\u7528<\/span><br \/>\n<span class=\"token function\">write<\/span><span class=\"token punctuation\">(<\/span>STDOUT_FILENO<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;Hello\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">6<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u8fd8\u53ef\u4ee5\u7528 strace \u89c2\u5bdf\u5b9e\u9645\u53d1\u751f\u7684\u7cfb\u7edf\u8c03\u7528&#xff1a;<\/p>\n<p><span class=\"token function\">strace<\/span> <span class=\"token parameter variable\">-f<\/span> <span class=\"token parameter variable\">-e<\/span> <span class=\"token assign-left variable\">trace<\/span><span class=\"token operator\">&#061;<\/span>process,write .\/a.out<br \/>\n<span class=\"token function\">strace<\/span> <span class=\"token parameter variable\">-f<\/span> <span class=\"token parameter variable\">-e<\/span> <span class=\"token assign-left variable\">trace<\/span><span class=\"token operator\">&#061;<\/span>process,write .\/a.out <span class=\"token operator\"><span class=\"token file-descriptor important\">2<\/span>&gt;<\/span><span class=\"token file-descriptor important\">&amp;1<\/span> <span class=\"token operator\">|<\/span> <span class=\"token function\">less<\/span><\/p>\n<hr \/>\n<h3>\u516b\u3001execve&#xff1a;\u4e0d\u662f\u521b\u5efa\u8fdb\u7a0b&#xff0c;\u800c\u662f\u66ff\u6362\u5f53\u524d\u8fdb\u7a0b<\/h3>\n<p>execve \u7684\u539f\u578b\u5982\u4e0b&#xff1a;<\/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><\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">execve<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span> <span class=\"token operator\">*<\/span>path<span class=\"token punctuation\">,<\/span><br \/>\n           <span class=\"token keyword\">char<\/span> <span class=\"token operator\">*<\/span><span class=\"token keyword\">const<\/span> argv<span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span><br \/>\n           <span class=\"token keyword\">char<\/span> <span class=\"token operator\">*<\/span><span class=\"token keyword\">const<\/span> envp<span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u5982\u679c\u8bf4 fork \u662f\u201c\u590d\u5236\u72b6\u6001\u673a\u201d&#xff0c;\u90a3\u4e48 execve \u5c31\u662f\u201c\u91cd\u7f6e\u72b6\u6001\u673a\u201d&#xff1a;<\/p>\n<p>\u5b83\u628a\u5f53\u524d\u8fdb\u7a0b\u6b63\u5728\u8fd0\u884c\u7684\u7a0b\u5e8f\u66ff\u6362\u6210 path \u6307\u5b9a\u7684\u65b0\u7a0b\u5e8f\u3002<\/p>\n<p>\u6210\u529f\u8c03\u7528\u540e&#xff1a;<\/p>\n<ul>\n<li>\u539f\u7a0b\u5e8f\u7684\u4ee3\u7801\u3001\u6570\u636e\u3001\u5806\u548c\u6808\u88ab\u65b0\u7a0b\u5e8f\u66ff\u6362&#xff1b;<\/li>\n<li>CPU \u4ece\u65b0\u7a0b\u5e8f\u5165\u53e3\u5f00\u59cb\u6267\u884c&#xff1b;<\/li>\n<li>\u65b0\u7a0b\u5e8f\u83b7\u5f97\u65b0\u7684\u53c2\u6570\u548c\u73af\u5883\u53d8\u91cf&#xff1b;<\/li>\n<li>\u8c03\u7528\u6210\u529f\u65f6 execve \u4e0d\u4f1a\u8fd4\u56de\u3002<\/li>\n<\/ul>\n<p>\u6700\u540e\u4e00\u6761\u5c24\u5176\u91cd\u8981\u3002\u4e0b\u9762\u7684\u8f93\u51fa\u53ea\u4f1a\u5728 execve \u5931\u8d25\u65f6\u51fa\u73b0&#xff1a;<\/p>\n<p><span class=\"token function\">execve<\/span><span class=\"token punctuation\">(<\/span>path<span class=\"token punctuation\">,<\/span> argv<span class=\"token punctuation\">,<\/span> envp<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;execve&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u80fd\u6267\u884c\u5230\u8fd9\u91cc&#xff0c;\u8bf4\u660e execve \u5931\u8d25<\/span><\/p>\n<h4>1. execve \u7684\u4e09\u4e2a\u53c2\u6570<\/h4>\n<h5>path&#xff1a;\u53ef\u6267\u884c\u6587\u4ef6\u8def\u5f84<\/h5>\n<p>\u4f8b\u5982&#xff1a;<\/p>\n<p><span class=\"token string\">&#034;\/bin\/echo&#034;<\/span><\/p>\n<p>\u5e95\u5c42 execve \u9700\u8981\u660e\u786e\u7684\u8def\u5f84&#xff0c;\u5b83\u672c\u8eab\u4e0d\u4f1a\u50cf shell \u90a3\u6837\u81ea\u52a8\u641c\u7d22 PATH\u3002<\/p>\n<h5>argv&#xff1a;\u547d\u4ee4\u884c\u53c2\u6570<\/h5>\n<p>argv \u662f\u4ee5\u7a7a\u6307\u9488\u7ed3\u5c3e\u7684\u5b57\u7b26\u4e32\u6307\u9488\u6570\u7ec4&#xff1a;<\/p>\n<p><span class=\"token keyword\">char<\/span> <span class=\"token operator\">*<\/span>argv<span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token string\">&#034;echo&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token string\">&#034;Hello from execve&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token constant\">NULL<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u6309\u7167\u7ea6\u5b9a&#xff0c;argv[0] \u901a\u5e38\u662f\u7a0b\u5e8f\u540d\u3002\u65b0\u7a0b\u5e8f\u7684 main(int argc, char *argv[]) \u53c2\u6570\u6b63\u662f\u7531\u8fd9\u91cc\u5efa\u7acb\u8d77\u6765\u7684\u3002<\/p>\n<h5>envp&#xff1a;\u73af\u5883\u53d8\u91cf<\/h5>\n<p>\u73af\u5883\u53d8\u91cf\u540c\u6837\u4f7f\u7528\u4ee5\u7a7a\u6307\u9488\u7ed3\u5c3e\u7684\u5b57\u7b26\u4e32\u6570\u7ec4&#xff1a;<\/p>\n<p><span class=\"token keyword\">char<\/span> <span class=\"token operator\">*<\/span>envp<span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token string\">&#034;LANG&#061;C&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token string\">&#034;MY_KEY&#061;hello&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token constant\">NULL<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u6bcf\u4e00\u9879\u901a\u5e38\u91c7\u7528 KEY&#061;VALUE \u7684\u5f62\u5f0f\u3002PATH\u3001HOME\u3001PWD\u3001LANG \u7b49\u90fd\u5c5e\u4e8e\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<p>Shell \u4e2d\u7684&#xff1a;<\/p>\n<p><span class=\"token builtin class-name\">export<\/span> <span class=\"token assign-left variable\">MY_KEY<\/span><span class=\"token operator\">&#061;<\/span>hello<\/p>\n<p>\u672c\u8d28\u4e0a\u662f\u5728\u4fee\u6539 shell \u81ea\u5df1\u7684\u73af\u5883&#xff0c;\u4f7f\u5b83\u4e4b\u540e\u521b\u5efa\u5e76\u6267\u884c\u7684\u5b50\u8fdb\u7a0b\u80fd\u591f\u7ee7\u627f\u6216\u83b7\u5f97\u76f8\u5e94\u53d8\u91cf\u3002<\/p>\n<h4>2. \u54ea\u4e9b\u72b6\u6001\u88ab\u66ff\u6362&#xff0c;\u54ea\u4e9b\u72b6\u6001\u4f1a\u4fdd\u7559&#xff1f;<\/h4>\n<p>execve \u5e76\u6ca1\u6709\u521b\u5efa\u65b0\u7684\u8fdb\u7a0b&#xff0c;\u6240\u4ee5\u8c03\u7528\u524d\u540e PID \u4e0d\u53d8\u3002\u53ef\u4ee5\u628a\u5e38\u89c1\u72b6\u6001\u5206\u6210\u4e24\u7c7b&#xff1a;<\/p>\n<table>\n<tr>\u88ab\u65b0\u7a0b\u5e8f\u66ff\u6362\u6216\u91cd\u65b0\u521d\u59cb\u5316\u901a\u5e38\u7ee7\u7eed\u4fdd\u7559<\/tr>\n<tbody>\n<tr>\n<td>\u4ee3\u7801\u6bb5\u3001\u6570\u636e\u6bb5\u3001\u5806\u3001\u6808<\/td>\n<td>PID\u3001PPID<\/td>\n<\/tr>\n<tr>\n<td>\u7a0b\u5e8f\u8ba1\u6570\u5668\u548c\u7528\u6237\u6001\u6267\u884c\u73b0\u573a<\/td>\n<td>\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/td>\n<\/tr>\n<tr>\n<td>\u539f\u7a0b\u5e8f\u7684\u5185\u5b58\u6620\u50cf<\/td>\n<td>\u5927\u90e8\u5206\u8fdb\u7a0b\u8eab\u4efd\u4fe1\u606f<\/td>\n<\/tr>\n<tr>\n<td>\u65b0\u7684 argv \u548c envp \u7531\u8c03\u7528\u8005\u63d0\u4f9b<\/td>\n<td>\u672a\u8bbe\u7f6e close-on-exec \u7684\u6587\u4ef6\u63cf\u8ff0\u7b26<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u5f20\u8868\u53ea\u5217\u51fa\u6700\u6838\u5fc3\u7684\u884c\u4e3a\u3002\u4fe1\u53f7\u5904\u7406\u65b9\u5f0f\u3001\u51ed\u636e\u3001\u5185\u5b58\u6620\u5c04\u3001\u5b9a\u65f6\u5668\u7b49\u90fd\u6709\u66f4\u7ec6\u7684\u89c4\u5219&#xff0c;\u7cfb\u7edf\u7f16\u7a0b\u65f6\u5e94\u67e5\u9605 execve(2)\u3002<\/p>\n<p>\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u9ed8\u8ba4\u53ef\u4ee5\u8de8\u8d8a execve \u4fdd\u7559&#xff0c;\u8fd9\u4e00\u70b9\u6781\u5176\u91cd\u8981&#xff1a;shell \u6b63\u662f\u501f\u52a9\u5b83\u5b9e\u73b0\u91cd\u5b9a\u5411\u548c\u7ba1\u9053\u3002<\/p>\n<p>\u5982\u679c\u67d0\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0d\u5e94\u8be5\u6cc4\u6f0f\u7ed9\u65b0\u7a0b\u5e8f&#xff0c;\u5e94\u8bbe\u7f6e FD_CLOEXEC&#xff0c;\u6216\u8005\u5728\u521b\u5efa\u6587\u4ef6\u63cf\u8ff0\u7b26\u65f6\u4f7f\u7528 O_CLOEXEC \u7b49\u539f\u5b50\u9009\u9879\u3002<\/p>\n<h4>3. execve \u4e0e execl\u3001execvp \u6709\u4ec0\u4e48\u5173\u7cfb&#xff1f;<\/h4>\n<p>\u5728 Linux \u4e0a&#xff0c;\u771f\u6b63\u8fdb\u5165\u5185\u6838\u6267\u884c\u65b0\u7a0b\u5e8f\u7684\u57fa\u7840\u63a5\u53e3\u662f execve\u3002libc \u53c8\u63d0\u4f9b\u4e86\u4e00\u7ec4\u4fbf\u4e8e\u4f7f\u7528\u7684 exec* \u51fd\u6570&#xff1a;<\/p>\n<table>\n<tr>\u51fd\u6570\u53c2\u6570\u5f62\u5f0f\u662f\u5426\u641c\u7d22 PATH\u662f\u5426\u663e\u5f0f\u4f20\u5165\u73af\u5883<\/tr>\n<tbody>\n<tr>\n<td>execl<\/td>\n<td>\u53c2\u6570\u5217\u8868 list<\/td>\n<td>\u5426<\/td>\n<td>\u5426<\/td>\n<\/tr>\n<tr>\n<td>execv<\/td>\n<td>\u53c2\u6570\u6570\u7ec4 vector<\/td>\n<td>\u5426<\/td>\n<td>\u5426<\/td>\n<\/tr>\n<tr>\n<td>execlp<\/td>\n<td>\u53c2\u6570\u5217\u8868<\/td>\n<td>\u662f<\/td>\n<td>\u5426<\/td>\n<\/tr>\n<tr>\n<td>execvp<\/td>\n<td>\u53c2\u6570\u6570\u7ec4<\/td>\n<td>\u662f<\/td>\n<td>\u5426<\/td>\n<\/tr>\n<tr>\n<td>execle<\/td>\n<td>\u53c2\u6570\u5217\u8868<\/td>\n<td>\u5426<\/td>\n<td>\u662f<\/td>\n<\/tr>\n<tr>\n<td>execve<\/td>\n<td>\u53c2\u6570\u6570\u7ec4<\/td>\n<td>\u5426<\/td>\n<td>\u662f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u53ef\u4ee5\u7528\u540e\u7f00\u5e2e\u52a9\u8bb0\u5fc6&#xff1a;<\/p>\n<ul>\n<li>l&#xff1a;list&#xff0c;\u53c2\u6570\u9010\u4e2a\u5199\u51fa&#xff1b;<\/li>\n<li>v&#xff1a;vector&#xff0c;\u53c2\u6570\u653e\u5728\u6570\u7ec4\u4e2d&#xff1b;<\/li>\n<li>p&#xff1a;\u6309\u7167 PATH \u641c\u7d22\u53ef\u6267\u884c\u6587\u4ef6&#xff1b;<\/li>\n<li>e&#xff1a;\u663e\u5f0f\u63d0\u4f9b environment\u3002<\/li>\n<\/ul>\n<p>\u56e0\u6b64&#xff1a;<\/p>\n<p><span class=\"token keyword\">char<\/span> <span class=\"token operator\">*<\/span>args<span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;ls&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;-l&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token constant\">NULL<\/span><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">execvp<\/span><span class=\"token punctuation\">(<\/span>args<span class=\"token punctuation\">[<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span> args<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u4f1a\u6839\u636e PATH \u67e5\u627e ls&#xff1b;\u800c&#xff1a;<\/p>\n<p><span class=\"token function\">execve<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\/bin\/ls&#034;<\/span><span class=\"token punctuation\">,<\/span> args<span class=\"token punctuation\">,<\/span> envp<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u5219\u76f4\u63a5\u6267\u884c\u7ed9\u5b9a\u8def\u5f84\u3002<\/p>\n<h4>4. PATH \u4e3a\u4ec0\u4e48\u4f1a\u5f71\u54cd\u7a0b\u5e8f\u6267\u884c&#xff1f;<\/h4>\n<p>PATH \u4fdd\u5b58\u4e86\u4e00\u7ec4\u7528\u5192\u53f7\u5206\u9694\u7684\u76ee\u5f55&#xff1a;<\/p>\n<p><span class=\"token builtin class-name\">echo<\/span> <span class=\"token string\">&#034;<span class=\"token environment constant\">$PATH<\/span>&#034;<\/span><\/p>\n<p>\u5f53 shell \u6216 execvp \u9700\u8981\u6267\u884c\u4e00\u4e2a\u4e0d\u5e26 \/ \u7684\u7a0b\u5e8f\u540d\u65f6&#xff0c;\u4f1a\u6309\u7167 PATH \u4e2d\u7684\u76ee\u5f55\u987a\u5e8f\u5c1d\u8bd5\u67e5\u627e\u3002<\/p>\n<p>\u4f8b\u5982 gcc \u5728\u7f16\u8bd1\u8fc7\u7a0b\u4e2d\u8fd8\u9700\u8981\u8c03\u7528\u6c47\u7f16\u5668\u3001\u94fe\u63a5\u5668\u7b49\u5176\u4ed6\u7a0b\u5e8f\u3002\u5982\u679c\u628a PATH \u6e05\u7a7a&#xff1a;<\/p>\n<p><span class=\"token assign-left variable\"><span class=\"token environment constant\">PATH<\/span><\/span><span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;&#034;<\/span> \/usr\/bin\/gcc a.c<\/p>\n<p>\u5373\u4f7f \/usr\/bin\/gcc \u672c\u8eab\u80fd\u591f\u542f\u52a8&#xff0c;\u5b83\u4e5f\u53ef\u80fd\u627e\u4e0d\u5230\u540e\u7eed\u9700\u8981\u6267\u884c\u7684\u5de5\u5177\u3002<\/p>\n<p>\u8fd9\u91cc\u8981\u533a\u5206\u4e24\u5c42&#xff1a;<\/p>\n<ul>\n<li>execve \u8d1f\u8d23\u6267\u884c\u4e00\u4e2a\u660e\u786e\u8def\u5f84&#xff1b;<\/li>\n<li>shell \u6216\u5e26 p \u540e\u7f00\u7684 libc \u5305\u88c5\u51fd\u6570\u8d1f\u8d23\u641c\u7d22 PATH&#xff0c;\u6700\u7ec8\u4ecd\u4f1a\u843d\u5230\u7c7b\u4f3c execve \u7684\u5e95\u5c42\u6267\u884c\u64cd\u4f5c\u4e0a\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>\u4e5d\u3001wait \u548c waitpid&#xff1a;\u7236\u8fdb\u7a0b\u5982\u4f55\u7b49\u5f85\u5e76\u56de\u6536\u5b50\u8fdb\u7a0b&#xff1f;<\/h3>\n<p>fork \u4e4b\u540e\u7236\u5b50\u8fdb\u7a0b\u5e76\u53d1\u6267\u884c&#xff0c;\u7236\u8fdb\u7a0b\u6709\u65f6\u9700\u8981\u7b49\u5b50\u8fdb\u7a0b\u7ed3\u675f\u518d\u7ee7\u7eed\u3002\u6700\u5e38\u7528\u7684\u63a5\u53e3\u662f&#xff1a;<\/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\/wait.h&gt;<\/span><\/span><\/p>\n<p><span class=\"token class-name\">pid_t<\/span> <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<span class=\"token class-name\">pid_t<\/span> <span class=\"token function\">waitpid<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">pid_t<\/span> 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><\/p>\n<p>wait(NULL) \u7b49\u5f85\u4efb\u610f\u4e00\u4e2a\u5b50\u8fdb\u7a0b&#xff1b;waitpid \u53ef\u4ee5\u6307\u5b9a\u7b49\u5f85\u54ea\u4e2a\u5b50\u8fdb\u7a0b&#xff0c;\u5e76\u63d0\u4f9b\u66f4\u591a\u9009\u9879\u3002<\/p>\n<p>\u4e00\u4e2a\u5178\u578b\u5199\u6cd5\u662f&#xff1a;<\/p>\n<p><span class=\"token keyword\">int<\/span> status<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token class-name\">pid_t<\/span> result <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">waitpid<\/span><span class=\"token punctuation\">(<\/span>pid<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>status<span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>result <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 punctuation\">{<\/span><br \/>\n    <span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;waitpid&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <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 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 exited with status %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> <span class=\"token keyword\">else<\/span> <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token function\">WIFSIGNALED<\/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 function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;child killed by signal %d\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n           <span class=\"token function\">WTERMSIG<\/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><\/p>\n<p>\u51e0\u4e2a\u5e38\u89c1\u5b8f\u7684\u542b\u4e49&#xff1a;<\/p>\n<table>\n<tr>\u5b8f\u542b\u4e49<\/tr>\n<tbody>\n<tr>\n<td>WIFEXITED(status)<\/td>\n<td>\u5b50\u8fdb\u7a0b\u662f\u5426\u6b63\u5e38\u9000\u51fa<\/td>\n<\/tr>\n<tr>\n<td>WEXITSTATUS(status)<\/td>\n<td>\u6b63\u5e38\u9000\u51fa\u65f6\u7684\u9000\u51fa\u7801<\/td>\n<\/tr>\n<tr>\n<td>WIFSIGNALED(status)<\/td>\n<td>\u662f\u5426\u88ab\u4fe1\u53f7\u7ec8\u6b62<\/td>\n<\/tr>\n<tr>\n<td>WTERMSIG(status)<\/td>\n<td>\u5bfc\u81f4\u7ec8\u6b62\u7684\u4fe1\u53f7\u7f16\u53f7<\/td>\n<\/tr>\n<tr>\n<td>WIFSTOPPED(status)<\/td>\n<td>\u662f\u5426\u56e0\u4fe1\u53f7\u800c\u505c\u6b62<\/td>\n<\/tr>\n<tr>\n<td>WNOHANG<\/td>\n<td>\u4f5c\u4e3a waitpid \u9009\u9879\u65f6&#xff0c;\u4e0d\u963b\u585e\u7b49\u5f85<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>wait \u4e0d\u53ea\u662f\u201c\u8ba9\u7236\u8fdb\u7a0b\u6682\u505c\u201d&#xff0c;\u8fd8\u627f\u62c5\u4e86\u56de\u6536\u5b50\u8fdb\u7a0b\u9000\u51fa\u4fe1\u606f\u7684\u8d23\u4efb\u3002\u5982\u679c\u7236\u8fdb\u7a0b\u4e00\u76f4\u4e0d\u7b49\u5f85\u5df2\u7ecf\u9000\u51fa\u7684\u5b50\u8fdb\u7a0b&#xff0c;\u5b50\u8fdb\u7a0b\u5c31\u53ef\u80fd\u4ee5\u50f5\u5c38\u72b6\u6001\u7559\u5728\u8fdb\u7a0b\u8868\u4e2d\u3002<\/p>\n<hr \/>\n<h3>\u5341\u3001exit \u4e0e _exit&#xff1a;\u770b\u8d77\u6765\u76f8\u4f3c&#xff0c;\u884c\u4e3a\u5e76\u4e0d\u76f8\u540c<\/h3>\n<p>\u8bfe\u7a0b\u4e2d\u7528 _exit \u8868\u793a\u7acb\u5373\u9500\u6bc1\u8fdb\u7a0b&#xff1a;<\/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><\/p>\n<p><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>\u4f46 C \u7a0b\u5e8f\u4e2d\u8fd8\u7ecf\u5e38\u89c1\u5230&#xff1a;<\/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;stdlib.h&gt;<\/span><\/span><\/p>\n<p><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>\u4e8c\u8005\u6700\u5173\u952e\u7684\u5dee\u5f02\u662f&#xff1a;<\/p>\n<table>\n<tr>exit_exit<\/tr>\n<tbody>\n<tr>\n<td>libc \u51fd\u6570<\/td>\n<td>POSIX \u8fdb\u7a0b\u7ec8\u6b62\u63a5\u53e3<\/td>\n<\/tr>\n<tr>\n<td>\u5237\u65b0\u5e76\u5173\u95ed stdio \u6d41<\/td>\n<td>\u4e0d\u5237\u65b0 stdio \u6d41<\/td>\n<\/tr>\n<tr>\n<td>\u8c03\u7528 atexit \u6ce8\u518c\u7684\u51fd\u6570<\/td>\n<td>\u4e0d\u8c03\u7528 atexit \u5904\u7406\u51fd\u6570<\/td>\n<\/tr>\n<tr>\n<td>\u5b8c\u6210\u7528\u6237\u6001\u6e05\u7406\u540e\u7ec8\u6b62\u8fdb\u7a0b<\/td>\n<td>\u66f4\u76f4\u63a5\u5730\u7ec8\u6b62\u8fdb\u7a0b<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48 fork \u540e\u7684\u5b50\u8fdb\u7a0b\u5728 execve \u5931\u8d25\u65f6&#xff0c;\u901a\u5e38\u5199\u6210&#xff1a;<\/p>\n<p><span class=\"token function\">execve<\/span><span class=\"token punctuation\">(<\/span>path<span class=\"token punctuation\">,<\/span> argv<span class=\"token punctuation\">,<\/span> envp<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;execve&#034;<\/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\">127<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u800c\u4e0d\u662f\u76f4\u63a5\u8c03\u7528 exit\u3002\u5982\u679c\u7236\u8fdb\u7a0b\u5728 fork \u524d\u5b58\u5728\u5c1a\u672a\u5237\u65b0\u7684 stdio \u7f13\u51b2&#xff0c;\u5b50\u8fdb\u7a0b\u8c03\u7528 exit \u53ef\u80fd\u518d\u6b21\u5237\u65b0\u88ab\u590d\u5236\u7684\u7f13\u51b2&#xff0c;\u9020\u6210\u91cd\u590d\u8f93\u51fa\u3002<\/p>\n<p>\u5728 Linux \u7684\u5b9e\u73b0\u7ec6\u8282\u4e2d&#xff0c;\u8fdb\u7a0b\u4e0e\u7ebf\u7a0b\u8fd8\u4f1a\u5e26\u6765 exit \u548c exit_group \u7684\u533a\u522b\u3002\u73b0\u4ee3 glibc \u7684 _exit \u5305\u88c5\u51fd\u6570\u4f1a\u7ec8\u6b62\u6574\u4e2a\u8fdb\u7a0b\u4e2d\u7684\u7ebf\u7a0b\u7ec4&#xff0c;\u800c\u539f\u59cb\u5185\u6838\u7cfb\u7edf\u8c03\u7528\u5c42\u9762\u7684\u884c\u4e3a\u9700\u8981\u7ed3\u5408\u5177\u4f53\u63a5\u53e3\u7406\u89e3\u3002\u521d\u5b66\u9636\u6bb5\u5148\u638c\u63e1\u201cexit \u4f1a\u505a libc \u6e05\u7406&#xff0c;_exit \u4e0d\u4f1a\u5237\u65b0 stdio\u201d\u5373\u53ef\u3002<\/p>\n<hr \/>\n<h3>\u5341\u4e00\u3001\u4e00\u4e2a\u5b8c\u6574\u7684 fork &#043; execve &#043; waitpid<\/h3>\n<p>\u4e0b\u9762\u7684\u7a0b\u5e8f\u5c55\u793a\u4e86 UNIX \u521b\u5efa\u65b0\u7a0b\u5e8f\u7684\u6807\u51c6\u9aa8\u67b6&#xff1a;<\/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;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\/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 \/>\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><\/p>\n<p><span class=\"token keyword\">extern<\/span> <span class=\"token keyword\">char<\/span> <span class=\"token operator\">*<\/span><span class=\"token operator\">*<\/span>environ<span class=\"token punctuation\">;<\/span><\/p>\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> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token class-name\">pid_t<\/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><\/p>\n<p>    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>pid <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 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><br \/>\n        <span class=\"token keyword\">return<\/span> EXIT_FAILURE<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <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 keyword\">char<\/span> <span class=\"token operator\">*<\/span>argv<span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token string\">&#034;echo&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n            <span class=\"token string\">&#034;Hello from the new program!&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n            <span class=\"token constant\">NULL<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token function\">execve<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\/bin\/echo&#034;<\/span><span class=\"token punctuation\">,<\/span> argv<span class=\"token punctuation\">,<\/span> environ<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u53ea\u6709 execve \u5931\u8d25\u65f6\u624d\u4f1a\u5230\u8fbe\u8fd9\u91cc<\/span><br \/>\n        <span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;execve&#034;<\/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\">127<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token keyword\">int<\/span> status<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token function\">waitpid<\/span><span class=\"token punctuation\">(<\/span>pid<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>status<span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/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 punctuation\">{<\/span><br \/>\n        <span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;waitpid&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> EXIT_FAILURE<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <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 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 %ld exited with status %d\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n               <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">long<\/span><span class=\"token punctuation\">)<\/span>pid<span class=\"token punctuation\">,<\/span> <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> <span class=\"token keyword\">else<\/span> <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token function\">WIFSIGNALED<\/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 function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;child %ld was killed by signal %d\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n               <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">long<\/span><span class=\"token punctuation\">)<\/span>pid<span class=\"token punctuation\">,<\/span> <span class=\"token function\">WTERMSIG<\/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><\/p>\n<p>    <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<p>\u7f16\u8bd1\u8fd0\u884c&#xff1a;<\/p>\n<p>gcc <span class=\"token parameter variable\">-Wall<\/span> <span class=\"token parameter variable\">-Wextra<\/span> <span class=\"token parameter variable\">-O2<\/span> spawn_demo.c <span class=\"token parameter variable\">-o<\/span> spawn_demo<br \/>\n.\/spawn_demo<\/p>\n<p>\u6574\u4e2a\u8fc7\u7a0b\u53ef\u4ee5\u6309\u65f6\u95f4\u987a\u5e8f\u7406\u89e3&#xff1a;<\/p>\n<li>\u7236\u8fdb\u7a0b\u8c03\u7528 fork&#xff1b;<\/li>\n<li>\u64cd\u4f5c\u7cfb\u7edf\u521b\u5efa\u5b50\u8fdb\u7a0b&#xff1b;<\/li>\n<li>\u5b50\u8fdb\u7a0b\u8c03\u7528 execve&#xff0c;\u628a\u81ea\u8eab\u66ff\u6362\u6210 \/bin\/echo&#xff1b;<\/li>\n<li>\u7236\u8fdb\u7a0b\u5728 waitpid \u4e2d\u7b49\u5f85&#xff1b;<\/li>\n<li>echo \u8f93\u51fa\u5e76\u9000\u51fa&#xff1b;<\/li>\n<li>\u7236\u8fdb\u7a0b\u83b7\u5f97\u9000\u51fa\u72b6\u6001&#xff0c;\u56de\u6536\u5b50\u8fdb\u7a0b\u4fe1\u606f&#xff1b;<\/li>\n<li>\u7236\u8fdb\u7a0b\u7ee7\u7eed\u6267\u884c\u5e76\u9000\u51fa\u3002<\/li>\n<p>\u8fd9\u5c31\u662f\u4e00\u4e2a\u6700\u5c0f\u7248 shell \u7684\u6838\u5fc3\u6d41\u7a0b\u3002<\/p>\n<hr \/>\n<h3>\u5341\u4e8c\u3001\u4e3a\u4ec0\u4e48 UNIX \u8981\u628a fork \u548c exec \u5206\u5f00&#xff1f;<\/h3>\n<p>\u4e4d\u770b\u4e4b\u4e0b&#xff0c;\u5148\u590d\u5236\u81ea\u5df1&#xff0c;\u518d\u7acb\u5373\u628a\u5b50\u8fdb\u7a0b\u66ff\u6362\u6389&#xff0c;\u4f3c\u4e4e\u6709\u4e9b\u591a\u4f59\u3002OSTEP \u7ed9\u51fa\u7684\u5173\u952e\u7b54\u6848\u662f&#xff1a;<\/p>\n<p>fork \u4e0e exec \u4e4b\u95f4\u7684\u7a7a\u9699&#xff0c;\u8ba9\u7236\u8fdb\u7a0b\u6216\u5b50\u8fdb\u7a0b\u6709\u673a\u4f1a\u4fee\u6539\u65b0\u7a0b\u5e8f\u5373\u5c06\u7ee7\u627f\u7684\u8fd0\u884c\u73af\u5883\u3002<\/p>\n<p>\u4f8b\u5982\u6267\u884c&#xff1a;<\/p>\n<p><span class=\"token function\">wc<\/span> <span class=\"token parameter variable\">-l<\/span> input.txt <span class=\"token operator\">&gt;<\/span> result.txt<\/p>\n<p>shell \u53ef\u4ee5\u5728\u5b50\u8fdb\u7a0b\u4e2d\u5148\u5b8c\u6210\u8fd9\u4e9b\u64cd\u4f5c&#xff1a;<\/p>\n<li>fork \u521b\u5efa\u5b50\u8fdb\u7a0b&#xff1b;<\/li>\n<li>\u5b50\u8fdb\u7a0b\u6253\u5f00 result.txt&#xff1b;<\/li>\n<li>\u4f7f\u7528 dup2 \u8ba9\u6807\u51c6\u8f93\u51fa STDOUT_FILENO \u6307\u5411\u8be5\u6587\u4ef6&#xff1b;<\/li>\n<li>\u8c03\u7528 execvp \u6267\u884c wc&#xff1b;<\/li>\n<li>wc \u5e76\u4e0d\u77e5\u9053\u81ea\u5df1\u88ab\u91cd\u5b9a\u5411&#xff0c;\u4ecd\u7136\u53ea\u5411\u6807\u51c6\u8f93\u51fa\u5199\u6570\u636e&#xff1b;<\/li>\n<li>\u7531\u4e8e\u6587\u4ef6\u63cf\u8ff0\u7b26\u8de8 exec \u4fdd\u7559&#xff0c;\u8f93\u51fa\u6700\u7ec8\u8fdb\u5165 result.txt\u3002<\/li>\n<p>\u7ba1\u9053\u7684\u601d\u8def\u4e5f\u7c7b\u4f3c&#xff1a;<\/p>\n<p><span class=\"token function\">cat<\/span> input.txt <span class=\"token operator\">|<\/span> <span class=\"token function\">grep<\/span> hello<\/p>\n<p>shell \u5148\u521b\u5efa\u7ba1\u9053&#xff0c;\u518d\u901a\u8fc7 fork \u5efa\u7acb\u5b50\u8fdb\u7a0b&#xff0c;\u5e76\u5728 exec \u524d\u628a\u4e0d\u540c\u8fdb\u7a0b\u7684\u6807\u51c6\u8f93\u5165\u3001\u6807\u51c6\u8f93\u51fa\u63a5\u5230\u7ba1\u9053\u4e24\u7aef\u3002<\/p>\n<p>\u56e0\u6b64&#xff0c;\u5206\u79bb fork \u548c exec \u5e76\u4e0d\u662f\u5386\u53f2\u4e0a\u968f\u610f\u7559\u4e0b\u7684\u602a\u63a5\u53e3&#xff0c;\u800c\u662f UNIX \u7ec4\u5408\u5f0f\u8bbe\u8ba1\u7684\u91cd\u8981\u57fa\u7840&#xff1a;<\/p>\n<ul>\n<li>fork \u51b3\u5b9a\u201c\u7531\u54ea\u4e2a\u8fdb\u7a0b\u6765\u6267\u884c\u201d&#xff1b;<\/li>\n<li>\u4e2d\u95f4\u6b65\u9aa4\u51b3\u5b9a\u201c\u4ee5\u600e\u6837\u7684\u73af\u5883\u6267\u884c\u201d&#xff1b;<\/li>\n<li>exec \u51b3\u5b9a\u201c\u6700\u7ec8\u6267\u884c\u54ea\u4e2a\u7a0b\u5e8f\u201d\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>\u5341\u4e09\u3001\u7528 strace \u89c2\u5bdf\u8fdb\u7a0b\u751f\u547d\u5468\u671f<\/h3>\n<p>\u53ea\u770b\u4ee3\u7801\u5bb9\u6613\u628a\u5e93\u51fd\u6570\u3001\u7cfb\u7edf\u8c03\u7528\u548c shell \u884c\u4e3a\u6df7\u5728\u4e00\u8d77\u3002strace \u53ef\u4ee5\u8bb0\u5f55\u7a0b\u5e8f\u4e0e\u5185\u6838\u4ea4\u4e92\u7684\u8fc7\u7a0b\u3002<\/p>\n<p>\u89c2\u5bdf\u8fdb\u7a0b\u76f8\u5173\u7cfb\u7edf\u8c03\u7528&#xff1a;<\/p>\n<p><span class=\"token function\">strace<\/span> <span class=\"token parameter variable\">-f<\/span> <span class=\"token parameter variable\">-e<\/span> <span class=\"token assign-left variable\">trace<\/span><span class=\"token operator\">&#061;<\/span>process .\/spawn_demo<\/p>\n<p>\u5176\u4e2d&#xff1a;<\/p>\n<ul>\n<li>-f \u8868\u793a\u7ee7\u7eed\u8ddf\u8e2a fork \u521b\u5efa\u7684\u5b50\u8fdb\u7a0b&#xff1b;<\/li>\n<li>-e trace&#061;process \u53ea\u663e\u793a\u8fdb\u7a0b\u521b\u5efa\u3001\u6267\u884c\u3001\u7b49\u5f85\u548c\u9000\u51fa\u76f8\u5173\u8c03\u7528\u3002<\/li>\n<\/ul>\n<p>\u89c2\u5bdf execve \u548c\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff1a;<\/p>\n<p><span class=\"token function\">strace<\/span> <span class=\"token parameter variable\">-f<\/span> <span class=\"token parameter variable\">-e<\/span> <span class=\"token assign-left variable\">trace<\/span><span class=\"token operator\">&#061;<\/span>execve,openat,close,dup2 .\/your_program<\/p>\n<p>\u89c2\u5bdf\u8f93\u51fa\u7a76\u7adf\u4f55\u65f6\u8fdb\u5165\u5185\u6838&#xff1a;<\/p>\n<p><span class=\"token function\">strace<\/span> <span class=\"token parameter variable\">-f<\/span> <span class=\"token parameter variable\">-e<\/span> <span class=\"token assign-left variable\">trace<\/span><span class=\"token operator\">&#061;<\/span>write .\/a.out<\/p>\n<p>\u5728\u73b0\u4ee3 Linux\/glibc \u4e0a&#xff0c;\u4f60\u8fd8\u53ef\u80fd\u770b\u5230&#xff1a;<\/p>\n<ul>\n<li>\u6e90\u4ee3\u7801\u8c03\u7528\u7684\u662f fork&#xff0c;\u8ddf\u8e2a\u7ed3\u679c\u4e2d\u51fa\u73b0 clone \u6216 clone3&#xff1b;<\/li>\n<li>\u6e90\u4ee3\u7801\u8c03\u7528 _exit&#xff0c;\u5e95\u5c42\u51fa\u73b0 exit_group&#xff1b;<\/li>\n<li>printf \u5e76\u4e0d\u7acb\u523b\u5bf9\u5e94 write&#xff0c;\u800c\u662f\u5728\u7f13\u51b2\u533a\u5237\u65b0\u65f6\u96c6\u4e2d\u5199\u51fa\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u5e76\u4e0d\u77db\u76fe\u3002\u5e94\u7528\u7a0b\u5e8f\u8c03\u7528\u7684 libc \u63a5\u53e3\u53ef\u4ee5\u5728\u5185\u90e8\u9009\u62e9\u5408\u9002\u7684\u5e95\u5c42\u7cfb\u7edf\u8c03\u7528\u3002\u5b66\u4e60\u64cd\u4f5c\u7cfb\u7edf\u65f6&#xff0c;\u8981\u6709\u610f\u8bc6\u5730\u533a\u5206&#xff1a;<\/p>\n<li>\u6e90\u4ee3\u7801\u4e2d\u7684\u5e93\u51fd\u6570&#xff1b;<\/li>\n<li>\u7528\u6237\u6001\u8fd0\u884c\u5e93\u7684\u5b9e\u73b0&#xff1b;<\/li>\n<li>\u771f\u6b63\u8fdb\u5165\u5185\u6838\u7684\u7cfb\u7edf\u8c03\u7528&#xff1b;<\/li>\n<li>\u5185\u6838\u5185\u90e8\u5b8c\u6210\u5de5\u4f5c\u7684\u673a\u5236\u3002<\/li>\n<hr \/>\n<h3>\u5341\u56db\u3001\u7406\u89e3\u72b6\u6001\u673a<\/h3>\n<p>\u628a\u6574\u8282\u8bfe\u538b\u7f29\u6210\u4e00\u6761\u4e3b\u7ebf&#xff0c;\u53ef\u4ee5\u5f97\u5230&#xff1a;<\/p>\n<li>\u7a0b\u5e8f\u63cf\u8ff0\u72b6\u6001\u673a\u7684\u521d\u59cb\u72b6\u6001\u548c\u8fc1\u79fb\u89c4\u5219&#xff1b;<\/li>\n<li>\u8fdb\u7a0b\u662f\u72b6\u6001\u673a\u6b63\u5728\u8fd0\u884c\u7684\u5b9e\u4f8b&#xff1b;<\/li>\n<li>\u64cd\u4f5c\u7cfb\u7edf\u4fdd\u5b58\u8fdb\u7a0b\u72b6\u6001&#xff0c;\u5e76\u5728\u591a\u4e2a\u8fdb\u7a0b\u4e4b\u95f4\u5206\u914d CPU&#xff1b;<\/li>\n<li>fork \u590d\u5236\u5f53\u524d\u8fdb\u7a0b&#xff1b;<\/li>\n<li>execve \u7528\u65b0\u7a0b\u5e8f\u66ff\u6362\u5f53\u524d\u8fdb\u7a0b&#xff1b;<\/li>\n<li>_exit \u7ec8\u6b62\u8fdb\u7a0b\u5e76\u7559\u4e0b\u9000\u51fa\u72b6\u6001&#xff1b;<\/li>\n<li>wait \/ waitpid \u8ba9\u7236\u8fdb\u7a0b\u7b49\u5f85\u5e76\u56de\u6536\u5b50\u8fdb\u7a0b&#xff1b;<\/li>\n<li>shell \u901a\u8fc7\u7ec4\u5408\u8fd9\u4e9b\u63a5\u53e3\u5b9e\u73b0\u547d\u4ee4\u6267\u884c\u3001\u91cd\u5b9a\u5411\u548c\u7ba1\u9053\u3002<\/li>\n<p>\u56e0\u6b64&#xff0c;\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u4e0d\u53ea\u662f\u56fa\u5b9a\u6a21\u677f&#xff1a;<\/p>\n<p><span class=\"token class-name\">pid_t<\/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><\/p>\n<p><span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>pid <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 punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u521b\u5efa\u5931\u8d25<\/span><br \/>\n<span class=\"token punctuation\">}<\/span> <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><br \/>\n    <span class=\"token comment\">\/\/ \u5b50\u8fdb\u7a0b&#xff1a;\u8bbe\u7f6e\u73af\u5883\u3001\u91cd\u5b9a\u5411\u3001exec<\/span><br \/>\n<span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u7236\u8fdb\u7a0b&#xff1a;\u7ee7\u7eed\u5de5\u4f5c\u6216\u7b49\u5f85\u5b50\u8fdb\u7a0b<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u5b83\u5b9e\u9645\u4e0a\u8868\u8fbe\u4e86\u4e00\u5957\u5b8c\u6574\u7684\u72b6\u6001\u673a\u7ba1\u7406\u601d\u60f3&#xff1a;<\/p>\n<p>\u590d\u5236\u72b6\u6001 \u2192 \u8c03\u6574\u73af\u5883 \u2192 \u88c5\u5165\u65b0\u7a0b\u5e8f \u2192 \u7b49\u5f85\u7ed3\u679c \u2192 \u56de\u6536\u72b6\u6001<\/p>\n<p>\u7406\u89e3\u5230\u8fd9\u91cc&#xff0c;\u8fdb\u7a0b\u5c31\u4e0d\u518d\u662f\u8bfe\u672c\u91cc\u4e00\u53e5\u201c\u6b63\u5728\u8fd0\u884c\u7684\u7a0b\u5e8f\u201d&#xff0c;\u800c\u662f\u4e00\u4e2a\u53ef\u4ee5\u88ab\u89c2\u5bdf\u3001\u590d\u5236\u3001\u66ff\u6362\u3001\u8c03\u5ea6\u548c\u56de\u6536\u7684\u771f\u5b9e\u5bf9\u8c61\u3002<\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>\u3010\u64cd\u4f5c\u7cfb\u7edf\u3011\u7a0b\u5e8f\u4e0e\u8fdb\u7a0b&#xff1a;\u4ece\u72b6\u6001\u673a\u5230 fork\u3001execve \u4e0e\u8fdb\u7a0b\u751f\u547d\u5468\u671f &#x1f525; \u661f\u6052\u968f\u98ce&#xff1a;\u4e2a\u4eba\u4e3b\u9875\u2744\ufe0f \u4e2a\u4eba\u4e13\u680f&#xff1a;\u300a\u6307\u9488\u5408\u96c6\u300b&#xff5c;\u300aC\u8bed\u8a00\u57fa\u7840\u300b&#xff5c;\u300a\u6570\u636e\u7ed3\u6784\u300b&#xff5c;\u300a\u673a\u5668\u5b66\u4e60\u5bfc\u8bba\u300b&#xff5c;\u300a\u524d\u7aef\u57fa\u7840\u300b&#xff5c; \u300apython\u57fa\u7840\u300b&#xff5c; \u300aC\u4ece\u5165\u95e8\u5230\u5165\u571f\u300b \u2728 \u6570\u636e\u5373\u77e5\u8bc6&#xff0c;\u538b\u7f29\u5373\u667a\u80fd\u6587\u7ae0\u76ee\u5f55\u3010\u64cd\u4f5c\u7cfb\u7edf\u3011\u7a0b\u5e8f\u4e0e\u8fdb\u7a0b&#xff1a;\u4ece\u72b6\u6001\u673a\u5230 fo<\/p>\n","protected":false},"author":2,"featured_media":83544,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[9099,174,58,371,190,305],"topic":[],"class_list":["post-83545","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-9099","tag-c","tag-linux","tag-371","tag-190","tag-305"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4ece0\u5f00\u59cb\u7684\u64cd\u4f5c\u7cfb\u7edf\uff084\uff09 - \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\/83545.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4ece0\u5f00\u59cb\u7684\u64cd\u4f5c\u7cfb\u7edf\uff084\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u3010\u64cd\u4f5c\u7cfb\u7edf\u3011\u7a0b\u5e8f\u4e0e\u8fdb\u7a0b&#xff1a;\u4ece\u72b6\u6001\u673a\u5230 fork\u3001execve \u4e0e\u8fdb\u7a0b\u751f\u547d\u5468\u671f &#x1f525; \u661f\u6052\u968f\u98ce&#xff1a;\u4e2a\u4eba\u4e3b\u9875\u2744\ufe0f \u4e2a\u4eba\u4e13\u680f&#xff1a;\u300a\u6307\u9488\u5408\u96c6\u300b&#xff5c;\u300aC\u8bed\u8a00\u57fa\u7840\u300b&#xff5c;\u300a\u6570\u636e\u7ed3\u6784\u300b&#xff5c;\u300a\u673a\u5668\u5b66\u4e60\u5bfc\u8bba\u300b&#xff5c;\u300a\u524d\u7aef\u57fa\u7840\u300b&#xff5c; \u300apython\u57fa\u7840\u300b&#xff5c; \u300aC\u4ece\u5165\u95e8\u5230\u5165\u571f\u300b \u2728 \u6570\u636e\u5373\u77e5\u8bc6&#xff0c;\u538b\u7f29\u5373\u667a\u80fd\u6587\u7ae0\u76ee\u5f55\u3010\u64cd\u4f5c\u7cfb\u7edf\u3011\u7a0b\u5e8f\u4e0e\u8fdb\u7a0b&#xff1a;\u4ece\u72b6\u6001\u673a\u5230 fo\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/83545.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-25T17:40:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260725174050-6a64f52202269.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=\"9 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/83545.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/83545.html\",\"name\":\"\u4ece0\u5f00\u59cb\u7684\u64cd\u4f5c\u7cfb\u7edf\uff084\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-07-25T17:40:51+00:00\",\"dateModified\":\"2026-07-25T17:40:51+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/83545.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/83545.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/83545.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4ece0\u5f00\u59cb\u7684\u64cd\u4f5c\u7cfb\u7edf\uff084\uff09\"}]},{\"@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":"\u4ece0\u5f00\u59cb\u7684\u64cd\u4f5c\u7cfb\u7edf\uff084\uff09 - \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\/83545.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4ece0\u5f00\u59cb\u7684\u64cd\u4f5c\u7cfb\u7edf\uff084\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u3010\u64cd\u4f5c\u7cfb\u7edf\u3011\u7a0b\u5e8f\u4e0e\u8fdb\u7a0b&#xff1a;\u4ece\u72b6\u6001\u673a\u5230 fork\u3001execve \u4e0e\u8fdb\u7a0b\u751f\u547d\u5468\u671f &#x1f525; \u661f\u6052\u968f\u98ce&#xff1a;\u4e2a\u4eba\u4e3b\u9875\u2744\ufe0f \u4e2a\u4eba\u4e13\u680f&#xff1a;\u300a\u6307\u9488\u5408\u96c6\u300b&#xff5c;\u300aC\u8bed\u8a00\u57fa\u7840\u300b&#xff5c;\u300a\u6570\u636e\u7ed3\u6784\u300b&#xff5c;\u300a\u673a\u5668\u5b66\u4e60\u5bfc\u8bba\u300b&#xff5c;\u300a\u524d\u7aef\u57fa\u7840\u300b&#xff5c; \u300apython\u57fa\u7840\u300b&#xff5c; \u300aC\u4ece\u5165\u95e8\u5230\u5165\u571f\u300b \u2728 \u6570\u636e\u5373\u77e5\u8bc6&#xff0c;\u538b\u7f29\u5373\u667a\u80fd\u6587\u7ae0\u76ee\u5f55\u3010\u64cd\u4f5c\u7cfb\u7edf\u3011\u7a0b\u5e8f\u4e0e\u8fdb\u7a0b&#xff1a;\u4ece\u72b6\u6001\u673a\u5230 fo","og_url":"https:\/\/www.wsisp.com\/helps\/83545.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-07-25T17:40:51+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260725174050-6a64f52202269.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"9 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/83545.html","url":"https:\/\/www.wsisp.com\/helps\/83545.html","name":"\u4ece0\u5f00\u59cb\u7684\u64cd\u4f5c\u7cfb\u7edf\uff084\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-07-25T17:40:51+00:00","dateModified":"2026-07-25T17:40:51+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/83545.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/83545.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/83545.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4ece0\u5f00\u59cb\u7684\u64cd\u4f5c\u7cfb\u7edf\uff084\uff09"}]},{"@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\/83545","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=83545"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/83545\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/83544"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=83545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=83545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=83545"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=83545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}