简体   繁体   English

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

[英]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.

相关问题 当线程分叉时会发生什么? - What happens when a thread forks? 当线程在C中调用longjmp()时会发生什么 - What happens when a thread calls longjmp() in c 当多个进程尝试从同一管道读取时会发生什么? - what happens when multiple processes try to read from the same pipe? 当两个进程等待同一个孩子时会发生什么? - What happens when two processes wait for the same child? 当两个进程写入mmaped文件的相同部分时会发生什么? - What happens when two processes write to same portion of a mmaped file? 当很多进程打开同一个特殊文件时会发生什么? - What happens when a lot of processes open the same special file? 在fork()进程中没有针对父进程的操作时会发生什么? - What happens when there are no actions for parent process during fork() processes? 当两个进程试图访问信号量= 0的关键部分时会发生什么? - What happens when two processes are trying to access a critical section with semaphore = 0? 程序收到SIGTSTP信号时会发生什么 - What happens when a program receives SIGTSTP signal gdb 在 linux 上调试多线程守护程序时如何中断新线程 - gdb how to break in new thread when debugging multi threaded daemon program on linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM