首页 > 代码库 > Linux Direct 文件读写(文件DIO)

Linux Direct 文件读写(文件DIO)

有时候,读写文件并不想要使用系统缓存(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);
View Code  

(2)读写操作的传输数据大小和缓冲区地址都需要按照一定的规则对齐:

Linux下,对于不同的文件系统和内核版本,需要的对齐边界不同,也没有统一的接口可以获取到该边界值。

对于kernel 2.4版本:传输大小和缓冲区地址均需要按照访问文件系统的逻辑块大小对齐,比如文件系统的块大小是4K,buffer地址需要按照4K对齐,需要读写4K倍数的数据

对于kernel 2.6版本:传输大小和缓冲区地址按照目标存储设备的扇区大小(一般512)对齐

 

可使用memalign (malloc.h)来分配指定地址对齐的资源接口:void *memalign(size_t boundary, size_t size);

 

完整示例程序: 

 

#define _GNU_SOURCE #include <sys/types.h>#include <fcntl.h>#include <malloc.h>#include <stdio.h>#include <errno.h>#include <string.h> int main(void) {        char hello_str[] = "Hello World!";        void *write_buffer;        void *read_buffer;        int fd;         int ret = 0;             fd = open("test.out", O_RDWR | O_CREAT | O_DIRECT, 0644);        if (fd < 0) {                printf("Failed to open file\n");                return fd;         }                /* allocate a 1024 bytes buffer */        write_buffer = memalign(512, 512 * 2); // align by 512        if (!write_buffer) {                printf("Failed to alloc write buffer\n");                ret = -ENOMEM;                goto bad_write_buffer;        }            memcpy(write_buffer, hello_str, sizeof(hello_str));         ret = write(fd, write_buffer, 512 * 2);         if (ret < 0) {                printf("Failed to write file\n");                 goto bad_write;        }            lseek(fd, 0, SEEK_SET); // read  previous write data             read_buffer = memalign(512, 512 * 2);        if (!read_buffer) {                printf("Failed to alloc read buffer\n");                ret = -ENOMEM;                goto bad_read_buffer;        }         ret = read(fd, read_buffer, 512 * 2);        if (ret <0) {                printf("Failed to read file\n");                goto bad_read;        }         printf("read from file : %s\n", read_buffer); bad_read:        free(read_buffer);bad_read_buffer:bad_write:        free(write_buffer);bad_write_buffer:        close(fd);        return ret;}
View Code

 

引用Linus的话:

"The thing that has always disturbed me about O_DIRECT is that the whole interface is just stupid, and was probably designed by a deranged monkey on some serious mind-controlling substances."—Linus(O_DIRECT就是一坨屎)

 

PS:

O_DIRECT的详细描述,可以看linux open系统调用的文档:http://man7.org/linux/man-pages/man2/open.2.html

 

Linux Direct 文件读写(文件DIO)