Linux在堆上的类中派生 [英] Linux fork within class on heap

查看:54
本文介绍了Linux在堆上的类中派生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我遇到以下情况时会发生什么:

What happens when I have the following situation:

A类:保留动态分配的对象B.它将创造并摧毁它们.

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

类B:具有由A调用的执行函数.Execute将fork(),并且子级将使用execvp来运行另一个进程.但是,可以设置一个标志,以便父级不会等待子级(允许它在后台运行).

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

我的问题是,在这种情况下fork在做什么?我知道孩子有父母过程的完整副本,但我有些困惑.那么,这是否意味着子进程具有自己的对象A,而该对象A持有B?如果B不等待,但是A删除了B,会发生什么呢?

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;
};

推荐答案

子进程与父进程完全独立,并具有父变量的完整副本.子级执行(调用execve()或其亲属之一)时,不会执行任何C ++析构函数.但是,这对父进程没有任何影响.

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.

因此,子进程之间没有任何干扰.父母是否等待孩子都没有关系. fork()(成功)返回到父进程后,子进程将独立运行,并且父进程对分配的变量所做的任何操作都不会影响子进程.

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.

如果您真的很努力并且已将共享内存和变量通过放置new分配到共享内存中,并且子代在调用execvp()之前或其他类似的牵强附会但实际上并非如此的孩子去清理共享内存中的变量在不可能的情况下,那么孩子和父母就不会完全独立.但是,如果您正在做的事情如此复杂,那么您可能也不会问这个问题.

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.

这篇关于Linux在堆上的类中派生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆