Linux waitpid

来源:互联网 发布:js强制弹窗 编辑:程序博客网 时间:2024/05/16 01:38
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
原创粉丝点击