2017-06-01 48 views
0

线程启动子进程我有一个情况如下:是好从主过程

main() 
{ 
create a thread executing function thread_func(); 

another_func(); 

} 

another_func() 
{ 
    //check something and do something. 
    // To do something, create a child process. 
    // after creating child process, current thread goes in checking state again 
    // child process independently running. 
} 

thread_func() 
{ 
infinite loop(); // checking something and doing something 
} 

线程使用并行线程创建。 请告诉:这是好的开始像上面一个子进程中的一个线索?如果这样做也会发生什么。

是否子进程创建其自己的另一份线程执行thread_func()?

感谢

+0

你可能有兴趣阅读[恰好给其他线程什么当一个线程叉()?](https://stackoverflow.com/questions/10080811/what-happens-to-other-threads-when-one-线程叉) –

+0

此链接也可能是有用的。 https://stackoverflow.com/questions/39890363/what-happens-when-a-thread-forks –

回答

0

这取决于你想要做什么...... 在这里你的线程将永远不会在another_func(),因为它卡在thread_func()因为infinite_loop()的去。所以这是您的主要程序,将创建子进程。

0

这不是从你的问题,你想要做什么很清楚。 ()API采用指向类型void *()的函数的在pthread_create(空隙)。实际的线程执行在该线程上,因此如果要在线程上创建子进程,则必须在thread_func()之间执行

如果要使用fork()API创建另一个进程,这是标准在Linux上,这将创建一个单独的进程与父进程的整个内存空间的副本。但是内存将是虚拟的,并被标记为写入时复制,所以实际上,除非您尝试写入内存,否则内存不会被复制。

如果你调用fork()后,你还调用exec()或任何在执行exec()系列的其它的API,那么你就需要用复制的记忆关心自己。在exec()API之后,子进程将拥有独立于父进程的独立内存空间。

相关问题