Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3992130
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:43:43+00:00 2026-05-20T06:43:43+00:00

What happens when I have the following situation: class A: holds on to dynamically

  • 0

What happens when I have the following situation:

class A: holds on to dynamically allocated object B’s. It will create and destroy these.

class B: has an execute function called by A. Execute will fork() and the child will use execvp to run another process. BUT, a flag can be set so that the parent will not wait for the child (allows it to run in the background).

My question is, what is fork doing in this case? I know that the child has a complete copy of the process of the parent, but I’m a little confused. So does that mean that the child process has its own object A which holds the B? And what happens if B is not waiting, but A deletes the B?

Here is some sample code. Note that it is simplified from what I am 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;
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:43:43+00:00Added an answer on May 20, 2026 at 6:43 am

    The child process is completely separate from the parent, and has a complete copy of the parent’s variables. When the child executes (calls execve() or one of its relatives), no C++ destructors are executed. However, this has no effect whatsoever on the parent process.

    So, there is no interference between the child and the process. It does not matter whether the parent waits for the child or not. As soon as the fork() returns (successfully) to the parent process, the child is running independently and nothing that the parent does to allocated variables will affect the child.

    If you really try hard and have shared memory and variables allocated into shared memory via placement new and if the child goes cleaning variables up in shared memory before calling execvp(), or some other similarly far-fetched but not actually impossible scenario, then the child and parent are not completely independent. However, if you were doing something as complex as that, you probably would not be asking the question, either.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following situation: typedef void (*F_POINTER)(ClassName*); class ClassName { public: F_POINTER f;
If I have the following situation: Execute() creates a new thread and executes function
I have the following issue and I would like to know what exactly happens.
I have a property on a class that happens to be a Table<T> .
I have the following situation: .net 3.5 WinForm client app accessing SQL Server 2008
The setting is the following: I have a cocoa object in a nib file
I came into a really strange situation. I have the following setup: Parent window
I have experience with object-oriented programming however this situation is unfamiliar for some reason.
I have the following situation: in the main file I do setContentView(R.layout.main); which looks
could somebody please explain why this happens? I have written the following: 1) a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.