Shared memory and IPC [closed]

只愿长相守 提交于 2020-01-01 09:45:47

问题


I was reading a tutorial about shared memory and found the following statement: "If a process wishes to notify another process that new data has been inserted to the shared memory, it will have to use signals, message queues, pipes, sockets, or other types of IPC.". So what is the main advantage of using shared memory and other type of IPC for notifying only instead of using an IPC that doesn't need any other IPC type to be used, like message queue and socket for example?


回答1:


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.



回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/5436730/shared-memory-and-ipc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!