0

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
  • Read up on Virtual Memory. On a modern system each process has its own virtual memory space. Shared memory will be mapped into the virtual memory space. – user4581301 Jan 23 '23 at 22:39
  • Side note: The C++ Standard doesn't cover any of this stuff. It abstracts away everything about what memory is and where it comes from. The notion of stacks and heaps isn't even discussed. As far as C++ is concerned, this could all be Elf magic powered by standing stones and mushroom circles so long as the Standard's rules are followed. – user4581301 Jan 23 '23 at 22:44
  • ***Does a parent process share the same heap with its child process?*** Most likely not. Each process should have its own virtual address space. Although I can't answer for every OS there is. – drescherjm Jan 23 '23 at 22:46
  • The whole first paragraph is not correct or not usually the case in modern OSs. Maybe it was the case some time long ago in the past but I don't believe this is a common scenario today for a desktop, server or even a mobile OS. – drescherjm Jan 23 '23 at 22:55

1 Answers1

0

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?

The child process gets a copy of parent heap. The page frames can be shared between the processes in copy-on-write mode as an optimization.

man fork:

fork() creates a new process by duplicating the calling process. The new process is referred to as the child process. The calling process is referred to as the parent process.

The child process and the parent process run in separate memory spaces. At the time of fork() both memory spaces have the same content. Memory writes, file mappings (mmap(2)), and unmappings (munmap(2)) performed by one of the processes do not affect the other.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271