linux munmap 不工作(或者至少不能立即工作)

标签 linux memory memory-management

我调用munmap没有错误,但映射仍然可见 /proc/<pid>/maps当我尝试 mmap(address_overlapping_with_what_I_tried_to_munmap, ...)我没有得到我请求的地址。

这是错误还是功能?我可以做些什么来确保未映射某些内容吗?

详细信息:32 位 Linux 4.1.18

编辑

最初, map 条目是

bfe50000-bfe71000 rw-p 00000000 00:00 0

然后我调用munmap带参数 0xbfe50000, 0x21000

入口是

bfe50000-bfe50000 rw-p 00000000 00:00 0

最佳答案

when I tried to mmap(address_overlapping_with_what_I_tried_to_munmap, ...) I did not get the address I requested.

再次仔细阅读mmap(2):

If addr is NULL, then the kernel chooses the address at which to create the mapping; this is the most portable method of creating a new mapping. If addr is not NULL, then the kernel takes it as a hint about where to place the mapping

及以后:

MAP_FIXED Don't interpret addr as a hint: place the mapping at exactly that address. addr must be a multiple of the page size.

因此,如果您确实需要将地址作为有用地址传递给 mmap,您应该使用 MAP_FIXED(谨慎,因为它可以覆盖并“粉碎”一个非常有用的预先存在的内存映射。

换句话说,我知道 mmap 的地址最好是 0,除非你说 MAP_FIXED。否则,它只是一个“提示”,我不知道这实际上意味着什么(尤其是 ASLR )。

AFAIU,/proc/self/maps/proc/1234/maps 反射(reflect)瞬间内核对您进程 virtual address space 的感知。当然,您需要打开那个伪文件,快速顺序阅读,然后尽快关闭它(不要将文件描述符保存到open-ed /proc/self/maps 几秒钟)。如果您的进程在 open/proc/*/maps 文件的 close 是恕我直言 undefined behavior - 或者至少是 unspecified behavior - 你不应该依赖它。

但是 mmapmunmap 在该系统调用返回后立即生效;更改虚拟地址空间没有“延迟”。虚拟内存机制可能会产生一些延迟(例如,当从某个远程网络获取某些页面时......)但应用程序无法访问该延迟(进程将处于 D 状态)。另请阅读有关 thrashing 的信息。

关于linux munmap 不工作(或者至少不能立即工作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36508965/

相关文章:

android - 为什么 fresco 在 android 5.0 或更高版本上选择不将位图放入 ashmem

.net - jconsole 相当于.Net?

iphone - 下载图像时出现内存警告和应用程序崩溃

java - "Background concurrent copying GC freed"日志消息后空闲时应用程序卡住

c++ - 编写自己的内存管理器

java - 无法创建Java虚拟机

php - 为什么 Linux 命令 dmidecode 在 exec 中使用时不返回任何内容?

linux - shell脚本-bump版本自动git

android - 将 Android 开发人员从 Linux 过渡到 Windows : toolchain signes APK differently?

ios - 字典、自动释放池和临时对象