小编Yas*_*oor的帖子

使用system()创建独立的子进程

我编写了一个程序,在主程序中创建一个线程,并用于system()从该线程启动另一个进程。我也使用主函数中的 来启动相同的过程system()。即使父进程死亡,从线程启动的进程似乎仍保持活动状态。但是从 main 函数调用的函数会随着父函数一起死亡。任何想法为什么会发生这种情况。

请找到下面的代码结构:

void *thread_func(void *arg)
{
     system(command.c_str());        
}

int main()
{
    pthread_create(&thread_id, NULL, thread_func, NULL);
    .... 
    system(command.c_str());
    while (true)
    {
        ....
    }
    pthread_join(thread_id, NULL);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ multithreading fork pthreads system-calls

1
推荐指数
1
解决办法
7600
查看次数

标签 统计

c++ ×1

fork ×1

multithreading ×1

pthreads ×1

system-calls ×1