2009-05-24 60 views

回答

8

阅读tmpfshere。以下内容是从该文章中复制的,特别解释了共享内存与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 
# 
-1

tmpfs是最慢的。共享内存和mmap的速度相同。

+2

请你解释一下......谢谢 – hhafez 2009-05-24 21:17:03

+1

事实上,似乎tmpfs的实际权力共享记忆?而且我认为,使用tmpfs作为底层传输时,mmap速度很快? – SyRenity 2009-05-25 09:19:11

2

“这取决于”。一般来说,它们都是内存中的,并且依赖于系统实现,所以性能将可以忽略不计,并且可用于大多数平台。如果你真的关心绩效,你应该分析和确定你的要求。将这些方法中的任何一种替换为另一种方法都很简单。这就是说,共享内存是最不密集的,因为没有涉及任何文件操作(但又是非常依赖实现的)。如果您需要反复打开和关闭(map/unmap),则可能会产生大量开销。

干杯!
肖恩

1

通过“共享内存”你的意思是系统V共享内存,对不对?

我觉得Linux mmap是一个隐藏的tmpfs,当你使用这个,所以它实际上和mmpping一个tmpfs是一样的。

做文件I /在tmpfs上O时将有一个点球......大部分(有特殊情况下,它可能是有意义的,比如> 4G在32位进程)

+0

如何?如果mmap使用共享内存,那么性能应该是相同的? – SyRenity 2009-05-25 09:19:02

相关问题