gpt4 book ai didi

c - 重新养育停止的过程

转载 作者:太空狗 更新时间:2023-10-29 11:07:56 25 4
gpt4 key购买 nike

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

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

#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>

然后子进程停止( T state in ps xaf )并且父进程休眠。 30 秒后,父进程从 sleep 中返回并终止(因为它到达了 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?

关于c - 重新养育停止的过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36039351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com