何时使用管道与何时使用共享内存 [英] When to use Pipes vs When to use Shared Memory

查看:346
本文介绍了何时使用管道与何时使用共享内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读各种IPC机制.我正在尝试找出使用共享内存和使用命名管道(FIFO)的场景.

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).

管道: 可以写入多个进程,但是只能读取一个进程.写操作是原子的.

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 ?

推荐答案

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

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.

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

A minor difference is that fifos are visible directly in the filesystem while shared memory regions need special tools like ipcs for 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).

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

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.

最重要的要点概述:用于一对一通信的管道,更少的编码和让OS处理事情,多对多的共享内存,对事物的更多手动控制,但以更多的工作和更辛苦的代价为代价调试.

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.

这篇关于何时使用管道与何时使用共享内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆