1

I understand that stack memory can only be shared by threads within same process.

In Inter-Process Communication, processes can share same segment of memory via shmget() system call.

What can this shared memory segment be? a heap or anything else?

Update:

I came up with this question after browsing questions about difference between stack and heap memory. Could heap memory be the shared memory segment via shmget()? That is, could heap memory be shared among multiple processes?

Update II:

Does a parent process share the same heap with its child process? I find something online: "The heap, code and library regions of the parent are shared by the child. A new stack is allocated to the child and the parent's stack is copied into the child's stack."

Does this mean same heap is shared between difference processes?

"Also there might be a global heap (look at Win32 GlobalAlloc() family functions for example) which is shared between processes, persists for the system runtime and indeed can be used for interprocess communications." reference: Is heap memory per-process? (or) Common memory location shared by different processes?

thinkdeep
  • 945
  • 1
  • 14
  • 32
  • Stack and heap are memory managed by/for a specific process. Shared memory has to be accessible by multiple processes. Hence, I don't think it fits into stack or heap but is memory which is managed by the OS. – Scheff's Cat Jan 15 '23 at 18:46
  • "Stack" and "Heap" are just concepts. It all comes down to bits and bytes. Feel free to invent your own term for wherever shared memory actually exists. How about: it exists in "Ether" memory? – Sam Varshavchik Jan 15 '23 at 18:48
  • 1
    @SamVarshavchik Ether memory was disproved at the same time that pointer arithmetic was discovered! (true story) – Captain Giraffe Jan 15 '23 at 18:56
  • 1
    @SamVarshavchik the problem with inventing your own terms for things is that nobody will know what you are talking about when you use your new terms :/ – Jeremy Friesner Jan 15 '23 at 19:13
  • 2
    Well, @JeremyFriesner, it's been my experience that I don't need to invent my own terms for that to happen... – Sam Varshavchik Jan 15 '23 at 19:20
  • Actually, stack is per thread and heap is per process. If 2 threads use the same stack, they'll return to functions that another thread has called and initialize variables of that other thread (without mentioning complete stack destruction because of the wrong increments of stack registers). The heap is per process and dynamic. The kernel returns pointers to the thread and the thread is responsible to not write outside its allocated region. To write to another thread's memory region, it needs to access its pointers. – user123 Jan 15 '23 at 22:08
  • Threads pass pointers to their respective allocated region of the heap to share heap memory. They need to synchronize their accesses to avoid concurrent access to the same memory which would leave the thread in an unknown state. – user123 Jan 15 '23 at 22:08
  • Thank you for the discussion. I've updated the question in "Update II" – thinkdeep Jan 16 '23 at 18:40

1 Answers1

5

In the Unix operating system, shared memory lives outside of any individual process space. By using shmat you basically get a pointer to some space that the kernel has allocated for you. These spaces can be shared between process and attached to by any number of processes. Like a file you can set permissions so that not every process can see and/or attach to the memory.

In this context shared memory is not a traditional stack or heap - it's a chunk of memory that the kernel gives you access to (assuming the correct permissions). Again, it lives outside of any one process space as the kernel manages it. Usually the memory remains in use even if no processes are attached to it. In Linux, an ipcs -m shows these segments.

stdunbar
  • 16,263
  • 11
  • 31
  • 53
  • thank you so much for such detailed answer. 1) Is this shared memory somewhere in RAM? 2) can a heap memory be a shared memory? – thinkdeep Jan 15 '23 at 19:02
  • 1
    Generally yes, the memory will be in RAM somewhere. Depending on a variety of factors it may be subject to be swapped to disk but that would be transparent to the user. Heap memory is associated with a process so no, share memory is a different from the heap. It is a totally different category from stack and heap. – stdunbar Jan 15 '23 at 19:06
  • Thanks for such detailed answer. would you mind taking a look at "Update II" in the question? – thinkdeep Jan 16 '23 at 18:41
  • 1
    @thinkdeep - you've really got at least 3 questions now. It might be better to ask new questions rather than appending to this one. – stdunbar Jan 16 '23 at 18:53
  • Sure, I've posted a new one. Would you mind taking a look? https://stackoverflow.com/questions/75215763/same-heap-is-shared-between-difference-processes – thinkdeep Jan 23 '23 at 22:37
  • Your answer to this question is accepted! – thinkdeep Jan 23 '23 at 22:37