Linux waitpid

pid_t waitpid(pid_t pid, int *status, int options);

options包括(但不限于)以下三个

__WCLONE
    Wait for "clone" children only.  If omitted then wait for "non-clone" children only.  
    (A "clone" child is one which delivers no signal, or a signal other than SIGCHLD to its parent upon termination.)  
    This option is ignored if __WALL is also specified.

__WALL (since Linux 2.4)
    Wait for all children, regardless of type ("clone" or "non-clone").

__WNOTHREAD (since Linux 2.4)
    Do not wait for children of other threads in the same thread group.  This was the default before Linux 2.4.
__WALL

就是所有children;

__WCLONE

clone children;一般就是pthread_create出来的线程;
至于上面说不给parent发SIGCHLD,找了一下内核代码,如下

if (clone_flags & CLONE_THREAD) {
        p->exit_signal = -1;
        p->group_leader = current->group_leader;
        p->tgid = current->tgid;
}

exit_signal置为 -1 了;

__WNOTHREAD

排除同一线程组里其它线程的children;
~~~~~~~~~~~~~~~~~~~~~~~~~~~

线程的ppid

进程 p 通过pthread_create创建了线程 t ,t 的ppid是啥呢?
原以为是 p 的 pid,测了一下发现不是,而是 p 的 ppid;
还是找内核代码,如下

if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) {
        p->real_parent = current->real_parent;
        p->parent_exec_id = current->parent_exec_id;
}

pthread_create创建线程时设置了 CLONE_THREAD,通过strace很容易验证;
知道了答案再想想,也是,p和t是同一个线程组的,是平级关系,ppid也应是相同的;

用喜闻乐见的方式总结一下

进程关系图

A,A1,A2是一个线程组(A通过pthread_create创建A1,A2;下同);
B,B1是一个线程组;
C,C1是一个线程组;
对A来说,WNOTHREAD排除的就是A1,A2的children;

略丑,凑合看。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值