c - 重新附加到 Linux 中的进程

标签 c linux

我遵循 C 程序设置 -> 1)- 我的父进程 fork 了一个子进程,然后在该 fork 进程中执行 execl 来运行另一个程序。 fork 的程序位于 while(1) 中,因此它始终在运行。 2)-我在我的父进程中完成了 waitpid ,这样如果 child 死了我就会知道它。 3)- 现在如果 parent 本身去世了怎么办?我仍然想监视那个 fork 进程。然而,当我的 parent 去世时, fork 进程就归 init 所有。因此init(进程id=1)成为子进程的新父进程。但由于某些原因,我仍然想通过其他程序重新连接我的子进程。我能做到吗?

最佳答案

您可以将ptracePTRACE_ATTACH一起使用,然后您可以对该进程执行waitpid。请检查 man ptracethis了解更多信息。

以下来自 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/

相关文章:

c++ - 将 Apache ActiveMQ 与 C/C++ 结合使用

c - 需要获得更好的代码来将文件读入变量

Linux UDP 最大接收缓冲区大小

c - Win32 运行时创建的 LISTBOX 和 LB_ADDSTRING 错误

c - 查找元素是否在指定数组中

c++ - 如何在linux上编译CFugue库?

php - 在亚马逊 Linux 上安装 PHP 的 mongo 驱动程序时出错

linux - 将固定宽度的文件从文本转换为 csv

python - 无法在 Linux 上导入 Python 包

arrays - 在 C 中,如何 "directly"将结构复制到数组成员?