{"id":115413,"date":"2026-10-12T04:25:04","date_gmt":"2026-10-11T20:25:04","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/115413.html"},"modified":"2026-10-12T04:25:04","modified_gmt":"2026-10-11T20:25:04","slug":"%e3%80%90linux%e7%b3%bb%e7%bb%9f%e3%80%91%e3%80%90%e4%bb%8e%e8%8f%9c%e9%b8%9f%e9%a9%bf%e7%ab%99%e5%88%b0%e6%93%8d%e4%bd%9c%e7%b3%bb%e7%bb%9f%ef%bc%9a%e4%b8%80%e8%8a%82%e8%af%be%e6%89%93%e9%80%9alinux","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/115413.html","title":{"rendered":"\u3010Linux\u7cfb\u7edf\u3011\u3010\u4ece\u83dc\u9e1f\u9a7f\u7ad9\u5230\u64cd\u4f5c\u7cfb\u7edf\uff1a\u4e00\u8282\u8bfe\u6253\u901aLinux\u91cd\u5b9a\u5411\u4e0e\u7f13\u51b2\u533a\u771f\u76f8\u3011\u6d41\u98df\u822c\u6295\u5582"},"content":{"rendered":"<h2>\u4ece\u83dc\u9e1f\u9a7f\u7ad9\u5230\u64cd\u4f5c\u7cfb\u7edf&#xff1a;\u4e00\u8282\u8bfe\u5f7b\u5e95\u6253\u901a Linux \u91cd\u5b9a\u5411\u3001VFS \u4e0e\u7f13\u51b2\u533a<\/h2>\n<p>\u5c11\u5e74\u4eec&#xff0c;\u5982\u679c\u4f60\u66fe\u7591\u60d1\u8fc7&#xff1a;\u4e3a\u4ec0\u4e48 .\/a.out &gt; log.txt \u65f6\u9519\u8bef\u4fe1\u606f\u4f9d\u7136\u8d56\u5728\u5c4f\u5e55\u4e0a&#xff1f;\u4e3a\u4ec0\u4e48 fork() \u4e4b\u540e\u6587\u4ef6\u91cc\u7a81\u7136\u5192\u51fa 7 \u884c\u8f93\u51fa&#xff1f;\u4e3a\u4ec0\u4e48 printf \u660e\u660e\u8fd4\u56de\u4e86&#xff0c;\u6570\u636e\u5374\u53ef\u80fd\u6ca1\u843d\u76d8&#xff1f;\u8fd9\u8282\u8bfe\u7684\u76ee\u6807&#xff0c;\u5c31\u662f\u628a\u8fd9\u4e9b\u9ed1\u76d2\u7838\u788e&#xff0c;\u628a\u4ece C \u8bed\u8a00\u6807\u51c6\u5e93\u5230\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u7684\u6570\u636e\u6d41\u52a8\u8def\u7ebf&#xff0c;\u5f7b\u5f7b\u5e95\u5e95\u5730\u753b\u5728\u4f60\u7684\u8111\u5b50\u91cc\u3002<\/p>\n<hr \/>\n<h3>\u4e00\u3001\u91cd\u5b9a\u5411\u7684\u672c\u8d28&#xff1a;\u4e0d\u662f\u201c\u7bad\u5934\u201d&#xff0c;\u800c\u662f\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u201c\u5077\u5929\u6362\u65e5\u201d<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261011202502-6acbf09e7c3eb.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h4>1.1 \u201c\u6807\u51c6\u8f93\u51fa\u201d\u548c\u201c\u6807\u51c6\u9519\u8bef\u201d\u4e3a\u4ec0\u4e48\u5206\u5f97\u90a3\u4e48\u6e05&#xff1f;<\/h4>\n<p>\u6bcf\u4e2a\u8fdb\u7a0b\u542f\u52a8\u65f6&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u90fd\u4f1a\u9ed8\u9ed8\u6253\u5f00\u4e09\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;fd&#xff09;&#xff1a;<\/p>\n<ul>\n<li>\u6807\u51c6\u8f93\u5165&#xff08;stdin&#xff09;&#xff1a;fd &#061; 0<\/li>\n<li>\u6807\u51c6\u8f93\u51fa&#xff08;stdout&#xff09;&#xff1a;fd &#061; 1<\/li>\n<li>\u6807\u51c6\u9519\u8bef&#xff08;stderr&#xff09;&#xff1a;fd &#061; 2<\/li>\n<\/ul>\n<p>\u91cd\u70b9\u6765\u4e86&#xff1a; stdout \u548c stderr \u9ed8\u8ba4\u90fd\u6307\u5411\u4f60\u7684\u663e\u793a\u5668&#xff0c;\u4f46\u5b83\u4eec\u662f\u4e24\u4e2a\u72ec\u7acb\u7684 fd&#xff0c;\u5c31\u50cf\u4e24\u4e2a\u4e0d\u540c\u7684\u6c34\u9f99\u5934&#xff0c;\u53ea\u4e0d\u8fc7\u90fd\u63a5\u5230\u4e86\u540c\u4e00\u4e2a\u6c34\u6c60\u91cc\u3002<\/p>\n<p>\u8bfe\u4e0a\u8001\u5e08\u5199\u4e86\u4e00\u6bb5 C\/C&#043;&#043; \u6df7\u7f16\u4ee3\u7801\u6765\u9a8c\u8bc1&#xff1a;<\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;iostream&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;cstdio&gt;<\/span><\/span><\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    std<span class=\"token double-colon punctuation\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;hello cout&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token double-colon punctuation\">::<\/span>endl<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u6807\u51c6\u8f93\u51fa&#xff0c;fd&#061;1<\/span><br \/>\n    <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;hello printf\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>               <span class=\"token comment\">\/\/ \u6807\u51c6\u8f93\u51fa&#xff0c;fd&#061;1<\/span><br \/>\n    <span class=\"token function\">fprintf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token constant\">stderr<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;hello stderr\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>      <span class=\"token comment\">\/\/ \u6807\u51c6\u9519\u8bef&#xff0c;fd&#061;2<\/span><br \/>\n    std<span class=\"token double-colon punctuation\">::<\/span>cerr <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;hello cerr&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token double-colon punctuation\">::<\/span>endl<span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u6807\u51c6\u9519\u8bef&#xff0c;fd&#061;2<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u7f16\u8bd1\u8fd0\u884c\u540e&#xff0c;\u5c4f\u5e55\u4e0a\u7406\u6240\u5f53\u7136\u5730\u6253\u5370\u4e86 4 \u884c\u5185\u5bb9\u3002<img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261011202502-6acbf09ed00ad.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u6b64\u65f6&#xff0c;\u5982\u679c\u4f60\u6267\u884c&#xff1a;<\/p>\n<p>.\/a.out <span class=\"token operator\">&gt;<\/span> log.txt<\/p>\n<p>\u4f60\u4f1a\u53d1\u73b0&#xff0c;\u53ea\u6709 stdout \u7684\u5185\u5bb9\u88ab\u5199\u8fdb\u4e86\u6587\u4ef6&#xff0c;stderr \u7684\u5185\u5bb9\u4f9d\u65e7\u5927\u6447\u5927\u6446\u5730\u663e\u793a\u5728\u5c4f\u5e55\u4e0a\u3002<img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261011202502-6acbf09ee285c.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u4e3a\u4ec0\u4e48&#xff1f; \u56e0\u4e3a &gt; \u7684\u672c\u8d28\u662f&#xff1a;\u6253\u5f00\u65b0\u6587\u4ef6&#xff0c;\u62ff\u5230\u4e00\u4e2a\u65b0\u7684 fd&#xff08;\u6bd4\u5982 3&#xff09;&#xff0c;\u7136\u540e\u628a\u8fd9\u4e2a fd \u91cc\u7684\u6307\u9488\u5185\u5bb9\u62f7\u8d1d\u5230 fd&#061;1 \u4e2d\u3002\u5b83\u53ea\u52a8\u4e86 1&#xff0c;\u6ca1\u52a8 2\u3002\u6240\u4ee5 fd&#061;2 \u4ecd\u7136\u6307\u7740\u663e\u793a\u5668\u3002&#xff08;\u524d\u4e24\u4e2a\u90fd\u662f\u5c5e\u4e8e\u6807\u51c6\u8f93\u51fa\u90a3\u7684\u5e93\u51fd\u6570&#xff0c;\u76f4\u63a5\u5199\u5411\u663e\u793a\u5668\u7684&#xff0c;\u73b0\u57281\u53f7\u90a3\u91cc\u88ab\u62f7\u8d1d\u6210log.txt\u7684\u6307\u9488&#xff0c;\u6307\u5411log.txt&#xff0c;\u539f\u672c\u5199\u5411\u663e\u793a\u5668\u7684\u5c31\u53d8\u6210\u5199\u5411log.txt\u7684\u4e86&#xff09;<img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261011202502-6acbf09ef2987.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h4>1.2 \u5408\u5e76\u91cd\u5b9a\u5411\u7684\u6b63\u786e\u59ff\u52bf\u4e0e\u6df1\u5751<\/h4>\n<p>&#x1f4cc;\u5355\u770b\u8fd9\u4e2a&#034;1&gt;log.txt&#034;&#xff1a;\u672c\u8d28\u662f\u628a\u6307\u5411log.txt\u7684\u6307\u9488\u62f7\u8d1d\u5230\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868&#xff0c;\u4e0b\u6807\u4e3a1\u7684\u90a3\u4e2a\u7a7a\u5185\u3002<\/p>\n<p>\u5982\u679c\u4f60\u60f3\u628a stdout \u548c stderr \u5206\u522b\u5b58\u5230\u4e0d\u540c\u6587\u4ef6&#xff0c;\u53ef\u4ee5&#xff1a;<br \/>\n&#xff08;\u524d\u4e24\u4e2a\u8981\u7acb\u5373\u5237\u65b0\u7f13\u51b2\u533a&#xff09;<img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261011202503-6acbf09f0af97.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>.\/a.out <span class=\"token operator\"><span class=\"token file-descriptor important\">1<\/span>&gt;<\/span> out.log <span class=\"token operator\"><span class=\"token file-descriptor important\">2<\/span>&gt;<\/span> err.log<\/p>\n<p>\u5982\u679c\u60f3\u5408\u5e76\u5230\u540c\u4e00\u4e2a\u6587\u4ef6&#xff0c;\u5f88\u591a\u540c\u5b66\u4f1a\u672c\u80fd\u5730\u5199&#xff1a;<\/p>\n<p>.\/a.out <span class=\"token operator\">&gt;<\/span> log.txt <span class=\"token operator\"><span class=\"token file-descriptor important\">2<\/span>&gt;&gt;<\/span> log.txt<\/p>\n<p>\u8001\u5e08\u5728\u8fd9\u91cc\u6572\u4e86\u9ed1\u677f&#xff1a;\u8fd9\u5f88\u5371\u9669&#xff01; \u7b2c\u4e00\u6b21\u91cd\u5b9a\u5411\u6253\u5f00\u6587\u4ef6\u65f6\u4f1a\u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9&#xff1b;\u7b2c\u4e8c\u6b21\u4ee5\u8ffd\u52a0\u65b9\u5f0f\u6253\u5f00&#xff0c;\u867d\u7136\u5185\u5bb9\u80fd\u8fdb\u6765&#xff0c;\u4f46\u4e24\u6b21\u72ec\u7acb\u7684 open \u4f1a\u5bfc\u81f4\u6587\u4ef6\u504f\u79fb\u91cf\u548c\u5199\u5165\u987a\u5e8f\u4e0d\u53ef\u63a7&#xff0c;\u53ef\u80fd\u51fa\u73b0\u6570\u636e\u8986\u76d6\u6216\u4e71\u5e8f\u3002<\/p>\n<p>\u63a8\u8350\u7684\u6b63\u786e\u5199\u6cd5\u662f&#xff1a;<br \/>\n&#xff08;\u6ce8\u610f\u4e0d\u8981\u624b\u8bef\u6253\u7a7a\u683c&#xff0c;\u628a\u4e00\u4e2a\u547d\u4ee4\u62c6\u6389\u4e86&#xff09;<\/p>\n<p>.\/a.out <span class=\"token operator\"><span class=\"token file-descriptor important\">1<\/span>&gt;<\/span> log.txt <span class=\"token operator\"><span class=\"token file-descriptor important\">2<\/span>&gt;<\/span><span class=\"token file-descriptor important\">&amp;1<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261011202503-6acbf09f1ed0d.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u5b83\u7684\u8bed\u4e49\u662f&#xff1a;\u5148\u628a fd&#061;1 \u6307\u5411 log.txt&#xff1b;\u518d\u628a fd&#061;2 \u6307\u5411 fd&#061;1 \u5f53\u524d\u6307\u5411\u7684\u90a3\u4e2a\u6587\u4ef6\u3002\u8fd9\u6837&#xff0c;1 \u548c 2 \u6700\u7ec8\u6307\u5411\u4e86\u540c\u4e00\u4e2a\u6587\u4ef6\u5b9e\u4f53&#xff0c;\u907f\u514d\u4e86\u4e24\u6b21\u6253\u5f00\u5e26\u6765\u7684\u7ade\u4e89\u95ee\u9898\u3002<\/p>\n<h4>1.3 \u4e3a\u4ec0\u4e48\u8981\u641e\u51fa stderr \u8fd9\u4e2a\u4e1c\u897f&#xff1f;<\/h4>\n<p>\u8fd9\u662f\u5f88\u591a\u540c\u5b66&#xff08;\u5305\u62ec\u5f53\u5e74\u542c\u8bfe\u7684\u6211&#xff09;\u7684\u7591\u95ee&#xff1a;\u4e0d\u90fd\u662f\u5f80\u663e\u793a\u5668\u4e0a\u6253\u5417&#xff1f;\u5206\u90a3\u4e48\u6e05\u5e72\u561b&#xff1f;<\/p>\n<p>\u7b54\u6848\u662f&#xff1a;\u4e3a\u4e86\u628a\u5e38\u89c4\u6d88\u606f\u548c\u9519\u8bef\u6d88\u606f\u505a\u5206\u79bb\u3002<\/p>\n<p>\u7a0b\u5e8f\u6253\u65e5\u5fd7\u6709\u4e24\u79cd\u9700\u6c42&#xff1a;\u4e00\u662f\u4e1a\u52a1\u5fc5\u987b\u8f93\u51fa\u7684\u6b63\u5e38\u4fe1\u606f&#xff1b;\u4e8c\u662f\u4e3a\u4e86 debug \u7684\u9519\u8bef\u4fe1\u606f\u3002\u5982\u679c\u6df7\u5728\u4e00\u8d77&#xff0c;\u4f60\u627ebug\u65f6\u5f97\u5728\u4e00\u5806\u6b63\u5e38\u65e5\u5fd7\u91cc\u6dd8\u91d1\u3002\u6709\u4e86 stderr&#xff0c;\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u91cd\u5b9a\u5411\u80fd\u529b&#xff0c;\u8ba9\u6b63\u786e\u6d41\u548c\u9519\u8bef\u6d41\u201c\u7269\u7406\u9694\u79bb\u201d&#xff0c;\u5f62\u6210\u72ec\u7acb\u7684\u65e5\u5fd7\u6587\u4ef6\u3002\u8fd9\u4e5f\u662f perror\u3001cerr \u5b58\u5728\u7684\u8bbe\u8ba1\u6839\u57fa\u3002<\/p>\n<hr \/>\n<h3>\u4e8c\u3001\u201c\u4e00\u5207\u7686\u6587\u4ef6\u201d\u7684\u5e95\u6c14&#xff1a;VFS (Virtual File System\u201d&#xff08;\u865a\u62df\u6587\u4ef6\u7cfb\u7edf&#xff09;) \u4e0e C \u8bed\u8a00\u5199\u51fa\u7684\u201c\u591a\u6001\u201d<\/h3>\n<h4>2.1 \u4ece PCB \u5230 file&#xff08;FILE* \u90a3\u4e2a\u8fd4\u56de\u503c&#xff09; \u7ed3\u6784\u4f53<\/h4>\n<p>\u4e0a\u8282\u8bfe\u6211\u4eec\u77e5\u9053\u4e86 fd \u7684\u672c\u8d28\u662f\u6570\u7ec4\u4e0b\u6807\u3002\u8fd9\u8282\u8bfe\u8001\u5e08\u5e26\u6211\u4eec\u6253\u5f00\u4e86 Linux 2.6 \u5185\u6838\u6e90\u7801&#xff08;task_struct \u2192 files_struct \u2192 file&#xff09;&#xff0c;\u9a8c\u8bc1\u4e86\u8fd9\u4e2a\u6570\u7ec4\u91cc\u5b58\u7684\u662f\u6307\u5411 struct file \u7684\u6307\u9488\u3002<\/p>\n<p>struct file \u91cc\u6709\u4ec0\u4e48\u5173\u952e\u4fe1\u606f&#xff1f;<\/p>\n<ul>\n<li>\u5f15\u7528\u8ba1\u6570&#xff08;f_count&#xff09;&#xff1a;\u8bb0\u5f55\u591a\u5c11\u4e2a fd \u6307\u5411\u4e86\u8fd9\u4e2a\u6587\u4ef6\u5bf9\u8c61&#xff1b;<\/li>\n<li>\u8bfb\u5199\u4f4d\u7f6e&#xff08;f_pos&#xff09;&#xff1a;\u8001\u5e08\u5728\u8fd9\u91cc\u8bf4\u4e86\u4e00\u53e5\u975e\u5e38\u918d\u9190\u704c\u9876\u7684\u8bdd\u2014\u2014\u201c\u65e0\u8bba\u6587\u672c\u6587\u4ef6\u8fd8\u662f\u4e8c\u8fdb\u5236\u6587\u4ef6&#xff0c;\u5728\u6211\u770b\u6765\u90fd\u662f char \u7c7b\u578b\u7684\u4e00\u7ef4\u6570\u7ec4&#xff01;\u201d f_pos \u5c31\u662f\u4f60\u5f53\u524d\u8bfb\u5199\u5230\u8fd9\u4e2a\u6570\u7ec4\u7684\u7b2c\u51e0\u4e2a\u5143\u7d20&#xff1b;<\/li>\n<li>\u5185\u6838\u7f13\u51b2\u533a&#xff1a;\u901a\u8fc7 struct file \u80fd\u627e\u5230\u6587\u4ef6\u5bf9\u5e94\u7684\u5185\u6838\u7ea7\u7f13\u51b2&#xff08;page cache \u76f8\u5173&#xff09;&#xff1b;<\/li>\n<li>inode \u6307\u9488&#xff1a;\u6587\u4ef6\u7684\u786c\u5c5e\u6027&#xff08;\u5927\u5c0f\u3001\u6743\u9650\u3001ACM \u65f6\u95f4&#xff09;\u5e76\u4e0d\u76f4\u63a5\u653e\u5728 file \u91cc&#xff0c;\u800c\u662f\u653e\u5728 inode \u4e2d&#xff0c;\u901a\u8fc7 file \u95f4\u63a5\u627e\u5230\u3002<\/li>\n<\/ul>\n<h4>2.2 \u4e0d\u540c\u786c\u4ef6\u7684\u8bfb\u5199\u65b9\u6cd5\u5929\u5dee\u5730\u522b<\/h4>\n<p>\u64cd\u4f5c\u7cfb\u7edf\u5e95\u5c42\u9762\u5bf9\u7684\u662f\u4ec0\u4e48&#xff1f;\u78c1\u76d8\u3001\u663e\u793a\u5668\u3001\u952e\u76d8\u3001\u9f20\u6807\u3001\u7f51\u5361\u2026\u2026\u6bcf\u79cd\u786c\u4ef6\u7684 I\/O \u65b9\u6cd5\u5b8c\u5168\u4e0d\u540c&#xff1a;<\/p>\n<ul>\n<li>\u8bfb\u78c1\u76d8&#xff1a;\u78c1\u5934\u5bfb\u9053\u3001\u65cb\u8f6c\u3001DMA\u2026\u2026<\/li>\n<li>\u5199\u663e\u793a\u5668&#xff1a;\u5f80\u663e\u5b58\u6620\u5c04\u533a\u5199\u6570\u636e\u2026\u2026<\/li>\n<li>\u8bfb\u952e\u76d8&#xff1a;\u626b\u63cf\u7801\u3001\u4e2d\u65ad\u2026\u2026<\/li>\n<\/ul>\n<p>\u95ee\u9898\u6765\u4e86&#xff1a; \u5982\u679c\u8ba9\u7528\u6237\u8fdb\u7a0b\u76f4\u63a5\u9762\u5bf9\u8fd9\u4e9b\u5dee\u5f02&#xff0c;\u90a3\u6bcf\u8bbf\u95ee\u4e00\u79cd\u786c\u4ef6\u5c31\u5f97\u6362\u4e00\u5957\u63a5\u53e3&#xff0c;\u7a0b\u5e8f\u6ca1\u6cd5\u5199\u4e86\u3002<\/p>\n<h4>2.3 VFS&#xff08;Virtual File System\u201d&#xff08;\u865a\u62df\u6587\u4ef6\u7cfb\u7edf&#xff09;&#xff09;&#xff1a;\u52a0\u4e00\u5c42\u8f6f\u4ef6\u5c42&#xff0c;\u5c4f\u853d\u4e00\u5207\u5dee\u5f02<\/h4>\n<p>\u8fd9\u91cc\u8001\u5e08\u5f15\u7528\u4e86\u8f6f\u4ef6\u5de5\u7a0b\u91cc\u7684\u4e00\u53e5\u7ecf\u5178\u540d\u8a00&#xff1a;<\/p>\n<p>\u201c\u4efb\u4f55\u8ba1\u7b97\u673a\u95ee\u9898\u90fd\u53ef\u4ee5\u901a\u8fc7\u589e\u52a0\u4e00\u5c42\u8f6f\u4ef6\u5c42\u6765\u89e3\u51b3\u3002\u201d<\/p>\n<p>Linux \u5728\u5185\u6838\u4e2d\u52a0\u4e86\u4e00\u5c42 VFS&#xff08;Virtual File System&#xff0c;\u865a\u62df\u6587\u4ef6\u7cfb\u7edf&#xff09;\u3002<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261011202503-6acbf09f3325a.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u5728 struct file \u4e2d&#xff0c;\u6709\u4e00\u4e2a\u6210\u5458\u53eb f_op&#xff0c;\u5b83\u662f\u4e00\u4e2a\u6307\u9488&#xff0c;\u6307\u5411 struct file_operations&#xff1a;<\/p>\n<p><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">file<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ &#8230;<\/span><br \/>\n    <span class=\"token keyword\">const<\/span> <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">file_operations<\/span> <span class=\"token operator\">*<\/span>f_op<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/ &#8230;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">file_operations<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token class-name\">ssize_t<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>read<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">file<\/span> <span class=\"token operator\">*<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">char<\/span> __user <span class=\"token operator\">*<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">size_t<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">loff_t<\/span> <span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token class-name\">ssize_t<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span>write<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">file<\/span> <span class=\"token operator\">*<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span> __user <span class=\"token operator\">*<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">size_t<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">loff_t<\/span> <span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/ &#8230;<\/span><br \/>\n    <span class=\"token comment\">\/\/\u5b58\u7684\u662fI\/O\u65b9\u6cd5\u7684\u51fd\u6570\u6307\u9488<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u5f53\u8fdb\u7a0b\u6253\u5f00\u4e00\u4e2a\u8bbe\u5907\u6587\u4ef6\u65f6&#xff0c;\u5185\u6838\u4f1a\u6839\u636e\u8bbe\u5907\u7c7b\u578b&#xff0c;\u628a f_op \u6307\u5411\u8be5\u8bbe\u5907\u9a71\u52a8\u63d0\u4f9b\u7684\u5177\u4f53\u51fd\u6570&#xff1a;<\/p>\n<ul>\n<li>\u6253\u5f00\u78c1\u76d8\u6587\u4ef6 \u2192 f_op-&gt;read \u6307\u5411\u78c1\u76d8\u7684\u8bfb\u65b9\u6cd5&#xff1b;<\/li>\n<li>\u6253\u5f00\u663e\u793a\u5668 \u2192 f_op-&gt;write \u6307\u5411\u663e\u5b58\u7684\u5199\u65b9\u6cd5&#xff1b;<\/li>\n<li>\u6253\u5f00\u952e\u76d8 \u2192 f_op-&gt;read \u6307\u5411\u952e\u76d8\u9a71\u52a8\u7684\u8bfb\u65b9\u6cd5\u2026\u2026<\/li>\n<\/ul>\n<p>\u4e0a\u5c42\u7528\u6237\u53ea\u8c03\u7528\u7edf\u4e00\u7684 read\u3001write&#xff0c;\u5e95\u5c42\u901a\u8fc7\u51fd\u6570\u6307\u9488\u52a8\u6001\u7ed1\u5b9a\u5230\u4e0d\u540c\u7684\u786c\u4ef6\u5b9e\u73b0\u3002<\/p>\n<h4>2.4 C \u8bed\u8a00\u5b9e\u73b0\u7684\u591a\u6001<\/h4>\n<p>\u8001\u5e08\u5728\u8fd9\u91cc\u6fc0\u52a8\u5730\u6572\u4e86\u4e00\u884c\u7ed3\u8bba&#xff1a;\u201c\u8fd9\u5c31\u662f\u591a\u6001&#xff01;\u201d<\/p>\n<p>C \u8bed\u8a00\u6ca1\u6709 class&#xff0c;\u6ca1\u6709\u865a\u51fd\u6570&#xff0c;\u4f46\u5b83\u7528\u7ed3\u6784\u4f53 &#043; \u51fd\u6570\u6307\u9488\u5b8c\u7f8e\u5b9e\u73b0\u4e86\u9762\u5411\u5bf9\u8c61\u91cc\u7684\u591a\u6001\u7279\u6027\u3002C&#043;&#043; \u7684\u865a\u51fd\u6570\u8868&#xff0c;\u672c\u8d28\u4e0a\u4e5f\u662f\u4e00\u5f20\u51fd\u6570\u6307\u9488\u8868&#xff1b;\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u65e9\u5728 C&#043;&#043; \u666e\u53ca\u4e4b\u524d&#xff0c;\u5c31\u7528\u8fd9\u5957\u529e\u6cd5\u628a\u786c\u4ef6\u5dee\u5f02\u62b9\u5e73\u4e86\u3002<\/p>\n<p>\u6240\u4ee5&#xff0c;\u201c\u4e00\u5207\u7686\u6587\u4ef6\u201d\u4e0d\u662f\u4e00\u53e5\u53e3\u53f7&#xff0c;\u800c\u662f\u8bf4&#xff1a;\u8fdb\u7a0b\u88ab\u9a97\u8fc7\u53bb\u4e86\u2014\u2014\u5b83\u4ee5\u4e3a\u81ea\u5df1\u5728\u64cd\u4f5c\u201c\u6587\u4ef6\u201d&#xff0c;\u5176\u5b9e\u64cd\u4f5c\u7684\u662f VFS \u5c42\u7edf\u4e00\u62bd\u8c61\u51fa\u6765\u7684 struct file&#xff1b;\u5185\u6838\u518d\u901a\u8fc7\u51fd\u6570\u6307\u9488&#xff0c;\u628a\u8bf7\u6c42\u5206\u53d1\u5230\u78c1\u76d8\u3001\u663e\u793a\u5668\u3001\u7f51\u5361\u7b49\u5b8c\u5168\u4e0d\u540c\u7684\u9a71\u52a8\u4e0a\u3002<\/p>\n<hr \/>\n<h3>\u4e09\u3001\u7f13\u51b2\u533a\u673a\u5236&#xff1a;\u85cf\u5728 printf \u80cc\u540e\u7684\u901f\u5ea6\u4e0e\u9677\u9631<\/h3>\n<h4>3.1 \u4ec0\u4e48\u662f\u7f13\u51b2\u533a&#xff1f;<\/h4>\n<p>\u8001\u5e08\u7684\u5b9a\u4e49\u6781\u7b80\u6709\u529b&#xff1a;<\/p>\n<p>\u7f13\u51b2\u533a&#xff0c;\u5c31\u662f\u5185\u5b58\u4e2d\u7684\u4e00\u6bb5\u7a7a\u95f4\u3002<\/p>\n<p>\u4f46\u5173\u952e\u5728\u4e8e&#xff1a;\u7f13\u51b2\u533a\u4e0d\u6b62\u4e00\u5c42\u3002\u4ece\u7528\u6237\u4ee3\u7801\u5230\u786c\u4ef6&#xff0c;\u6570\u636e\u8981\u7ecf\u5386\u4e24\u4e2a\u4e3b\u8981\u7f13\u5b58\u7ad9&#xff1a;<\/p>\n<li>\u7528\u6237\u7ea7\u7f13\u51b2\u533a&#xff1a;\u7531 C \u6807\u51c6\u5e93&#xff08;glibc&#xff09;\u7ef4\u62a4&#xff0c;\u85cf\u5728 FILE \u7ed3\u6784\u4f53\u91cc\u3002printf\u3001fprintf\u3001fwrite \u5148\u628a\u6570\u636e\u5199\u5230\u8fd9\u91cc&#xff1b;<\/li>\n<li>\u5185\u6838\u7ea7\u7f13\u51b2\u533a&#xff1a;\u7531 OS \u5185\u6838\u7ef4\u62a4&#xff0c;\u4e0e struct file \u5173\u8054\u3002write \u7cfb\u7edf\u8c03\u7528\u628a\u6570\u636e\u4ece\u7528\u6237\u7a7a\u95f4\u62f7\u8d1d\u5230\u8fd9\u91cc&#xff0c;\u518d\u7531 OS \u51b3\u5b9a\u4f55\u65f6\u5237\u5230\u786c\u4ef6\u3002<\/li>\n<h4>3.2 \u4e3a\u4ec0\u4e48\u9700\u8981\u7f13\u51b2\u533a&#xff1f;\u83dc\u9e1f\u9a7f\u7ad9\u6a21\u578b<\/h4>\n<p>\u8001\u5e08\u4e3a\u4e86\u8bb2\u6e05\u8fd9\u4e2a\u95ee\u9898&#xff0c;\u4e3e\u4e86\u4e00\u4e2a\u751f\u6d3b\u5316\u7684\u4f8b\u5b50\u2014\u2014\u83dc\u9e1f\u9a7f\u7ad9\u3002<\/p>\n<p>\u5047\u8bbe\u6ca1\u6709\u83dc\u9e1f\u9a7f\u7ad9&#xff08;\u6ca1\u6709\u7f13\u51b2\u533a&#xff09;&#xff1a;<\/p>\n<ul>\n<li>\u4f60\u662f\u7528\u6237&#xff0c;\u5feb\u9012\u5458&#xff08;OS&#xff09;\u6bcf\u6b21\u6765\u4e00\u4e2a\u5305\u88f9&#xff08;\u6570\u636e&#xff09;&#xff0c;\u90fd\u5fc5\u987b\u5728\u697c\u4e0b\u6253\u7535\u8bdd\u7b49\u4f60&#xff0c;\u4f60\u5f97\u7acb\u523b\u4e0b\u697c\u53d6\u3002\u5982\u679c\u4f60\u5728\u505a\u66f4\u91cd\u8981\u7684\u4e8b&#xff08;CPU \u5728\u6267\u884c\u8ba1\u7b97&#xff09;&#xff0c;\u4f60\u5c31\u88ab\u9891\u7e41\u6253\u65ad\u3002<\/li>\n<li>\u5feb\u9012\u5458\u6bcf\u6b21\u4e5f\u8981\u7b49\u4f60&#xff0c;\u4e00\u5929\u53d1\u4e0d\u4e86\u51e0\u4e2a\u4ef6\u3002<\/li>\n<\/ul>\n<p>\u6709\u4e86\u83dc\u9e1f\u9a7f\u7ad9&#xff08;\u5f15\u5165\u7f13\u51b2\u533a&#xff09;\u540e&#xff1a;<\/p>\n<ul>\n<li>\u5feb\u9012\u5458\u628a\u5305\u88f9\u6279\u91cf\u4e22\u8fdb\u9a7f\u7ad9&#xff0c;\u4e0d\u7528\u7b49\u4f60&#xff1b;<\/li>\n<li>\u4f60\u4ec0\u4e48\u65f6\u5019\u6709\u7a7a&#xff0c;\u4e0b\u697c\u4e00\u6b21\u6027\u62ff\u4e00\u5806\u3002<\/li>\n<\/ul>\n<p>\u6620\u5c04\u5230\u8ba1\u7b97\u673a&#xff1a;<\/p>\n<ul>\n<li>\u7528\u6237\u7ea7\u7f13\u51b2\u533a&#xff1a;\u51cf\u5c11\u4f60\u7684\u7cfb\u7edf\u8c03\u7528\u6b21\u6570\u3002printf \u5341\u6b21&#xff0c;\u53ef\u80fd\u53ea\u89e6\u53d1\u4e00\u6b21 write&#xff1b;<\/li>\n<li>\u5185\u6838\u7ea7\u7f13\u51b2\u533a&#xff1a;\u64cd\u4f5c\u7cfb\u7edf\u4e5f\u4e0d\u7528\u6bcf\u6b21\u90fd\u53bb\u9a9a\u6270\u786c\u4ef6&#xff0c;\u6512\u591f\u4e00\u6ce2\u518d\u7edf\u4e00\u5199\u76d8\u3002<\/li>\n<\/ul>\n<p>\u7ed3\u8bba&#xff1a;\u7f13\u51b2\u533a\u5b58\u5728\u7684\u6839\u672c\u76ee\u7684&#xff0c;\u662f\u63d0\u9ad8\u4f7f\u7528\u8005\u548c\u7cfb\u7edf\u7684\u6548\u7387\u3002<\/p>\n<h4>3.3 \u4e09\u79cd\u5237\u65b0\u7b56\u7565<\/h4>\n<table>\n<tr>\u7b56\u7565\u89e6\u53d1\u6761\u4ef6\u9002\u7528\u573a\u666f\u5907\u6ce8<\/tr>\n<tbody>\n<tr>\n<td>\u65e0\u7f13\u51b2\/\u7acb\u5373\u5237\u65b0<\/td>\n<td>\u5199\u7acb\u5373\u5237<\/td>\n<td>stderr&#xff08;\u9ed8\u8ba4&#xff09;<\/td>\n<td>\u9519\u8bef\u4fe1\u606f\u8981\u5c3d\u5feb\u8ba9\u4eba\u770b\u5230<\/td>\n<\/tr>\n<tr>\n<td>\u884c\u7f13\u51b2<\/td>\n<td>\u9047\u5230 \\\\n \u6216\u7f13\u51b2\u533a\u6ee1<\/td>\n<td>\u6807\u51c6\u8f93\u51fa\u5230\u663e\u793a\u5668<\/td>\n<td>\u5c0a\u91cd\u4eba\u7c7b\u201c\u4e00\u884c\u4e00\u884c\u9605\u8bfb\u201d\u7684\u4e60\u60ef<\/td>\n<\/tr>\n<tr>\n<td>\u5168\u7f13\u51b2<\/td>\n<td>\u7f13\u51b2\u533a\u5199\u6ee1\u624d\u5237<\/td>\n<td>\u666e\u901a\u6587\u4ef6\u64cd\u4f5c<\/td>\n<td>\u6548\u7387\u6700\u9ad8&#xff0c;\u7cfb\u7edf\u8c03\u7528\u6b21\u6570\u6700\u5c11<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e00\u4e2a\u9690\u85cf\u6781\u6df1\u7684\u77e5\u8bc6\u70b9&#xff1a;\u91cd\u5b9a\u5411\u4f1a\u6539\u53d8\u7f13\u51b2\u7b56\u7565&#xff01;<\/p>\n<p>\u5f53\u4f60\u628a stdout \u91cd\u5b9a\u5411\u5230\u6587\u4ef6\u65f6&#xff0c;\u5b83\u7684\u7f13\u51b2\u6a21\u5f0f\u4f1a\u4ece\u884c\u7f13\u51b2\u6084\u6084\u53d8\u6210\u5168\u7f13\u51b2\u3002\u8fd9\u4e2a\u7ec6\u8282&#xff0c;\u662f\u7406\u89e3\u4e0b\u9762\u8fd9\u4e2a\u7ecf\u5178\u6848\u4f8b\u7684\u94a5\u5319\u3002<\/p>\n<hr \/>\n<h3>\u56db\u3001\u7ecf\u5178\u6848\u4f8b&#xff1a;Fork \u4e4b\u540e\u4e3a\u4ec0\u4e48\u6253\u5370\u4e86 7 \u6761&#xff1f;<\/h3>\n<p>\u8fd9\u662f\u672c\u8282\u8bfe\u6700\u6838\u5fc3\u3001\u6700\u8003\u5bdf\u7406\u89e3\u6df1\u5ea6\u7684\u4f8b\u5b50\u3002\u8001\u5e08\u73b0\u573a\u5199\u4e86\u4e00\u6bb5\u4ee3\u7801&#xff1a;<\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;stdio.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;unistd.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;string.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;sys\/types.h&gt;<\/span><\/span><\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;hello printf\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>               <span class=\"token comment\">\/\/ \u5e93\u51fd\u6570&#xff08;\u884c\u7f13\u51b2\/\u5168\u7f13\u51b2\u53d6\u51b3\u4e8e\u76ee\u6807&#xff09;<\/span><br \/>\n    <span class=\"token function\">fprintf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token constant\">stdout<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;hello fprintf\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>     <span class=\"token comment\">\/\/ \u5e93\u51fd\u6570<\/span><br \/>\n    <span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span><span class=\"token operator\">*<\/span> msg1 <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;hello fwrite\\\\n&#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token function\">fwrite<\/span><span class=\"token punctuation\">(<\/span>msg1<span class=\"token punctuation\">,<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token function\">strlen<\/span><span class=\"token punctuation\">(<\/span>msg1<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token constant\">stdout<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u5e93\u51fd\u6570<\/span><\/p>\n<p>    <span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span><span class=\"token operator\">*<\/span> msg2 <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;hello write\\\\n&#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token function\">write<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> msg2<span class=\"token punctuation\">,<\/span> <span class=\"token function\">strlen<\/span><span class=\"token punctuation\">(<\/span>msg2<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>          <span class=\"token comment\">\/\/ \u7cfb\u7edf\u8c03\u7528&#xff01;<\/span><\/p>\n<p>    <span class=\"token function\">fork<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u5728\u7a0b\u5e8f\u7ed3\u5c3e fork<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h4>4.1 \u73b0\u8c61\u5dee\u5f02<\/h4>\n<p>\u573a\u666f A&#xff1a;\u76f4\u63a5\u8fd0\u884c&#xff08;\u5411\u663e\u793a\u5668\u6253\u5370&#xff09;<\/p>\n<p>.\/a.out<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261011202503-6acbf09f7c2b1.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u8f93\u51fa 4 \u6761\u3002\u56e0\u4e3a\u663e\u793a\u5668\u662f\u884c\u7f13\u51b2&#xff0c;\\\\n \u89e6\u53d1\u4e86\u5237\u65b0&#xff0c;fork \u4e4b\u524d\u7528\u6237\u7f13\u51b2\u533a\u5df2\u7ecf\u7a7a\u4e86\u3002<\/p>\n<p>\u573a\u666f B&#xff1a;\u91cd\u5b9a\u5411\u5230\u6587\u4ef6<\/p>\n<p>.\/a.out <span class=\"token operator\">&gt;<\/span> log.txt<br \/>\n<span class=\"token function\">cat<\/span> log.txt<\/p>\n<p>\u8f93\u51fa 7 \u6761&#xff01; \u800c\u4e14\u4f60\u4f1a\u53d1\u73b0&#xff1a;<img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261011202503-6acbf09f8a0cc.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<ul>\n<li>write \u7684\u5185\u5bb9\u53ea\u51fa\u73b0 1 \u6b21&#xff1b;<\/li>\n<li>printf\u3001fprintf\u3001fwrite \u7684\u5185\u5bb9\u5404\u51fa\u73b0\u4e86 2 \u6b21\u3002<\/li>\n<\/ul>\n<h4>4.2 \u539f\u56e0\u5256\u6790<\/h4>\n<p>write \u4e3a\u4ec0\u4e48\u53ea\u6253\u5370 1 \u6b21&#xff1f;<br \/>\nwrite \u662f\u7eaf\u7cfb\u7edf\u8c03\u7528&#xff0c;\u5b83\u76f4\u63a5\u628a\u6570\u636e\u62f7\u8d1d\u8fdb\u4e86\u5185\u6838\u7ea7\u7f13\u51b2\u533a\u3002fork \u4e4b\u524d&#xff0c;\u6570\u636e\u5df2\u7ecf\u5c5e\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u4e86&#xff0c;\u4e0d\u5c5e\u4e8e\u8fdb\u7a0b\u7684\u7528\u6237\u6001\u5185\u5b58\u7a7a\u95f4\u3002\u6240\u4ee5 fork \u4e4b\u540e&#xff0c;\u65e0\u8bba\u7236\u5b50\u8fdb\u7a0b&#xff0c;\u8fd9\u4efd\u6570\u636e\u90fd\u4e0d\u4f1a\u88ab\u590d\u5236&#xff0c;\u81ea\u7136\u53ea\u51fa\u73b0\u4e00\u6b21\u3002<\/p>\n<p>printf\/fprintf\/fwrite \u4e3a\u4ec0\u4e48\u6253\u5370 2 \u6b21&#xff1f;<br \/>\n\u8fd9\u4e09\u4e2a\u90fd\u662fC \u6807\u51c6\u5e93\u51fd\u6570&#xff0c;\u5b83\u4eec\u5148\u628a\u6570\u636e\u5199\u8fdb\u4e86\u7528\u6237\u7ea7\u7f13\u51b2\u533a\u3002\u5f53\u6267\u884c\u5230 fork() \u65f6&#xff1a;<\/p>\n<ul>\n<li>fork \u4f1a\u590d\u5236\u7236\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4&#xff0c;\u5176\u4e2d\u5305\u62ec\u7528\u6237\u7ea7\u7f13\u51b2\u533a\u7684\u5185\u5bb9&#xff1b;<\/li>\n<li>\u7236\u5b50\u8fdb\u7a0b\u5404\u81ea\u62e5\u6709\u4e86\u4e00\u4efd\u76f8\u540c\u7684\u7f13\u51b2\u533a\u526f\u672c&#xff1b;<\/li>\n<li>\u8fdb\u7a0b\u9000\u51fa\u65f6&#xff0c;C \u5e93\u4f1a\u81ea\u52a8\u5237\u65b0\u7f13\u51b2\u533a&#xff0c;\u7236\u8fdb\u7a0b\u5237\u4e00\u6b21&#xff0c;\u5b50\u8fdb\u7a0b\u5237\u4e00\u6b21&#xff0c;\u4e8e\u662f\u540c\u6837\u7684\u5185\u5bb9\u5c31\u88ab\u5199\u4e86\u4e24\u6b21\u3002<\/li>\n<\/ul>\n<p>\u4e3a\u4ec0\u4e48\u663e\u793a\u5668\u662f 4 \u6761&#xff0c;\u6587\u4ef6\u662f 7 \u6761&#xff1f;<br \/>\n\u56e0\u4e3a\u91cd\u5b9a\u5411\u5230\u6587\u4ef6\u65f6&#xff0c;\u7f13\u51b2\u7b56\u7565\u7531\u884c\u7f13\u51b2\u964d\u7ea7&#xff08;\u5347\u7ea7&#xff09;\u4e3a\u5168\u7f13\u51b2\u3002\u6570\u636e\u9047\u5230 \\\\n \u4e0d\u4f1a\u7acb\u5373\u5237\u65b0&#xff0c;\u800c\u662f\u7ee7\u7eed\u7559\u5728\u4e86\u7528\u6237\u7ea7\u7f13\u51b2\u533a\u91cc&#xff0c;\u76f4\u5230 fork \u540e\u624d\u88ab\u5404\u81ea\u5237\u51fa&#xff0c;\u4e8e\u662f\u53d1\u751f\u4e86\u590d\u5236\u3002<\/p>\n<h4>4.3 \u8865\u5145\u9a8c\u8bc1&#xff1a;\u63d0\u524d\u5173\u95ed fd \u5bfc\u81f4\u6570\u636e\u4e22\u5931<\/h4>\n<p>\u8001\u5e08\u8fd8\u56de\u987e\u4e86\u4e00\u4e2a\u4e0a\u8282\u8bfe\u7684\u4f8b\u5b50&#xff1a;<\/p>\n<p><span class=\"token function\">close<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>                           <span class=\"token comment\">\/\/ \u5173\u95ed stdout<\/span><br \/>\n<span class=\"token keyword\">int<\/span> fd <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">open<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;log.txt&#034;<\/span><span class=\"token punctuation\">,<\/span> O_CREAT<span class=\"token operator\">|<\/span>O_WRONLY<span class=\"token operator\">|<\/span>O_APPEND<span class=\"token punctuation\">,<\/span> <span class=\"token number\">0666<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;hello bit\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/ close(fd);  \/\/ \u5982\u679c\u5728 fflush \u4e4b\u524d\u5173\u95ed<\/span><\/p>\n<p>\u5982\u679c\u4f60\u5148 printf&#xff0c;\u518d close(fd)&#xff0c;\u6700\u540e\u624d\u8fdb\u7a0b\u9000\u51fa\u2014\u2014\u4f60\u4f1a\u53d1\u73b0 log.txt \u662f\u7a7a\u7684&#xff01;<br \/>\n&#x1f4cc;\u8fd9\u91cc\u7ed9\u7528\u6237\u7ea7\u7f13\u51b2\u533a\u5237\u5230\u5185\u6838\u7ea7\u7f13\u51b2\u533a\u7684\u4e09\u4e2a\u6761\u4ef6&#xff1a;<\/p>\n<li>\u5f3a\u5236\u5237\u65b0<\/li>\n<li>\u5237\u65b0\u6761\u4ef6\u6ee1\u8db3<\/li>\n<li>\u8fdb\u7a0b\u9000\u51fa<\/li>\n<ul>\n<li>\u56e0\u4e3a printf \u7684\u6570\u636e\u8fd8\u5728\u7528\u6237\u7ea7\u7f13\u51b2\u533a&#xff0c;\u4f60\u63d0\u524d\u628a fd \u5173\u4e86&#xff08;\u5237\u65b0\u8981\u7528\u5230fd&#xff09;&#xff1b;<\/li>\n<li>\u8fdb\u7a0b\u9000\u51fa\u65f6\u60f3\u5237\u65b0&#xff0c;\u4f46 write(fd, &#8230;) \u53d1\u73b0 fd \u5df2\u5931\u6548&#xff0c;\u5237\u65b0\u5931\u8d25&#xff0c;\u6570\u636e\u5c31\u4e22\u4e86\u3002<\/li>\n<li>\u4fee\u590d\u65b9\u6cd5&#xff1a;\u5728 close \u524d\u8c03\u7528 fflush(stdout)&#xff0c;\u5f3a\u5236\u628a\u8bed\u8a00\u5c42\u7f13\u51b2\u533a\u5148\u5237\u8fdb\u5185\u6838\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>\u4e94\u3001\u624b\u6495 C \u6807\u51c6\u5e93&#xff1a;\u6a21\u62df fopen \/ fwrite \/ fflush<\/h3>\n<p>\u7406\u89e3\u4e86\u539f\u7406\u540e&#xff0c;\u8001\u5e08\u5e26\u9886\u6211\u4eec\u81ea\u5df1\u5c01\u88c5\u4e86\u4e00\u4e2a my_stdio \u5e93\u3002\u76ee\u7684\u4e0d\u662f\u66ff\u4ee3 glibc&#xff0c;\u800c\u662f\u8ba9\u4f60\u4eb2\u773c\u770b\u5230&#xff1a;\u5e93\u51fd\u6570\u7684\u5e95\u5c42\u903b\u8f91&#xff0c;\u4e0d\u8fc7\u5982\u6b64\u3002<\/p>\n<h4>5.1 \u6570\u636e\u7ed3\u6784&#xff1a;MyFILE<\/h4>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">define<\/span> <span class=\"token macro-name\">MAX_BUFFER_SIZE<\/span> <span class=\"token expression\"><span class=\"token number\">1024<\/span><\/span><\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u5237\u65b0\u7b56\u7565\u6807\u5fd7\u4f4d<\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">define<\/span> <span class=\"token macro-name\">FLUSH_NONE<\/span>   <span class=\"token expression\"><span class=\"token number\">0<\/span>        <\/span><span class=\"token comment\">\/\/ \u65e0\u7f13\u51b2&#xff08;\u7acb\u5373\u5237\u65b0&#xff09;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">define<\/span> <span class=\"token macro-name\">FLUSH_LINE<\/span>   <span class=\"token expression\"><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token operator\">&lt;&lt;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span>   <\/span><span class=\"token comment\">\/\/ \u884c\u7f13\u51b2<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">define<\/span> <span class=\"token macro-name\">FLUSH_ALL<\/span>    <span class=\"token expression\"><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token operator\">&lt;&lt;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span>   <\/span><span class=\"token comment\">\/\/ \u5168\u7f13\u51b2<\/span><\/span><\/p>\n<p><span class=\"token keyword\">typedef<\/span> <span class=\"token keyword\">struct<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> fd<span class=\"token punctuation\">;<\/span>                   <span class=\"token comment\">\/\/ \u5c01\u88c5\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> flag<span class=\"token punctuation\">;<\/span>                 <span class=\"token comment\">\/\/ \u6253\u5f00\u65b9\u5f0f&#xff08;r\/w\/a&#xff09;<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> flush_method<span class=\"token punctuation\">;<\/span>         <span class=\"token comment\">\/\/ \u5237\u65b0\u7b56\u7565<\/span><br \/>\n    <span class=\"token keyword\">char<\/span> outbuffer<span class=\"token punctuation\">[<\/span>MAX_BUFFER_SIZE<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u7528\u6237\u7ea7\u7f13\u51b2\u533a<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> size<span class=\"token punctuation\">;<\/span>                 <span class=\"token comment\">\/\/ \u5f53\u524d\u7f13\u51b2\u533a\u6709\u6548\u957f\u5ea6<\/span><br \/>\n<span class=\"token punctuation\">}<\/span> MyFILE<span class=\"token punctuation\">;<\/span><\/p>\n<h4>5.2 \u5173\u952e\u63a5\u53e3\u5b9e\u73b0\u903b\u8f91<\/h4>\n<p>my_fopen&#xff1a;<\/p>\n<ul>\n<li>\u5e95\u5c42\u8c03\u7528\u7cfb\u7edf\u8c03\u7528 open() \u83b7\u53d6 fd&#xff1b;<\/li>\n<li>malloc \u4e00\u4e2a MyFILE \u5bf9\u8c61&#xff1b;<\/li>\n<li>\u521d\u59cb\u5316 outbuffer&#xff0c;\u8bbe\u7f6e size &#061; 0&#xff1b;<\/li>\n<li>\u6839\u636e\u6587\u4ef6\u7c7b\u578b\u8bbe\u7f6e\u5237\u65b0\u7b56\u7565&#xff1a;\u5982\u679c\u662f\u663e\u793a\u5668&#xff08;isatty&#xff09;&#xff0c;\u9ed8\u8ba4\u8bbe\u6210\u884c\u7f13\u51b2&#xff1b;\u5982\u679c\u662f\u666e\u901a\u6587\u4ef6&#xff0c;\u8bbe\u6210\u5168\u7f13\u51b2\u3002<\/li>\n<\/ul>\n<p>my_fwrite&#xff1a;<\/p>\n<ul>\n<li>\u5199\u5165\u7684\u672c\u8d28\u662f\u4ec0\u4e48&#xff1f;\u8001\u5e08\u8bf4&#xff1a;\u662f\u62f7\u8d1d&#xff08;memcpy&#xff09;&#xff1b;<\/li>\n<li>\u628a\u7528\u6237\u8981\u5199\u7684\u5b57\u7b26\u4e32&#xff0c;\u6309\u957f\u5ea6 memcpy \u5230 MyFILE-&gt;outbuffer \u7684\u5c3e\u90e8&#xff1b;<\/li>\n<li>\u66f4\u65b0 size&#xff1b;<\/li>\n<li>\u5c1d\u8bd5\u5224\u65ad\u5237\u65b0\u6761\u4ef6&#xff1a;\n<ul>\n<li>\u5982\u679c\u662f\u884c\u7f13\u51b2&#xff0c;\u68c0\u67e5\u7f13\u51b2\u533a\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u662f\u4e0d\u662f \\\\n&#xff0c;\u662f\u5219\u8c03\u7528 my_fflush&#xff1b;<\/li>\n<li>\u5982\u679c\u662f\u5168\u7f13\u51b2&#xff0c;\u68c0\u67e5 size \u662f\u5426\u8fbe\u5230 MAX_BUFFER_SIZE&#xff0c;\u662f\u5219\u8c03\u7528 my_fflush\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>my_fflush&#xff1a;<\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">my_fflush<\/span><span class=\"token punctuation\">(<\/span>MyFILE <span class=\"token operator\">*<\/span>fp<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>fp<span class=\"token operator\">-&gt;<\/span>size <span class=\"token operator\">&gt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token function\">write<\/span><span class=\"token punctuation\">(<\/span>fp<span class=\"token operator\">-&gt;<\/span>fd<span class=\"token punctuation\">,<\/span> fp<span class=\"token operator\">-&gt;<\/span>outbuffer<span class=\"token punctuation\">,<\/span> fp<span class=\"token operator\">-&gt;<\/span>size<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u7cfb\u7edf\u8c03\u7528&#xff0c;\u5165\u5185\u6838&#xff01;<\/span><br \/>\n        fp<span class=\"token operator\">-&gt;<\/span>size <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span>                           <span class=\"token comment\">\/\/ \u6e05\u7a7a\u7528\u6237\u7f13\u51b2\u533a\u8ba1\u6570<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u53ef\u9009&#xff1a;fsync(fp-&gt;fd) \u5f3a\u5236\u5237\u5230\u786c\u4ef6<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>my_fclose&#xff1a;<\/p>\n<ul>\n<li>\u5fc5\u987b\u5148 my_fflush&#xff0c;\u518d close(fd)&#xff0c;\u6700\u540e free(fp)\u3002<\/li>\n<li>\u8fd9\u5bf9\u5e94\u4e86 glibc \u5728\u8fdb\u7a0b\u9000\u51fa\u524d\u81ea\u52a8\u626b\u63cf\u94fe\u8868\u3001\u91ca\u653e FILE \u5bf9\u8c61\u7684\u903b\u8f91\u3002<\/li>\n<\/ul>\n<h4>5.3 \u91d1\u53e5&#xff1a;\u6570\u636e\u6d41\u52a8\u7684\u672c\u8d28\u53ea\u6709\u201c\u62f7\u8d1d\u201d<\/h4>\n<p>\u8001\u5e08\u5728\u8bfe\u4e0a\u53cd\u590d\u5f3a\u8c03&#xff0c;\u4e0d\u8981\u8ff7\u4fe1 read\u3001write \u8fd9\u79cd\u540d\u5b57&#xff1a;<\/p>\n<p>\u201c\u5728\u6211\u770b\u6765&#xff0c;\u8fd9\u4e2a\u4e16\u754c\u4e0a\u53ea\u6709\u62f7\u8d1d\u3002read \u662f\u62f7\u8d1d&#xff0c;write \u4e5f\u662f\u62f7\u8d1d\u3002\u6570\u636e\u4ece\u7528\u6237\u7f13\u51b2\u533a\u62f7\u8d1d\u5230\u5185\u6838\u7f13\u51b2\u533a&#xff0c;\u518d\u4ece\u5185\u6838\u7f13\u51b2\u533a\u62f7\u8d1d\u5230\u786c\u4ef6&#xff0c;\u4e00\u5207\u90fd\u662f\u62f7\u8d1d\u3002\u201d<\/p>\n<p>\u8ba1\u7b97\u673a\u6570\u636e\u6d41\u52a8\u7684\u672c\u8d28&#xff0c;\u5c31\u662f\u6570\u636e\u5728\u5404\u7ea7\u7f13\u51b2\u533a\u4e4b\u95f4\u7684\u62f7\u8d1d\u3002<\/p>\n<hr \/>\n<h3>\u516d\u3001\u90a3\u4e9b\u4e0d\u5bb9\u5ffd\u89c6\u7684\u5c0f\u77e5\u8bc6\u70b9<\/h3>\n<p>\u9664\u4e86\u4e3b\u7ebf\u8109\u7edc&#xff0c;\u8001\u5e08\u8fd8\u7a7f\u63d2\u4e86\u4e0d\u5c11\u5bb9\u6613\u5ffd\u7565\u4f46\u6781\u6709\u4ef7\u503c\u7684\u7ec6\u8282&#xff1a;<\/p>\n<li>fd \u7684\u4e0a\u9650&#xff1a;\u9ed8\u8ba4 fd \u8868\u5927\u5c0f\u662f 32 \u6216 64&#xff0c;\u4f46\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u6269\u5c55\u5230 65535&#xff0c;\u8fd9\u5728\u540e\u9762\u5b66\u7f51\u7edc\u65f6\u4f1a\u9047\u5230&#xff1b;<\/li>\n<li>\u6587\u4ef6\u662f\u4e00\u7ef4\u6570\u7ec4&#xff1a;fseek\u3001ftell\u3001rewind \u672c\u8d28\u4e0a\u90fd\u662f\u5728\u64cd\u4f5c\u6570\u7ec4\u4e0b\u6807 f_pos&#xff1b;<\/li>\n<li>\u5f15\u7528\u8ba1\u6570&#xff1a;struct file \u91cc\u7684 f_count \u8ba9\u4f60\u53ef\u4ee5\u8fdb\u884c\u201c\u4e00\u4e2a\u6587\u4ef6\u88ab\u591a\u4e2a fd \u6307\u5411\u201d\u7684\u64cd\u4f5c&#xff08;\u6bd4\u5982 dup2&#xff09;&#xff1b;<\/li>\n<li>\u5185\u5b58\u7ba1\u7406&#xff1a;\u64cd\u4f5c\u7cfb\u7edf\u7684\u7269\u7406\u5185\u5b58\u4ee5 4KB&#xff08;\u9875&#xff09; \u4e3a\u5355\u4f4d\u7ba1\u7406\u3002\u6587\u4ef6\u7684\u5185\u6838\u7f13\u51b2\u533a\u4e5f\u548c\u9875\u7f13\u5b58&#xff08;page cache&#xff09;\u7d27\u5bc6\u5173\u8054&#xff1b;<\/li>\n<li>\u7b97\u6cd5\u9898\u8d85\u65f6\u4e0e\u7f13\u51b2\u533a&#xff1a;\u5982\u679c\u4f60\u5728\u505a OJ \u65f6&#xff0c;\u540c\u6837\u7684\u903b\u8f91\u5728 C&#043;&#043; \u91cc\u7528 std::cout &lt;&lt; &#8230; &lt;&lt; std::endl \u72c2\u5237\u65b0\u5bfc\u81f4\u8d85\u65f6&#xff0c;\u6362\u6210 printf \u6216\u8005\u5173\u95ed\u540c\u6b65\/\u51cf\u5c11 endl&#xff08;\u56e0\u4e3a endl \u4f1a\u5f3a\u5236\u5237\u65b0&#xff09;&#xff0c;\u6548\u7387\u53ef\u80fd\u5927\u5e45\u63d0\u5347&#xff1b;<\/li>\n<li>\u8fdb\u5ea6\u6761\u5b9e\u9a8c&#xff1a;\u5f53\u5e74\u5199\u8fdb\u5ea6\u6761\u65f6&#xff0c;\u4e0d\u52a0 \\\\n \u4e0d\u663e\u793a&#xff0c;\u5c31\u662f\u56e0\u4e3a\u884c\u7f13\u51b2\u5728\u7b49\u6362\u884c\u7b26&#xff1b;\u7a0b\u5e8f\u9000\u51fa\u65f6\u624d\u4e00\u6b21\u6027\u5237\u51fa\u6765\u3002<\/li>\n<hr \/>\n<h3>\u4e03\u3001\u603b\u7ed3&#xff1a;\u628a\u8fd9\u8282\u8bfe\u538b\u7f29\u6210\u4e09\u53e5\u8bdd<\/h3>\n<li>\u91cd\u5b9a\u5411\u73a9\u7684\u4e0d\u662f\u6587\u4ef6\u540d&#xff0c;\u800c\u662f\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u6307\u5411\u5173\u7cfb\u30021&gt; file \u662f\u62f7\u8d1d\u6307\u9488&#xff1b;2&gt;&amp;1 \u662f\u8ba9\u9519\u8bef\u6d41\u8ffd\u968f\u8f93\u51fa\u6d41\u3002<\/li>\n<li>\u4e00\u5207\u7686\u6587\u4ef6\u7684\u80cc\u540e&#xff0c;\u662f VFS \u5c42\u901a\u8fc7\u7ed3\u6784\u4f53 &#043; \u51fd\u6570\u6307\u9488\u5b9e\u73b0\u7684\u591a\u6001&#xff0c;\u8ba9\u8fdb\u7a0b\u4ee5\u4e3a\u5168\u4e16\u754c\u90fd\u662f struct file&#xff0c;\u4ece\u800c\u5c4f\u853d\u4e86\u786c\u4ef6\u5dee\u5f02\u3002<\/li>\n<li>\u7f13\u51b2\u533a\u6709\u4e24\u7ea7&#xff1a;C \u5e93\u7684&#034;\u7528\u6237\u7f13\u51b2\u533a&#034;\u662f\u4e3a\u4e86\u51cf\u5c11\u4f60\u7684\u7cfb\u7edf\u8c03\u7528\u6b21\u6570&#xff1b;\u5185\u6838\u7684&#034;\u6587\u4ef6\u7f13\u51b2\u533a&#034;\u662f\u4e3a\u4e86\u51cf\u5c11\u64cd\u4f5c\u7cfb\u7edf\u9a9a\u6270\u786c\u4ef6\u7684\u6b21\u6570\u3002\u7406\u89e3\u5237\u65b0\u7b56\u7565\u548cfork \u590d\u5236\u7528\u6237\u7a7a\u95f4\u7684\u673a\u5236&#xff0c;\u662f\u4f60\u672a\u6765\u6392\u67e5\u8be1\u5f02 I\/O Bug \u7684\u7ec8\u6781\u6b66\u5668\u3002<\/li>\n","protected":false},"excerpt":{"rendered":"<p>\u4ece\u83dc\u9e1f\u9a7f\u7ad9\u5230\u64cd\u4f5c\u7cfb\u7edf&#xff1a;\u4e00\u8282\u8bfe\u5f7b\u5e95\u6253\u901a Linux \u91cd\u5b9a\u5411\u3001VFS \u4e0e\u7f13\u51b2\u533a\u5c11\u5e74\u4eec&#xff0c;\u5982\u679c\u4f60\u66fe\u7591\u60d1\u8fc7&#xff1a;\u4e3a\u4ec0\u4e48 .\/a.out &gt; log.txt \u65f6\u9519\u8bef\u4fe1\u606f\u4f9d\u7136\u8d56\u5728\u5c4f\u5e55\u4e0a&#xff1f;\u4e3a\u4ec0\u4e48 fork() \u4e4b\u540e\u6587\u4ef6\u91cc\u7a81\u7136\u5192\u51fa 7 \u884c\u8f93\u51fa&#xff1f;\u4e3a\u4ec0\u4e48 printf \u660e\u660e\u8fd4\u56de\u4e86&#xff0c;\u6570\u636e\u5374\u53ef\u80fd\u6ca1\u843d\u76d8&#xff1f;\u8fd9\u8282\u8bfe\u7684\u76ee\u6807&#xff0c;\u5c31\u662f\u628a\u8fd9\u4e9b\u9ed1\u76d2\u7838\u788e&#xff0c;\u628a\u4ece C \u8bed\u8a00\u6807\u51c6\u5e93\u5230\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u7684\u6570\u636e\u6d41\u52a8\u8def<\/p>\n","protected":false},"author":2,"featured_media":115404,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[12392,58,190,43,305,44],"topic":[],"class_list":["post-115413","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-12392","tag-linux","tag-190","tag-43","tag-305","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010Linux\u7cfb\u7edf\u3011\u3010\u4ece\u83dc\u9e1f\u9a7f\u7ad9\u5230\u64cd\u4f5c\u7cfb\u7edf\uff1a\u4e00\u8282\u8bfe\u6253\u901aLinux\u91cd\u5b9a\u5411\u4e0e\u7f13\u51b2\u533a\u771f\u76f8\u3011\u6d41\u98df\u822c\u6295\u5582 - \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\/115413.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010Linux\u7cfb\u7edf\u3011\u3010\u4ece\u83dc\u9e1f\u9a7f\u7ad9\u5230\u64cd\u4f5c\u7cfb\u7edf\uff1a\u4e00\u8282\u8bfe\u6253\u901aLinux\u91cd\u5b9a\u5411\u4e0e\u7f13\u51b2\u533a\u771f\u76f8\u3011\u6d41\u98df\u822c\u6295\u5582 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4ece\u83dc\u9e1f\u9a7f\u7ad9\u5230\u64cd\u4f5c\u7cfb\u7edf&#xff1a;\u4e00\u8282\u8bfe\u5f7b\u5e95\u6253\u901a Linux \u91cd\u5b9a\u5411\u3001VFS \u4e0e\u7f13\u51b2\u533a\u5c11\u5e74\u4eec&#xff0c;\u5982\u679c\u4f60\u66fe\u7591\u60d1\u8fc7&#xff1a;\u4e3a\u4ec0\u4e48 .\/a.out &gt; log.txt \u65f6\u9519\u8bef\u4fe1\u606f\u4f9d\u7136\u8d56\u5728\u5c4f\u5e55\u4e0a&#xff1f;\u4e3a\u4ec0\u4e48 fork() \u4e4b\u540e\u6587\u4ef6\u91cc\u7a81\u7136\u5192\u51fa 7 \u884c\u8f93\u51fa&#xff1f;\u4e3a\u4ec0\u4e48 printf \u660e\u660e\u8fd4\u56de\u4e86&#xff0c;\u6570\u636e\u5374\u53ef\u80fd\u6ca1\u843d\u76d8&#xff1f;\u8fd9\u8282\u8bfe\u7684\u76ee\u6807&#xff0c;\u5c31\u662f\u628a\u8fd9\u4e9b\u9ed1\u76d2\u7838\u788e&#xff0c;\u628a\u4ece C \u8bed\u8a00\u6807\u51c6\u5e93\u5230\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u7684\u6570\u636e\u6d41\u52a8\u8def\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/115413.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-11T20:25:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261011202502-6acbf09e7c3eb.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=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/115413.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/115413.html\",\"name\":\"\u3010Linux\u7cfb\u7edf\u3011\u3010\u4ece\u83dc\u9e1f\u9a7f\u7ad9\u5230\u64cd\u4f5c\u7cfb\u7edf\uff1a\u4e00\u8282\u8bfe\u6253\u901aLinux\u91cd\u5b9a\u5411\u4e0e\u7f13\u51b2\u533a\u771f\u76f8\u3011\u6d41\u98df\u822c\u6295\u5582 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-11T20:25:04+00:00\",\"dateModified\":\"2026-10-11T20:25:04+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/115413.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/115413.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/115413.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010Linux\u7cfb\u7edf\u3011\u3010\u4ece\u83dc\u9e1f\u9a7f\u7ad9\u5230\u64cd\u4f5c\u7cfb\u7edf\uff1a\u4e00\u8282\u8bfe\u6253\u901aLinux\u91cd\u5b9a\u5411\u4e0e\u7f13\u51b2\u533a\u771f\u76f8\u3011\u6d41\u98df\u822c\u6295\u5582\"}]},{\"@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":"\u3010Linux\u7cfb\u7edf\u3011\u3010\u4ece\u83dc\u9e1f\u9a7f\u7ad9\u5230\u64cd\u4f5c\u7cfb\u7edf\uff1a\u4e00\u8282\u8bfe\u6253\u901aLinux\u91cd\u5b9a\u5411\u4e0e\u7f13\u51b2\u533a\u771f\u76f8\u3011\u6d41\u98df\u822c\u6295\u5582 - \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\/115413.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010Linux\u7cfb\u7edf\u3011\u3010\u4ece\u83dc\u9e1f\u9a7f\u7ad9\u5230\u64cd\u4f5c\u7cfb\u7edf\uff1a\u4e00\u8282\u8bfe\u6253\u901aLinux\u91cd\u5b9a\u5411\u4e0e\u7f13\u51b2\u533a\u771f\u76f8\u3011\u6d41\u98df\u822c\u6295\u5582 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4ece\u83dc\u9e1f\u9a7f\u7ad9\u5230\u64cd\u4f5c\u7cfb\u7edf&#xff1a;\u4e00\u8282\u8bfe\u5f7b\u5e95\u6253\u901a Linux \u91cd\u5b9a\u5411\u3001VFS \u4e0e\u7f13\u51b2\u533a\u5c11\u5e74\u4eec&#xff0c;\u5982\u679c\u4f60\u66fe\u7591\u60d1\u8fc7&#xff1a;\u4e3a\u4ec0\u4e48 .\/a.out &gt; log.txt \u65f6\u9519\u8bef\u4fe1\u606f\u4f9d\u7136\u8d56\u5728\u5c4f\u5e55\u4e0a&#xff1f;\u4e3a\u4ec0\u4e48 fork() \u4e4b\u540e\u6587\u4ef6\u91cc\u7a81\u7136\u5192\u51fa 7 \u884c\u8f93\u51fa&#xff1f;\u4e3a\u4ec0\u4e48 printf \u660e\u660e\u8fd4\u56de\u4e86&#xff0c;\u6570\u636e\u5374\u53ef\u80fd\u6ca1\u843d\u76d8&#xff1f;\u8fd9\u8282\u8bfe\u7684\u76ee\u6807&#xff0c;\u5c31\u662f\u628a\u8fd9\u4e9b\u9ed1\u76d2\u7838\u788e&#xff0c;\u628a\u4ece C \u8bed\u8a00\u6807\u51c6\u5e93\u5230\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u7684\u6570\u636e\u6d41\u52a8\u8def","og_url":"https:\/\/www.wsisp.com\/helps\/115413.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-11T20:25:04+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261011202502-6acbf09e7c3eb.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/115413.html","url":"https:\/\/www.wsisp.com\/helps\/115413.html","name":"\u3010Linux\u7cfb\u7edf\u3011\u3010\u4ece\u83dc\u9e1f\u9a7f\u7ad9\u5230\u64cd\u4f5c\u7cfb\u7edf\uff1a\u4e00\u8282\u8bfe\u6253\u901aLinux\u91cd\u5b9a\u5411\u4e0e\u7f13\u51b2\u533a\u771f\u76f8\u3011\u6d41\u98df\u822c\u6295\u5582 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-11T20:25:04+00:00","dateModified":"2026-10-11T20:25:04+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/115413.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/115413.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/115413.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010Linux\u7cfb\u7edf\u3011\u3010\u4ece\u83dc\u9e1f\u9a7f\u7ad9\u5230\u64cd\u4f5c\u7cfb\u7edf\uff1a\u4e00\u8282\u8bfe\u6253\u901aLinux\u91cd\u5b9a\u5411\u4e0e\u7f13\u51b2\u533a\u771f\u76f8\u3011\u6d41\u98df\u822c\u6295\u5582"}]},{"@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\/115413","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=115413"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/115413\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/115404"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=115413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=115413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=115413"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=115413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}