当前位置: 技术问答>linux和unix
如果父进程有多个线程,那么它的子进程也该拥有这些线程
来源: 互联网 发布时间:2016-07-16
本文导语: 刚看《现代操作系统》一书出现的问题。 如果父进程有多个线程,那么它的子进程也该拥有这些线程? 谁能帮忙解答。谢谢 | 这是不对的。fork后子进程只能有一个线程。 道理其实很简单,如果需要继承所有线程...
刚看《现代操作系统》一书出现的问题。
如果父进程有多个线程,那么它的子进程也该拥有这些线程?
谁能帮忙解答。谢谢
如果父进程有多个线程,那么它的子进程也该拥有这些线程?
谁能帮忙解答。谢谢
|
这是不对的。fork后子进程只能有一个线程。
道理其实很简单,如果需要继承所有线程,简直是无法想象的。
比如,进程A里,线程A_1正在操作一个链表,这时线程A_2
fork出了进程B, 难道要让B_1应该在哪里接着运行呢?
根据Unix 98标准, fork后,子进程只能有一个线程。
A process shall be created with a single thread. If a multi-threaded process calls fork(), the new process shall contain a replica of the calling thread and its entire address space, possibly including the states of mutexes and other resources. Consequently, to avoid errors, the child process may only execute async-signal-safe operations until such time as one of the exec functions is called. [THR] Fork handlers may be established by means of the pthread_atfork() function in order to maintain application invariants across fork() calls.
When the application calls fork() from a signal handler and any of the fork handlers registered by pthread_atfork() calls a function that is not asynch-signal-safe, the behavior is undefined.
|
如果子进程是在多线程以后生出来,就拥有,否则没有。