2009-05-24 67 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