Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 使用MAP_填充以写入文件_C_Linux_Mmap - Fatal编程技术网

C 使用MAP_填充以写入文件

C 使用MAP_填充以写入文件,c,linux,mmap,C,Linux,Mmap,我的应用程序将大量文件写入磁盘。由于各种原因,使用mmap和内存写入比使用fwrite接口更方便 以这种方式写入文件的缓慢部分是页面错误。使用mmap和MAP\u POPULATE应该会有所帮助;从手册页: MAP_POPULATE (since Linux 2.5.46) Populate (prefault) page tables for a mapping. For a file mapping, this causes read-ahead on the file. Thi

我的应用程序将大量文件写入磁盘。由于各种原因,使用
mmap
和内存写入比使用
fwrite
接口更方便

以这种方式写入文件的缓慢部分是页面错误。使用
mmap
MAP\u POPULATE
应该会有所帮助;从手册页:

MAP_POPULATE (since Linux 2.5.46)
    Populate (prefault) page tables for a mapping.  For a file mapping, this causes read-ahead on the file.  This will help  to
    reduce blocking on page faults later.  MAP_POPULATE is supported for private mappings only since Linux 2.6.23.
(为了回答一个显而易见的问题:我在4.15和5.1版本中对相对较新的内核进行了测试)

但是,在写入映射文件时,这似乎无法减少页面错误

最小示例代码:test.c:

#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
静态int exit_,带有错误(const char*msg){
佩罗尔(味精);
退出(退出失败);
}
int main(){

const size_t len=1UL因为您对编写文件感兴趣,所以我不认为使用MAP_POPULATE有什么意义,它旨在加快读取速度…@JL.Sanchez也许我是盲人,但它在哪里说它只是为了加快读取速度?对于文件映射,这会导致文件提前读取是的,手册页上是这么说的。但它确实如此并不是说,它确实会对页面进行预故障处理。这确实是我问题的要点。顺便说一句,如果你确定,那么请写一个答案。因为你对编写文件感兴趣,我不认为使用MAP_POPULATE有什么意义,这是为了加快阅读速度…@JL.Sanchez也许我是盲人,但它在哪里呢ay,这只是为了加快阅读速度?对于文件映射,这会导致文件提前阅读是的,手册页上是这么说的。但它没有说,它确实会导致预故障页面在写入时无效。顺便说一句,这确实是我问题的要点,所以如果你确定,请写一个答案。