重新养育停止的过程

re-parenting stopped process

停止进程的重新父级如何运行?为什么停止的进程在重新设置后就终止了?

更准确地说,假设我有这样的代码

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/user.h> 
#include <sys/syscall.h>
#include <stdio.h>


int main(void) {
    pid_t child;

    child = fork();

    if (child == 0) {
        int t = 0;
        while (true) {
            printf("%d. I'm %d, my parent is %d\n", t++, getpid(), getppid());
            sleep(1);
        }
    } else {
        printf("I'm the parent. My pid is %d\n", getpid());
        printf("Starting to wait for 30 seconds\n");
        sleep(30);
        printf("Done waiting, aborting\n");
    }
}

当我 运行 此代码时,子进程工作而父进程只是休眠。 30 秒后,父进程终止,子进程现在成为 init 的子进程并继续 运行ning。一切正常。

但是如果我 运行 这段代码并且在它执行的前 30 秒内我也会 运行

kill -SIGSTOP <child_pid>

然后子进程停止(T状态在ps xaf),父进程休眠。 30 秒后,父进程 returns 从睡眠状态传递并终止(因为它到达 main 的末尾),但子进程并没有在停止状态下重新成为 init 的父进程终止。我在 ps xaf 中没有看到它,如果 运行 lastcomm 我看到这个输出:

a.out             F  X equi     pts/5      0.00 secs Wed Mar 16 17:44

为什么会发生这种情况,即停止的进程在重新建立父级后死亡?是否可以在 linux 中重新调用停止的进程?

来自http://www.gnu.org/software/libc/manual/html_node/Job-Control-Signals.html

When a process in an orphaned process group (see Orphaned Process Groups) receives a SIGTSTP, SIGTTIN, or SIGTTOU signal and does not handle it, the process does not stop. Stopping the process would probably not be very useful, since there is no shell program that will notice it stop and allow the user to continue it. What happens instead depends on the operating system you are using. Some systems may do nothing; others may deliver another signal instead, such as SIGKILL or SIGHUP. On GNU/Hurd systems, the process dies with SIGKILL; this avoids the problem of many stopped, orphaned processes lying around the system.

另请参阅:What's the difference between SIGSTOP and SIGTSTP?