在mmap()的手册页:为什么文件中MMAP启动时偏移()必须是多页面大小的
它的原型是:
void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
和说明:
The mmap() function asks to map 'length' bytes starting at offset 'offset'
from the file (or other object) specified by the file descriptor fd into
memory, preferably at address 'start'.
Sepcifically ,最后一个参数:
'offset' should be a multiple of the page size as returned by getpagesize(2).
从我的练习中,offset
必须是页面大小的倍数,例如我的Linux上的4096,否则,mmap()将返回Invalid argument
,offset
用于文件偏移,为什么它必须是虚拟内存系统页面大小的倍数?
谢谢,
“操作系统必须确保这个位置充满文件的内容,但操作系统只能检测您是否访问内存页面 - 而不是单个位置。”你能在这里更简单明了吗?无法吸收。谢谢! –
@GauravMinocha操作系统只能检测到整个页面正在被读取(在本例中为4096字节),而不是单个字节地址(RAM中的每个单字节内存都可以被寻址)。因此,文件的整个页面一次加载。 – jwbensley