Linux fork inside class on heap

What happens when I have the following situation:

class A: holds dynamically allocated object B. This will create and destroy them.

class B: has an execution function called A. Execution will be fork (), and the child will use execvp to start another process. BUT, the flag can be set so that the parent does not wait for the child (allows it to work in the background).

My question is: what does the plug do in this case? I know that the child has a full copy of the parent process, but I'm a bit confused. Does this mean that the child process has its own object A, which contains B? And what happens if B doesn't wait, but A deletes B?

Here is a sample code. Please note that this is simplified from what I'm actually doing.

class B; class A { public: void addAction( const std::string &name ) { _bq.push( new B( name ) ); } void doActions( bool wait = true ) { while ( !_bq.empty() ) { B* b = _bq.front(); b->execute( wait ); _bq.pop(); delete b; } } ~A() { //omitted, but just deletes everything in queue } private: std::queue<B*> _bq; }; class B { public: B( const std::string &name ) { args.push_back( name.c_str() ); args.push_back( NULL ); } void execute( bool waitForChild ) { pid_t pid = fork(); if ( pid != 0 ) { if (waitForChild) { int status; wait( &status ); // check status... } } else { execvp( args[0], const_cast<char**>( &args[0] ) ); // problem exit( 100 ); } } private: std::vector<char*> args; }; 
+4
source share
3 answers

The child process is completely separate from the parent and has a full copy of the parent variables. When a child executes (calls execve() or one of its relatives), C ++ destructors are not executed. However, this does not affect the parent process.

Thus, there is no interference between the child and the process. It does not matter if the child is expecting a baby or not. As soon as fork() returns (successfully) to the parent process, the child process runs independently, and nothing the parent does for the selected variables will affect the child process.

If you really try and have shared memory and variables distributed in shared memory through the new placement, and if the child goes through clearing the variables into shared memory before calling execvp() or some other similarly far-fetched, but not actually impossible scenario, then the child and parent are not are completely independent. However, if you were doing something complicated, you probably would not ask a question either.

+4
source

When you remake your process, you get the whole copy of the process (although this is usually done using copy-on-write), and β€œwhole copy” means the whole copy; including the various pages that were allocated for this process, so logically there is a whole different copy of the heap and for each thread a copy of this thread and the stack associated with it, so yes, you have two copies of A and B.

The more pressing question, however, when it comes to forking, what happens to threads? Or what happens if you add an "atfork" hook to one of your threads? This is one of the few ways that fork + exec can be quite broken or complicated in a UNIX context. It is generally recommended that you solve this problem using the library or use popen , rather than trying to solve this problem several times around.

+2
source

The fork will copy the heap along with the rest of the process data.

0
source

Source: https://habr.com/ru/post/1340578/


All Articles