{"id":15944,"date":"2024-11-18T09:06:05","date_gmt":"2024-11-18T01:06:05","guid":{"rendered":"https:\/\/fwq.ai\/blog\/15944\/"},"modified":"2024-11-18T09:06:05","modified_gmt":"2024-11-18T01:06:05","slug":"linux%e5%8f%af%e4%bb%a5%e9%80%9a%e8%bf%87%e5%ad%90%e8%bf%9b%e7%a8%8b%e6%89%a7%e8%a1%8c%e5%87%bd%e6%95%b0%e5%90%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/15944\/","title":{"rendered":"linux\u53ef\u4ee5\u901a\u8fc7\u5b50\u8fdb\u7a0b\u6267\u884c\u51fd\u6570\u5417"},"content":{"rendered":"<blockquote><p>\n  linux\u4e2d\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u5b50\u8fdb\u7a0b\u5e76\u5728\u5b50\u8fdb\u7a0b\u4e2d\u6267\u884c\u51fd\u6570\u6765\u5b9e\u73b0\u76f8\u5173\u529f\u80fd\uff0c\u901a\u8fc7\u4f7f\u7528\u201cfork()\u201d\u548c\u201cexec()\u201d\u7cfb\u5217\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a1\u3001\u4f7f\u7528\u201cfork()\u201d\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\uff1b2\u3001\u5728\u5b50\u8fdb\u7a0b\u4e2d\uff0c\u4f7f\u7528\u201cexec()\u201d\u7cfb\u5217\u51fd\u6570\u6765\u6267\u884c\u7279\u5b9a\u7684\u51fd\u6570\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202307\/05\/2023070513452940658.jpg\" class=\"aligncenter\" title=\"linux\u53ef\u4ee5\u901a\u8fc7\u5b50\u8fdb\u7a0b\u6267\u884c\u51fd\u6570\u5417\u63d2\u56fe\" alt=\"linux\u53ef\u4ee5\u901a\u8fc7\u5b50\u8fdb\u7a0b\u6267\u884c\u51fd\u6570\u5417\u63d2\u56fe\" \/><\/p>\n<p>\u672c\u6559\u7a0b\u64cd\u4f5c\u7cfb\u7edf\uff1aLinux5.18.14\u7cfb\u7edf\u3001Dell G3\u7535\u8111\u3002<\/p>\n<p><strong>\u5728Linux\u4e2d\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u5b50\u8fdb\u7a0b\u5e76\u5728\u5b50\u8fdb\u7a0b\u4e2d\u6267\u884c\u51fd\u6570\u6765\u5b9e\u73b0\u76f8\u5173\u529f\u80fd\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528fork()\u548cexec()\u7cfb\u5217\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/strong>\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\n<p>\u4f7f\u7528fork()\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u3002fork()\u4f1a\u590d\u5236\u5f53\u524d\u8fdb\u7a0b\u7684\u4ee3\u7801\u3001\u6570\u636e\u548c\u8d44\u6e90\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u4e0e\u7236\u8fdb\u7a0b\u51e0\u4e4e\u5b8c\u5168\u76f8\u540c\u7684\u5b50\u8fdb\u7a0b\u3002\u5728\u7236\u8fdb\u7a0b\u4e2d\uff0cfork()\u8fd4\u56de\u5b50\u8fdb\u7a0b\u7684ID\uff0c\u800c\u5728\u5b50\u8fdb\u7a0b\u4e2d\uff0cfork()\u8fd4\u56de0\u3002\u901a\u8fc7\u5224\u65adfork()\u7684\u8fd4\u56de\u503c\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u533a\u5206\u7236\u8fdb\u7a0b\u548c\u5b50\u8fdb\u7a0b\u7684\u903b\u8f91\u8def\u5f84\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728\u5b50\u8fdb\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528exec()\u7cfb\u5217\u51fd\u6570\u6765\u6267\u884c\u7279\u5b9a\u7684\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u901a\u8fc7execl()\u51fd\u6570\u53ef\u4ee5\u5728\u5b50\u8fdb\u7a0b\u4e2d\u6267\u884c\u6307\u5b9a\u7684\u51fd\u6570\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>#include&nbsp;&lt;stdio.h&gt;\n#include&nbsp;&lt;stdlib.h&gt;\n#include&nbsp;&lt;unistd.h&gt;\nvoid&nbsp;child_function()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;printf(\"This&nbsp;is&nbsp;the&nbsp;child&nbsp;process\n\");\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5b50\u8fdb\u7a0b\u6267\u884c\u7684\u903b\u8f91\u5904\u7406\n}\nint&nbsp;main()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;pid_t&nbsp;pid&nbsp;=&nbsp;fork();\n&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(pid&nbsp;&lt;p&gt;\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cexec()\u7cfb\u5217\u51fd\u6570\u4f1a\u66ff\u6362\u5f53\u524d\u8fdb\u7a0b\u7684\u6620\u50cf\uff0c\u76f4\u63a5\u52a0\u8f7d\u65b0\u7684\u53ef\u6267\u884c\u6587\u4ef6\u6216\u51fd\u6570\uff0c\u5e76\u4e0d\u4f1a\u8fd4\u56de\u539f\u6765\u51fd\u6570\u7684\u4f4d\u7f6e\u3002\u56e0\u6b64\u5982\u679c\u9700\u8981\u5728\u5b50\u8fdb\u7a0b\u4e2d\u6267\u884c\u7279\u5b9a\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e93\u51fd\u6570\u6216\u8005\u5176\u4ed6\u65b9\u6cd5\u6765\u5b8c\u6210\u3002&lt;\/p&gt;\n&lt;p&gt;&lt;strong&gt;&lt;br&gt;&lt;\/strong&gt;&lt;br&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\u662f\u53ef\u4ee5\u901a\u8fc7\u5b50\u8fdb\u7a0b\u6267\u884c\u51fd\u6570\u5417\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>linux\u4e2d\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u5b50\u8fdb\u7a0b\u5e76\u5728\u5b50\u8fdb\u7a0b\u4e2d\u6267\u884c\u51fd\u6570\u6765\u5b9e\u73b0\u76f8\u5173\u529f\u80fd\uff0c\u901a\u8fc7\u4f7f\u7528\u201cfork()\u201d\u548c\u201cexec()\u201d\u7cfb\u5217\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a1\u3001\u4f7f\u7528\u201cfork()\u201d\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\uff1b2\u3001\u5728\u5b50\u8fdb\u7a0b\u4e2d\uff0c\u4f7f\u7528\u201cexec()\u201d\u7cfb\u5217\u51fd\u6570\u6765\u6267\u884c\u7279\u5b9a\u7684\u51fd\u6570\u3002 \u672c\u6559\u7a0b\u64cd\u4f5c\u7cfb\u7edf\uff1aLinux5.18.14\u7cfb\u7edf\u3001Dell G3\u7535\u8111\u3002 \u5728Linux\u4e2d\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u5b50\u8fdb\u7a0b\u5e76\u5728\u5b50\u8fdb\u7a0b\u4e2d\u6267\u884c\u51fd\u6570\u6765\u5b9e\u73b0\u76f8\u5173\u529f\u80fd\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528fork()\u548cexec()\u7cfb\u5217\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a \u4f7f\u7528fork()\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u3002fork()\u4f1a\u590d\u5236\u5f53\u524d\u8fdb\u7a0b\u7684\u4ee3\u7801\u3001\u6570\u636e\u548c\u8d44\u6e90\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u4e0e\u7236\u8fdb\u7a0b\u51e0\u4e4e\u5b8c\u5168\u76f8\u540c\u7684\u5b50\u8fdb\u7a0b\u3002\u5728\u7236\u8fdb\u7a0b\u4e2d\uff0cfork()\u8fd4\u56de\u5b50\u8fdb\u7a0b\u7684ID\uff0c\u800c\u5728\u5b50\u8fdb\u7a0b\u4e2d\uff0cfork()\u8fd4\u56de0\u3002\u901a\u8fc7\u5224\u65adfork()\u7684\u8fd4\u56de\u503c\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u533a\u5206\u7236\u8fdb\u7a0b\u548c\u5b50\u8fdb\u7a0b\u7684\u903b\u8f91\u8def\u5f84\u3002 \u5728\u5b50\u8fdb\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528exec()\u7cfb\u5217\u51fd\u6570\u6765\u6267\u884c\u7279\u5b9a\u7684\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u901a\u8fc7execl()\u51fd\u6570\u53ef\u4ee5\u5728\u5b50\u8fdb\u7a0b\u4e2d\u6267\u884c\u6307\u5b9a\u7684\u51fd\u6570\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a #include&nbsp;&lt;stdio.h&gt; #include&nbsp;&lt;stdlib.h&gt; #include&nbsp;&lt;unistd.h&gt; void&nbsp;child_function()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;This&nbsp;is&nbsp;the&nbsp;child&nbsp;process &#8220;); &nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5b50\u8fdb\u7a0b\u6267\u884c\u7684\u903b\u8f91\u5904\u7406 } int&nbsp;main()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;pid_t&nbsp;pid&nbsp;=&nbsp;fork(); &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(pid&nbsp;&lt;p&gt;\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cexec()\u7cfb\u5217\u51fd\u6570\u4f1a\u66ff\u6362\u5f53\u524d\u8fdb\u7a0b\u7684\u6620\u50cf\uff0c\u76f4\u63a5\u52a0\u8f7d\u65b0\u7684\u53ef\u6267\u884c\u6587\u4ef6\u6216\u51fd\u6570\uff0c\u5e76\u4e0d\u4f1a\u8fd4\u56de\u539f\u6765\u51fd\u6570\u7684\u4f4d\u7f6e\u3002\u56e0\u6b64\u5982\u679c\u9700\u8981\u5728\u5b50\u8fdb\u7a0b\u4e2d\u6267\u884c\u7279\u5b9a\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e93\u51fd\u6570\u6216\u8005\u5176\u4ed6\u65b9\u6cd5\u6765\u5b8c\u6210\u3002&lt;\/p&gt; &lt;p&gt;&lt;strong&gt;&lt;br&gt;&lt;\/strong&gt;&lt;br&gt;&lt;\/p&gt;&lt;\/unistd.h&gt;&lt;\/stdlib.h&gt;&lt;\/stdio.h&gt; \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u5c31\u662f\u53ef\u4ee5\u901a\u8fc7\u5b50\u8fdb\u7a0b\u6267\u884c\u51fd\u6570\u5417\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\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-15944","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15944","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=15944"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15944\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=15944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=15944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=15944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}