find_new_reaper

如果父進程在子進程之前退出,必須有機制來保證子進程能找到一個新的父親,否則這些成爲孤兒的進程就會在退出時永遠處於僵死狀態,白白地耗費內存。對於這個問題,解決方法是給子進程在當前線程組內找一個線程作爲父親,如果不行,就讓init做它們的父進程。在do_exit()中會調用exit_notify(),該函數會調用forget_original_parent(),而後者會調用find_new_reaper()來執行尋父過程:

/*
 * When we die, we re-parent all our children.
 * Try to give them to another thread in our thread
 * group, and if no such member exists, give it to
 * the child reaper process (ie "init") in our pid
 * space.
 */
static struct task_struct *find_new_reaper(struct task_struct *father)
{
	struct pid_namespace *pid_ns = task_active_pid_ns(father);
	struct task_struct *thread;

	thread = father;
	while_each_thread(father, thread) {
		//遍歷該結束的進程所在線程組的下一個進程
		if (thread->flags & PF_EXITING)//如果得到的下一個進程被標記了 PF_EXITING ,就不符合要求,需要繼續遍歷
			continue;
		if (unlikely(pid_ns->child_reaper == father))
			/*
			child_reaper 表示進程結束後,需要這個child_reaper指向的進程對這個結束的進程進行託管,
			其中的一個目的是對孤兒進程進行回收。
			若該託管進程是該結束進程本身,就需要重新設置託管進程,
			設置爲該結束進程所在線程組的下一個符合要求的進程即可。
			*/
			pid_ns->child_reaper = thread;
		return thread;//在該結束進程所在的線程組中找到符合要求的進程,返回即可
	}

	/*
	如果該結束進程所在的線程組中沒有其他的進程,
	函數就返回該結束進程所在命名空間的 child_reaper 指向的託管進程
	(前提是該託管進程不是該結束進程本身)
	*/
	if (unlikely(pid_ns->child_reaper == father)) {
		/*
		如果該結束進程所在命名空間的 child_reaper 指向的託管進程就是該結束進程本身,
		而程序運行至此,說明在該線程組中已經找不到符合要求的進程,
		此時,需要將託管進程設置爲 init 進程,供函數返回
		*/
		write_unlock_irq(&tasklist_lock);
		if (unlikely(pid_ns == &init_pid_ns))
			panic("Attempted to kill init!");

		zap_pid_ns_processes(pid_ns);
		write_lock_irq(&tasklist_lock);
		/*
		 * We can not clear ->child_reaper or leave it alone.
		 * There may by stealth EXIT_DEAD tasks on ->children,
		 * forget_original_parent() must move them somewhere.
		 */
		pid_ns->child_reaper = init_pid_ns.child_reaper;
	}

	return pid_ns->child_reaper;
}

總結一下,在 find_new_reaper 中有可能返回以下3種進程作爲新父進程:

1、原結束進程所在線程組中的一個符合要求的進程

2、原結束進程所在進程命名空間中 child_reaper指向的託管進程

3、init進程


現在,給子進程找到合適的新父進程了,只需要遍歷所有的子進程併爲他們設置新的父進程

reaper = find_new_reaper(father);

	list_for_each_entry_safe(p, n, &father->children, sibling) {
		p->real_parent = reaper;
		if (p->parent == father) {
			BUG_ON(task_ptrace(p));
			p->parent = p->real_parent;
		}
		reparent_thread(father, p, &dead_children);
	}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章