【发布时间】:2016-03-16 14:59:54
【问题描述】:
如何对已停止的进程进行重新设置?为什么停止的进程在重新育儿后就终止了?
更准确地说,假设我有这样的代码
#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 的子进程,继续运行。一切正常。
但是如果我运行这段代码并且在它执行的前 30 秒内我也会运行
kill -SIGSTOP <child_pid>
然后子进程停止(ps xaf 中的T 状态)并且父进程休眠。经过 30 秒后,父进程从睡眠中返回并终止(因为它到达 main 的末尾),但子进程在停止状态下没有被重新设置为 init,而是终止了。我在 ps xaf 中看不到它,如果运行 lastcomm 我看到这个输出:
a.out F X equi pts/5 0.00 secs Wed Mar 16 17:44
为什么会发生这种情况,停止的进程在重新养育后死亡?在linux中是否可以重新启动停止的进程?
【问题讨论】:
-
你为什么要这样做?
-
@nzc 有
criu之类的东西(参见criu.org 或github.com/xemul/criu)我想在criu restore中添加功能--leave-stopped(目前仅适用于criu dump) -
我认为如果您在问题中包含其中一些细节,您可能会获得更好的结果。我认为(但不确定)问题的一部分是您可能以不打算使用的方式使用作业控制信号。包括您为什么要使用它们的上下文可能会帮助知道的人解释原因并可能建议正确的方法。