{"id":117389,"date":"2026-10-24T18:57:14","date_gmt":"2026-10-24T10:57:14","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/117389.html"},"modified":"2026-10-24T18:57:14","modified_gmt":"2026-10-24T10:57:14","slug":"linux-%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0%ef%bc%88%e4%ba%8c%e5%8d%81%ef%bc%89%ef%bc%9a%e5%9f%ba%e7%a1%80io-%e7%b3%bb%e7%bb%9f%e8%b0%83%e7%94%a8%e3%80%81%e6%96%87%e4%bb%b6%e6%8f%8f","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/117389.html","title":{"rendered":"Linux \u5b66\u4e60\u7b14\u8bb0\uff08\u4e8c\u5341\uff09\uff1a\u57fa\u7840IO\u2014\u2014\u7cfb\u7edf\u8c03\u7528\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0e\u91cd\u5b9a\u5411"},"content":{"rendered":"<h3>\u4e00\u3001 \u7cfb\u7edf\u8c03\u7528 vs \u5e93\u51fd\u6570<\/h3>\n<p>\u6211\u4eec\u5e38\u7528 C \u8bed\u8a00\u7684 fopen\u3001printf&#xff0c;\u8fd9\u5c5e\u4e8e C \u6807\u51c6\u5e93\u51fd\u6570&#xff08;\u7528\u6237\u6001&#xff09;\u3002\u800c Linux \u7cfb\u7edf\u5e95\u5c42\u771f\u6b63\u5e72\u6d3b\u7684\u662f \u7cfb\u7edf\u8c03\u7528\u63a5\u53e3&#xff1a;open\u3001read\u3001write\u3001close\u3002<br \/>\n\u5e93\u51fd\u6570\u672c\u8d28\u4e0a\u662f\u5bf9\u7cfb\u7edf\u8c03\u7528\u7684\u5c01\u88c5&#xff0c;\u5e95\u5c42\u8fd8\u662f\u901a\u8fc7\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;fd&#xff09;\u6765\u64cd\u4f5c\u3002<\/p>\n<h3>\u4e8c\u3001 \u6df1\u5165\u7406\u89e3\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;fd&#xff09;<\/h3>\n<p>\u4f7f\u7528 open \u6253\u5f00\u6587\u4ef6\u540e&#xff0c;\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u6574\u6570&#xff0c;\u8fd9\u5c31\u662f fd\u3002\u5b83\u662f\u600e\u4e48\u4ea7\u751f\u7684&#xff1f;<br \/>\n\u5728\u64cd\u4f5c\u7cfb\u7edf\u7684\u8fdb\u7a0b\u63a7\u5236\u5757&#xff08;PCB&#xff09;\u4e2d&#xff0c;\u6709\u4e00\u4e2a\u6307\u9488\u6307\u5411 files_struct \u7ed3\u6784\u4f53&#xff0c;\u91cc\u9762\u5305\u542b\u4e00\u4e2a\u6307\u9488\u6570\u7ec4\u3002\u8fd9\u4e2a\u6570\u7ec4\u7684\u4e0b\u6807\u5c31\u662f fd&#xff01;<\/p>\n<ul>\n<li>\n<p>0&#xff1a;\u6807\u51c6\u8f93\u5165&#xff08;\u952e\u76d8&#xff09;<\/p>\n<\/li>\n<li>\n<p>1&#xff1a;\u6807\u51c6\u8f93\u51fa&#xff08;\u663e\u793a\u5668&#xff09;<\/p>\n<\/li>\n<li>\n<p>2&#xff1a;\u6807\u51c6\u9519\u8bef&#xff08;\u663e\u793a\u5668&#xff09;<br \/>\n\u5206\u914d\u89c4\u5219&#xff1a;\u7cfb\u7edf\u5206\u914d fd \u65f6&#xff0c;\u6c38\u8fdc\u627e\u5f53\u524d\u672a\u88ab\u4f7f\u7528\u7684\u6700\u5c0f\u4e0b\u6807\u3002\u6240\u4ee5\u6211\u4eec\u65b0\u6253\u5f00\u7684\u6587\u4ef6\u9ed8\u8ba4\u662f 3&#xff1b;\u5982\u679c\u5173\u6389 0&#xff0c;\u65b0\u6253\u5f00\u7684\u6587\u4ef6\u5c31\u4f1a\u53d8\u6210 0\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>\u4e09\u3001 \u91cd\u5b9a\u5411\u7684\u672c\u8d28<\/h3>\n<p>\u6211\u4eec\u5e73\u65f6\u7528\u7684 &gt;, &gt;&gt; \u7b26\u53f7&#xff0c;\u672c\u8d28\u4e0a\u662f\u6539\u53d8\u4e86 fd \u7684\u6307\u5411\u3002<br \/>\n\u5982\u679c\u6807\u51c6\u8f93\u51fa&#xff08;1\u53f7&#xff09;\u539f\u672c\u6307\u5411\u663e\u793a\u5668&#xff0c;\u6211\u4eec\u901a\u8fc7\u4ee3\u7801\u8ba9 1 \u53f7\u6307\u5411\u4e00\u4e2a\u78c1\u76d8\u6587\u4ef6&#xff0c;\u90a3\u4e48 printf \u8f93\u51fa\u7684\u5185\u5bb9\u5c31\u4f1a\u8dd1\u5230\u6587\u4ef6\u91cc\u3002<br \/>\n\u5b9e\u73b0\u8fd9\u4e2a\u9b54\u6cd5\u7684\u6838\u5fc3\u7cfb\u7edf\u8c03\u7528\u662f dup2(oldfd, newfd)\u3002\u5b83\u4f1a\u5c06 oldfd \u590d\u5236\u7ed9 newfd&#xff0c;\u8ba9 newfd \u4e5f\u6307\u5411\u540c\u4e00\u4e2a\u6587\u4ef6\u3002<\/p>\n<h3 style=\"background-color:transparent\">\u56db\u3001 \u5b9e\u6218\u4ee3\u7801&#xff1a;\u4f53\u9a8c\u91cd\u5b9a\u5411<\/h3>\n<p>int fd &#061; open(&#034;log.txt&#034;, O_CREAT | O_WRONLY | O_TRUNC, 0644);<br \/>\n\/\/ \u6b64\u65f6 fd \u662f 3&#xff0c;\u6307\u5411 log.txt<br \/>\n\/\/ \u8ba9 1 \u53f7&#xff08;\u6807\u51c6\u8f93\u51fa&#xff09;\u4e5f\u6307\u5411 log.txt<br \/>\ndup2(fd, 1);<br \/>\n\/\/ \u63a5\u4e0b\u6765 printf \u7684\u5185\u5bb9\u5168\u90fd\u4f1a\u5199\u5165 log.txt&#xff0c;\u4e0d\u518d\u663e\u793a\u5728\u5c4f\u5e55\u4e0a<br \/>\nprintf(&#034;\u8fd9\u53e5\u8bdd\u88ab\u91cd\u5b9a\u5411\u5230\u6587\u4ef6\u4e86&#xff01;\\\\n&#034;);<br \/>\nclose(fd);<\/p>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u201c\u4e00\u5207\u7686\u6587\u4ef6\u201d\u662f Linux \u7684\u6838\u5fc3\u54f2\u5b66\u3002\u901a\u8fc7\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;Linux \u628a\u786c\u4ef6&#xff08;\u952e\u76d8\u3001\u663e\u793a\u5668&#xff09;\u548c\u78c1\u76d8\u6587\u4ef6\u7edf\u4e00\u6210\u4e86\u540c\u4e00\u5957\u64cd\u4f5c\u63a5\u53e3\u3002\u638c\u63e1 dup2&#xff0c;\u5c31\u80fd\u7406\u89e3\u5404\u79cd\u91cd\u5b9a\u5411\u548c\u7ba1\u9053\u5e95\u5c42\u7684\u8fd0\u4f5c\u673a\u5236\u3002<\/p>\n<h3>\u81ea\u6211\u5b9e\u6218\u64cd\u4f5c<\/h3>\n<p>cd ~\/linux_study<br \/>\nmkdir io_test<br \/>\ncd io_test<\/p>\n<h5>\u5b9e\u6218 1&#xff1a;\u4f53\u9a8c\u7cfb\u7edf\u8c03\u7528 I\/O<\/h5>\n<p>vim sysio.c<\/p>\n<p>(\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801)<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/stat.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>int main() {<br \/>\n    \/\/ \u6253\u5f00\u6587\u4ef6&#xff0c;\u5982\u679c\u6ca1\u6709\u5219\u521b\u5efa&#xff0c;\u6743\u9650\u8bbe\u4e3a 0644<br \/>\n    int fd &#061; open(&#034;test.txt&#034;, O_WRONLY | O_CREAT | O_TRUNC, 0644);<br \/>\n    if (fd &lt; 0) {<br \/>\n        perror(&#034;open&#034;);<br \/>\n        return 1;<br \/>\n    }<\/p>\n<p>    printf(&#034;\u6210\u529f\u6253\u5f00\u6587\u4ef6&#xff0c;\u5f97\u5230\u7684 fd \u662f: %d\\\\n&#034;, fd); \/\/ \u5927\u6982\u7387\u662f 3<\/p>\n<p>    const char *msg &#061; &#034;Hello System IO!\\\\n&#034;;<br \/>\n    \/\/ \u7528\u7cfb\u7edf\u8c03\u7528 write \u5199\u5165<br \/>\n    write(fd, msg, strlen(msg));<\/p>\n<p>    close(fd);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u7f16\u8bd1\u8fd0\u884c&#xff1a;<\/p>\n<p>gcc sysio.c -o sysio<br \/>\n.\/sysio<br \/>\ncat test.txt<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"177\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261024105713-6adc8f09071a6.png\" width=\"578\" \/><\/p>\n<p>&#x1f449; \u4f60\u4f1a\u53d1\u73b0 fd \u662f 3\u3002\u56e0\u4e3a 0\u30011\u30012 \u88ab\u7cfb\u7edf\u5360\u7528\u4e86\u3002<\/p>\n<h5>\u5b9e\u6218 2&#xff1a;\u9a8c\u8bc1 fd \u7684\u5206\u914d\u89c4\u5219<\/h5>\n<p>vim fd_rule.c<\/p>\n<p>(\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801)<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/stat.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>int main() {<br \/>\n    close(0); \/\/ \u5173\u6389\u6807\u51c6\u8f93\u5165&#xff01;<br \/>\n    int fd &#061; open(&#034;test.txt&#034;, O_RDONLY);<br \/>\n    printf(&#034;\u5173\u6389 0 \u53f7\u540e&#xff0c;\u65b0\u6253\u5f00\u7684 fd \u662f: %d\\\\n&#034;, fd); \/\/ \u4f60\u4f1a\u53d1\u73b0\u53d8\u6210\u4e86 0&#xff01;<br \/>\n    close(fd);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"120\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261024105713-6adc8f091b47d.png\" width=\"650\" \/><\/p>\n<p>(\u73b0\u8c61&#xff1a;\u7cfb\u7edf\u6c38\u8fdc\u627e\u6700\u5c0f\u7684\u7a7a\u4f4d\u3002\u8fd9\u8bc1\u660e\u4e86 fd \u5c31\u662f\u6570\u7ec4\u4e0b\u6807\u3002)<\/p>\n<h5>\u5b9e\u6218 3&#xff1a;\u7528 dup2 \u5b8c\u6210\u8f93\u51fa\u91cd\u5b9a\u5411<\/h5>\n<p>vim redir.c<\/p>\n<p>(\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801)<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<\/p>\n<p>int main() {<br \/>\n    \/\/ 1. \u6253\u5f00\u4e00\u4e2a\u6587\u4ef6&#xff08;\u6b64\u65f6 fd \u662f 3&#xff09;<br \/>\n    int fd &#061; open(&#034;log.txt&#034;, O_CREAT | O_WRONLY | O_TRUNC, 0644);<\/p>\n<p>    \/\/ 2. \u5c06 fd&#xff08;3&#xff09;\u590d\u5236\u7ed9 1&#xff08;\u6807\u51c6\u8f93\u51fa&#xff09;<br \/>\n    \/\/ \u539f\u672c 1 \u6307\u5411\u663e\u793a\u5668&#xff0c;\u73b0\u5728 1 \u548c 3 \u6307\u5411\u540c\u4e00\u4e2a\u6587\u4ef6&#xff01;<br \/>\n    dup2(fd, 1); <\/p>\n<p>    \/\/ 3. \u6b64\u65f6\u7528 printf \u8f93\u51fa&#xff0c;\u539f\u672c\u8be5\u4e0a\u5c4f\u5e55\u7684\u5b57&#xff0c;\u5168\u8fdb log.txt \u4e86&#xff01;<br \/>\n    printf(&#034;\u8fd9\u53e5\u8bdd\u4f1a\u88ab\u5199\u8fdb\u6587\u4ef6\u91cc&#xff01;\\\\n&#034;);<br \/>\n    printf(&#034;\u6807\u51c6\u8f93\u51fa\u5df2\u7ecf\u88ab\u91cd\u5b9a\u5411\u5566&#xff01;\\\\n&#034;);<\/p>\n<p>    close(fd);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u7f16\u8bd1\u8fd0\u884c&#xff1a;<\/p>\n<p>gcc redir.c -o redir<br \/>\n.\/redir<br \/>\n# \u5c4f\u5e55\u4e0a\u4ec0\u4e48\u90fd\u4e0d\u4f1a\u663e\u793a&#xff01;<br \/>\ncat log.txt # \u6253\u5f00\u6587\u4ef6\u770b&#xff0c;\u5b57\u5168\u5728\u91cc\u9762\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"142\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261024105713-6adc8f092b723.png\" width=\"601\" \/><\/p>\n<h5>\u5b9e\u6218 4&#xff1a;\u8fdb\u9636\u2014\u2014\u7ed9\u4e4b\u524d\u5199\u7684\u5fae\u578b Shell \u52a0\u4e0a\u91cd\u5b9a\u5411&#xff08;\u9009\u505a&#xff09;<\/h5>\n<p>\u7b2c\u4e00\u6b65&#xff1a;\u521b\u5efa\u65b0\u7684 Shell \u6587\u4ef6<br \/>\n\u8fdb\u5165\u76ee\u5f55&#xff0c;\u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6&#xff1a;<\/p>\n<p>cd ~\/linux_study\/io_test<br \/>\nvim myshell_redir.c<\/p>\n<p>\u7b2c\u4e8c\u6b65&#xff1a;\u590d\u5236\u5e76\u4fdd\u5b58\u4ee5\u4e0b\u4ee3\u7801<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/wait.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/stat.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<\/p>\n<p>#define MAX_ARGS 10<\/p>\n<p>\/\/ \u5b9a\u4e49\u91cd\u5b9a\u5411\u7c7b\u578b<br \/>\n#define NoneRedir 0<br \/>\n#define OutputRedir 1   \/\/ &gt;<br \/>\n#define AppRedir 2      \/\/ &gt;&gt;<\/p>\n<p>int main() {<br \/>\n    char buf[1024];<br \/>\n    char *argv[MAX_ARGS];<br \/>\n    int redir &#061; NoneRedir;<br \/>\n    char *filename &#061; NULL;<\/p>\n<p>    while (1) {<br \/>\n        \/\/ \u6bcf\u6b21\u5faa\u73af\u91cd\u7f6e\u72b6\u6001<br \/>\n        memset(argv, 0, sizeof(argv));<br \/>\n        redir &#061; NoneRedir;<br \/>\n        filename &#061; NULL;<\/p>\n<p>        printf(&#034;myshell&gt; &#034;);<br \/>\n        fflush(stdout);<\/p>\n<p>        \/\/ 1. \u83b7\u53d6\u7528\u6237\u8f93\u5165<br \/>\n        if (fgets(buf, sizeof(buf), stdin) &#061;&#061; NULL) continue;<br \/>\n        buf[strcspn(buf, &#034;\\\\n&#034;)] &#061; 0; \/\/ \u53bb\u9664\u6362\u884c\u7b26<\/p>\n<p>        if (strcmp(buf, &#034;exit&#034;) &#061;&#061; 0) break;<br \/>\n        if (strlen(buf) &#061;&#061; 0) continue;<\/p>\n<p>        \/\/ 2. \u89e3\u6790\u91cd\u5b9a\u5411&#xff08;\u975e\u5e38\u7b80\u5355\u7c97\u66b4\u7684\u89e3\u6790&#xff1a;\u627e &gt; \u6216 &gt;&gt;&#xff09;<br \/>\n        char *ptr &#061; strstr(buf, &#034;&gt;&gt;&#034;);<br \/>\n        if (ptr !&#061; NULL) {<br \/>\n            redir &#061; AppRedir;<br \/>\n            *ptr &#061; &#039;\\\\0&#039;;                  \/\/ \u628a &gt;&gt; \u53d8\u6210\u5b57\u7b26\u4e32\u7ed3\u675f\u7b26&#xff0c;\u5207\u65ad\u524d\u9762\u547d\u4ee4<br \/>\n            filename &#061; ptr &#043; 2;           \/\/ \u6587\u4ef6\u540d\u5728 &gt;&gt; \u7684\u540e\u9762<br \/>\n            while (*filename &#061;&#061; &#039; &#039;) filename&#043;&#043;; \/\/ \u8df3\u8fc7\u7a7a\u683c<br \/>\n        } else {<br \/>\n            ptr &#061; strchr(buf, &#039;&gt;&#039;);<br \/>\n            if (ptr !&#061; NULL) {<br \/>\n                redir &#061; OutputRedir;<br \/>\n                *ptr &#061; &#039;\\\\0&#039;;              \/\/ \u628a &gt; \u53d8\u6210\u5b57\u7b26\u4e32\u7ed3\u675f\u7b26<br \/>\n                filename &#061; ptr &#043; 1;       \/\/ \u6587\u4ef6\u540d\u5728 &gt; \u7684\u540e\u9762<br \/>\n                while (*filename &#061;&#061; &#039; &#039;) filename&#043;&#043;; \/\/ \u8df3\u8fc7\u7a7a\u683c<br \/>\n            }<br \/>\n        }<\/p>\n<p>        \/\/ 3. \u89e3\u6790\u547d\u4ee4\u884c&#xff08;\u6309\u7a7a\u683c\u5207\u5206&#xff0c;\u6ce8\u610f&#xff1a;\u6b64\u65f6 buf \u91cc\u53ea\u5269\u4e0b\u547d\u4ee4\u90e8\u5206\u4e86&#xff09;<br \/>\n        int argc &#061; 0;<br \/>\n        argv[argc] &#061; strtok(buf, &#034; &#034;);<br \/>\n        while (argv[argc] !&#061; NULL) {<br \/>\n            argc&#043;&#043;;<br \/>\n            argv[argc] &#061; strtok(NULL, &#034; &#034;);<br \/>\n        }<br \/>\n        if (argc &#061;&#061; 0) continue; \/\/ \u7528\u6237\u53ea\u6572\u4e86\u56de\u8f66\u6216\u91cd\u5b9a\u5411\u7b26\u53f7<\/p>\n<p>        \/\/ 4. Fork \u521b\u5efa\u5b50\u8fdb\u7a0b\u6267\u884c<br \/>\n        pid_t pid &#061; fork();<br \/>\n        if (pid &#061;&#061; 0) {<br \/>\n            \/\/ &#8212; \u5b50\u8fdb\u7a0b\u5185\u90e8 &#8212;<\/p>\n<p>            \/\/ \u6838\u5fc3&#xff1a;\u5904\u7406\u91cd\u5b9a\u5411&#xff01;<br \/>\n            if (redir !&#061; NoneRedir &amp;&amp; filename !&#061; NULL) {<br \/>\n                int fd &#061; -1;<br \/>\n                if (redir &#061;&#061; OutputRedir) {<br \/>\n                    \/\/ \u4ee5\u5199\u65b9\u5f0f\u6253\u5f00&#xff0c;\u4e0d\u5b58\u5728\u5219\u521b\u5efa&#xff0c;\u5b58\u5728\u5219\u6e05\u7a7a<br \/>\n                    fd &#061; open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);<br \/>\n                } else if (redir &#061;&#061; AppRedir) {<br \/>\n                    \/\/ \u4ee5\u8ffd\u52a0\u65b9\u5f0f\u6253\u5f00&#xff0c;\u4e0d\u5b58\u5728\u5219\u521b\u5efa<br \/>\n                    fd &#061; open(filename, O_WRONLY | O_CREAT | O_APPEND, 0644);<br \/>\n                }<\/p>\n<p>                if (fd &lt; 0) {<br \/>\n                    perror(&#034;open error&#034;);<br \/>\n                    exit(1);<br \/>\n                }<\/p>\n<p>                \/\/ \u5c06\u6807\u51c6\u8f93\u51fa&#xff08;1&#xff09;\u6307\u5411\u6211\u4eec\u6253\u5f00\u7684\u6587\u4ef6<br \/>\n                dup2(fd, 1);<br \/>\n                close(fd); \/\/ \u590d\u5236\u5b8c\u5c31\u53ef\u4ee5\u5173\u6389\u539f\u6765\u7684 fd \u4e86<br \/>\n            }<\/p>\n<p>            \/\/ \u6267\u884c\u547d\u4ee4<br \/>\n            execvp(argv[0], argv);<br \/>\n            perror(&#034;execvp error&#034;); \/\/ \u8d70\u5230\u8fd9\u8bf4\u660e\u547d\u4ee4\u6ca1\u627e\u5230<br \/>\n            exit(1);<br \/>\n        } else if (pid &gt; 0) {<br \/>\n            \/\/ \u7236\u8fdb\u7a0b\u7b49\u5f85\u56de\u6536<br \/>\n            wait(NULL);<br \/>\n        }<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u7b2c\u4e09\u6b65&#xff1a;\u7f16\u8bd1\u5e76\u8fd0\u884c<\/p>\n<p>gcc myshell_redir.c -o myshell_redir<br \/>\n.\/myshell_redir<\/p>\n<p>\u7b2c\u56db\u6b65&#xff1a;\u89c1\u8bc1\u5947\u8ff9\u7684\u65f6\u523b<br \/>\n\u5728 myshell&gt; \u63d0\u793a\u7b26\u4e0b&#xff0c;\u4f9d\u6b21\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u4f53\u9a8c&#xff1a;<\/p>\n<li>\n<p>\u6d4b\u8bd5\u666e\u901a\u547d\u4ee4&#xff1a;<\/p>\n<p>myshell&gt; ls -l<\/p>\n<p>(\u5e94\u8be5\u6b63\u5e38\u5728\u5c4f\u5e55\u4e0a\u6253\u5370\u51fa\u5f53\u524d\u76ee\u5f55\u7684\u6587\u4ef6\u5217\u8868)<\/p>\n<\/li>\n<li>\n<p>\u6d4b\u8bd5\u8f93\u51fa\u91cd\u5b9a\u5411 &gt;&#xff1a;<\/p>\n<p>myshell&gt; ls -l &gt; out.txt<\/p>\n<p>(\u5c4f\u5e55\u4e0a\u4ec0\u4e48\u90fd\u4e0d\u6253\u5370&#xff0c;\u9000\u56de\u5230 myshell&gt; \u63d0\u793a\u7b26\u3002\u6b64\u65f6\u4f60\u8f93\u5165 cat out.txt \u6216\u9000\u51fa shell \u540e\u770b&#xff0c;\u5185\u5bb9\u5168\u5728\u91cc\u9762)<\/p>\n<\/li>\n<li>\n<p>\u6d4b\u8bd5\u8ffd\u52a0\u91cd\u5b9a\u5411 &gt;&gt;&#xff1a;<\/p>\n<p>myshell&gt; echo &#034;hello&#034; &gt;&gt; out.txt<br \/>\nmyshell&gt; echo &#034;world&#034; &gt;&gt; out.txt<\/p>\n<p>(\u518d\u7528 cat out.txt \u67e5\u770b&#xff0c;\u4f60\u4f1a\u53d1\u73b0 hello \u548c world \u90fd\u8ffd\u52a0\u8fdb\u53bb\u4e86&#xff0c;\u6ca1\u6709\u88ab\u8986\u76d6)<\/p>\n<\/li>\n<li>\n<p>\u6d4b\u8bd5\u9519\u8bef\u547d\u4ee4&#xff1a;<\/p>\n<p>myshell&gt; abcdefg<\/p>\n<p>(\u4f1a\u63d0\u793a execvp error: No such file or directory&#xff0c;\u56e0\u4e3a\u7cfb\u7edf\u627e\u4e0d\u5230\u8fd9\u4e2a\u547d\u4ee4)<\/p>\n<\/li>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001 \u7cfb\u7edf\u8c03\u7528 vs \u5e93\u51fd\u6570\u6211\u4eec\u5e38\u7528 C \u8bed\u8a00\u7684 fopen\u3001printf&#xff0c;\u8fd9\u5c5e\u4e8e C \u6807\u51c6\u5e93\u51fd\u6570&#xff08;\u7528\u6237\u6001&#xff09;\u3002\u800c Linux \u7cfb\u7edf\u5e95\u5c42\u771f\u6b63\u5e72\u6d3b\u7684\u662f \u7cfb\u7edf\u8c03\u7528\u63a5\u53e3&#xff1a;open\u3001read\u3001write\u3001close\u3002<br \/>\n\u5e93\u51fd\u6570\u672c\u8d28\u4e0a\u662f\u5bf9\u7cfb\u7edf\u8c03\u7528\u7684\u5c01\u88c5&#xff0c;\u5e95\u5c42\u8fd8\u662f\u901a\u8fc7\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;fd&#xff09;\u6765\u64cd\u4f5c\u3002\u4e8c\u3001 \u6df1\u5165\u7406\u89e3\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;fd&#xff09;\u4f7f\u7528 open \u6253\u5f00\u6587\u4ef6\u540e&#xff0c;\u8fd4<\/p>\n","protected":false},"author":2,"featured_media":117386,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,371,305],"topic":[],"class_list":["post-117389","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-linux","tag-371","tag-305"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Linux \u5b66\u4e60\u7b14\u8bb0\uff08\u4e8c\u5341\uff09\uff1a\u57fa\u7840IO\u2014\u2014\u7cfb\u7edf\u8c03\u7528\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0e\u91cd\u5b9a\u5411 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/117389.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linux \u5b66\u4e60\u7b14\u8bb0\uff08\u4e8c\u5341\uff09\uff1a\u57fa\u7840IO\u2014\u2014\u7cfb\u7edf\u8c03\u7528\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0e\u91cd\u5b9a\u5411 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001 \u7cfb\u7edf\u8c03\u7528 vs \u5e93\u51fd\u6570\u6211\u4eec\u5e38\u7528 C \u8bed\u8a00\u7684 fopen\u3001printf&#xff0c;\u8fd9\u5c5e\u4e8e C \u6807\u51c6\u5e93\u51fd\u6570&#xff08;\u7528\u6237\u6001&#xff09;\u3002\u800c Linux \u7cfb\u7edf\u5e95\u5c42\u771f\u6b63\u5e72\u6d3b\u7684\u662f \u7cfb\u7edf\u8c03\u7528\u63a5\u53e3&#xff1a;open\u3001read\u3001write\u3001close\u3002 \u5e93\u51fd\u6570\u672c\u8d28\u4e0a\u662f\u5bf9\u7cfb\u7edf\u8c03\u7528\u7684\u5c01\u88c5&#xff0c;\u5e95\u5c42\u8fd8\u662f\u901a\u8fc7\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;fd&#xff09;\u6765\u64cd\u4f5c\u3002\u4e8c\u3001 \u6df1\u5165\u7406\u89e3\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;fd&#xff09;\u4f7f\u7528 open \u6253\u5f00\u6587\u4ef6\u540e&#xff0c;\u8fd4\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/117389.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-24T10:57:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261024105713-6adc8f09071a6.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/117389.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/117389.html\",\"name\":\"Linux \u5b66\u4e60\u7b14\u8bb0\uff08\u4e8c\u5341\uff09\uff1a\u57fa\u7840IO\u2014\u2014\u7cfb\u7edf\u8c03\u7528\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0e\u91cd\u5b9a\u5411 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-24T10:57:14+00:00\",\"dateModified\":\"2026-10-24T10:57:14+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/117389.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/117389.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/117389.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux \u5b66\u4e60\u7b14\u8bb0\uff08\u4e8c\u5341\uff09\uff1a\u57fa\u7840IO\u2014\u2014\u7cfb\u7edf\u8c03\u7528\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0e\u91cd\u5b9a\u5411\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Linux \u5b66\u4e60\u7b14\u8bb0\uff08\u4e8c\u5341\uff09\uff1a\u57fa\u7840IO\u2014\u2014\u7cfb\u7edf\u8c03\u7528\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0e\u91cd\u5b9a\u5411 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/117389.html","og_locale":"zh_CN","og_type":"article","og_title":"Linux \u5b66\u4e60\u7b14\u8bb0\uff08\u4e8c\u5341\uff09\uff1a\u57fa\u7840IO\u2014\u2014\u7cfb\u7edf\u8c03\u7528\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0e\u91cd\u5b9a\u5411 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001 \u7cfb\u7edf\u8c03\u7528 vs \u5e93\u51fd\u6570\u6211\u4eec\u5e38\u7528 C \u8bed\u8a00\u7684 fopen\u3001printf&#xff0c;\u8fd9\u5c5e\u4e8e C \u6807\u51c6\u5e93\u51fd\u6570&#xff08;\u7528\u6237\u6001&#xff09;\u3002\u800c Linux \u7cfb\u7edf\u5e95\u5c42\u771f\u6b63\u5e72\u6d3b\u7684\u662f \u7cfb\u7edf\u8c03\u7528\u63a5\u53e3&#xff1a;open\u3001read\u3001write\u3001close\u3002 \u5e93\u51fd\u6570\u672c\u8d28\u4e0a\u662f\u5bf9\u7cfb\u7edf\u8c03\u7528\u7684\u5c01\u88c5&#xff0c;\u5e95\u5c42\u8fd8\u662f\u901a\u8fc7\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;fd&#xff09;\u6765\u64cd\u4f5c\u3002\u4e8c\u3001 \u6df1\u5165\u7406\u89e3\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;fd&#xff09;\u4f7f\u7528 open \u6253\u5f00\u6587\u4ef6\u540e&#xff0c;\u8fd4","og_url":"https:\/\/www.wsisp.com\/helps\/117389.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-24T10:57:14+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261024105713-6adc8f09071a6.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/117389.html","url":"https:\/\/www.wsisp.com\/helps\/117389.html","name":"Linux \u5b66\u4e60\u7b14\u8bb0\uff08\u4e8c\u5341\uff09\uff1a\u57fa\u7840IO\u2014\u2014\u7cfb\u7edf\u8c03\u7528\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0e\u91cd\u5b9a\u5411 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-24T10:57:14+00:00","dateModified":"2026-10-24T10:57:14+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/117389.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/117389.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/117389.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Linux \u5b66\u4e60\u7b14\u8bb0\uff08\u4e8c\u5341\uff09\uff1a\u57fa\u7840IO\u2014\u2014\u7cfb\u7edf\u8c03\u7528\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0e\u91cd\u5b9a\u5411"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/117389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=117389"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/117389\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/117386"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=117389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=117389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=117389"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=117389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}