{"id":51410,"date":"2024-12-03T14:05:58","date_gmt":"2024-12-03T06:05:58","guid":{"rendered":"https:\/\/fwq.ai\/blog\/51410\/"},"modified":"2024-12-03T14:05:58","modified_gmt":"2024-12-03T06:05:58","slug":"%e6%b7%b1%e5%85%a5%e6%8e%a2%e8%ae%a8linux%e8%bf%9b%e7%a8%8b%e7%9a%84%e5%90%af%e5%8a%a8%e6%96%b9%e6%b3%95-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/51410\/","title":{"rendered":"\u6df1\u5165\u63a2\u8ba8Linux\u8fdb\u7a0b\u7684\u542f\u52a8\u65b9\u6cd5"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u6df1\u5165\u63a2\u8ba8Linux\u8fdb\u7a0b\u7684\u542f\u52a8\u65b9\u6cd5<\/h1>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6587\u7ae0\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u6df1\u5165\u63a2\u8ba8Linux\u8fdb\u7a0b\u7684\u542f\u52a8\u65b9\u6cd5\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p><strong>Linux\u8fdb\u7a0b\u542f\u52a8\u65b9\u5f0f\u5168\u9762\u89e3\u6790<\/strong><\/p>\n<p>\u5728Linux\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u8fdb\u7a0b\u662f\u8ba1\u7b97\u673a\u7cfb\u7edf\u4e2d\u6700\u57fa\u672c\u7684\u6267\u884c\u5355\u5143\u3002\u8fdb\u7a0b\u7684\u521b\u5efa\u4e0e\u542f\u52a8\u662f\u64cd\u4f5c\u7cfb\u7edf\u7ba1\u7406\u548c\u8c03\u5ea6\u7684\u91cd\u8981\u73af\u8282\uff0c\u672c\u6587\u5c06\u5168\u9762\u89e3\u6790Linux\u7cfb\u7edf\u4e2d\u8fdb\u7a0b\u7684\u542f\u52a8\u65b9\u5f0f\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u6765\u8bf4\u660e\u5b9e\u73b0\u7ec6\u8282\u3002<\/p>\n<h3>1. <strong>fork()\u7cfb\u7edf\u8c03\u7528<\/strong><\/h3>\n<p><code>fork()<\/code>\u7cfb\u7edf\u8c03\u7528\u662fLinux\u7cfb\u7edf\u4e2d\u6700\u5e38\u7528\u7684\u8fdb\u7a0b\u521b\u5efa\u65b9\u5f0f\uff0c\u901a\u8fc7<code>fork()<\/code>\u7cfb\u7edf\u8c03\u7528\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\uff0c\u65b0\u8fdb\u7a0b\u662f\u8c03\u7528\u8fdb\u7a0b\u7684\u526f\u672c\u3002\u5177\u4f53\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>#include &lt;stdio.h&gt;\n#include &lt;unistd.h&gt;\n\nint main() {\n    pid_t pid = fork();\n    \n    if (pid == 0) {\n        printf(\"Child process is running\n\");\n    } else if (pid &gt; 0) {\n        printf(\"Parent process is running\n\");\n    } else {\n        printf(\"Failed to create child process\n\");\n    }\n\n    return 0;\n}<\/pre>\n<h3>2. <strong>exec()\u7cfb\u5217\u51fd\u6570<\/strong><\/h3>\n<p><code>exec()<\/code>\u7cfb\u5217\u51fd\u6570\u7528\u4e8e\u6267\u884c\u4e00\u4e2a\u65b0\u7684\u7a0b\u5e8f\uff0c\u66ff\u6362\u5f53\u524d\u8fdb\u7a0b\u7684\u4ee3\u7801\u548c\u6570\u636e\u7a7a\u95f4\u3002\u5e38\u7528\u7684<code>exec()<\/code>\u51fd\u6570\u5305\u62ec<code>execl()<\/code>\u3001<code>execv()<\/code>\u3001<code>execle()<\/code>\u7b49\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>#include &lt;stdio.h&gt;\n#include &lt;unistd.h&gt;\n\nint main() {\n    execl(\"\/bin\/ls\", \"ls\", \"-l\", NULL);\n    return 0;\n}<\/pre>\n<h3>3. <strong>clone()\u7cfb\u7edf\u8c03\u7528<\/strong><\/h3>\n<p><code>clone()<\/code>\u7cfb\u7edf\u8c03\u7528\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u52a0\u7075\u6d3b\u7684\u521b\u5efa\u8fdb\u7a0b\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u8c03\u7528\u8fdb\u7a0b\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\uff0c\u5e76\u4e0e\u8c03\u7528\u8fdb\u7a0b\u5171\u4eab\u90e8\u5206\u8d44\u6e90\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>#define _GNU_SOURCE\n#include &lt;stdio.h&gt;\n#include &lt;sched.h&gt;\n#include &lt;unistd.h&gt;\n\nint child_func(void *arg) {\n    printf(\"Child process is running\n\");\n    return 0;\n}\n\nint main() {\n    char stack[1024 * 1024];\n    int flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND;\n    \n    pid_t pid = clone(child_func, stack + (1024 * 1024), flags, NULL);\n    \n    if (pid &lt; 0) {\n        printf(\"Failed to create child process\n\");\n    }\n\n    printf(\"Parent process is running\n\");\n    return 0;\n}<\/pre>\n<h3>\u7ed3\u8bed<\/h3>\n<p>\u672c\u6587\u5bf9Linux\u7cfb\u7edf\u4e2d\u8fdb\u7a0b\u7684\u542f\u52a8\u65b9\u5f0f\u8fdb\u884c\u4e86\u5168\u9762\u89e3\u6790\uff0c\u5e76\u63d0\u4f9b\u4e86\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u6765\u8bf4\u660e\u5b9e\u73b0\u7ec6\u8282\u3002\u4e86\u89e3\u8fdb\u7a0b\u7684\u521b\u5efa\u65b9\u5f0f\u5bf9\u4e8e\u7406\u89e3\u64cd\u4f5c\u7cfb\u7edf\u7684\u5185\u90e8\u539f\u7406\u548c\u5b9e\u73b0\u673a\u5236\u5177\u6709\u91cd\u8981\u610f\u4e49\uff0c\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u6df1\u5165\u5730\u7406\u89e3Linux\u7cfb\u7edf\u4e2d\u8fdb\u7a0b\u7ba1\u7406\u7684\u539f\u7406\u4e0e\u6280\u672f\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u6df1\u5165\u63a2\u8ba8Linux\u8fdb\u7a0b\u7684\u542f\u52a8\u65b9\u6cd5\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u5206\u6790\u548c\u89e3\u51b3 Golang \u4e2d\u7684\u7cbe\u5ea6\u4e22\u5931\u95ee\u9898\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u63a2\u8ba8Linux\u8fdb\u7a0b\u7684\u542f\u52a8\u65b9\u6cd5 \u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6587\u7ae0\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u6df1\u5165\u63a2\u8ba8Linux\u8fdb\u7a0b\u7684\u542f\u52a8\u65b9\u6cd5\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~ Linux\u8fdb\u7a0b\u542f\u52a8\u65b9\u5f0f\u5168\u9762\u89e3\u6790 \u5728Linux\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u8fdb\u7a0b\u662f\u8ba1\u7b97\u673a\u7cfb\u7edf\u4e2d\u6700\u57fa\u672c\u7684\u6267\u884c\u5355\u5143\u3002\u8fdb\u7a0b\u7684\u521b\u5efa\u4e0e\u542f\u52a8\u662f\u64cd\u4f5c\u7cfb\u7edf\u7ba1\u7406\u548c\u8c03\u5ea6\u7684\u91cd\u8981\u73af\u8282\uff0c\u672c\u6587\u5c06\u5168\u9762\u89e3\u6790Linux\u7cfb\u7edf\u4e2d\u8fdb\u7a0b\u7684\u542f\u52a8\u65b9\u5f0f\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u6765\u8bf4\u660e\u5b9e\u73b0\u7ec6\u8282\u3002 1. fork()\u7cfb\u7edf\u8c03\u7528 fork()\u7cfb\u7edf\u8c03\u7528\u662fLinux\u7cfb\u7edf\u4e2d\u6700\u5e38\u7528\u7684\u8fdb\u7a0b\u521b\u5efa\u65b9\u5f0f\uff0c\u901a\u8fc7fork()\u7cfb\u7edf\u8c03\u7528\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\uff0c\u65b0\u8fdb\u7a0b\u662f\u8c03\u7528\u8fdb\u7a0b\u7684\u526f\u672c\u3002\u5177\u4f53\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a #include &lt;stdio.h&gt; #include &lt;unistd.h&gt; int main() { pid_t pid = fork(); if (pid == 0) { printf(&#8220;Child process is running &#8220;); } else if (pid &gt; 0) { printf(&#8220;Parent process is running &#8220;); } else { printf(&#8220;Failed to create child process &#8220;); } return 0; } 2. exec()\u7cfb\u5217\u51fd\u6570 [&hellip;]<\/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-51410","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/51410","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=51410"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/51410\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=51410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=51410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=51410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}