fcntl:打开和关闭磁盘缓冲

我写了一个简短的C程序来打开和关闭unix文件缓冲.下面的代码是打开它.我使用fcntl来获取设置,设置O_SYNC标志,然后将设置写回内核.但是当我再次获得设置时,它们没有设置O_SYNC标志.

int result, s;
s = fcntl(*fd, F_GETFL);
s |= O_SYNC; // set SYNC bit
result = fcntl(*fd, F_SETFL, s);

if( result == -1 )
   perror("setting SYNC");
else
{
    // Check buffering is on
    s = fcntl(*fd, F_GETFL); //
    if((s & O_SYNC) == O_SYNC) // check if SYNC bit is set
        printf("In function buffering_off(): Buffering is OFF\n");
    else
        printf("In function buffering_off(): Buffering is ON\n");
}

任何帮助,将不胜感激.干杯:)

最佳答案 我在Linux内核列表中找到了这个讨论:
http://choon.net/forum/read.php?21,22539

简而言之:

The problem is that fcntl(fd, F_SETFL, flags|O_SYNC) appears to work, but silently ignores the O_SYNC flag.
Opening the file with O_SYNC works okay, but setting it later on via fcntl doesn’t work.

听起来这个bug从第一天开始就存在,但最近可能已经修复了(就运输内核而言,我不确定当前的状态).

I am using SuSE Linux, a version that is about 6 years old.

讨论大约一年前.鉴于你的内核的年龄,它肯定受到bug的影响.

点赞