例如,如果我这样做:
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上工作,有人知道这种可移植性可能有多大吗?
非常感谢您的帮助。