linux多进程子进程继承,Linux-fork调用后,父进程的线程是否会被子进程继承?

不可能继承,因为fork后子进程只能有一个线程。道理其实很简单,如果需要继承所有线程,简直是无法想象的。比如,进程A里,线程A_1正在操作一个链表,这时线程A_2正在操作一个栈,而线程A_3fork出了进程B, 难道要让B_1应该在哪里接着运行呢?是操作A_1的链表还是操作A_2的栈?

根据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.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值