Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a child process inherit the data structures of the parent process?

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)?

like image 299
user2831683 Avatar asked Dec 15 '22 01:12

user2831683


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.

like image 198
Guntram Blohm Avatar answered Dec 28 '22 10:12

Guntram Blohm


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.

like image 40
πάντα ῥεῖ Avatar answered Dec 28 '22 08:12

πάντα ῥεῖ