消息队列和共享内存的区别? [英] difference between message queue and shared memory?

查看:209
本文介绍了消息队列和共享内存的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了很多关于消息队列和共享内存之间差异的文章.但仍不清楚哪一个有利于实现良好的性能.

I read a lot of articles about differences between message queue and shared memory. But still not clear which one is good for achieving good performance.

像共享内存一样被认为比队列好,但在同步它的情况下也有性能问题.

Like shared memory are suppose to be good over queues but that also has performance issue in case of synchronizing it.

推荐答案

共享内存和消息队列都可以用来在进程间交换信息.区别在于如何使用它们.

Both shared memory and message queues can be used to exchange information between processes. The difference is in how they are used.

共享内存正是您所想的:它是一个可以由多个进程读取和写入的存储区域.它不提供固有的同步;换句话说,由程序员来确保一个进程不会破坏另一个进程的数据.但它在吞吐量方面是高效的:读取和写入是相对较快的操作.

Shared memory is exactly what you'd think: it's an area of storage that can be read and written by more than one process. It provides no inherent synchronization; in other words, it's up to the programmer to ensure that one process doesn't clobber another's data. But it's efficient in terms of throughput: reading and writing are relatively fast operations.

消息队列是一种单向管道:一个进程写入队列,另一个进程按照写入的顺序读取数据,直到出现数据结束条件.创建队列时,会设置消息大小(每条消息的字节数,通常相当小)和队列长度(待处理消息的最大数量).访问比共享内存慢,因为每个读/写操作通常是一条消息.但是队列保证每个操作要么成功处理整个消息,要么失败而不改变队列.所以写者永远不会在只写部分消息后失败,而读者要么检索完整的消息,要么什么都不检索.

A message queue is a one-way pipe: one process writes to the queue, and another reads the data in the order it was written until an end-of-data condition occurs. When the queue is created, the message size (bytes per message, usually fairly small) and queue length (maximum number of pending messages) are set. Access is slower than shared memory because each read/write operation is typically a single message. But the queue guarantees that each operation will either processes an entire message successfully or fail without altering the queue. So the writer can never fail after writing only a partial message, and the reader will either retrieve a complete message or nothing at all.

这篇关于消息队列和共享内存的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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