[英]What happens when multi thread program creates new processes?
I am a bit confused I would like to know in detail, what happens if a C program with more than one thread creates new processes. 我想详细了解一下,如果一个带有多个线程的C程序创建新进程,会发生什么呢? Does the behaviour depends on which thread is creating new processes or how many threads create new processes? 行为取决于哪个线程正在创建新进程还是多少线程创建新进程?
With pthreads, only the calling thread is forked in the new process when fork
is called. 使用pthreads时,在调用fork
时,仅在新进程中派生调用线程。
From the Linux man page : 在Linux手册页中 :
The child process is created with a single thread--the one that called fork(). 子进程由一个线程创建,该线程称为fork()。 The entire virtual address space of the parent is replicated in the child, including the states of mutexes, condition variables, and other pthreads objects; 父级的整个虚拟地址空间都在子级中复制,包括互斥体的状态,条件变量和其他pthreads对象; the use of 指某东西的用途
pthread_atfork(3) may be helpful for dealing with problems that this can cause. pthread_atfork(3)可能有助于解决可能引起的问题。
There are however some versions of fork
on Solaris that duplicate all threads. 但是,Solaris上有一些版本的fork
可以复制所有线程。
From the Solaris man page : 从Solaris手册页 :
A call to forkall() or forkallx() replicates in the child process all of the threads (see thr_create(3C) and pthread_create(3C)) in the parent process. 对forkall()或forkallx()的调用将在子进程中复制父进程中的所有线程(请参见thr_create(3C)和pthread_create(3C))。 A call to fork1() or forkx() replicates only the calling thread in the child process. 对fork1()或forkx()的调用仅复制子进程中的调用线程。
A call to fork() is identical to a call to fork1(); 对fork()的调用与对fork1()的调用相同; only the calling thread is replicated in the child process. 在子进程中仅复制调用线程。 This is the POSIX-specified behavior for fork(). 这是fork()的POSIX指定行为。
In releases of Solaris prior to Solaris 10, the behavior of fork() depended on whether or not the application was linked with the POSIX threads library. 在Solaris 10之前的Solaris发行版中,fork()的行为取决于应用程序是否与POSIX线程库链接。 When linked with -lthread (Solaris Threads) but not linked with -lpthread (POSIX Threads), fork() was the same as forkall(). 当与-lthread(Solaris线程)链接但未与-lpthread(POSIX线程)链接时,fork()与forkall()相同。 When linked with -lpthread, whether or not also linked with -lthread, fork() was the same as fork1(). 与-lpthread链接时,是否也与-lthread链接,fork()与fork1()相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.