Memory Mapping the same file multiple times?

荒凉一梦 提交于 2019-12-11 08:24:09

问题


What are the performance characteristics of memory mapping the same file multiple times? Will the OS reuse/cache between the mappings or will it read in the file multiple times into different parts of the memory?

i.e. if I read and write to a memory mapped file from two different processes will it go through the disk or will they communicate in memory? If I read from a memory mapped file from two different process alternatively thread, will they read from the same memory?


回答1:


You can use MAP_PRIVATE in order to get a private copy-on-write mapping of the underlying file. You can use MAP_SHARED in order to get a view of the underlying file. I do not know what happens if you have the same region mapped in MAP_PRIVATE mappings and MAP_SHARED mappings at the same time, but I suspect writes to either mapping cause a copy.

In order to sync with disk, you want to use msync. On most systems, multiple MAP_SHARED mappings will give you a piece of memory, nominally backed by disk, that is shared among processes. I'm not sure whether this behaviour is specified or merely a happy consequence of the typical implementation.




回答2:


It will use the file-cache, so assuming there is ENOUGH RAM in the machine, it should cache the two processes' writes. Of course also assuming the reads and writes actually hit the same area of the file...



来源:https://stackoverflow.com/questions/24172861/memory-mapping-the-same-file-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!