As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center提供指导。




7年前关闭。




有人知道以下3种在速度方面的比较吗?
  • 共享内存
  • tmpfs(/dev/shm)
  • mmap(/dev/shm)

  • 谢谢!

    最佳答案

    了解有关tmpfs here的信息。以下内容摘自该文章,特别说明了共享内存和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)
    

    因此,当您实际使用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
    #
    

    08-05 10:38