什么时候使用mmap map_fixed [英] When would one use mmap map_fixed

查看:536
本文介绍了什么时候使用mmap map_fixed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找mmap函数的不同标志,即MAP_FIXED,MAP_SHARED和MAP_PRIVATE.有人可以向我解释MAP_FIXED的目的吗?无法保证首先使用地址空间.

I've been looking at the different flags for the mmap function, namely MAP_FIXED, MAP_SHARED, MAP_PRIVATE. Can someone explain to me the purpose of MAP_FIXED? There's no guarantee that the address space will be used in the first place.

推荐答案

MAP_FIXEDdup2用于内存映射,在dup2对于文件描述符有用的完全相同的情况下,它很有用:执行 replace 操作,该操作原子地重新分配资源标识符(对于MAP_FIXED,为内存范围,对于dup2为fd),以引用新资源,而不会出现种族冲突如果您首先释放了旧资源,然后尝试为新资源重新获得它,则它可能会被重新分配给其他资源.

MAP_FIXED is dup2 for memory mappings, and it's useful in exactly the same situations where dup2 is useful for file descriptors: when you want to perform a replace operation that atomically reassigns a resource identifier (memory range in the case of MAP_FIXED, or fd in the case of dup2) to refer to a new resource without the possibility of races where it might get reassigned to something else if you first released the old resource then attempted to regain it for the new resource.

作为示例,以加载共享库(通过动态加载器)为例.它至少包含三种类型的映射:程序代码的只读+ exec-only映射和可执行文件中的只读数据,初始化数据(也来自可执行文件,但通常具有不同的映射)的已初始化数据的读写映射.相对偏移量)和可读写的零初始化匿名内存(用于.bss).将它们创建为单独的映射将不起作用,因为它们必须相对于彼此位于固定的相对地址上.因此,取而代之的是,首先在没有MAP_FIXED的情况下对所需的总长度进行虚拟映射(此映射的类型无关紧要),只是在内核分配的位置保留足够范围的连续地址,然后使用MAP_FIXED可以根据需要使用您需要创建的三个或更多映射来映射到该范围的顶部.

As an example, take loading a shared library (by the dynamic loader). It consists of at least three types of mappings: read+exec-only mapping of the program code and read-only data from the executable file, read-write mapping of the initialized data (also from the executable file, but typically with a different relative offset), and read-write zero-initialized anonymous memory (for .bss). Creating these as separate mappings would not work because they must be at fixed relative addresses relative to one another. So instead you first make a dummy mapping of the total length needed (the type of this mapping doesn't matter) without MAP_FIXED just to reserve a sufficient range of contiguous addresses at a kernel-assigned location, then you use MAP_FIXED to map over top of parts of this range as needed with the three or more mappings you need to create.

此外,请注意,将MAP_FIXED与硬编码地址或随机地址一起使用始终是一个错误.使用MAP_FIXED的唯一正确方法是替换现有映射,该映射的地址是由先前成功调用mmap而不分配MAP_FIXED分配的,或者以其他可以安全替换整个页面的方式.这方面也与dup2完全相似.当调用方尚未在目标fd上具有要替换文件的打开文件时,使用dup2总是一个错误.

Further, note that use of MAP_FIXED with a hard-coded address or a random address is always a bug. The only correct way to use MAP_FIXED is to replace an existing mapping whose address was assigned by a previous successful call to mmap without MAP_FIXED, or in some other way where you feel it's safe to replace whole pages. This aspect too is completely analogous to dup2; it's always a bug to use dup2 when the caller doesn't already have an open file on the target fd with the intent to replace it.

这篇关于什么时候使用mmap map_fixed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆