Linux 何时使用管道 vs 何时使用共享内存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9701757/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:10:42  来源:igfitidea点击:

When to use Pipes vs When to use Shared Memory

linuxipcpipeshared-memory

提问by vamsi

I am reading about various IPC mechanism. I am trying to figure out the scenarios, where we use Shared Memory and where we use named Pipes(FIFO).

我正在阅读各种 IPC 机制。我试图弄清楚我们使用共享内存和使用命名管道(FIFO)的场景。

Pipes: Multiple process can Write, however only one process can read. Write operation is atomic.

管道:多个进程可以写,但只有一个进程可以读。写操作是原子的。

Shared Memory: Multiple process can read and write. And also user needs to provide mutual exclusion for read & write.

共享内存:多个进程可以读写。并且用户还需要提供读写互斥。

Is this the only difference of application of shared memory and pipe ?

这是应用共享内存和管道的唯一区别吗?

采纳答案by Micha? Kosmulski

Essentially, pipes - whether named or anonymous - are used like message passing. Someone sends a piece of information to the recipient and the recipient can receive it. Shared memory is more like publishing data - someone puts data in shared memory and the readers (potentially many) must use synchronization e.g. via semaphores to learn about the fact that there is new data and must know how to read the memory region to find the information.

本质上,管道 - 无论是命名的还是匿名的 - 都像消息传递一样使用。有人向接收者发送一条信息,接收者可以接收到它。共享内存更像是发布数据——有人将数据放在共享内存中,读者(可能很多)必须使用同步,例如通过信号量来了解有新数据的事实,并且必须知道如何读取内存区域以查找信息.

With pipes the synchronization is simple and built into the pipe mechanism itself - your reads and writes will freeze and unfreeze the app when something interesting happens. With shared memory, it is easier to work asynchronously and check for new data only once in a while - but at the cost of much more complex code. Plus you can get many-to-many communication but it requires more work again. Also, due to the above, debugging of pipe-based communication is easier than debugging shared memory.

使用管道,同步很简单,并且内置于管道机制本身 - 当发生有趣的事情时,您的读取和写入将冻结和解冻应用程序。使用共享内存,异步工作更容易,并且只偶尔检查一次新数据 - 但代价是代码更复杂。此外,您可以获得多对多通信,但它又需要更多的工作。此外,由于上述原因,调试基于管道的通信比调试共享内存更容易。

A minor difference is that fifos are visible directly in the filesystem while shared memory regions need special tools like ipcsfor their management in case you e.g. create a shared memory segment but your app dies and doesn't clean up after itself (same goes for semaphores and many other synchronization mechanisms which you might need to use together with shared memory).

一个微小的区别是,fifos 直接在文件系统中可见,而共享内存区域需要特殊的工具ipcs来管理它们,例如,如果您创建了一个共享内存段,但您的应用程序死了并且不会自行清理(信号量和您可能需要与共享内存一起使用的许多其他同步机制)。

Shared memory also gives you more control over bufferring and resource use - within limits allowed by the OS it's you who decides how much memory to allocate and how to use it. With pipes, the OS controls things automatically, so once again you loose some flexibility but are relieved of much work.

共享内存还使您可以更好地控制缓冲和资源使用 - 在操作系统允许的限制内,您可以决定分配多少内存以及如何使用它。使用管道,操作系统会自动控制事物,因此您再次失去了一些灵活性,但可以减轻很多工作。

Summary of most important points: pipes for one-to-one communication, less coding and letting the OS handle things, shared memory for many-to-many, more manual control over things but at the cost of more work and harder debugging.

最重要的要点总结:用于一对一通信的管道、更少的编码并让操作系统处理事物、用于多对多的共享内存、对事物的更多手动控制,但代价是更多的工作和更难的调试。