What's the difference between shared memory for IPCs and threads' shared memory?

耗尽温柔 提交于 2019-12-14 01:33:43

问题


Let's use POSIX shared memory like shmget() - a common call to coordinate inter-process communication. How is calling shmget() and coordinating communication on the shared memory segment any different than how Linux implements shared memory and synchronization between threads in a single process. Is one of them more light-weight?


回答1:


SHM is for IPC in multiple processes. In modern OS, each process cannot see each others' memory space. Using common key for shmget() to get the share memory and using shmat() to map share memory page to local memory address inside each process. The mapped shared memory address might be different due to different memory usage and shared libraries loaded into each process space. And the SHM key, size are predefined and fixed among those process.

For threads' memory, we might not call it shared memory because threads are all in a single process memory space addressing. They can see and read/write in the same process space.




回答2:


Honestly, not much. On Linux, there are no OS level threads. One process, one thread. So when you use pthreads, you are actually using multiple processes that share all memory besides thread specific storage areas. On a different UNIX, such as OSX, though, this may not be the case. But you can see this for yourself you you make a simple pthreads process, background it, and type ps at the shell prompt.



来源:https://stackoverflow.com/questions/13345059/whats-the-difference-between-shared-memory-for-ipcs-and-threads-shared-memory

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