重新连接到Linux中的进程

时间:2014-12-04 18:28:58

标签: c linux

我跟随C程序设置 - > 1) - 我的父进程分叉一个子进程,然后执行该分叉进程以运行另一个程序。分叉程序在while(1)中,因此它始终在运行。 2) - 我在父进程中完成了waitpid,这样如果孩子死了,我就会知道。 3) - 如果父母本身死了怎么办?我仍然希望监视分叉进程。但是,当我的父亲去世时,分叉进程归init所有。因此init(进程id = 1)成为子进程的新父进程。但由于某些原因,我仍然希望通过其他程序重新附加我的子进程。我可以这样做吗?

1 个答案:

答案 0 :(得分:0)

您可以将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.)