本文介绍了什么是内存映射页和匿名页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解 linux 中的内存映射页面和匿名页面.有人可以用一个例子来解释它吗?与它们相关的内核数据结构是什么?

I am not able to understand memory mapped page and anonymous page in linux. Can someone please explain it with an example? What are the kernel data structures related to them?

推荐答案

正确的术语是内存映射文件和匿名映射.当提到内存映射时,通常指的是 mmap(2).使用 mmap 有 2 个类别.一类是共享映射与私有映射.另一个类别是文件与匿名映射.混合在一起,您将获得以下 4 种组合:

The correct terms are memory mapped files and anonymous mappings. When referring to memory mapping, one is usually referring to mmap(2). There are 2 categories for using mmap. One category is SHARED vs PRIVATE mappings. The other category is FILE vs ANONYMOUS mappings. Mixed together you get the 4 following combinations:

  1. 私人文件映射
  2. 共享文件映射
  3. 私人匿名映射
  4. 共享匿名映射

文件映射指定磁盘上的一个文件,它将有 N 个字节映射到内存中.函数 mmap(2) 将要映射到内存的文件的文件描述符作为其第四个参数.第 5 个参数是要读入的字节数,作为偏移量.使用mmap创建内存映射文件的典型流程是

A File Mapping specifies a file, on disk, that will have N many bytes mapped into memory. The function mmap(2) takes, as its 4th argument a file descriptor to the file to be mapped into memory. The 5th argument is the number of bytes to be read in, as an offset. The typical process of using mmap to create a memory mapped file goes

  1. open(2) 文件以获取文件描述符.
  2. fstat(2) 从文件描述符数据结构中获取文件大小.
  3. mmap(2) 使用从 open(2) 返回的文件描述符来映射文件.
  4. 关闭(2) 文件描述符.
  5. 对内存映射文件执行任何操作.

当文件被映射为 PRIVATE 时,所做的更改不会提交到基础文件.它是文件的私有内存副本.当文件被映射为 SHARED 时,内核会自动将所做的更改提交给底层文件.映射为共享的文件可用于所谓的内存映射 I/O 和 IPC.如果您需要文件的持久性,您可以为 IPC 使用内存映射文件而不是共享内存段

When a file is mapped in as PRIVATE, changes made are not committed to the underlying file. It is a PRIVATE, in-memory copy of the file. When a file is mapped SHARED, changes made are committed to the underlying file by the kernel automatically. Files mapped in as shared can be used for what is called Memory Mapped I/O, and IPC. You would use a memory mapped file for IPC instead of a shared memory segment if you need the persistence of the file

如果您使用 strace(1) 观察进程初始化,您会注意到文件的不同部分使用 mmap(2) 作为私有文件映射进行映射.系统库也是如此.

If you use strace(1) to watch a process initialize, you will notice that the different sections of the file are mapped in using mmap(2) as private file mappings. The same is true for system libs.

strace(1) 的输出示例,其中 mmap(2) 用于在库中映射到进程.

Examples of output from strace(1) where mmap(2) is being used to map in libraries to the process.

open("/etc/ld.so.cache", O_RDONLY)      = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=42238, ...}) = 0
mmap(NULL, 42238, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7ff7ca71e000
close(3)                                = 0
open("/lib64/libc.so.6", O_RDONLY)      = 3
read(3, "177ELF21133>1p356341n8"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1926760, ...}) = 0
mmap(0x386ee00000, 3750152, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x386ee00000

匿名映射不受文件支持.具体来说,当 MAP_ANONYMOUS 标志用作 mmap(2) 的第三个参数时,甚至不使用 mmap(2) 的第 4 个(文件描述符)和第 5 个(偏移量)参数.使用 MAP_ANONYMOUS 标志的替代方法是使用/dev/zero 作为文件.

Anonymous mappings are not backed by a file. To be specific, the 4th (file descriptor) and 5th (offset) argument of mmap(2) are not even used when the MAP_ANONYMOUS flag is used as the 3rd argument to mmap(2). An alternative to using the MAP_ANONYMOUS flag is to use /dev/zero as the file.

匿名"这个词对我来说是一个糟糕的选择,因为它听起来好像文件是匿名映射的.相反,它是匿名的文件,即.没有指定文件.

The word 'Anonymous' is, to me, a poor choice in that it sounds as if the file is mapped anonymously. Instead, it is the file that is anonymous, ie. there isn't a file specified.

在用户域编程中很少使用私有匿名映射.您可以使用共享匿名映射,以便应用程序可以共享内存区域,但我不知道为什么不使用 SYSV 或 POSIX 共享内存.

Uses for private anonymous mappings are few in user land programming. You could use a shared anonymous mapping so that applications could share a region of memory, but I do not know the reason why you wouldn't use SYSV or POSIX shared memory instead.

由于使用匿名映射映射的内存保证为零填充,因此对于某些期望/需要零填充内存区域的应用程序以这种方式使用 mmap(2) 而不是 malloc(2) +memset(2) 组合.

Since memory mapped in using Anonymous mappings is guaranteed to be zero filled, it could be useful for some applications that expect/require zero filled regions of memory to use mmap(2) in this way instead of the malloc(2) + memset(2) combo.

这篇关于什么是内存映射页和匿名页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 14:12