Shmem vs tmpfs vs mmap

SyR*_*ity 11 c++ linux mmap

有人知道以下3个在速度方面的比较:

  • 共享内存

  • tmpfs(/ dev/shm)

  • mmap(/ dev/shm)

谢谢!

Joh*_*itb 8

tmpfs 这里阅读.从该文章中复制以下内容,特别说明了共享内存之间的关系tmpfs.

1) There is always a kernel internal mount which you will not see at
   all. This is used for shared anonymous mappings and SYSV shared
   memory. 

   This mount does not depend on CONFIG_TMPFS. If CONFIG_TMPFS is not
   set the user visible part of tmpfs is not build, but the internal
   mechanisms are always present.

2) glibc 2.2 and above expects tmpfs to be mounted at /dev/shm for
   POSIX shared memory (shm_open, shm_unlink). Adding the following
   line to /etc/fstab should take care of this:

    tmpfs   /dev/shm    tmpfs   defaults    0 0

   Remember to create the directory that you intend to mount tmpfs on
   if necessary (/dev/shm is automagically created if you use devfs).

   This mount is _not_ needed for SYSV shared memory. The internal
   mount is used for that. (In the 2.3 kernel versions it was
   necessary to mount the predecessor of tmpfs (shm fs) to use SYSV
   shared memory)
Run Code Online (Sandbox Code Playgroud)

因此,当您实际使用POSIX共享内存(我之前使用过)时,glibc则会创建一个文件/dev/shm,用于在应用程序之间共享数据.它返回的文件描述符将引用该文件,您可以传递给mmap该文件以告诉它将该文件映射到内存中,就像它可以对任何"真实"文件一样.您列出的技术因此是互补的.他们没有竞争.Tmpfs只是文件系统提供内存中的文件作为实现技术glibc.

作为一个例子,我的盒子上运行的进程当前已经注册了这样一个共享内存对象:

# pwd
/dev/shm
# ls -lh
insgesamt 76K
-r-------- 1 js js 65M 24. Mai 16:37 pulse-shm-1802989683
#
Run Code Online (Sandbox Code Playgroud)