我遵循 C 程序设置 -> 1)- 我的父进程 fork 了一个子进程,然后在该 fork 进程中执行 execl 来运行另一个程序。 fork 的程序位于 while(1) 中,因此它始终在运行。 2)-我在我的父进程中完成了 waitpid ,这样如果 child 死了我就会知道它。 3)- 现在如果 parent 本身去世了怎么办?我仍然想监视那个 fork 进程。然而,当我的 parent 去世时, fork 进程就归 init 所有。因此init(进程id=1)成为子进程的新父进程。但由于某些原因,我仍然想通过其他程序重新连接我的子进程。我能做到吗?
最佳答案
您可以将ptrace
与PTRACE_ATTACH
一起使用,然后您可以对该进程执行waitpid。请检查 man ptrace
和 this了解更多信息。
以下来自 ptrace 的联机帮助页:
The ptrace() system call provides a means by which one process (the "tracer") may observe and control the execution of another process (the
"tracee"), and examine and change the tracee's memory and registers. It is primarily used to implement breakpoint debugging and system
call tracing.
......
PTRACE_ATTACH
Attach to the process specified in pid, making it a tracee of the calling process. The tracee is sent a SIGSTOP, but will not nec‐
essarily have stopped by the completion of this call; use waitpid(2) to wait for the tracee to stop. See the "Attaching and detach‐
ing" subsection for additional information. (addr and data are ignored.)
关于c - 重新附加到 Linux 中的进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27301299/