gpt4 book ai didi

c - fork 和现有线程?

转载 作者:太空狗 更新时间:2023-10-29 16:40:56 25 4
gpt4 key购买 nike

在 linux 系统上,子进程是否以与父进程相同的方式查看现有线程?

int main() {

//create thread 1

int child_pid = fork();

if ( 0 == child_pid)
{
..
}
else
{
..
}

由于为子进程复制了整个地址空间,线程的状态会发生什么变化。如果上述段中的线程 1 正在等待条件信号怎么办。在子进程中是否也处于等待状态?

最佳答案

如今 Linux 上的线程试图保持 POSIX 兼容。仅复制调用线程,而不复制其他线程(请注意,例如在 Solaris 上,您可以根据链接到的库选择 fork 的功能)

来自 http://www.opengroup.org/onlinepubs/000095399/functions/fork.html (POSIX 2004):

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. Fork handlers may be established by means of the pthread_atfork() function in order to maintain application invariants across fork() calls.

fork() 的 POSIX 2018 规范是相似的。

关于c - fork 和现有线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1073954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com