{"id":51686,"date":"2024-12-03T16:27:24","date_gmt":"2024-12-03T08:27:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/51686\/"},"modified":"2024-12-03T16:27:24","modified_gmt":"2024-12-03T08:27:24","slug":"%e6%b7%b1%e5%85%a5%e6%8e%a2%e8%ae%a8linux%e8%bf%9b%e7%a8%8b%e9%99%b7%e5%85%a5%e4%bc%91%e7%9c%a0%e7%9a%84%e6%a0%b9%e6%9c%ac%e5%8e%9f%e5%9b%a0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/51686\/","title":{"rendered":"\u6df1\u5165\u63a2\u8ba8Linux\u8fdb\u7a0b\u9677\u5165\u4f11\u7720\u7684\u6839\u672c\u539f\u56e0"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u6df1\u5165\u63a2\u8ba8Linux\u8fdb\u7a0b\u9677\u5165\u4f11\u7720\u7684\u6839\u672c\u539f\u56e0<\/h1>\n<p>Linux\u8fdb\u7a0b\u8fdb\u5165\u7761\u7720\u662f\u6307\u8fdb\u7a0b\u4ece\u8fd0\u884c\u72b6\u6001\u8f6c\u6362\u4e3a\u7761\u7720\u72b6\u6001\u3002\u5bfc\u81f4\u8fdb\u7a0b\u7761\u7720\u7684\u539f\u56e0\u5305\u62ec\uff1a * \u8d44\u6e90\u7b49\u5f85\uff1a\u8fdb\u7a0b\u9700\u8981\u7279\u5b9a\u8d44\u6e90\uff0c\u5982\u6587\u4ef6\u6216\u9501\uff0c\u800c\u8fd9\u4e9b\u8d44\u6e90\u4e0d\u53ef\u7528\u3002 * I\/O\u64cd\u4f5c\uff1a\u8fdb\u7a0b\u7b49\u5f85I\/O\u64cd\u4f5c\u5b8c\u6210\uff0c\u5982\u78c1\u76d8\u8bfb\u5199\u6216\u7f51\u7edc\u901a\u4fe1\u3002 * \u4fe1\u53f7\u7b49\u5f85\uff1a\u8fdb\u7a0b\u7b49\u5f85\u4fe1\u53f7\uff08\u5982\u952e\u76d8\u8f93\u5165\u6216\u8fdb\u7a0b\u7ec8\u6b62\uff09\u7684\u5230\u6765\u3002<\/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\/wait.h&gt;\n#include &lt;sys\/types.h&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}<\/pre>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5b50\u8fdb\u7a0b2\u88ab\u963b\u585e\u5728<code>read()<\/code>\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}<\/pre>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>fgets()<\/code>\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}<\/pre>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u8fdb\u7a0b\u901a\u8fc7<code>pause()<\/code>\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>\u4eca\u5929\u5173\u4e8e\u300a\u6df1\u5165\u63a2\u8ba8Linux\u8fdb\u7a0b\u9677\u5165\u4f11\u7720\u7684\u6839\u672c\u539f\u56e0\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8eLinux,\u7761\u7720,\u8fdb\u7a0b\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u63a2\u5bfb\u5d4c\u5165\u5f0fLinux\u7684\u6982\u5ff5\u53ca\u5e94\u7528\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u63a2\u8ba8Linux\u8fdb\u7a0b\u9677\u5165\u4f11\u7720\u7684\u6839\u672c\u539f\u56e0 Linux\u8fdb\u7a0b\u8fdb\u5165\u7761\u7720\u662f\u6307\u8fdb\u7a0b\u4ece\u8fd0\u884c\u72b6\u6001\u8f6c\u6362\u4e3a\u7761\u7720\u72b6\u6001\u3002\u5bfc\u81f4\u8fdb\u7a0b\u7761\u7720\u7684\u539f\u56e0\u5305\u62ec\uff1a * \u8d44\u6e90\u7b49\u5f85\uff1a\u8fdb\u7a0b\u9700\u8981\u7279\u5b9a\u8d44\u6e90\uff0c\u5982\u6587\u4ef6\u6216\u9501\uff0c\u800c\u8fd9\u4e9b\u8d44\u6e90\u4e0d\u53ef\u7528\u3002 * I\/O\u64cd\u4f5c\uff1a\u8fdb\u7a0b\u7b49\u5f85I\/O\u64cd\u4f5c\u5b8c\u6210\uff0c\u5982\u78c1\u76d8\u8bfb\u5199\u6216\u7f51\u7edc\u901a\u4fe1\u3002 * \u4fe1\u53f7\u7b49\u5f85\uff1a\u8fdb\u7a0b\u7b49\u5f85\u4fe1\u53f7\uff08\u5982\u952e\u76d8\u8f93\u5165\u6216\u8fdb\u7a0b\u7ec8\u6b62\uff09\u7684\u5230\u6765\u3002 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\/wait.h&gt; #include &lt;sys\/types.h&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 [&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-51686","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/51686","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=51686"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/51686\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=51686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=51686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=51686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}