例如,如果我这样做:
char *pMap1; /* First mapping */
char *pReq; /* Address we would like the second mapping at */
char *pMap2; /* Second mapping */
/* Map the first 1 MB of the file. */
pMap1 = (char *)mmap(0, 1024*1024, PROT_READ, MAP_SHARED, fd, 0);
assert( pMap1!=MAP_FAILED );
/* Now map the second MB of the file. Request that the OS positions the
** second mapping immediately after the first in virtual memory. */
pReq = pMap1 + 1024*1024;
pMap2 = (char *)mmap(pReq, 1024*1024, PROT_READ, MAP_SHARED, fd, 1024*1024);
assert( pMap2!=MAP_FAILED );
/* Unmap the mappings created above. */
if( pMap2==pReq ){
munmap(pMap1, 2 * 1024*1024);
}else{
munmap(pMap1, 1 * 1024*1024);
munmap(pMap2, 1 * 1024*1024);
}
并且操作系统确实将我的第二个映射放在请求的位置(以便 (pMap2==pReq) 条件为真),对 munmap() 的单次调用是否会释放所有分配的资源?
Linux 手册页说“munmap() 系统调用删除了指定地址范围的映射...”,这表明这将起作用,但我仍然对此有点紧张。即使它确实可以在 Linux 上运行,是否有人知道它的可移植性如何?
首先十分感谢。