简体   繁体   English

mmap():如果底层文件发生变化(收缩)会发生什么?

[英]mmap(): what happens if underlying file changes (shrinks)?

If you memory map a file using mmap(), but then the underlying file changes to a much smaller size. 如果内存使用mmap()映射文件,但是底层文件会变为更小的大小。 What happens if you access a memory offset that was shaved off from the file? 如果您访问从文件中删除的内存偏移量会发生什么?

IBM says it is undefined http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=%2Fapis%2Fmmap.htm IBM表示它未定义http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=%2Fapis%2Fmmap.htm

If the size of the mapped file is decreased after mmap(), attempts to reference beyond the end of the file are undefined and may result in an MCH0601 exception. 如果在mmap()之后减小了映射文件的大小, 则未定义超出文件末尾的引用尝试,并可能导致MCH0601异常。

If the size of the file increases after the mmap() function completes, then the whole pages beyond the original end of file will not be accessible via the mapping. 如果在mmap()函数完成后文件的大小增加,则无法通过映射访问文件原始末尾之外的整个页面。

The same is said in SingleUnixSpecification: http://pubs.opengroup.org/onlinepubs/7908799/xsh/mmap.html 在SingleUnixSpecification中也是如此: http ://pubs.opengroup.org/onlinepubs/7908799/xsh/mmap.html

If the size of the mapped file changes after the call to mmap() as a result of some other operation on the mapped file, the effect of references to portions of the mapped region that correspond to added or removed portions of the file is unspecified . 如果由于对映射文件的某些其他操作而在调用mmap()之后映射文件的大小发生更改, 则未指定对映射区域的与文件的添加或删除部分对应的部分的引用的效果。

'undefined' or 'unspecified' means - the OS is allowed to start formatting of disk or anything. 'undefined'或'unspecified'表示 - 允许操作系统开始格式化磁盘或任何东西。 Most probable is SIGSEGV-killing your application. 最可能的是SIGSEGV - 杀死你的应用程序。

According to the man pages mmap returns EINVAL error when you try to access an address that is too large for the current file mapping. 根据手册页,当您尝试访问对于当前文件映射来说太大的地址时,mmap会返回EINVAL错误。

"dnotify" and "inotify" are the current file change notification services in the Linux kernel. “dnotify”和“inotify”是Linux内核中当前的文件更改通知服务。 Presumably, they would inform the mmap subsystem of changes to the file. 据推测,他们会通知mmap子系统文件的更改。

It depends on what flags you gave to mmap , the man page: 这取决于你给mmap标志,手册页:

MAP_SHARED Share this mapping. MAP_SHARED分享此映射。 Updates to the mapping are visible to other processes that map this file, and are carried through to the underlying file. 映射的更新对于映射此文件的其他进程是可见的,并且会传递到基础文件。 The file may not actually be updated until msync(2) or munmap() is called. 在调用msync(2)或munmap()之前,实际上可能不会更新该文件。

and

MAP_PRIVATE Create a private copy-on-write mapping. MAP_PRIVATE创建私有的写时复制映射。 Updates to the mapping are not visible to other processes mapping the same file, and are not carried through to the underlying file. 映射的更新对映射同一文件的其他进程不可见,并且不会传递到基础文件。 It is unspecified whether changes made to the file after the mmap() call are visible in the mapped region. 未指定在mmap()调用之后对文件所做的更改是否在映射区域中可见。

So for MAP_PRIVATE , doesn't matter, each writer effectively has a "private" copy. 所以对于MAP_PRIVATE ,无所谓,每个作家都有一个“私人”副本。 (though it is only copies when a mutating operation occurs). (虽然只有在发生变异操作时才会复制)。

I would think that if you use MAP_SHARED , then no other process would be allowed to open the file with write privileged. 认为如果您使用MAP_SHARED ,则不允许其他进程使用write特权打开该文件。 But that's a guess. 但这是猜测。

EDIT : ninjalj is right, the file can be modified even when you mmap with MAP_SHARED . 编辑 :ninjalj是正确的,即使你使用MAP_SHARED mmap ,也可以修改该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM