我编写了一个程序,在主程序中创建一个线程,并用于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)