In Linux, if the parent process has any data structures (e.g., trees, lists), are those data structures inherited by the child? I mean, does the child get access to the same data structure (any kind of pointer to that data structure)?
2 Answers
If you're talking about Linux/Unix processes after a fork()
, yes. They get their own copy of the data of the parent process, so whatever one of them does after the fork is not seen by the other (which is normally implemented by copy-on-write, so the memory pages won't get copied until written to, but that's a detail the user program doesn't see).
If you're talking about Windows starting a new process with CreateProcess()
, no, the new process does not inherit any data structure from the parent.
Both of these have much more to do with which OS you're using than with any specific programming language.

- 10,148
- 7
- 57
- 107

- 9,667
- 2
- 24
- 31
-
(linux), does the child process gets the copy of the data structure with the data or it just get gets it's new data structure(e.g tree). – user2831683 Mar 01 '14 at 21:29
-
If, with "its new data structure", you mean an empty tree while the tree of the parent had some entries - no. The child's data space is an exact copy of the parent's at the time of the fork. So it's a new data structure in the sense that it's a copy, not the original, and if you modify the child processes' copy, the parent won't notice. But until, after the fork, one of the processes changes content, the contents of the data structure will be exactly the same. – Guntram Blohm Mar 01 '14 at 21:32
-
is there any way that the changes made in one reflect in the other – user2831683 Mar 01 '14 at 21:35
-
Yes, for example if you use shared memory (man 2 shmget) or memory maps without setting the MAP_PRIVATE flag (man 2 mmap). – Guntram Blohm Mar 01 '14 at 21:39
Assuming you are using something like fork()
to create the child processes, they'll inherit everything that's global for the actual parent process' context:
- Environment variable settings
- Opened file descriptors
- etc.
Global scope variables will be copied to the child process context from the state they actually are. Changes to these variables will not be reflected in the parent process.
If you want to communicate between parent and child processes, consider using pipes or shared memory.
-
yes fork(); the child process will have access to the same data structures, or it will get a copy of that data structure – user2831683 Mar 01 '14 at 21:31
-
@user2831683 No copies! Just references, take care of race conditions when accessing such data. – πάντα ῥεῖ Mar 01 '14 at 21:33
-
so , if i make some changes in the data structure in child processs then those changes will also be for the parent process – user2831683 Mar 01 '14 at 21:38
-
@user2831683 Sorry, I think I was misinformed about this! Global process data will be copied to the new child process context, and changes won't be seen by the parent. – πάντα ῥεῖ Mar 01 '14 at 21:41