{"id":111526,"date":"2026-10-01T11:21:34","date_gmt":"2026-10-01T03:21:34","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/111526.html"},"modified":"2026-10-01T11:21:34","modified_gmt":"2026-10-01T03:21:34","slug":"linux%e7%b3%bb%e7%bb%9f-%e5%9f%ba%e7%a1%80io%ef%bc%88%e4%b8%8b%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/111526.html","title":{"rendered":"Linux\u7cfb\u7edf\u2014\u2014\u57fa\u7840IO\uff08\u4e0b\uff09"},"content":{"rendered":"<p>\u00a0&#x1f481;\u200d\u2642\ufe0f\u4e2a\u4eba\u4e3b\u9875&#xff1a;\u8fdb\u51fb\u7684\u8346\u68d8<\/p>\n<p>&#x1f447;\u4f5c\u8005\u5176\u5b83\u4e13\u680f&#xff1a;<\/p>\n<p>\u300a\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u300b\u300a\u7b97\u6cd5\u300b\u300aC&#043;&#043;\u8d77\u59cb\u4e4b\u8def\u300b\u300aLinux\u300b<\/p>\n<hr \/>\n<h3>\u76ee\u5f55<\/h3>\n<h3>1.\u7406\u89e3\u201c\u6587\u4ef6\u201d<\/h3>\n<h3>2.C\u6587\u4ef6\u63a5\u53e3<\/h3>\n<h3>3.\u7cfb\u7edf\u6587\u4ef6I\/O<\/h3>\n<h3>4.\u7406\u89e3\u201c\u4e00\u5207\u7686\u6587\u4ef6\u201d<\/h3>\n<h3>5.\u7f13\u51b2\u533a<\/h3>\n<hr \/>\n<h2>4.\u7406\u89e3\u201c\u4e00\u5207\u7686\u6587\u4ef6\u201d<\/h2>\n<p>\u9996\u5148&#xff0c;\u5728windows\u4e2d\u662f\u6587\u4ef6\u7684\u4e1c\u897f&#xff0c;\u5728linux\u4e2d\u4e5f\u662f\u6587\u4ef6&#xff1b;\u5176\u6b21\u4e00\u4e9b\u5728windows\u4e2d\u4e0d\u662f\u6587\u4ef6\u7684\u4e1c\u897f&#xff0c;\u5982&#xff1a;\u8fdb\u7a0b\u3001\u78c1\u76d8\u3001\u663e\u793a\u5668\u3001\u952e\u76d8\u8fd9\u6837\u7684\u786c\u4ef6\u8bbe\u5907\u4e5f\u88ab\u62bd\u8c61\u6210\u4e86\u6587\u4ef6&#xff0c;\u53ef\u4ee5\u4f7f\u7528\u8bbf\u95ee\u6587\u4ef6\u7684\u65b9\u6cd5\u8bbf\u95ee\u5b83\u4eec\u83b7\u5f97\u4fe1\u606f&#xff1b;\u751a\u81f3\u7ba1\u9053\u4e5f\u662f\u6587\u4ef6&#xff1b;\u5c06\u6765\u7f51\u7edc\u7f16\u7a0b\u4e2d\u7684socket&#xff08;\u5957\u63a5\u5b57&#xff09;\u8fd9\u6837\u7684\u4e1c\u897f&#xff0c;\u4f7f\u7528\u7684\u63a5\u53e3\u8ddf\u6587\u4ef6\u63a5\u53e3\u4e5f\u662f\u4e00\u81f4\u7684\u3002<\/p>\n<p>\u8fd9\u6837\u505a\u6700\u660e\u663e\u7684\u597d\u5904\u662f&#xff0c;\u5f00\u53d1\u8005\u4ec5\u9700\u4f7f\u7528\u4e00\u5957API\u548c\u5f00\u53d1\u5de5\u5177&#xff0c;\u5373\u53ef\u8c03\u53d6Linux\u7cfb\u7edf\u4e2d\u7edd\u5927\u90e8\u5206\u7684\u8d44\u6e90\u3002\u4f8b&#xff1a;Linux\u4e2d\u51e0\u4e4e\u6240\u6709\u8bfb&#xff08;\u8bfb\u6587\u4ef6&#xff0c;\u8bfb\u7cfb\u7edf\u72b6\u6001&#xff0c;\u8bfbPIPE&#xff09;\u7684\u64cd\u4f5c\u90fd\u53ef\u4ee5\u7528read\u51fd\u6570\u8fdb\u884c&#xff1b;\u51e0\u4e4e\u6240\u6709&#xff08;\u66f4\u6539\u6587\u4ef6&#xff0c;\u66f4\u6539\u7cfb\u7edf\u53c2\u6570&#xff0c;\u5199PIPE&#xff09;\u7684\u64cd\u4f5c\u90fd\u53ef\u4ee5\u7528write\u51fd\u6570\u6765\u8fdb\u884c\u3002<\/p>\n<p>\u5f53\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u662f&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u4e3a\u4e86\u7ba1\u7406\u6240\u6253\u5f00\u7684\u6587\u4ef6&#xff0c;\u90fd\u4f1a\u4e3a\u8fd9\u4e2a\u6587\u521b\u5efa\u4e00\u4e2afile\u7ed3\u6784\u4f53&#xff0c;\u8be5\u7ed3\u6784\u4f53\u5b9a\u4e49\u5728\u5728\/usr\/src\/kernels\/3.10.0-1160.71.1.el7.x86_64\/include\/linux\/fs.h\u4e0b&#xff0c;\u4ee5\u4e0b\u5c55\u793a\u4e86\u6211\u4eec\u6240\u5173\u5fc3\u7684\u5185\u5bb9&#xff1a;<\/p>\n<p>struct file {<br \/>\n    &#8230;<br \/>\n    struct inode *f_inode; \/* cached value *\/<br \/>\n    const struct file_operations *f_op;<br \/>\n    &#8230;<br \/>\n    atomic_long_t f_count; \/\/ \u8868\u2f70\u6253\u5f00\u2f42\u4ef6\u7684\u5f15\u2f64\u8ba1\u6570&#xff0c;\u5982\u679c\u6709\u591a\u4e2a\u2f42\u4ef6\u6307\u9488\u6307<br \/>\n\u5411\u5b83&#xff0c;\u5c31\u4f1a\u589e\u52a0f_count\u7684\u503c\u3002<br \/>\n    unsigned int f_flags; \/\/ \u8868\u2f70\u6253\u5f00\u2f42\u4ef6\u7684\u6743\u9650<br \/>\n    fmode_t f_mode; \/\/ \u8bbe\u7f6e\u5bf9\u2f42\u4ef6\u7684\u8bbf\u95ee\u6a21\u5f0f,\u4f8b\u5982&#xff1a;\u53ea\u8bfb&#xff0c;\u53ea\u5199\u7b49\u3002\u6240<br \/>\n\u6709\u7684\u6807\u5fd7\u5728\u5934\u2f42\u4ef6&lt;fcntl.h&gt; \u4e2d\u5b9a\u4e49<br \/>\n    loff_t f_pos; \/\/ \u8868\u2f70\u5f53\u524d\u8bfb\u5199\u2f42\u4ef6\u7684\u4f4d\u7f6e<br \/>\n    &#8230;<br \/>\n} __attribute__((aligned(4))); \/* lest something weird decides that 2 is OK<br \/>\n*\/ <\/p>\n<p>struct file\u4e2d\u7684f_op\u6307\u9488\u6307\u5411\u4e86\u4e00\u4e2afile_operations\u7ed3\u6784\u4f53&#xff0c;\u8fd9\u4e2a\u7ed3\u6784\u4f53\u4e2d\u7684\u6210\u5458\u9664\u4e86struct module* owner\u5176\u4f59\u90fd\u662f\u51fd\u6570\u6307\u9488\u3002\u8be5\u7ed3\u6784\u548cstruct file\u90fd\u5728fs.h\u4e0b<\/p>\n<p>truct file_operations {<br \/>\n    struct module *owner;<br \/>\n    \/\/\u6307\u5411\u62e5\u6709\u8be5\u6a21\u5757\u7684\u6307\u9488&#xff1b;<br \/>\n    loff_t (*llseek) (struct file *, loff_t, int);<br \/>\n    \/\/llseek \u2f45\u6cd5\u2f64\u4f5c\u6539\u53d8\u2f42\u4ef6\u4e2d\u7684\u5f53\u524d\u8bfb\/\u5199\u4f4d\u7f6e, \u5e76\u4e14\u65b0\u4f4d\u7f6e\u4f5c\u4e3a(\u6b63\u7684)\u8fd4\u56de\u503c.<br \/>\n    ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);<br \/>\n    \/\/\u2f64\u6765\u4ece\u8bbe\u5907\u4e2d\u83b7\u53d6\u6570\u636e. \u5728\u8fd9\u4e2a\u4f4d\u7f6e\u7684\u2f00\u4e2a\u7a7a\u6307\u9488\u5bfc\u81f4 read \u7cfb\u7edf\u8c03\u2f64\u4ee5 &#8211;<br \/>\nEINVAL(&#034;Invalid argument&#034;) \u5931\u8d25. \u2f00\u4e2a\u2fae\u8d1f\u8fd4\u56de\u503c\u4ee3\u8868\u4e86\u6210\u529f\u8bfb\u53d6\u7684\u5b57\u8282\u6570( \u8fd4\u56de\u503c\u662f\u2f00\u4e2a<br \/>\n&#034;signed size&#034; \u7c7b\u578b, \u5e38\u5e38\u662f\u2f6c\u6807\u5e73\u53f0\u672c\u5730\u7684\u6574\u6570\u7c7b\u578b).<br \/>\n    ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);<br \/>\n    \/\/\u53d1\u9001\u6570\u636e\u7ed9\u8bbe\u5907. \u5982\u679c NULL, -EINVAL \u8fd4\u56de\u7ed9\u8c03\u2f64 write \u7cfb\u7edf\u8c03\u2f64\u7684\u7a0b\u5e8f. \u5982\u679c\u2fae\u8d1f,<br \/>\n\u8fd4\u56de\u503c\u4ee3\u8868\u6210\u529f\u5199\u7684\u5b57\u8282\u6570.<br \/>\n    ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long,<br \/>\nloff_t);<br \/>\n    \/\/\u521d\u59cb\u5316\u2f00\u4e2a\u5f02\u6b65\u8bfb &#8212; \u53ef\u80fd\u5728\u51fd\u6570\u8fd4\u56de\u524d\u4e0d\u7ed3\u675f\u7684\u8bfb\u64cd\u4f5c.<br \/>\n    ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned<br \/>\nlong, loff_t);<br \/>\n    \/\/\u521d\u59cb\u5316\u8bbe\u5907\u4e0a\u7684\u2f00\u4e2a\u5f02\u6b65\u5199.<br \/>\n    int (*readdir) (struct file *, void *, filldir_t);<br \/>\n    \/\/\u5bf9\u4e8e\u8bbe\u5907\u2f42\u4ef6\u8fd9\u4e2a\u6210\u5458\u5e94\u5f53\u4e3a NULL; \u5b83\u2f64\u6765\u8bfb\u53d6\u2f6c\u5f55, \u5e76\u4e14\u4ec5\u5bf9**\u2f42\u4ef6\u7cfb\u7edf**\u6709\u2f64.<br \/>\n    unsigned int (*poll) (struct file *, struct poll_table_struct *);<br \/>\n    int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);<br \/>\n    long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);<br \/>\n    long (*compat_ioctl) (struct file *, unsigned int, unsigned long);<br \/>\n    int (*mmap) (struct file *, struct vm_area_struct *);<br \/>\n    \/\/mmap \u2f64\u6765\u8bf7\u6c42\u5c06\u8bbe\u5907\u5185\u5b58\u6620\u5c04\u5230\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4. \u5982\u679c\u8fd9\u4e2a\u2f45\u6cd5\u662f NULL, mmap \u7cfb\u7edf\u8c03\u2f64<br \/>\n\u8fd4\u56de -ENODEV.<br \/>\n    int (*open) (struct inode *, struct file *);<br \/>\n    \/\/\u6253\u5f00\u2f00\u4e2a\u2f42\u4ef6<br \/>\n    int (*flush) (struct file *, fl_owner_t id);<br \/>\n    \/\/flush \u64cd\u4f5c\u5728\u8fdb\u7a0b\u5173\u95ed\u5b83\u7684\u8bbe\u5907\u2f42\u4ef6\u63cf\u8ff0\u7b26\u7684\u62f7\u2ec9\u65f6\u8c03\u2f64;<br \/>\n    int (*release) (struct inode *, struct file *);<br \/>\n    \/\/\u5728\u2f42\u4ef6\u7ed3\u6784\u88ab\u91ca\u653e\u65f6\u5f15\u2f64\u8fd9\u4e2a\u64cd\u4f5c. \u5982\u540c open, release \u53ef\u4ee5\u4e3a NULL.<br \/>\n    int (*fsync) (struct file *, struct dentry *, int datasync);<br \/>\n    \/\/\u2f64\u2f3e\u8c03\u2f64\u6765\u5237\u65b0\u4efb\u4f55\u6302\u7740\u7684\u6570\u636e.<br \/>\n    int (*aio_fsync) (struct kiocb *, int datasync);<br \/>\n    int (*fasync) (int, struct file *, int);<br \/>\n    int (*lock) (struct file *, int, struct file_lock *);<br \/>\n    \/\/lock \u2f45\u6cd5\u2f64\u6765\u5b9e\u73b0\u2f42\u4ef6\u52a0\u9501; \u52a0\u9501\u5bf9\u5e38\u89c4\u2f42\u4ef6\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u7279\u6027, \u4f46\u662f\u8bbe\u5907\u9a71\u52a8\u2f0f\u4e4e\u4ece\u4e0d\u5b9e<br \/>\n\u73b0\u5b83.<br \/>\n    ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *,<br \/>\nint);<br \/>\n    unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned<br \/>\n    long, unsigned long, unsigned long);<br \/>\n    int (*check_flags)(int);<br \/>\n    int (*flock) (struct file *, int, struct file_lock *);<br \/>\n    ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t<br \/>\n*, size_t, unsigned int);<br \/>\n    ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *,<br \/>\nsize_t, unsigned int);<br \/>\n    int (*setlease)(struct file *, long, struct file_lock **);<br \/>\n}; <\/p>\n<p>file_operation\u5c31\u662f\u628a\u7cfb\u7edf\u8c03\u7528\u548c\u9a71\u52a8\u7a0b\u5e8f\u5173\u8054\u8d77\u6765\u7684\u5173\u952e\u6570\u636e\u7ed3\u6784&#xff0c;\u8fd9\u4e2a\u7ed3\u6784\u7684\u6bcf\u4e00\u4e2a\u6210\u5458\u90fd\u5bf9\u5e94\u7740\u4e00\u4e2a\u7cfb\u7edf\u8c03\u7528\u3002\u8bfb\u53d6file_operation\u4e2d\u76f8\u5e94\u7684\u51fd\u6570\u6307\u9488&#xff0c;\u63a5\u7740\u628a\u63a7\u5236\u6743\u8f6c\u4ea4\u7ed9\u51fd\u6570&#xff0c;\u4ece\u800c\u5b8c\u6210Linux\u8bbe\u5907\u9a71\u52a8\u7a0b\u5e8f\u7684\u5de5\u4f5c\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"415\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261001032133-6abdd1bd0dc26.png\" width=\"1048\" \/><\/p>\n<p>\u4e0a\u56fe\u4e2d\u7684\u5916\u8bbe&#xff0c;\u6bcf\u4e2a\u8bbe\u5907\u90fd\u53ef\u4ee5\u6709\u81ea\u5df1\u7684read\u3001write&#xff0c;\u4f46\u4e00\u5b9a\u662f\u5bf9\u5e94\u7740\u4e0d\u540c\u7684\u64cd\u4f5c\u65b9\u6cd5\u3002\u4f46\u901a\u8fc7struct file\u4e0bfile_operation\u4e2d\u7684\u5404\u79cd\u51fd\u6570\u56de\u8c03&#xff0c;\u8ba9\u5f00\u53d1\u8005\u53ea\u7528file\u4fbf\u53ef\u8c03\u53d6Linux\u7cfb\u7edf\u4e2d\u7edd\u5927\u90e8\u5206\u7684\u8d44\u6e90\u3002<\/p>\n<h2>5.\u7f13\u51b2\u533a<\/h2>\n<h3>5.1\u4ec0\u4e48\u662f\u7f13\u51b2\u533a<\/h3>\n<p>\u7f13\u51b2\u533a\u662f\u5185\u5b58\u7a7a\u95f4\u7684\u4e00\u90e8\u5206\u3002\u5373\u5728\u5185\u5b58\u7a7a\u95f4\u9884\u7559\u4e86\u4e00\u5b9a\u7684\u5b58\u50a8\u7a7a\u95f4&#xff0c;\u8fd9\u4e9b\u5b58\u50a8\u7a7a\u95f4\u7528\u6765\u7f13\u51b2\u8f93\u5165\u6216\u8f93\u51fa\u7684\u6570\u636e&#xff0c;\u8fd9\u90e8\u5206\u9884\u7559\u7684\u7a7a\u95f4\u5c31\u53eb\u505a\u7f13\u51b2\u533a\u3002\u7f13\u51b2\u533a\u6839\u636e\u5176\u5bf9\u5e94\u7684\u662f\u8f93\u5165\u8bbe\u5907\u8fd8\u662f\u8f93\u51fa\u8bbe\u5907&#xff0c;\u5206\u4e3a\u8f93\u5165\u7f13\u51b2\u533a\u548c\u8f93\u51fa\u7f13\u51b2\u533a\u3002<\/p>\n<h3>5.2\u4e3a\u4ec0\u4e48\u8981\u5f15\u5165\u7f13\u51b2\u533a\u673a\u5236<\/h3>\n<p>\u8bfb\u5199\u6587\u4ef6\u65f6&#xff0c;\u82e5\u4e0d\u4f1a\u5f00\u8f9f\u5bf9\u6587\u4ef6\u64cd\u4f5c\u7684\u7f13\u51b2\u533a&#xff0c;\u76f4\u63a5\u901a\u8fc7\u7cfb\u7edf\u8c03\u7528\u5bf9\u78c1\u76d8\u8fdb\u884c\u64cd\u4f5c&#xff08;\u8bfb\u3001\u5199\u7b49&#xff09;&#xff0c;\u90a3\u4e48\u6bcf\u6b21\u5bf9\u6587\u4ef6\u8fdb\u884c\u4e00\u6b21\u8bfb\u5199\u64cd\u4f5c\u65f6&#xff0c;\u90fd\u9700\u8981\u4f7f\u7528\u8bfb\u5199\u7cfb\u7edf\u8c03\u7528\u6765\u5904\u7406\u6b64\u64cd\u4f5c&#xff0c;\u5373\u9700\u8981\u6267\u884c\u4e00\u6b21\u7cfb\u7edf\u8c03\u7528&#xff0c;\u6267\u884c\u4e00\u6b21\u7cfb\u7edf\u8c03\u7528\u5c06\u6d89\u53ca\u5230CPU\u72b6\u6001\u7684\u5207\u6362&#xff0c;\u5373\u4ece\u7528\u6237\u7a7a\u95f4\u5207\u6362\u5230\u5185\u6838\u7a7a\u95f4&#xff0c;\u5b9e\u73b0\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u7684\u5207\u6362&#xff0c;\u8fd9\u5c06\u635f\u8017\u4e00\u5b9a\u7684CPU\u65f6\u95f4&#xff0c;\u9891\u7e41\u7684\u78c1\u76d8\u8bbf\u95ee\u5bf9\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u9020\u6210\u5f88\u5927\u7684\u5f71\u54cd\u3002<\/p>\n<p>\u4e3a\u4e86\u51cf\u5c11\u4f7f\u7528\u7cfb\u7edf\u8c03\u7528\u7684\u6b21\u6570&#xff0c;\u63d0\u9ad8\u6548\u7387&#xff0c;\u6211\u4eec\u5c31\u53ef\u4ee5\u91c7\u7528\u7f13\u5b58\u673a\u5236\u3002\u6bd4\u5982&#xff1a;\u4ece\u78c1\u76d8\u4e2d\u53d6\u4fe1\u606f&#xff0c;\u53ef\u4ee5\u5728\u78c1\u76d8\u6587\u4ef6\u8fdb\u884c\u64cd\u4f5c\u65f6&#xff0c;\u53ef\u4ee5\u4e00\u6b21\u4ece\u6587\u4ef6\u4e2d\u8bfb\u51fa\u5927\u91cf\u7684\u6570\u636e\u5230\u7f13\u51b2\u533a\u4e2d&#xff0c;\u4ee5\u540e\u5bf9\u8fd9\u90e8\u5206\u7684\u8bbf\u95ee\u5c31\u4e0d\u9700\u8981\u518d\u4f7f\u7528\u7cfb\u7edf\u8c03\u7528\u4e86&#xff0c;\u7b49\u7f13\u51b2\u533a\u7684\u6570\u636e\u53d6\u5b8c\u540e\u518d\u53bb\u78c1\u76d8\u4e2d\u8bfb\u53d6&#xff0c;\u8fd9\u6837\u5c31\u53ef\u4ee5\u51cf\u5c11\u78c1\u76d8\u7684\u8bfb\u5199\u6b21\u6570&#xff0c;\u518d\u52a0\u4e0a\u8ba1\u7b97\u673a\u5bf9\u7f13\u51b2\u533a\u7684\u64cd\u4f5c\u5927\u5927\u5feb\u4e8e\u5bf9\u78c1\u76d8\u7684\u64cd\u4f5c&#xff0c;\u6545\u5e94\u7528\u7f13\u51b2\u533a\u53ef\u5927\u5927\u63d0\u9ad8\u8ba1\u7b97\u673a\u7684\u8fd0\u884c\u901f\u5ea6\u3002<\/p>\n<p>\u53c8\u5982&#xff1a;\u6211\u4eec\u4f7f\u7528\u6253\u5370\u673a\u6253\u5370\u6587\u6863&#xff0c;\u7531\u4e8e\u6253\u5370\u673a\u7684\u6253\u5370\u901f\u5ea6\u76f8\u5bf9\u8f83\u6162&#xff0c;\u53ef\u4ee5\u5148\u628a\u6587\u6863\u8f93\u51fa\u5230\u6253\u5370\u673a\u76f8\u5e94\u7684\u7f13\u51b2\u533a&#xff0c;\u6253\u5370\u673a\u518d\u81ea\u884c\u9010\u6b65\u6253\u5370&#xff0c;\u8fd9\u65f6CPU\u53ef\u4ee5\u5904\u7406\u5176\u5b83\u4e8b\u60c5\u3002\u53ef\u4ee5\u770b\u51fa&#xff0c;\u7f13\u51b2\u533a\u5c31\u662f\u4e00\u5757\u5185\u5b58\u533a&#xff0c;\u5b83\u7528\u518d\u8f93\u5165\u8f93\u51fa\u8bbe\u5907\u548cCPU\u4e4b\u95f4&#xff0c;\u7528\u6765\u7f13\u5b58\u6570\u636e\u3002\u5b83\u4f7f\u5f97\u4f4e\u901f\u7684\u8f93\u5165\u8f93\u51fa\u8bbe\u5907\u548c\u9ad8\u901f\u7684CPU\u80fd\u591f\u534f\u8c03\u5de5\u4f5c&#xff0c;\u907f\u514d\u4f4e\u901f\u7684\u8f93\u5165\u8f93\u51fa\u8bbe\u5907\u5360\u7528CPU&#xff0c;\u89e3\u653e\u51faCPU&#xff0c;\u4f7f\u5176\u80fd\u9ad8\u6548\u5de5\u4f5c\u3002<\/p>\n<h3>5.3\u7f13\u51b2\u7c7b\u578b<\/h3>\n<p>\u6807\u51c6I\/O\u63d0\u4f9b\u4e863\u79cd\u7c7b\u578b\u7684\u7f13\u51b2\u533a<\/p>\n<p>\u25cf\u5168\u7f13\u51b2\u533a&#xff1a;\u8fd9\u79cd\u7f13\u5b58\u65b9\u5f0f\u8981\u6c42\u586b\u6ee1\u6574\u4e2a\u7f13\u51b2\u533a\u540e\u624d\u8fdb\u884cI\/O\u7cfb\u7edf\u8c03\u7528\u64cd\u4f5c\u3002\u5bf9\u4e8e\u78c1\u76d8\u6587\u4ef6\u7684\u64cd\u4f5c\u901a\u5e38\u4f7f\u7528\u5168\u7f13\u51b2\u7684\u65b9\u5f0f\u8bbf\u95ee\u3002<\/p>\n<p>\u25cf\u884c\u7f13\u51b2\u533a&#xff1a;\u5728\u884c\u7f13\u51b2\u60c5\u51b5\u4e0b&#xff0c;\u5f53\u8f93\u5165\u548c\u8f93\u51fa\u79cd\u9047\u5230\u6362\u884c\u7b26\u65f6&#xff0c;\u6807\u51c6I\/O\u5e93\u51fd\u6570\u5c06\u4f1a\u6267\u884c\u7cfb\u7edf\u8c03\u7528\u64cd\u4f5c\u3002\u5f53\u6240\u64cd\u4f5c\u7684\u6d41\u6d89\u53ca\u4e00\u4e2a\u7ec8\u7aef\u65f6&#xff08;\u5982\u6807\u51c6\u8f93\u5165\u548c\u6807\u51c6\u8f93\u51fa&#xff09;&#xff0c;\u4f7f\u7528\u884c\u7f13\u51b2\u65b9\u5f0f\u3002\u56e0\u4e3a\u6807\u51c6I\/O\u5e93\u6bcf\u884c\u7684\u7f13\u51b2\u533a\u957f\u5ea6\u662f\u56fa\u5b9a\u7684&#xff0c;\u6240\u4ee5\u53ea\u8981\u586b\u6ee1\u4e86\u7f13\u51b2\u533a&#xff0c;\u5373\u4f7f\u8fd8\u6ca1\u6709\u9047\u5230\u6362\u884c\u7b26&#xff0c;\u4e5f\u4f1a\u6267\u884cI\/O\u7cfb\u7edf\u8c03\u7528\u64cd\u4f5c&#xff0c;\u9ed8\u8ba4\u884c\u7f13\u51b2\u533a\u7684\u5927\u5c0f\u662f1024\u3002<\/p>\n<p>\u25cf\u65e0\u7f13\u51b2\u533a&#xff1a;\u65e0\u7f13\u51b2\u533a\u662f\u6307\u6807\u51c6I\/O\u5e93\u4e0d\u5bf9\u5b57\u7b26\u8fdb\u884c\u7f13\u5b58&#xff0c;\u76f4\u63a5\u8c03\u7528\u7cfb\u7edf\u8c03\u7528\u3002\u6807\u51c6\u51fa\u9519\u6d41stderr\u901a\u5e38\u662f\u4e0d\u5e26\u7f13\u51b2\u533a\u7684&#xff0c;\u8fd9\u4f7f\u5f97\u51fa\u9519\u4fe1\u606f\u80fd\u591f\u5c3d\u5feb\u5730\u663e\u793a\u51fa\u6765\u3002<\/p>\n<p>\u9664\u4e86\u4e0a\u8ff0\u5217\u4e3e\u7684\u9ed8\u8ba4\u5237\u65b0\u65b9\u5f0f&#xff0c;\u4e0b\u5217\u7279\u6b8a\u60c5\u51b5\u4e5f\u4f1a\u5f15\u53d1\u7f13\u51b2\u533a\u7684\u5237\u65b0&#xff1a;<\/p>\n<p>1.\u7f13\u51b2\u533a\u6ee1\u65f6&#xff1b;<\/p>\n<p>2.\u6267\u884cflsuh\u8bed\u53e5&#xff1b;<\/p>\n<p>3.\u8fdb\u7a0b\u7ed3\u675f<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.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(1);<br \/>\n    int fd &#061; open(&#034;log.txt&#034;, O_WRONLY | O_CREAT | O_TRUNC, 0666);<br \/>\n    if (fd &lt; 0) {<br \/>\n        perror(&#034;open&#034;);<br \/>\n        return 0;<br \/>\n    }<br \/>\n    printf(&#034;hello world: %d\\\\n&#034;, fd);<br \/>\n    close(fd);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u7684\u672c\u610f\u662f\u5e94\u7528\u91cd\u5b9a\u5411\u601d\u7ef4&#xff0c;\u8ba9\u672c\u5e94\u6253\u5370\u5230\u663e\u793a\u5668\u4e0a\u7684\u5185\u5bb9\u5199\u5230\u201clog.txt\u201d\u6587\u4ef6\u4e2d&#xff0c;\u4f46\u5b9e\u9645\u4e0a&#xff0c;\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u675f\u540e&#xff0c;\u6587\u4ef6\u4e2d\u5e76\u6ca1\u6709\u5199\u5165\u5185\u5bb9&#xff1a;<\/p>\n<p>[sjx&#064;VM-8-12-centos buffer]$ .\/myfile<br \/>\n[sjx&#064;VM-8-12-centos buffer]$ ls<br \/>\nlog.txt makefile myfile myfile.c<br \/>\n[sjx&#064;VM-8-12-centos buffer]$ cat log.txt<br \/>\n[sjx&#064;VM-8-12-centos buffer]$<\/p>\n<p>\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u5c061\u53f7\u63cf\u8ff0\u7b26\u91cd\u5b9a\u5411\u5230\u78c1\u76d8\u6587\u4ef6\u540e&#xff0c;\u7f13\u51b2\u533a\u7684\u5237\u65b0\u65b9\u5f0f\u6210\u4e3a\u4e86\u5168\u7f13\u51b2\u3002\u800c\u5199\u5165\u7684\u5185\u5bb9\u5e76\u6ca1\u6709\u586b\u6ee1\u6574\u4e2a\u7f13\u51b2\u533a&#xff0c;\u5bfc\u81f4\u5e76\u4e0d\u4f1a\u5c06\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u5237\u65b0\u5230\u78c1\u76d8\u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528fflush\u5f3a\u5236\u5237\u65b0\u7f13\u51b2\u533a\u3002<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.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(1);<br \/>\n    int fd &#061; open(&#034;log.txt&#034;, O_WRONLY | O_CREAT | O_TRUNC, 0666);<br \/>\n    if (fd &lt; 0) {<br \/>\n        perror(&#034;open&#034;);<br \/>\n        return 0;<br \/>\n    }<br \/>\n    printf(&#034;hello world: %d\\\\n&#034;, fd);<br \/>\n    fflush(stdout);<br \/>\n    close(fd);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u8fd8\u6709\u4e00\u79cd\u89e3\u51b3\u65b9\u6cd5&#xff0c;\u5e76\u4e14\u53ef\u4ee5\u9a8c\u8bc1stderr\u662f\u4e0d\u5e26\u7f13\u51b2\u533a\u7684<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.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(2);<br \/>\n    int fd &#061; open(&#034;log.txt&#034;, O_WRONLY | O_CREAT | O_TRUNC, 0666);<br \/>\n    if (fd &lt; 0) {<br \/>\n        perror(&#034;open&#034;);<br \/>\n        return 0;<br \/>\n    }<br \/>\n    perror(&#034;hello world&#034;);<br \/>\n    close(fd);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5c062\u53f7\u6587\u4ef6\u63cf\u8ff0\u7b26\u91cd\u5b9a\u5411\u81f3\u6587\u4ef6&#xff0c;\u7531\u4e8estderr\u6ca1\u6709\u7f13\u51b2\u533a&#xff0c;\u201chello world\u201d\u4e0d\u7528fflush\u5c31\u53ef\u4ee5\u5199\u5165\u6587\u4ef6&#xff1a;<\/p>\n<p>[sjx&#064;VM-8-12-centos buffer]$ .\/myfile<br \/>\n[sjx&#064;VM-8-12-centos buffer]$ cat log.txt<br \/>\nhello world: Success<\/p>\n<h3>5.4FILE<\/h3>\n<p>\u25cf\u56e0\u4e3aIO\u76f8\u5173\u51fd\u6570\u4e0e\u7cfb\u7edf\u8c03\u7528\u63a5\u53e3\u5bf9\u5e94&#xff0c;\u5e76\u4e14\u5e93\u51fd\u6570\u5c01\u88c5\u7cfb\u7edf\u8c03\u7528&#xff0c;\u6240\u4ee5\u672c\u8d28\u4e0a&#xff0c;\u8bbf\u95ee\u6587\u4ef6\u90fd\u662f\u901a\u8fc7fd\u8bbf\u95ee\u7684<\/p>\n<p>\u25cf\u6240\u4ee5C\u5e93\u4e2dFILE\u7ed3\u6784\u4f53\u5185\u90e8&#xff0c;\u5fc5\u5b9a\u5c01\u88c5\u4e86fd<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>int main()<br \/>\n{<br \/>\n    const char *msg0&#061;&#034;hello printf\\\\n&#034;;<br \/>\n    const char *msg1&#061;&#034;hello fwrite\\\\n&#034;;<br \/>\n    const char *msg2&#061;&#034;hello write\\\\n&#034;;<br \/>\n    printf(&#034;%s&#034;, msg0);<br \/>\n    fwrite(msg1, strlen(msg0), 1, stdout);<br \/>\n    write(1, msg2, strlen(msg2));<br \/>\n    fork();<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u7ed3\u679c&#xff1a;<\/p>\n<p>hello printf<\/p>\n<p>hello fwrite<\/p>\n<p>hello write<\/p>\n<p>\u4f46\u82e5\u5bf9\u8fdb\u7a0b\u5b9e\u73b0\u8f93\u51fa\u91cd\u5b9a\u5411&#xff0c;.\/hello &gt; file&#xff0c;\u7ed3\u679c\u5c31\u53d8\u6210\u4e86&#xff1a;<\/p>\n<p>hello write<\/p>\n<p>hello printf<\/p>\n<p>hello fwrite<\/p>\n<p>hello printf<\/p>\n<p>hello fwrite<\/p>\n<p>\u5176\u4e2dprintf\u548cfwrite&#xff08;\u5e93\u51fd\u6570&#xff09;\u90fd\u8f93\u51fa\u4e862\u6b21&#xff0c;\u800cwrite\u53ea\u8f93\u51fa\u4e86\u4e00\u6b21&#xff08;\u7cfb\u7edf\u8c03\u7528&#xff09;\u3002\u4e3a\u4ec0\u4e48fork\u4f1a\u5bfc\u81f4\u8fd9\u79cd\u7ed3\u679c&#xff1f;<\/p>\n<p>\u25cf\u4e00\u822cC\u5e93\u51fd\u6570\u5199\u5165\u6587\u4ef6\u65f6\u662f\u5168\u7f13\u51b2\u7684&#xff0c;\u800c\u5199\u5165\u663e\u793a\u5668\u662f\u884c\u7f13\u51b2<\/p>\n<p>\u25cfprintf\u3001fwrite\u5e93\u51fd\u6570\u4f1a\u81ea\u5e26\u7f13\u51b2\u533a&#xff0c;\u5f53\u53d1\u751f\u91cd\u5b9a\u5411\u5230\u666e\u901a\u6587\u4ef6\u65f6&#xff0c;\u6570\u636e\u7684\u7f13\u51b2\u65b9\u5f0f\u7531\u884c\u7f13\u51b2\u53d8\u6210\u4e86\u5168\u7f13\u51b2<\/p>\n<p>\u25cf\u800c\u6211\u4eec\u653e\u51fa\u7f13\u51b2\u533a\u4e2d\u7684\u6570\u636e&#xff0c;\u5c31\u4e0d\u4f1a\u88ab\u7acb\u5373\u5237\u65b0&#xff0c;\u8bbe\u7f6efork\u4e4b\u540e<\/p>\n<p>\u25cf\u4f46\u662f\u8fdb\u7a0b\u9000\u51fa\u540e&#xff0c;\u4f1a\u7edf\u4e00\u5c5e\u6027&#xff0c;\u5199\u5165\u6587\u4ef6\u5f53\u4e2d<\/p>\n<p>\u25cf\u4f46\u662ffork\u7684\u65f6\u5019&#xff0c;\u7236\u5b50\u6570\u636e\u4f1a\u53d1\u751f\u5199\u65f6\u62f7\u8d1d&#xff0c;\u5f53\u7236\u8fdb\u7a0b\u51c6\u5907\u5237\u65b0\u7684\u65f6\u5019&#xff0c;\u5b50\u8fdb\u7a0b\u4e5f\u5c31\u6709\u4e86\u76f8\u540c\u7684\u4e00\u4efd\u6570\u636e&#xff0c;\u6240\u4ee5\u4ea7\u751f\u4e24\u4efd\u6570\u636e<\/p>\n<p>\u25cf\u4f46write\u53ea\u8f93\u51fa\u4e00\u6b21&#xff0c;\u662f\u56e0\u4e3a&#xff0c;write\u662f\u7cfb\u7edf\u8c03\u7528&#xff0c;\u76f4\u63a5\u5199\u5230\u5185\u6838\u7f13\u51b2\u533a<\/p>\n<p>\u7efc\u4e0a&#xff1a;printf&#xff0c;fwrite\u5e93\u51fd\u6570\u4f1a\u81ea\u5e26\u7f13\u51b2\u533a&#xff0c;\u800cwrite\u7cfb\u7edf\u8c03\u7528\u6ca1\u6709\u5e26\u7f13\u51b2\u533a\u3002\u53e6\u5916&#xff0c;\u4e0a\u6587\u63d0\u5230\u7684\u7f13\u51b2\u533a&#xff0c;\u90fd\u662f\u7528\u6237\u7ea7\u7f13\u51b2\u533a\u3002\u5b9e\u9645\u4e0a\u4e3a\u4e86\u63d0\u5347\u6574\u673a\u6027\u80fd&#xff0c;OS\u4e5f\u4f1a\u63d0\u4f9b\u76f8\u5173\u5185\u6838\u7ea7\u7f13\u51b2\u533a\u3002<\/p>\n<p>\u90a3\u8fd9\u4e2a\u7f13\u51b2\u533a\u7531\u8c01\u63d0\u4f9b&#xff1f;printf&#xff0c;fwrite\u662f\u5e93\u51fd\u6570&#xff0c;write\u662f\u7cfb\u7edf\u8c03\u7528&#xff0c;\u5e93\u51fd\u6570\u5728\u7cfb\u7edf\u8c03\u7528\u7684\u201c\u4e0a\u5c42\u201d&#xff0c;\u662f\u5bf9\u7cfb\u7edf\u8c03\u7528\u7684\u201c\u5c01\u88c5\u201d&#xff0c;\u4f46\u662fwrite\u6ca1\u6709\u63d0\u4f9b\u7f13\u51b2\u533a&#xff08;\u7528\u6237\u7ea7&#xff09;&#xff0c;\u800cprintf&#xff0c;fwrite\u6709&#xff0c;\u5c31\u53ef\u4ee5\u8bf4\u660e&#xff0c;\u8be5\u7f13\u51b2\u533a\u662f\u4e8c\u6b21\u52a0\u4e0a\u7684&#xff0c;\u56e0\u4e3a\u662fC\u5e93\u51fd\u6570&#xff0c;\u6240\u4ee5\u7531C\u6807\u51c6\u5e93\u63d0\u4f9b\u3002<\/p>\n<p>C\u5e93\u4e2dFILE\u7ed3\u6784\u4f53&#xff1a; \u00a0<\/p>\n<p>\u5728\/usr\/include\/libio.h<br \/>\nstruct _IO_FILE {<br \/>\n    int _flags; \/* High-order word is _IO_MAGIC; rest is flags.<br \/>\n*\/<br \/>\n#define _IO_file_flags _flags<br \/>\n    \/\/\u7f13\u51b2\u533a\u76f8\u5173<br \/>\n    \/* The following pointers correspond to the C&#043;&#043; streambuf protocol. *\/<br \/>\n    \/* Note: Tk uses the _IO_read_ptr and _IO_read_end fields directly. *\/<br \/>\n    char* _IO_read_ptr; \/* Current read pointer *\/<br \/>\n    char* _IO_read_end; \/* End of get area. *\/<br \/>\n    char* _IO_read_base; \/* Start of putback&#043;get area. *\/<br \/>\n    char* _IO_write_base; \/* Start of put area. *\/<br \/>\n    char* _IO_write_ptr; \/* Current put pointer. *\/<br \/>\n    char* _IO_write_end; \/* End of put area. *\/<br \/>\n    char* _IO_buf_base; \/* Start of reserve area. *\/<br \/>\n    char* _IO_buf_end; \/* End of reserve area. *\/<br \/>\n    \/* The following fields are used to support backing up and undo. *\/<br \/>\n    char *_IO_save_base; \/* Pointer to start of non-current get area. *\/<br \/>\n    char *_IO_backup_base; \/* Pointer to first valid character of backup area<br \/>\n    *\/<br \/>\n    char *_IO_save_end; \/* Pointer to end of non-current get area. *\/<br \/>\n    struct _IO_marker *_markers;<br \/>\n    struct _IO_FILE *_chain;<br \/>\n    int _fileno; \/\/\u5c01\u88c5\u7684\u2f42\u4ef6\u63cf\u8ff0\u7b26<br \/>\n#if 0<br \/>\n    int _blksize;<br \/>\n#else<br \/>\n    int _flags2;<br \/>\n#endif<br \/>\n    _IO_off_t _old_offset; \/* This used to be _offset but it&#039;s too small. *\/<br \/>\n#define __HAVE_COLUMN \/* temporary *\/<br \/>\n    \/* 1&#043;column number of pbase(); 0 is unknown. *\/<br \/>\n    unsigned short _cur_column;<br \/>\n    signed char _vtable_offset;<br \/>\n    char _shortbuf[1];<br \/>\n    \/* char* _save_gptr; char* _save_egptr; *\/<br \/>\n    _IO_lock_t *_lock;<br \/>\n#ifdef _IO_USE_OLD_IO_FILE<br \/>\n};<\/p>\n<h3>5.5\u624b\u5199\u4e00\u4e2a\u7b80\u5355\u7684libc\u5e93<\/h3>\n<p>mystdio.h<\/p>\n<p>#pragma once<\/p>\n<p>#include &lt;stdio.h&gt;<\/p>\n<p>#define MAX 1024<br \/>\n\/\/\u7f13\u51b2\u533a\u5237\u65b0<br \/>\n#define NONE_FLUSH 1<br \/>\n#define LINE_FLUSH 2<br \/>\n#define FULL_FLUSH 3<\/p>\n<p>typedef struct IO_FILE{<br \/>\n  int fileno;<br \/>\n  int filemode;<br \/>\n  char outbuffer[MAX];<br \/>\n  int bufferlen;<br \/>\n  int flush_method;<br \/>\n}MyFile;<\/p>\n<p>MyFile* MyFOpen(const char *path, const char *mode);<br \/>\nvoid MyFClose(MyFile *);<br \/>\nint MyFWrite(MyFile *,void *str,int len);<br \/>\nvoid MyFFlush(MyFile* );<\/p>\n<p>mystdio.c<\/p>\n<p>#include &#034;mystdio.h&#034;<\/p>\n<p>#include &lt;string.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;stdlib.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>static MyFile* buyfile(int fd,int filemode){<br \/>\n  MyFile* file&#061;(MyFile*)malloc(sizeof(MyFile));<br \/>\n  if(file&#061;&#061;NULL) return NULL;<br \/>\n  file-&gt;filemode&#061;filemode;<br \/>\n  file-&gt;fileno&#061;fd;<br \/>\n  file-&gt;bufferlen&#061;0;<br \/>\n  file-&gt;flush_method&#061;LINE_FLUSH;<br \/>\n  memset(file-&gt;outbuffer,0,sizeof(file-&gt;outbuffer));<br \/>\n  return file;<br \/>\n}<\/p>\n<p>MyFile* MyFOpen(const char *path, const char *mode){<br \/>\n  int fd&#061;-1;<br \/>\n  int filemode&#061;0;<br \/>\n  if(strcmp(mode,&#034;w&#034;)&#061;&#061;0){<br \/>\n    filemode&#061;O_CREAT|O_WRONLY|O_TRUNC;<br \/>\n  }<br \/>\n  else if(strcmp(mode,&#034;r&#034;)&#061;&#061;0){<br \/>\n    filemode&#061;O_RDONLY;<br \/>\n  }<br \/>\n  else if(strcmp(mode,&#034;a&#034;)&#061;&#061;0){<br \/>\n    filemode&#061;O_CREAT|O_WRONLY|O_APPEND;<br \/>\n  }<br \/>\n  else{<br \/>\n    \/\/<br \/>\n  }<br \/>\n  fd&#061;open(path,filemode,0666);<\/p>\n<p>  if(fd&lt;0) return NULL;<br \/>\n  return buyfile(fd,filemode);<br \/>\n}<\/p>\n<p>void MyFClose(MyFile *file){<br \/>\n  if(file-&gt;filemode&lt;0) return ;<br \/>\n  MyFFlush(file);<br \/>\n  file-&gt;fileno&#061;0;<br \/>\n  free(file);<br \/>\n}<\/p>\n<p>int MyFWrite(MyFile *file,void *str,int len){<br \/>\n  \/\/1.\u62f7\u8d1d<br \/>\n  memcpy(file-&gt;outbuffer&#043;file-&gt;bufferlen,str,len);<br \/>\n  file-&gt;bufferlen&#043;&#061;len;<br \/>\n  \/\/\u5224\u65ad\u5237\u65b0<br \/>\n  if((file-&gt;flush_method&amp;LINE_FLUSH)&amp;&amp;(file-&gt;outbuffer[file-&gt;bufferlen-1]&#061;&#061;&#039;\\\\n&#039;))<br \/>\n    MyFFlush(file);<br \/>\n  return 0;<br \/>\n}<\/p>\n<p>void MyFFlush(MyFile* file){<br \/>\n  \/\/\u628a\u6570\u636e\u4ece\u7528\u6237\u62f7\u8d1d\u5230\u5185\u6838\u6587\u4ef6\u7f13\u51b2\u533a\u4e2d<br \/>\n  int n&#061;write(file-&gt;fileno,file-&gt;outbuffer,file-&gt;bufferlen);<br \/>\n  (void)n;\/\/\u6d88\u9664\u7f16\u8bd1\u5668\u62a5\u8b66<br \/>\n  fsync(file-&gt;fileno);\/\/\u5f3a\u5236\u5c06\u6587\u4ef6\u6570\u636e\u548c\u5143\u6570\u636e\u4ece\u5185\u6838\u9875\u7f13\u5b58\u5199\u5165\u78c1\u76d8<br \/>\n  file-&gt;bufferlen&#061;0;<br \/>\n}<\/p>\n<p>usercode.c<\/p>\n<p>#include &#034;mystdio.h&#034;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>int main(){<br \/>\n  MyFile* fp&#061;MyFOpen(&#034;log.txt&#034;,&#034;w&#034;);<br \/>\n  if(!fp){<br \/>\n    printf(&#034;MyfOpen failed\\\\n&#034;);<br \/>\n    return 1;<br \/>\n  }<br \/>\n  const char* msg&#061;&#034;hello world&#034;;<br \/>\n  int cnt&#061;10;<br \/>\n  while(cnt&#8211;){<br \/>\n    MyFWrite(fp,(char*)msg,strlen(msg));<br \/>\n    MyFFlush(fp);<br \/>\n    printf(&#034;buffer:%s\\\\n&#034;,fp-&gt;outbuffer);<br \/>\n    sleep(1);<br \/>\n  }<br \/>\n  MyFClose(fp);<br \/>\n  return 0;<br \/>\n}<\/p>\n<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#x1f481;\u200d\u2642\ufe0f\u4e2a\u4eba\u4e3b\u9875&#xff1a;\u8fdb\u51fb\u7684\u8346\u68d8 &#x1f447;\u4f5c\u8005\u5176\u5b83\u4e13\u680f&#xff1a; \u300a\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u300b\u300a\u7b97\u6cd5\u300b\u300aC\u8d77\u59cb\u4e4b\u8def\u300b\u300aLinux\u300b \u76ee\u5f55<br \/>\n1.\u7406\u89e3\u201c\u6587\u4ef6\u201d<br \/>\n2.C\u6587\u4ef6\u63a5\u53e3<br \/>\n3.\u7cfb\u7edf\u6587\u4ef6I\/O<br \/>\n4.\u7406\u89e3\u201c\u4e00\u5207\u7686\u6587\u4ef6\u201d<br \/>\n5.\u7f13\u51b2\u533a 4.\u7406\u89e3\u201c\u4e00\u5207\u7686\u6587\u4ef6\u201d<br \/>\n\u9996\u5148&#xff0c;\u5728windows\u4e2d\u662f\u6587\u4ef6\u7684\u4e1c\u897f&#xff0c;\u5728linux\u4e2d\u4e5f\u662f\u6587\u4ef6&#xff1b;\u5176\u6b21\u4e00\u4e9b\u5728windows\u4e2d\u4e0d\u662f\u6587\u4ef6\u7684\u4e1c\u897f&#xff0c;\u5982&#xff1a;\u8fdb\u7a0b\u3001<\/p>\n","protected":false},"author":2,"featured_media":111525,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"topic":[],"class_list":["post-111526","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Linux\u7cfb\u7edf\u2014\u2014\u57fa\u7840IO\uff08\u4e0b\uff09 - \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\/111526.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linux\u7cfb\u7edf\u2014\u2014\u57fa\u7840IO\uff08\u4e0b\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"&#x1f481;\u200d\u2642\ufe0f\u4e2a\u4eba\u4e3b\u9875&#xff1a;\u8fdb\u51fb\u7684\u8346\u68d8 &#x1f447;\u4f5c\u8005\u5176\u5b83\u4e13\u680f&#xff1a; \u300a\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u300b\u300a\u7b97\u6cd5\u300b\u300aC\u8d77\u59cb\u4e4b\u8def\u300b\u300aLinux\u300b \u76ee\u5f55 1.\u7406\u89e3\u201c\u6587\u4ef6\u201d 2.C\u6587\u4ef6\u63a5\u53e3 3.\u7cfb\u7edf\u6587\u4ef6I\/O 4.\u7406\u89e3\u201c\u4e00\u5207\u7686\u6587\u4ef6\u201d 5.\u7f13\u51b2\u533a 4.\u7406\u89e3\u201c\u4e00\u5207\u7686\u6587\u4ef6\u201d \u9996\u5148&#xff0c;\u5728windows\u4e2d\u662f\u6587\u4ef6\u7684\u4e1c\u897f&#xff0c;\u5728linux\u4e2d\u4e5f\u662f\u6587\u4ef6&#xff1b;\u5176\u6b21\u4e00\u4e9b\u5728windows\u4e2d\u4e0d\u662f\u6587\u4ef6\u7684\u4e1c\u897f&#xff0c;\u5982&#xff1a;\u8fdb\u7a0b\u3001\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/111526.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-01T03:21:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261001032133-6abdd1bd0dc26.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=\"8 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/111526.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/111526.html\",\"name\":\"Linux\u7cfb\u7edf\u2014\u2014\u57fa\u7840IO\uff08\u4e0b\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-01T03:21:34+00:00\",\"dateModified\":\"2026-10-01T03:21:34+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/111526.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/111526.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/111526.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux\u7cfb\u7edf\u2014\u2014\u57fa\u7840IO\uff08\u4e0b\uff09\"}]},{\"@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\u7cfb\u7edf\u2014\u2014\u57fa\u7840IO\uff08\u4e0b\uff09 - \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\/111526.html","og_locale":"zh_CN","og_type":"article","og_title":"Linux\u7cfb\u7edf\u2014\u2014\u57fa\u7840IO\uff08\u4e0b\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"&#x1f481;\u200d\u2642\ufe0f\u4e2a\u4eba\u4e3b\u9875&#xff1a;\u8fdb\u51fb\u7684\u8346\u68d8 &#x1f447;\u4f5c\u8005\u5176\u5b83\u4e13\u680f&#xff1a; \u300a\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u300b\u300a\u7b97\u6cd5\u300b\u300aC\u8d77\u59cb\u4e4b\u8def\u300b\u300aLinux\u300b \u76ee\u5f55 1.\u7406\u89e3\u201c\u6587\u4ef6\u201d 2.C\u6587\u4ef6\u63a5\u53e3 3.\u7cfb\u7edf\u6587\u4ef6I\/O 4.\u7406\u89e3\u201c\u4e00\u5207\u7686\u6587\u4ef6\u201d 5.\u7f13\u51b2\u533a 4.\u7406\u89e3\u201c\u4e00\u5207\u7686\u6587\u4ef6\u201d \u9996\u5148&#xff0c;\u5728windows\u4e2d\u662f\u6587\u4ef6\u7684\u4e1c\u897f&#xff0c;\u5728linux\u4e2d\u4e5f\u662f\u6587\u4ef6&#xff1b;\u5176\u6b21\u4e00\u4e9b\u5728windows\u4e2d\u4e0d\u662f\u6587\u4ef6\u7684\u4e1c\u897f&#xff0c;\u5982&#xff1a;\u8fdb\u7a0b\u3001","og_url":"https:\/\/www.wsisp.com\/helps\/111526.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-01T03:21:34+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261001032133-6abdd1bd0dc26.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"8 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/111526.html","url":"https:\/\/www.wsisp.com\/helps\/111526.html","name":"Linux\u7cfb\u7edf\u2014\u2014\u57fa\u7840IO\uff08\u4e0b\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-01T03:21:34+00:00","dateModified":"2026-10-01T03:21:34+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/111526.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/111526.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/111526.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Linux\u7cfb\u7edf\u2014\u2014\u57fa\u7840IO\uff08\u4e0b\uff09"}]},{"@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\/111526","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=111526"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/111526\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/111525"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=111526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=111526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=111526"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=111526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}