Linux mmap函数中的MAP_SHARED和MAP_PRIVATE有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9519648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between MAP_SHARED and MAP_PRIVATE in the mmap function?
提问by ntl0ve
Playing around with mmap
for the fun of it, I have the following code:
玩弄mmap
了它的乐趣,我有以下代码:
(.. snip ..)
fd = open("/home/me/straight_a.txt", O_RDONLY);
if (fd == -1) {
perror("open");
exit(1);
}
m = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_FILE|MAP_PRIVATE, fd, 0);
if (m == MAP_FAILED) {
perror("mmap");
exit(1);
}
printf("m is %p\n", m);
printf("*m = %c\n", *m);
printf("*(m+1) = %c\n", *(m+1));
(.. snip ..)
This works as expected. But before I got to this, I tried...
这按预期工作。但在我开始之前,我试过......
m = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, fd, 0);
... and mmap errored out with:
...并且 mmap 出错:
mmap: Permission denied
In general, what's the difference between the two flags (man page isn't to generous on this subject)? What sort of permission (and where) am I missing?
一般来说,这两个标志之间有什么区别(手册页在这个主题上不是很慷慨)?我缺少什么样的许可(以及在哪里)?
EDIT
编辑
Like it usually happens.. partially figured it out.
就像它通常发生的那样......部分地想通了。
Turns out open
needed an O_RDWR
flag.
原来open
需要一个O_RDWR
标志。
So, am I correct to assume that:
那么,我是否正确假设:
- MAP_PRIVATE - changes are made in memory only, not saved to disk?
- MAP_SHARED - changes would be saved to disk...
- MAP_PRIVATE - 更改仅在内存中进行,不保存到磁盘?
- MAP_SHARED - 更改将保存到磁盘...
... but I'm not saving anything to disk anywhere, I thought? Just operating on memory.
...但我不会在任何地方将任何内容保存到磁盘,我想?只是在内存上操作。
回答by Tim
Writes to a MAP_SHARED segment are carried through to the underlying file. You opened the file with O_RDONLY, which conflicts with the PROT_WRITE flag, thereby preventing MAP_SHARED from being able to write back to the file.
对 MAP_SHARED 段的写入将传递到底层文件。您使用 O_RDONLY 打开文件,这与 PROT_WRITE 标志冲突,从而阻止 MAP_SHARED 能够写回文件。
MAP_PRIVATE does not carry writes back to the underlying file, so the fact that you opened the file O_RDONLY is not an issue.
MAP_PRIVATE 不会将写回底层文件,因此您打开文件 O_RDONLY 的事实不是问题。
回答by karunski
You opened the file in read-only mode. Then you attempted to mmap part of it in read/write mode with MAP_SHARED set. In this context MAP_SHARED implies that if you write to the mmap'd region your changes will be committed back to the mapped file itself. You can't do this because you opened the file in read-only mode.
您以只读模式打开文件。然后您尝试使用 MAP_SHARED 设置以读/写模式映射其中的一部分。在这种情况下, MAP_SHARED 意味着如果您写入 mmap 的区域,您的更改将提交回映射文件本身。您不能这样做,因为您以只读模式打开了文件。
MAP_PRIVATE works because writes to the mmap'd region are not committed back to the original file. When you write to the region the pages that were written to are copied to a different region of memory, possibly backed by swap space.
MAP_PRIVATE 有效,因为对 mmap 区域的写入不会提交回原始文件。当您写入该区域时,写入的页面将被复制到不同的内存区域,可能由交换空间支持。