c - 重新养育停止的过程

标签 c linux process ps

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

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

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

相关文章:

fflush函数可以增加linux中的文件描述符数量吗?

c# - 如何在系统 Windows 用户帐户下运行控制台应用程序?

c - 错误 :invalid type argument of '->' (have 'Queue {aka struct Queue}

c - C中的随机访问函数问题

linux - 我如何找出我的代码在哪里导致 GLib-GObject-CRITICAL?

c# - Java 中的 Process.Start 帮助

c# - 如何获得窗口的位置?

c++ - 通用守护进程/服务器设计 - 最佳实践(C/C++、Linux)

C - System V - 删除共享内存段

java - 服务器关闭时远程启动 GlassFish v3