【发布时间】:2018-11-12 14:19:18
【问题描述】:
双叉
在我的理解中,当一个进程想要分叉一个后台进程时使用双叉,但是1.它不想等待它并且2.后台进程应该在它退出后重新获得。
因此,双叉只需要父进程等待子进程,子进程在fork孙子进程后立即退出,孙子进程作为后台进程负责真正的任务。
上下文
根据 APUE 的这段摘录,孙子会休眠 2 秒以确保其父(子)在退出之前退出,这样它就会成为孤儿,而 init 会照顾它并在它退出时收获它。
#include "apue.h"
#include <sys/wait.h>
int
main(void)
{
pid_t pid;
if ((pid = fork()) < 0) {
err_sys("fork error");
} else if (pid == 0) { /* first child */
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid > 0)
exit(0); /* parent from second fork == first child */
/*
* We're the second child; our parent becomes init as soon
* as our real parent calls exit() in the statement above.
* Here's where we'd continue executing, knowing that when
* we're done, init will reap our status.
*/
sleep(2);
printf("second child, parent pid = %ld\n", (long)getppid());
exit(0);
}
if (waitpid(pid, NULL, 0) != pid) /* wait for first child */
err_sys("waitpid error");
/*
* We're the parent (the original process); we continue executing,
* knowing that we're not the parent of the second child.
*/
exit(0);
}
问题
为什么孙子进程需要休眠那 2 秒?假设它在子进程退出之前就已经退出,子进程退出时仍会按照this question进行收割,父进程仍然不需要照顾。
这不是实现了使用双叉的最初目标吗?
【问题讨论】: