read(), write()系统调用在操作磁盘文件时不会直接发起磁盘访问,而是仅仅在用户空间缓冲区和内核缓冲区之间复制数据
2. stdio库中的缓冲
点击(此处)折叠或打开
- #include <stdio.h>
- int setvbuf(FILE *stream, char *buf, int mode, size_t size);
- /*returns 0 on success, or nonzero on error*/
_IONBF:不对I/O进行缓冲(stderr)
_IOLBF:采用行缓冲I/O(终端)
_IOFBF:采用全缓冲I/O
点击(此处)折叠或打开
- #include <stdio.h>
- void setbuf(FILE *stream, char *buf);
- setbuf(fp, buf) ==== setvbuf(fp, buf, (buf !=NULL) ? _IOFBF:_IONBF, BUFSIZ);
3. 控制文件I/O的内核缓冲
强制刷新内核缓冲区到输出文件是可能的,这有时候很有必要
同步I/O的两种分类
- 同步I/O数据完整性:同步数据,置于元数据,在后续需要的时候才同步
点击(此处)折叠或打开
- #include <unistd.h>
- int fdatasync(int fd);
- /*returns 0 on success, or -1 on error*/
- #include <unistd.h>
- 同步I/O文件完整性:上面的超集,对文件的一次更新过程中,要将所有发生更新的文件元数据都传递到磁盘上,即使有些在后续文件数据的读取操作中并不需要
点击(此处)折叠或打开
- #include <unistd.h>
- int fsync(int fd);
- /*returns 0 on success, or -1 on error/
- #include <unistd.h>
点击(此处)折叠或打开
- #include <stdio.h>
- int fileno(FILE *stream);
- /*returns file descriptor on success, or -1 on error*/
- FILE *fdopen(int fd, const char *mode);
- /*returns (new) file pointer on success, or NULL on error*/
点击(此处)折叠或打开
- #include <stdio.h>
- #include <wchar.h>
- int fwide(FILE *fp, int mode);
- /*returns: positive if stream is wide oriented; negative if stream is byte oriented; or 0 if stream has no orientation*/