Linux 共享内存:shmget() 与 mmap()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21311080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linux shared memory: shmget() vs mmap()?
提问by BowPark
In thisthread the OP is suggested to use mmap()
instead of shmget()
to get shared memory in Linux.
I visited thispage and thispage to get some documentation, but the second one gives an obscure example regarding mmap()
.
在此线程中,建议使用 OPmmap()
而不是shmget()
在 Linux 中获取共享内存。我访问了这个页面和这个页面来获取一些文档,但第二个给出了一个关于mmap()
.
Being almost a newbie, and needing to share some information (in text form) between two processes, should I use the shmget()
method or mmap()
? And why?
几乎是新手,并且需要在两个进程之间共享一些信息(以文本形式),我应该使用该shmget()
方法还是mmap()
?为什么?
采纳答案by Sergey L.
Both methods are viable. mmap
method is a little bit more restrictive then shmget
, but easier to use. shmget
is the old System V shared memory model and has the widest support. mmap
/shm_open
is the new POSIX way to do shared memory and is easier to use. If your OS permits the use of POSIX shared memory then I would suggest going with that.
这两种方法都是可行的。mmap
方法是有点限制然后shmget
,但更容易使用。shmget
是旧的 System V 共享内存模型,拥有最广泛的支持。mmap
/shm_open
是执行共享内存的新 POSIX 方式,并且更易于使用。如果您的操作系统允许使用 POSIX 共享内存,那么我建议您这样做。
Some hints:
一些提示:
- If you create your children via
fork
thenmmap
withMAP_ANONYMOUS | MAP_SHARED
is by far the easiest way - just one call.MAP_ANONYMOUS
is however a Linux extension not specified by POSIX. - If you start the processes independently, but can supply them with a shared memory name then
shm_open
(+ftruncate
) +mmap
withMAP_SHARED
is two/three calls. Requireslibrt
on some OSes. - If your OS has
/dev/shm/
thenshm_open
is equivalent to opening a file in/dev/shm/
.
- 如果您通过
fork
then创建您的孩子,那么mmap
withMAP_ANONYMOUS | MAP_SHARED
是迄今为止最简单的方法 - 只需一个电话。MAP_ANONYMOUS
然而,是POSIX 未指定的 Linux 扩展。 - 如果您独立启动进程,但可以为它们提供共享内存名称,则
shm_open
(+ftruncate
) +mmap
withMAP_SHARED
是两个/三个调用。librt
在某些操作系统上需要。 - 如果你的操作系统有
/dev/shm/
那么shm_open
相当于在/dev/shm/
.
回答by Duck
A lot of this has to do with history and future directions.
这在很大程度上与历史和未来方向有关。
Once upon a time there were two main (and somewhat competing) versions of unix - system V and BSD. SysV had its own versions of IPC, including the big 3 - shared memory, semaphores, and message queues. POSIX came along to try and unite things.
曾几何时,有两个主要的(有些竞争的)unix 版本——system V 和 BSD。SysV 有它自己的 IPC 版本,包括大 3 - 共享内存、信号量和消息队列。POSIX 的出现是为了尝试统一事物。
So at present we have two versions - posix shared memory, MQs, and semaphores and the sysV versions. Just to make things a little more confusing the sysV versions are alsopart of posix.
所以目前我们有两个版本——posix 共享内存、MQ 和信号量以及 sysV 版本。只是为了让事情变得更加混乱,sysV 版本也是posix 的一部分。
So basically your question is do you want to use Posix or sysV style shared memory? In general most people take the long term view and opt for Posix because that seems to be road to the future. But, realistically, sysV stuff is so embedded in so many systems you have to have serious doubts that it will ever go away.
所以基本上你的问题是你想使用 Posix 还是 sysV 风格的共享内存?一般来说,大多数人从长远来看并选择 Posix,因为这似乎是通向未来的道路。但是,实际上,sysV 的东西嵌入到如此多的系统中,您不得不严重怀疑它是否会消失。
So, eliminating the long term stuff, it comes down to what makes sense for your project and your tastes. In general the sysV versions tend to actually be somewhat more powerful but they have an clunky interface that most people find a little bewildering at first contact. The is particularly true of sysV semaphores and message queues. In terms of shared memory it can be argued both sysV and posix are awkward. The sysV versions carries the clunky ftok
and key stuff while the posix ends up taking multiple calls and some race conditions to set up. From the outside, the posix versions have an advantage in that they utilize the file system and can be maintained with standard command line functions like 'rm' rather than relying on separate utility programs (e.g. ipcs
) that sysV requires.
所以,消除长期的东西,归结为对你的项目和你的品味有意义的东西。一般来说,sysV 版本实际上往往更强大一些,但它们有一个笨拙的界面,大多数人在第一次接触时会觉得有点困惑。sysV 信号量和消息队列尤其如此。在共享内存方面,可以认为 sysV 和 posix 都很尴尬。sysV 版本带有笨重ftok
和关键的东西,而 posix 最终需要多次调用和一些竞争条件来设置。从外部看,posix 版本的优势在于它们利用文件系统并且可以使用标准命令行函数(如“rm”)进行维护,而不是依赖于ipcs
sysV 所需的单独的实用程序(例如)。
So which should you use? As a rule, the posix versions. But you should really familiarize yourself with sysV versions. They have some features that go beyond the capabilities of the posix versions which you may want to take advantage of in specific situations.
那么你应该使用哪个?通常,posix 版本。但是您应该真正熟悉 sysV 版本。它们具有一些超出 posix 版本功能的功能,您可能希望在特定情况下利用这些功能。