当多线程程序创建新进程时会发生什么?

What happens when multi thread program creates new processes?

我有点困惑我想详细了解一下,如果具有多个线程的 C 程序创建新进程会发生什么情况。该行为是否取决于哪个线程正在创建新进程或有多少线程创建新进程?

使用 pthreads,当调用 fork 时,只有调用线程在新进程中分叉。

来自Linux man page

The child process is created with a single thread--the one that called 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; the use of
pthread_atfork(3) may be helpful for dealing with problems that this can cause.

然而,在 Solaris 上有一些 fork 版本复制所有线程。

来自Solaris man page

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. A call to fork1() or forkx() replicates only the calling thread in the child process.

A call to fork() is identical to a call to fork1(); only the calling thread is replicated in the child process. This is the POSIX-specified behavior for fork().

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. When linked with -lthread (Solaris Threads) but not linked with -lpthread (POSIX Threads), fork() was the same as forkall(). When linked with -lpthread, whether or not also linked with -lthread, fork() was the same as fork1().