共享内存和IPC

发布于 2024-10-26 20:35:43 字数 1435 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吃兔兔 2024-11-02 20:35:43

这里的区别在于信令与共享状态的 IPC 机制。

信令(信号、消息队列、管道等)适用于往往简短、及时且可靠的信息。指导。这些机制上的事件往往会唤醒或中断另一个程序。打个比方,“一个人会向另一个人发送什么短信?”

  • 嘿,我向哈希表添加了一个新条目!
  • 嘿,我完成了你让我做的工作!
  • 嘿,这是我的猫的照片。他不是很可爱吗?
  • 嘿,今晚你想出去吗?有一个新的地方叫做硬盘。

共享内存与上述相比,对于共享相对较大、稳定且小部分变化或重复读取的对象更有效。程序可能会不时或在收到其他信号后查阅共享内存。考虑一下,一系列程序会在自家厨房的(大)白板上写什么?

  • 我们最喜欢的食谱。
  • 我们知道的事情。
  • 我们朋友的电话号码和其他联系信息。
  • 我们家族辉煌历史的最新手稿,按服刑时间整理。

通过这些示例,您可能会说,从严格意义上讲,共享内存更接近文件,而不是 IPC 机制,但明显的例外是共享内存是

  1. 随机访问,而文件是顺序访问。
  2. 易失性,而文件往往能在程序崩溃时幸存下来。

The distinction here is IPC mechanisms for signalling versus shared state.

Signalling (signals, message queues, pipes, etc.) is appropriate for information that tends to be short, timely and directed. Events over these mechanisms tend to wake up or interrupt another program. The analogy would be, "what would one program SMS to another?"

  • Hey, I added a new entry to the hash table!
  • Hey, I finished that work you asked me to do!
  • Hey, here's a picture of my cat. Isn't he cute?
  • Hey, would you like to go out, tonight? There's this new place called the hard drive.

Shared memory, compared with the above, is more effective for sharing relatively large, stable objects that change in small parts or are read repeatedly. Programs might consult shared memory from time to time or after receiving some other signal. Consider, what would a family of programs write on a (large) whiteboard in their home's kitchen?

  • Our favorite recipes.
  • Things we know.
  • Our friends' phone numbers and other contact information.
  • The latest manuscript of our family's illustrious history, organized by prison time served.

With these examples, you might say that shared memory is closer to a file than to an IPC mechanism in the strictest sense, with the obvious exceptions that shared memory is

  1. Random access, whereas files are sequential.
  2. Volatile, whereas files tend to survive program crashes.
能怎样 2024-11-02 20:35:43

您需要共享内存的一个示例是共享哈希表(或 btree 或其他复合结构)。您可以让每个进程接收更新消息并更新结构的私有副本,或者您可以将哈希表存储在共享内存中并使用信号量进行锁定。

An example of where you want shared memory is a shared hash table (or btree or other compound structure). You could have every process receive update messages and update a private copy of the structure, or you can store the hash table in shared memory and use semaphores for locking.

何必那么矫情 2024-11-02 20:35:43

共享内存非常快——这是您使用它的主要优点和原因。您可以使用部分内存来保存有关数据有效性的标志/时间戳,但如果您想避免轮询共享内存,则可以使用其他形式的 IPC 来发出信号。

Shared memory is very fast - that is the main advantage and reason you would use it. You can use part of the memory to keep flags/timestamps regarding the data validity, but you can use other forms of IPC for signaling if you want to avoid polling the shared memory.

勿忘初心 2024-11-02 20:35:43

共享内存用于在进程之间传输数据(以及快速读/写磁盘文件)。如果您不需要传输数据而只需要通知其他进程,请不要使用共享内存 - 请改用其他通知机制(信号量、事件等)。

Shared memory is used to transfer the data between processes (and also to read/write disk files fast). If you don't need to transfer the data and need to only notify other process, don't use shared memory - use other notification mechanisms (semaphores, events, etc) instead.

远昼 2024-11-02 20:35:43

根据从一个进程传递到另一个进程的数据量,共享内存会更有效,因为您可以最大限度地减少数据从用户态内存复制到内核内存并返回用户态内存的次数。

Depending on the amount of data to be passed from process to process, shared memory would be more efficient because you would minimize the number of times that data would be copied from userland memory to kernel memory and back to userland memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文