{"id":63155,"date":"2025-04-29T11:50:54","date_gmt":"2025-04-29T03:50:54","guid":{"rendered":"https:\/\/fwq.ai\/blog\/63155\/"},"modified":"2025-04-29T11:50:54","modified_gmt":"2025-04-29T03:50:54","slug":"linux%e4%b8%adfork%e5%87%bd%e6%95%b0%e8%af%a6%e8%a7%a3-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/63155\/","title":{"rendered":"Linux\u4e2dfork\u51fd\u6570\u8be6\u89e3"},"content":{"rendered":"<p><span>fork()\u662f\u548c\u5176\u4ed6unix-like\u7cfb\u7edf\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u4e00\u4e2a\u7cfb\u7edf\u8c03\u7528\uff0c\u5b83\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\uff0c\u8fd9\u4e2a\u65b0\u7684\u8fdb\u7a0b\u662f\u5f53\u524d\u8fdb\u7a0b\u7684\u526f\u672c\uff0c\u79f0\u4e3a\u5b50\u8fdb\u7a0b\u3002\u5b50\u8fdb\u7a0b\u4f1a\u83b7\u5f97\u7236\u8fdb\u7a0b\u4ee3\u7801\u3001\u6570\u636e\u3001\u5806\u3001\u6808\u7b49\u5185\u5bb9\u7684\u526f\u672c\uff0c\u4f46\u4e24\u4e2a\u8fdb\u7a0b\u62e5\u6709\u4e0d\u540c\u7684\u8fdb\u7a0bid\u548c\u4e00\u4e9b\u5176\u4ed6\u8d44\u6e90\uff0c\u6bd4\u5982\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/span><\/p>\n<p><span>\u4e0b\u9762\u662ffork()\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5\u548c\u6ce8\u610f\u4e8b\u9879\uff1a<\/span><\/p>\n<p><strong><span>\u51fd\u6570\u539f\u578b<\/span><\/strong><\/p>\n<p><span>c<\/span><\/p>\n<p><span><\/span><\/p>\n<pre>#include&nbsp;&lt;unistd.h&gt;&nbsp;&nbsp;\n&nbsp;&nbsp;\npid_t&nbsp;fork(void);&lt;\/unistd.h&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong><span>\u8fd4\u56de\u503c<\/span><\/strong><\/p>\n<p><span>\u5982\u679cfork()\u5728\u7236\u8fdb\u7a0b\u4e2d\u8c03\u7528\u6210\u529f\uff0c\u5219\u8fd4\u56de\u65b0\u521b\u5efa\u7684\u5b50\u8fdb\u7a0b\u7684\u8fdb\u7a0bID\u3002<\/span><\/p>\n<p><span>\u5982\u679cfork()\u5728\u5b50\u8fdb\u7a0b\u4e2d\u8c03\u7528\u6210\u529f\uff0c\u5219\u8fd4\u56de0\u3002<\/span><\/p>\n<p><span>\u5982\u679cfork()\u8c03\u7528\u5931\u8d25\uff0c\u5219\u8fd4\u56de-1\u3002<\/span><\/p>\n<p><strong><span>\u7279\u6027<\/span><\/strong><\/p>\n<p><span>\u7236\u5b50\u8fdb\u7a0b\uff1a\u8c03\u7528fork()\u7684\u8fdb\u7a0b\u662f\u7236\u8fdb\u7a0b\uff0c\u65b0\u521b\u5efa\u7684\u8fdb\u7a0b\u662f\u5b50\u8fdb\u7a0b\u3002<\/span><\/p>\n<p><span>\u6570\u636e\u590d\u5236\uff1a\u7236\u8fdb\u7a0b\u7684\u6570\u636e\uff08\u5305\u62ec\u4ee3\u7801\u3001\u5806\u3001\u6808\u7b49\uff09\u4f1a\u88ab\u590d\u5236\u5230\u5b50\u8fdb\u7a0b\u4e2d\uff0c\u4f46\u4e24\u4e2a\u8fdb\u7a0b\u62e5\u6709\u72ec\u7acb\u7684\u5730\u5740\u7a7a\u95f4\u3002<\/span><\/p>\n<p><span>\u5f02\u6b65\u6027\uff1afork()\u521b\u5efa\u5b50\u8fdb\u7a0b\u540e\uff0c\u7236\u8fdb\u7a0b\u548c\u5b50\u8fdb\u7a0b\u4ee5\u5f02\u6b65\u7684\u65b9\u5f0f\u6267\u884c\uff0c\u5b83\u4eec\u53ef\u4ee5\u76f8\u4e92\u72ec\u7acb\u5730\u8fd0\u884c\u3002<\/span><\/p>\n<p><span>\u8fdb\u7a0bID\uff1a\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u6709\u4e00\u4e2a\u552f\u4e00\u7684\u8fdb\u7a0bID\uff08PID\uff09\uff0c\u5b50\u8fdb\u7a0b\u7684PID\u4e0e\u7236\u8fdb\u7a0b\u7684PID\u4e0d\u540c\u3002<\/span><\/p>\n<p><strong><span>\u793a\u4f8b<\/span><\/strong><\/p>\n<p><span>c<\/span><\/p>\n<p><span><\/span><\/p>\n<pre>#include&nbsp;&lt;stdio.h&gt;&nbsp;&nbsp;\n#include&nbsp;&lt;stdlib.h&gt;&nbsp;&nbsp;\n#include&nbsp;&lt;unistd.h&gt;&nbsp;&nbsp;\n&nbsp;&nbsp;\nint&nbsp;main()&nbsp;{&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;pid_t&nbsp;pid;&nbsp;&nbsp;\n&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;pid&nbsp;=&nbsp;fork();&nbsp;&nbsp;\n&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(pid&nbsp;&lt;p&gt;&lt;strong&gt;&lt;span&gt;\u6ce8\u610f\u4e8b\u9879&lt;\/span&gt;&lt;\/strong&gt;&lt;\/p&gt;\n&lt;p&gt;&lt;span&gt;\u8d44\u6e90\u590d\u5236\uff1afork()\u4f1a\u590d\u5236\u7236\u8fdb\u7a0b\u7684\u6240\u6709\u8d44\u6e90\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\uff0c\u5c24\u5176\u662f\u5728\u5927\u578b\u7a0b\u5e8f\u4e2d\u3002\u56e0\u6b64\uff0c\u901a\u5e38\u63a8\u8350\u4f7f\u7528exec()\u7cfb\u5217\u51fd\u6570\u5728fork()\u540e\u66ff\u6362\u5b50\u8fdb\u7a0b\u7684\u4ee3\u7801\uff0c\u4ece\u800c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8d44\u6e90\u590d\u5236\u3002&lt;\/span&gt;&lt;\/p&gt;\n&lt;p&gt;&lt;span&gt;\u7ade\u6001\u6761\u4ef6\uff1a\u7531\u4e8e\u7236\u8fdb\u7a0b\u548c\u5b50\u8fdb\u7a0b\u662f\u5f02\u6b65\u6267\u884c\u7684\uff0c\u56e0\u6b64\u53ef\u80fd\u4f1a\u51fa\u73b0\u7ade\u6001\u6761\u4ef6\u3002\u4f8b\u5982\uff0c\u4e24\u4e2a\u8fdb\u7a0b\u53ef\u80fd\u540c\u65f6\u5c1d\u8bd5\u8bbf\u95ee\u6216\u4fee\u6539\u540c\u4e00\u6587\u4ef6\uff0c\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u3002&lt;\/span&gt;&lt;\/p&gt;\n&lt;p&gt;&lt;span&gt;\u9519\u8bef\u5904\u7406\uff1a\u5728\u8c03\u7528fork()\u540e\uff0c\u5e94\u59cb\u7ec8\u68c0\u67e5\u5176\u8fd4\u56de\u503c\uff0c\u4ee5\u5904\u7406\u53ef\u80fd\u7684\u9519\u8bef\u60c5\u51b5\u3002&lt;\/span&gt;&lt;\/p&gt;\n&lt;p&gt;&lt;span&gt;\u603b\u7684\u6765\u8bf4\uff0cfork()\u662fLinux\u7cfb\u7edf\u4e2d\u975e\u5e38\u57fa\u7840\u548c\u91cd\u8981\u7684\u7cfb\u7edf\u8c03\u7528\uff0c\u7528\u4e8e\u521b\u5efa\u65b0\u7684\u8fdb\u7a0b\u3002\u7136\u800c\uff0c\u7531\u4e8e\u5b83\u7684\u590d\u6742\u6027\u548c\u6f5c\u5728\u7684\u6027\u80fd\u95ee\u9898\uff0c\u4f7f\u7528\u65f6\u9700\u8981\u683c\u5916\u5c0f\u5fc3\u3002&lt;\/span&gt;&lt;\/p&gt;&lt;\/unistd.h&gt;&lt;\/stdlib.h&gt;&lt;\/stdio.h&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fLinux\u4e2dfork\u51fd\u6570\u8be6\u89e3\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8FDCServers\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>fork()\u662f\u548c\u5176\u4ed6unix-like\u7cfb\u7edf\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u4e00\u4e2a\u7cfb\u7edf\u8c03\u7528\uff0c\u5b83\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\uff0c\u8fd9\u4e2a\u65b0\u7684\u8fdb\u7a0b\u662f\u5f53\u524d\u8fdb\u7a0b\u7684\u526f\u672c\uff0c\u79f0\u4e3a\u5b50\u8fdb\u7a0b\u3002\u5b50\u8fdb\u7a0b\u4f1a\u83b7\u5f97\u7236\u8fdb\u7a0b\u4ee3\u7801\u3001\u6570\u636e\u3001\u5806\u3001\u6808\u7b49\u5185\u5bb9\u7684\u526f\u672c\uff0c\u4f46\u4e24\u4e2a\u8fdb\u7a0b\u62e5\u6709\u4e0d\u540c\u7684\u8fdb\u7a0bid\u548c\u4e00\u4e9b\u5176\u4ed6\u8d44\u6e90\uff0c\u6bd4\u5982\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002 \u4e0b\u9762\u662ffork()\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5\u548c\u6ce8\u610f\u4e8b\u9879\uff1a \u51fd\u6570\u539f\u578b c #include&nbsp;&lt;unistd.h&gt;&nbsp;&nbsp; &nbsp;&nbsp; pid_t&nbsp;fork(void);&lt;\/unistd.h&gt; \u767b\u5f55\u540e\u590d\u5236 \u8fd4\u56de\u503c \u5982\u679cfork()\u5728\u7236\u8fdb\u7a0b\u4e2d\u8c03\u7528\u6210\u529f\uff0c\u5219\u8fd4\u56de\u65b0\u521b\u5efa\u7684\u5b50\u8fdb\u7a0b\u7684\u8fdb\u7a0bID\u3002 \u5982\u679cfork()\u5728\u5b50\u8fdb\u7a0b\u4e2d\u8c03\u7528\u6210\u529f\uff0c\u5219\u8fd4\u56de0\u3002 \u5982\u679cfork()\u8c03\u7528\u5931\u8d25\uff0c\u5219\u8fd4\u56de-1\u3002 \u7279\u6027 \u7236\u5b50\u8fdb\u7a0b\uff1a\u8c03\u7528fork()\u7684\u8fdb\u7a0b\u662f\u7236\u8fdb\u7a0b\uff0c\u65b0\u521b\u5efa\u7684\u8fdb\u7a0b\u662f\u5b50\u8fdb\u7a0b\u3002 \u6570\u636e\u590d\u5236\uff1a\u7236\u8fdb\u7a0b\u7684\u6570\u636e\uff08\u5305\u62ec\u4ee3\u7801\u3001\u5806\u3001\u6808\u7b49\uff09\u4f1a\u88ab\u590d\u5236\u5230\u5b50\u8fdb\u7a0b\u4e2d\uff0c\u4f46\u4e24\u4e2a\u8fdb\u7a0b\u62e5\u6709\u72ec\u7acb\u7684\u5730\u5740\u7a7a\u95f4\u3002 \u5f02\u6b65\u6027\uff1afork()\u521b\u5efa\u5b50\u8fdb\u7a0b\u540e\uff0c\u7236\u8fdb\u7a0b\u548c\u5b50\u8fdb\u7a0b\u4ee5\u5f02\u6b65\u7684\u65b9\u5f0f\u6267\u884c\uff0c\u5b83\u4eec\u53ef\u4ee5\u76f8\u4e92\u72ec\u7acb\u5730\u8fd0\u884c\u3002 \u8fdb\u7a0bID\uff1a\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u6709\u4e00\u4e2a\u552f\u4e00\u7684\u8fdb\u7a0bID\uff08PID\uff09\uff0c\u5b50\u8fdb\u7a0b\u7684PID\u4e0e\u7236\u8fdb\u7a0b\u7684PID\u4e0d\u540c\u3002 \u793a\u4f8b c #include&nbsp;&lt;stdio.h&gt;&nbsp;&nbsp; #include&nbsp;&lt;stdlib.h&gt;&nbsp;&nbsp; #include&nbsp;&lt;unistd.h&gt;&nbsp;&nbsp; &nbsp;&nbsp; int&nbsp;main()&nbsp;{&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;pid_t&nbsp;pid;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;pid&nbsp;=&nbsp;fork();&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(pid&nbsp;&lt;p&gt;&lt;strong&gt;&lt;span&gt;\u6ce8\u610f\u4e8b\u9879&lt;\/span&gt;&lt;\/strong&gt;&lt;\/p&gt; &lt;p&gt;&lt;span&gt;\u8d44\u6e90\u590d\u5236\uff1afork()\u4f1a\u590d\u5236\u7236\u8fdb\u7a0b\u7684\u6240\u6709\u8d44\u6e90\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\uff0c\u5c24\u5176\u662f\u5728\u5927\u578b\u7a0b\u5e8f\u4e2d\u3002\u56e0\u6b64\uff0c\u901a\u5e38\u63a8\u8350\u4f7f\u7528exec()\u7cfb\u5217\u51fd\u6570\u5728fork()\u540e\u66ff\u6362\u5b50\u8fdb\u7a0b\u7684\u4ee3\u7801\uff0c\u4ece\u800c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8d44\u6e90\u590d\u5236\u3002&lt;\/span&gt;&lt;\/p&gt; &lt;p&gt;&lt;span&gt;\u7ade\u6001\u6761\u4ef6\uff1a\u7531\u4e8e\u7236\u8fdb\u7a0b\u548c\u5b50\u8fdb\u7a0b\u662f\u5f02\u6b65\u6267\u884c\u7684\uff0c\u56e0\u6b64\u53ef\u80fd\u4f1a\u51fa\u73b0\u7ade\u6001\u6761\u4ef6\u3002\u4f8b\u5982\uff0c\u4e24\u4e2a\u8fdb\u7a0b\u53ef\u80fd\u540c\u65f6\u5c1d\u8bd5\u8bbf\u95ee\u6216\u4fee\u6539\u540c\u4e00\u6587\u4ef6\uff0c\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u3002&lt;\/span&gt;&lt;\/p&gt; &lt;p&gt;&lt;span&gt;\u9519\u8bef\u5904\u7406\uff1a\u5728\u8c03\u7528fork()\u540e\uff0c\u5e94\u59cb\u7ec8\u68c0\u67e5\u5176\u8fd4\u56de\u503c\uff0c\u4ee5\u5904\u7406\u53ef\u80fd\u7684\u9519\u8bef\u60c5\u51b5\u3002&lt;\/span&gt;&lt;\/p&gt; &lt;p&gt;&lt;span&gt;\u603b\u7684\u6765\u8bf4\uff0cfork()\u662fLinux\u7cfb\u7edf\u4e2d\u975e\u5e38\u57fa\u7840\u548c\u91cd\u8981\u7684\u7cfb\u7edf\u8c03\u7528\uff0c\u7528\u4e8e\u521b\u5efa\u65b0\u7684\u8fdb\u7a0b\u3002\u7136\u800c\uff0c\u7531\u4e8e\u5b83\u7684\u590d\u6742\u6027\u548c\u6f5c\u5728\u7684\u6027\u80fd\u95ee\u9898\uff0c\u4f7f\u7528\u65f6\u9700\u8981\u683c\u5916\u5c0f\u5fc3\u3002&lt;\/span&gt;&lt;\/p&gt;&lt;\/unistd.h&gt;&lt;\/stdlib.h&gt;&lt;\/stdio.h&gt; \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u5c31\u662fLinux\u4e2dfork\u51fd\u6570\u8be6\u89e3\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8FDCServers\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-63155","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/63155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=63155"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/63155\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=63155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=63155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=63155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}