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
Random access, whereas files are sequential.
Volatile, whereas files tend to survive program crashes.
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.
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.
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.
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.
发布评论
评论(5)
这里的区别在于信令与共享状态的 IPC 机制。
信令(信号、消息队列、管道等)适用于往往简短、及时且可靠的信息。指导。这些机制上的事件往往会唤醒或中断另一个程序。打个比方,“一个人会向另一个人发送什么短信?”
共享内存与上述相比,对于共享相对较大、稳定且小部分变化或重复读取的对象更有效。程序可能会不时或在收到其他信号后查阅共享内存。考虑一下,一系列程序会在自家厨房的(大)白板上写什么?
通过这些示例,您可能会说,从严格意义上讲,共享内存更接近文件,而不是 IPC 机制,但明显的例外是共享内存是
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?"
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?
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
您需要共享内存的一个示例是共享哈希表(或 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.
共享内存非常快——这是您使用它的主要优点和原因。您可以使用部分内存来保存有关数据有效性的标志/时间戳,但如果您想避免轮询共享内存,则可以使用其他形式的 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.
共享内存用于在进程之间传输数据(以及快速读/写磁盘文件)。如果您不需要传输数据而只需要通知其他进程,请不要使用共享内存 - 请改用其他通知机制(信号量、事件等)。
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.
根据从一个进程传递到另一个进程的数据量,共享内存会更有效,因为您可以最大限度地减少数据从用户态内存复制到内核内存并返回用户态内存的次数。
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.