setvbuf不缓冲完整的缓冲区大小

时间:2018-07-02 16:10:41

标签: c vxworks

我试图推迟将数据写入文件,直到使用fopen,setvbuf和fwrite组合将其缓冲到512KB。在我的用例中,fwrite将一次调用大约1400字节的数据。

static const int bufferSize = ((32 * 1024) * 16); // 512K buffer

...

semGive(OS_FDTableMutex);

FILE *file = fopen(local_path, permString);

semTake(OS_FDTableMutex,WAIT_FOREVER);

if (file != (FILE *)NULL)
{
    status = setvbuf(file, NULL, _IOFBF, bufferSize);
    ...
    //Copy file pointer into OS_FDTable[filedes].OSfp

后来写的时候:

else if (OS_FDTable[filedes].OSfp != (FILE *)NULL)
{
    //nbytes is normally ~1400
    status = fwrite(buffer, sizeof(char), nbytes, OS_FDTable[filedes].OSfp);

    /* if the number of bytes written is < nbytes, then it is an error. */
    if (status < nbytes )
        return OS_FS_ERROR;
    else
        return  status;
}

但是,当我进行测试时,没有看到文件大小以512KB的增量填充,而是以32KB的增量填充。 32KB恰好是我正在写入的磁盘上的页面大小。

是什么导致文件以32KB块而不是512KB块的形式写入?

0 个答案:

没有答案
相关问题