"I ask you, have you ever known what it is to be an orphan?"
/*
* Determine if a process group is "orphaned", according to the POSIX
* definition in 2.2.2.52. Orphaned process groups are not to be affected
* by terminal-generated stop signals. Newly orphaned process groups are
* to receive a SIGHUP and a SIGCONT.
*
* "I ask you, have you ever known what it is to be an orphan?"
*/
linus 很不质疑的问看代码的人,“爷问你, 你知道啥是orphaned group 吗?!”
int is_orphaned_pgrp(int pgrp) { struct task_struct **p; for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) { if (!(*p) || ((*p)->pgrp != pgrp) || ((*p)->state == TASK_ZOMBIE) || ((*p)->p_pptr->pid == 1)) continue; if (((*p)->p_pptr->pgrp != pgrp) && ((*p)->p_pptr->session == (*p)->session)) return 0; } return(1); /* (sighing) "Often!" */ }
Orphaned Process Groups
When a controlling process terminates, its terminal becomes free and a new session can be established on it. (In fact, another user could login on the terminal.) This could cause a problem if any processes from the old
session are still trying to use that terminal.
To prevent problems, process groups that continue running even after the session leader has terminated are marked as
orphaned processgroups.
When a process group becomes an orphan, its processes are sent a SIGHUP
signal. Ordinarily, this causes the processes toterminate. However, if a program ignores this signal or establishes ahandler for it
, it can continue running as inthe orphan process group even after its controlling process terminates;but it still cannot access the terminal any more.
这个是GNU给出的解释
http://www.gnu.org/software/libc/manual/html_node/Orphaned-Process-Groups.html
我简单的注释了一下。。。。估计还是很抽象
int is_orphaned_pgrp(int pgrp) //判断是否为一个孤儿进程组 { struct task_struct **p; for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) { if (!(*p) || //如果进程不存在,下一个 ((*p)->pgrp != pgrp) || // 如果进程所处的进程组不是pgrp,下一个 ((*p)->state == TASK_ZOMBIE) || // 进程的状态是zombie,下一个 ((*p)->p_pptr->pid == 1)) //进程parent是init ,下一个 continue; if (((*p)->p_pptr->pgrp != pgrp) && //如果父进程所在的组不在pgrp,但是父进程所在的session存在 ((*p)->p_pptr->session == (*p)->session)) return 0; } return(1); /* (sighing) "Often!" */ }
有图有真相
orphan 与否是靠 parent 和child 的关系来维护,鉴别的
假设判断group2 是否是一个 orphaned group
搜索所有在grp2 里面的进程,只要这个grp2里面的任意进程的parent 进程在当前session,且不在当前process group,那么说明这个group和外界还有联系,他就不是orphan group,只有没有联系了,grp2所有的进程的parent 进程都不在group 2之外,这时候,group内的进程没有能通过parent child关系来和外界发生联系,It‘s a orphan group!
"I ask you, have you ever known what it is to be an orphan?",布布扣,bubuko.com