使用O_DIRECT的话,就必须以页为单位进行I/O

O_DIRECT undeclared

加宏 #define _GUN_SOURCE

Page cache这种内核提供的缓存机制并不是强制使用的,如果进程在open()一个文件的时候指定flags为O_DIRECT,那进程和这个文件的数据交互就直接在用户提供的buffer和磁盘之间进行,page cache就被bypass了,借用硬件cache的术语就是uncachable,这种文件访问方式被称为direct I/O,适用于用户使用自己设备提供的缓存机制的场景,比如某些数据库应用

O_DIRECT只是绕过了page cache,但它并不等待数据真正写到了磁盘上。open()中flags参数使用O_SYNC才能保证writepage()会等到数据可靠的写入磁盘后再返回,适用于某些不容许数据丢失的关键应用。O_SYNC模式下可以使用或者不使用page cache,如果使用page cache,则相当于硬件cache的write through机制

即使用fd=open(s, O_RDWR | O_CREAT | O_DIRECT | O_SYNC);

本文参考:

http://blog.csdn.net/wallwind/article/details/7461701

首先来看O_DIRECT参数适用要求:

O_DIRECT
    Try to minimize cache effects of the I/O to and from this file. In general this will degrade performance, but it is useful in special situations, such as when applications do their own caching. File I/O is done directly to/from user space buffers. The I/O is synchronous, i.e., at the completion of a read(2) or write(2), data is guaranteed to have been transferred. Under Linux 2.4 transfer sizes, and the alignment of user buffer and file offset must all be multiples of the logical block size of the file system.

上述也就是说buffer的地址以及大小都必须是block size 的整数倍,buffer的大小可以通过代码设定,但buffer的首地址的控制,需要通过posix_menalign()函数来数据对齐

ps:上述所说的buffer地址为block size整数倍,就是数据对齐的概念

int posix_memalign (void **memptr, size_t alignment, size_t size);

成功会返回size字节的动态内存,且内存地址为alignment的整数倍

下面看具体代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#define __USE_GNU
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <unistd.h>
# include <pwd.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
int  main( void )
{
     int         fd;
     int         i;
     const int   SIZE = 4096 * 10 ; //(每次写入文件数据块大小)
     char*       buffer;
     char s[ 100 ];  
     int pagesize=getpagesize();
     printf( "pagesize = %d\n" ,pagesize);
    pid_t my_pid;
     my_pid=getpid();
     printf( "%d\n" ,my_pid);
     if (mkdir( "dir1" ,S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH)< 0 ) //创建新目录
     {
         printf( "mkdir failed\n" );
         return - 1 ;     
     }
     if ( 0 != posix_memalign(( void **)&buffer, 4096 , 40960 )){
                 printf( "Errori in posix_memalign\n" );
     }
     for (i= 0 ;i< 10 ;i++)
     {
         sprintf(s, "./dir1/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa%d.txt" ,i);
         printf( "%s\n" ,s);
         fd=open(s, O_RDWR | O_CREAT | O_DIRECT);
         write(fd,buffer,SIZE);
                 fsync(fd);
         close(fd);
     }
     free(buffer);
     sync();
     return 0 ;
}

上述代码有几个点:

1.mkdir创建目录

2.posix_menalign()进行数据对齐

3.获取block size,即内存页大小getpagesize()

4.编译时需要gcc -o blktrace_study1 blktrace_study1.c -D_GNU_SOURCE

5.open函数返回的为int ,区别fopen函数返回FILE *

6.fsync和sync区别见另一篇博客

参考:http://sundayhut.is-programmer.com/posts/50419.html

O_DIRECT使用相关推荐

  1. linux open系统调用的O_DIRECT标记

    前言 open系统调用中针对打开的文件描述符,可以增加一个O_DIRECT标记,该标记能够使得针对该文件描述符的写操作绕过操作系统page cache,直接进入通用块设备层,从而减少页缓存对IO效率的 ...

  2. open的O_DIRECT选项

    http://blog.chinaunix.net/uid-223060-id-2127385.html http://blog.csdn.net/hhtang/article/details/660 ...

  3. InnoDB O_DIRECT选项漫谈(一)【转】

    本文来自:http://insidemysql.blog.163.com/blog/static/2028340422013671186977/ 最近和文件系统内核开发人员做技术交流,对O_DIREC ...

  4. O_DIRECT打开文件失败

    内核版本: Linux 4.1.39-g35786aadeab-dirty 文件系统:UBIFS 返回错误errno值:22     对应  EINVAL 官网说明:https://man.cx/op ...

  5. DirectIO(O_DIRECT) 详解

    DirectIO(O_DIRECT) 详解 文章目录 DirectIO(O_DIRECT) 详解 什么是DirectIO 如何使用DirectIO DirectIO的性能 DirectIO的应用 什么 ...

  6. O_DIRECT对齐

    这是我对quora上的一篇回答的理解. O_DIRECT用于直接将内存中的数据写入存储,而不经过操作系统文件缓存.通常的文件写操作需要经过"用户空间->内核空间->存储" ...

  7. linux open o direct,文件操作O_DIRECT使用

    一般如果在Linux内核中读写一个文件,其IO流程都需要经过Kernel内的page cache层次,若想要使用自己开发的缓存系统,那么就可以在打开这个文件的时候,对该文件加以O_DIRECT的标志位 ...

  8. linux 下安装MySQL

    1. 安装必要的组件 1 2 # yum install –y autoconf automake imake libxml2-devel\ expat-devel cmake gcc gcc-c++ ...

  9. linux 管道非阻塞,在Linux中管道上的非阻塞读取

    可以在管道上进行非阻塞I / O吗? fcntl无法设置O_NONBLOCK. Linux编程接口的页面918包括一个表'从管道读取n个字节或FIFO(p)'的语义.此表列出了管道和FIFO的行为,其 ...

最新文章

  1. Help Johnny-(类似杭电acm3568题)
  2. php 实现对称加密算法,PHP实现简单的对称加密和解密方法
  3. Android自带的emoji表情的使用
  4. Axure8.0深入一点(篇)
  5. Memcache 内存分配策略和性能(使用)状态检查
  6. mysql 5.7.11 my.ini,mysql5.7以上版本配置my.ini的详细步骤
  7. C# 密封类sealed
  8. LeetCode 1049. 最后一块石头的重量 II(DP)
  9. [记录] --- linux上项目
  10. 华为路由器ospf路由表解读_网络-路由交换-路由基础-华为-OSPF的工作原理
  11. JSON有关的一道题
  12. python改变日期的输出格式,关于python:解析日期字符串并更改格式
  13. python-day1-用户的输入输出
  14. 论文笔记_S2D.45_ORBSLAM-Atlas: 一个稳健和精确的多建图系统
  15. java疯狂讲义pdf_《疯狂Java讲义(第3版)》PDF 下载
  16. Java性能优化全攻略
  17. python绘图设置新罗马字体_更改matplotlib中的字体
  18. codeforces CF487E Tourists 边双连通分量 树链剖分
  19. kafka启动失败 The Cluster ID doesn‘t match...
  20. 黑苹果驱动板载intel蓝牙

热门文章

  1. Calendar( 日历)
  2. vue-devTools Chrome安装配置
  3. 区块链学习(Fisco搭建)【Day04-05,09】
  4. XP系统忘记密码?第一篇-U深度PE系统
  5. 初征——淘特多店铺智能化运营的订单管理系统
  6. 魔塔之拯救白娘子~我的第一个VB6+DX8做的小游戏源码~10地图编辑器-卡通绘制
  7. 学习“写给asp.net的新手学习经验”
  8. 强推,黑白照片一步完美上色,Picture Colorizer Pro Mac版
  9. 基于Kubernetes和Jenkins Pipeline的持续自动化项目
  10. DCMM数据管理能力成熟度的八个关键过程域