PinkElephantsOnParade
PinkElephantsOnParade

Reputation: 6592

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

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?

Upvotes: 6

Views: 2437

Answers (2)

jclin
jclin

Reputation: 2559

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.

Upvotes: 4

Linuxios
Linuxios

Reputation: 35803

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.

Upvotes: 2

Related Questions