带有 NULL 地址的 mmap 如何工作?

How mmap with NULL addr works?

指定当使用mmapNULL addr时,内核选择创建映射的(页面对齐的)地址:

mmap() creates a new mapping in the virtual address space of the calling process. The starting address for the new mapping is specified in addr. The length argument specifies the length of the mapping (which must be greater than 0). If addr is NULL, then the kernel chooses the (page-aligned) address at which to create the mapping; this is the most portable method of creating a new mapping.

假设我有以下代码:

void (*x)(void);
void (*y)(void);
x=mmap(NULL, 0x500, PROT..., FLAGS..., FD..., 0);
y=mmap(NULL, 0x500, PROT..., FLAGS..., FD..., 0);

是否意味着 y 将在 x 之后紧随其后?

谢谢。

不,它没有说明映射的相对定位,因此您不能对此做出任何假设。将每个映射与所有其他映射完全分开。