{"id":63310,"date":"2025-04-29T17:50:27","date_gmt":"2025-04-29T09:50:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/63310\/"},"modified":"2025-04-29T17:50:27","modified_gmt":"2025-04-29T09:50:27","slug":"%e6%8e%a2%e7%a9%b6linux%e8%bf%9b%e7%a8%8b%e8%bf%9b%e5%85%a5%e7%9d%a1%e7%9c%a0%e7%9a%84%e5%8e%9f%e5%9b%a0-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/63310\/","title":{"rendered":"\u63a2\u7a76Linux\u8fdb\u7a0b\u8fdb\u5165\u7761\u7720\u7684\u539f\u56e0"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/171094176570803.jpg\" class=\"aligncenter\" title=\"\u63a2\u7a76Linux\u8fdb\u7a0b\u8fdb\u5165\u7761\u7720\u7684\u539f\u56e0\u63d2\u56fe\" alt=\"\u63a2\u7a76Linux\u8fdb\u7a0b\u8fdb\u5165\u7761\u7720\u7684\u539f\u56e0\u63d2\u56fe\" \/><\/p>\n<p>Linux\u8fdb\u7a0b\u8fdb\u5165\u7761\u7720\u662f\u6307\u8fdb\u7a0b\u7531\u8fd0\u884c\u72b6\u6001\u8f6c\u6362\u4e3a\u7761\u7720\u72b6\u6001\u7684\u8fc7\u7a0b\u3002\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u8fdb\u7a0b\u8fdb\u5165\u7761\u7720\u7684\u539f\u56e0\u6709\u5f88\u591a\uff0c\u4e3b\u8981\u5305\u62ec\u7b49\u5f85\u67d0\u4e9b\u8d44\u6e90\u3001\u7b49\u5f85I\/O\u64cd\u4f5c\u5b8c\u6210\u3001\u7b49\u5f85\u4fe1\u53f7\u7b49\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u7a76Linux\u8fdb\u7a0b\u8fdb\u5165\u7761\u7720\u7684\u4e00\u4e9b\u5e38\u89c1\u539f\u56e0\uff0c\u5e76\u901a\u8fc7\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u6765\u8bf4\u660e\u3002<\/p>\n<h3>\u8d44\u6e90\u7b49\u5f85<\/h3>\n<p>\u8fdb\u7a0b\u53ef\u80fd\u56e0\u4e3a\u9700\u8981\u67d0\u4e9b\u8d44\u6e90\u800c\u8fdb\u5165\u7761\u7720\u72b6\u6001\uff0c\u6bd4\u5982\u7b49\u5f85\u5176\u4ed6\u8fdb\u7a0b\u91ca\u653e\u67d0\u4e2a\u5171\u4eab\u8d44\u6e90\u3002\u5728\u4ee5\u4e0b\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e24\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u5148\u83b7\u53d6\u8d44\u6e90\uff0c\u53e6\u4e00\u4e2a\u8fdb\u7a0b\u8981\u7b49\u5f85\u7b2c\u4e00\u4e2a\u8fdb\u7a0b\u91ca\u653e\u8d44\u6e90\u540e\u624d\u80fd\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<pre>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;sys&gt;\n#include &lt;sys&gt;\n\nint main() {\n    int fd[2];\n    pipe(fd);\n\n    pid_t pid1 = fork();\n    if (pid1 == 0) {\n        \/\/ \u5b50\u8fdb\u7a0b1\n        close(fd[0]); \/\/ \u5173\u95ed\u8bfb\u7aef\u53e3\n        sleep(2); \/\/ \u6a21\u62df\u83b7\u53d6\u8d44\u6e90\u7684\u8fc7\u7a0b\n        close(fd[1]); \/\/ \u91ca\u653e\u8d44\u6e90\n        exit(0);\n    }\n\n    pid_t pid2 = fork();\n    if (pid2 == 0) {\n        \/\/ \u5b50\u8fdb\u7a0b2\n        close(fd[1]); \/\/ \u5173\u95ed\u5199\u7aef\u53e3\n        printf(\"\u5b50\u8fdb\u7a0b2\u7b49\u5f85\u8d44\u6e90...\n\");\n        char buf[10];\n        read(fd[0], buf, sizeof(buf)); \/\/ \u963b\u585e\u7b49\u5f85\u8d44\u6e90\n        printf(\"\u5b50\u8fdb\u7a0b2\u83b7\u5f97\u8d44\u6e90\uff0c\u7ee7\u7eed\u6267\u884c\u3002\n\");\n        exit(0);\n    }\n\n    \/\/ \u7b49\u5f85\u5b50\u8fdb\u7a0b\u7ed3\u675f\n    wait(NULL);\n    wait(NULL);\n\n    return 0;\n}&lt;\/sys&gt;&lt;\/sys&gt;&lt;\/unistd.h&gt;&lt;\/stdlib.h&gt;&lt;\/stdio.h&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5b50\u8fdb\u7a0b2\u88ab\u963b\u585e\u5728read()\u51fd\u6570\u5904\uff0c\u76f4\u5230\u5b50\u8fdb\u7a0b1\u91ca\u653e\u8d44\u6e90\u540e\u624d\u80fd\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<h3>I\/O\u64cd\u4f5c<\/h3>\n<p>\u8fdb\u7a0b\u4e5f\u53ef\u80fd\u56e0\u4e3a\u9700\u8981\u8fdb\u884cI\/O\u64cd\u4f5c\u800c\u8fdb\u5165\u7761\u7720\u72b6\u6001\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u4e00\u4e2a\u8fdb\u7a0b\u7b49\u5f85\u7528\u6237\u8f93\u5165\u7684\u8fc7\u7a0b\u3002<\/p>\n<pre>#include &lt;stdio.h&gt;\n#include &lt;unistd.h&gt;\n\nint main() {\n    char buf[10];\n    printf(\"\u8bf7\u8f93\u5165\u4e00\u4e9b\u5185\u5bb9\uff1a\n\");\n    fgets(buf, sizeof(buf), stdin); \/\/ \u963b\u585e\u7b49\u5f85\u7528\u6237\u8f93\u5165\n    printf(\"\u60a8\u8f93\u5165\u7684\u5185\u5bb9\u662f\uff1a%s\", buf);\n    return 0;\n}&lt;\/unistd.h&gt;&lt;\/stdio.h&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0cfgets()\u51fd\u6570\u4f1a\u4e00\u76f4\u7b49\u5f85\u7528\u6237\u8f93\u5165\u5185\u5bb9\u3002<\/p>\n<h3>\u4fe1\u53f7\u7b49\u5f85<\/h3>\n<p>\u8fdb\u7a0b\u8fd8\u53ef\u80fd\u56e0\u4e3a\u7b49\u5f85\u4fe1\u53f7\u800c\u8fdb\u5165\u7761\u7720\u72b6\u6001\u3002\u4ee5\u4e0b\u7684\u793a\u4f8b\u5c55\u793a\u4e86\u4e00\u4e2a\u8fdb\u7a0b\u7b49\u5f85\u4fe1\u53f7\u7684\u8fc7\u7a0b\u3002<\/p>\n<pre>#include &lt;stdio.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;signal.h&gt;\n\nvoid signal_handler(int signal) {\n    printf(\"\u6536\u5230\u4fe1\u53f7\uff1a%d\n\", signal);\n}\n\nint main() {\n    signal(SIGUSR1, signal_handler); \/\/ \u6ce8\u518c\u4fe1\u53f7\u5904\u7406\u51fd\u6570\n\n    printf(\"\u7b49\u5f85\u4fe1\u53f7...\n\");\n    pause(); \/\/ \u8fdb\u7a0b\u4e00\u76f4\u7b49\u5f85\u4fe1\u53f7\n\n    return 0;\n}&lt;\/signal.h&gt;&lt;\/unistd.h&gt;&lt;\/stdio.h&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u8fdb\u7a0b\u901a\u8fc7pause()\u51fd\u6570\u4e00\u76f4\u7b49\u5f85\u4fe1\u53f7\u7684\u5230\u6765\u3002<\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230Linux\u8fdb\u7a0b\u8fdb\u5165\u7761\u7720\u7684\u539f\u56e0\u6709\u5f88\u591a\uff0c\u5305\u62ec\u7b49\u5f85\u67d0\u4e9b\u8d44\u6e90\u3001\u7b49\u5f85I\/O\u64cd\u4f5c\u5b8c\u6210\u3001\u7b49\u5f85\u4fe1\u53f7\u7b49\u3002\u8fd9\u4e9b\u90fd\u662fLinux\u7cfb\u7edf\u4e2d\u8fdb\u7a0b\u8c03\u5ea6\u548c\u8fd0\u884c\u7684\u91cd\u8981\u65b9\u9762\uff0c\u6df1\u5165\u4e86\u89e3\u8fd9\u4e9b\u539f\u7406\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u8fdb\u7a0b\u7684\u8fd0\u884c\u673a\u5236\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u63a2\u7a76Linux\u8fdb\u7a0b\u8fdb\u5165\u7761\u7720\u7684\u539f\u56e0\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>Linux\u8fdb\u7a0b\u8fdb\u5165\u7761\u7720\u662f\u6307\u8fdb\u7a0b\u7531\u8fd0\u884c\u72b6\u6001\u8f6c\u6362\u4e3a\u7761\u7720\u72b6\u6001\u7684\u8fc7\u7a0b\u3002\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u8fdb\u7a0b\u8fdb\u5165\u7761\u7720\u7684\u539f\u56e0\u6709\u5f88\u591a\uff0c\u4e3b\u8981\u5305\u62ec\u7b49\u5f85\u67d0\u4e9b\u8d44\u6e90\u3001\u7b49\u5f85I\/O\u64cd\u4f5c\u5b8c\u6210\u3001\u7b49\u5f85\u4fe1\u53f7\u7b49\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u7a76Linux\u8fdb\u7a0b\u8fdb\u5165\u7761\u7720\u7684\u4e00\u4e9b\u5e38\u89c1\u539f\u56e0\uff0c\u5e76\u901a\u8fc7\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u6765\u8bf4\u660e\u3002 \u8d44\u6e90\u7b49\u5f85 \u8fdb\u7a0b\u53ef\u80fd\u56e0\u4e3a\u9700\u8981\u67d0\u4e9b\u8d44\u6e90\u800c\u8fdb\u5165\u7761\u7720\u72b6\u6001\uff0c\u6bd4\u5982\u7b49\u5f85\u5176\u4ed6\u8fdb\u7a0b\u91ca\u653e\u67d0\u4e2a\u5171\u4eab\u8d44\u6e90\u3002\u5728\u4ee5\u4e0b\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e24\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u5148\u83b7\u53d6\u8d44\u6e90\uff0c\u53e6\u4e00\u4e2a\u8fdb\u7a0b\u8981\u7b49\u5f85\u7b2c\u4e00\u4e2a\u8fdb\u7a0b\u91ca\u653e\u8d44\u6e90\u540e\u624d\u80fd\u7ee7\u7eed\u6267\u884c\u3002 #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;unistd.h&gt; #include &lt;sys&gt; #include &lt;sys&gt; int main() { int fd[2]; pipe(fd); pid_t pid1 = fork(); if (pid1 == 0) { \/\/ \u5b50\u8fdb\u7a0b1 close(fd[0]); \/\/ \u5173\u95ed\u8bfb\u7aef\u53e3 sleep(2); \/\/ \u6a21\u62df\u83b7\u53d6\u8d44\u6e90\u7684\u8fc7\u7a0b close(fd[1]); \/\/ \u91ca\u653e\u8d44\u6e90 exit(0); } pid_t pid2 = fork(); if (pid2 == 0) { \/\/ \u5b50\u8fdb\u7a0b2 close(fd[1]); \/\/ \u5173\u95ed\u5199\u7aef\u53e3 [&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-63310","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/63310","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=63310"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/63310\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=63310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=63310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=63310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}