Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1850134
  • 博文数量: 184
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2388
  • 用 户 组: 普通用户
  • 注册时间: 2016-12-21 22:26
个人简介

90后空巢老码农

文章分类

全部博文(184)

文章存档

2021年(26)

2020年(56)

2019年(54)

2018年(47)

2017年(1)

我的朋友

分类: LINUX

2020-02-16 21:24:09

1. 文件I/O的内核缓冲
read(), write()系统调用在操作磁盘文件时不会直接发起磁盘访问,而是仅仅在用户空间缓冲区和内核缓冲区之间复制数据

2. stdio库中的缓冲

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. int setvbuf(FILE *stream, char *buf, int mode, size_t size);
  3. /*returns 0 on success, or nonzero on error*/
mode取值如下:
_IONBF:不对I/O进行缓冲(stderr)
_IOLBF:采用行缓冲I/O(终端)
_IOFBF:采用全缓冲I/O

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. void setbuf(FILE *stream, char *buf);
  3. setbuf(fp, buf) ==== setvbuf(fp, buf, (buf !=NULL) ? _IOFBF:_IONBF, BUFSIZ);

3. 控制文件I/O的内核缓冲

强制刷新内核缓冲区到输出文件是可能的,这有时候很有必要
同步I/O的两种分类
  • 同步I/O数据完整性:同步数据,置于元数据,在后续需要的时候才同步

  • 点击(此处)折叠或打开

    1. #include <unistd.h>
    2. int fdatasync(int fd);
    3. /*returns 0 on success, or -1 on error*/

  • 同步I/O文件完整性:上面的超集,对文件的一次更新过程中,要将所有发生更新的文件元数据都传递到磁盘上,即使有些在后续文件数据的读取操作中并不需要

  • 点击(此处)折叠或打开

    1. #include <unistd.h>
    2. int fsync(int fd);
    3. /*returns 0 on success, or -1 on error/


4. 混合使用库函数和系统调用进行文件I/O

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. int fileno(FILE *stream);
  3. /*returns file descriptor on success, or -1 on error*/
  4. FILE *fdopen(int fd, const char *mode);
  5. /*returns (new) file pointer on success, or NULL on error*/


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <wchar.h>
  3. int fwide(FILE *fp, int mode);
  4. /*returns: positive if stream is wide oriented; negative if stream is byte oriented; or 0 if stream has no orientation*/











阅读(972) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~