mmap映射内存的大小是不是不能超过文件大小?
做了以下实验:
1.文本文件 HELLO.txt 的大小有6个字节;
2.使用mmap成功映射这个文件到内存;mmap( NULL,6, ... )
3.修改mmap返回的指针,程序退出后查看文件,文件内容已成功改变。
我把mmap的第二个参数设置的很大,但是最后文件 HELLO.txt 只有6个字节。
我想问一下,是不是mmap大小不能超过映射文件的大小?
假如我有一个程序是从数据库导出大量数据,经过处理后写入mmap映射的文件,
那我必须预先计算好将要写入的数据大小,否则就写不进去了。
[解决办法]
不能超过
摘自APUE
Since the starting offset of the mapped file is tied to the system's virtual memory page size, what happens if the length of the mapped region isn't a multiple of the page size? Assume that the file size is 12 bytes and that the system's page size is 512 bytes. In this case, the system normally provides a mapped region of 512 bytes, and the final 500 bytes of this region are set to 0. We can modify the final 500 bytes, but any changes we make to them are not reflected in the file. Thus, we cannot append to a file with mmap. We must first grow the file
[解决办法]
_lseeki64