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*/











10-27 21:16
查看更多