有时候,读写文件并不想要使用系统缓存(page cache),此时 direct 文件读写就派上了用场,使用方法:
(1)打开文件时,添加O_DIRECT参数:
需要定义_GNU_SOURCE,否则找不到O_DIRECT宏定义
示例片段:
#define _GNU_SOURCE #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int fd = open("test.out", O_RDWR | O_CREAT | O_DIRECT, 0644);