【问题标题】:Reattaching to processes in Linux重新附加到 Linux 中的进程
【发布时间】:2014-12-04 18:28:58
【问题描述】:

我遵循 C 程序设置 -> 1)- 我的父进程分叉了一个子进程,然后在该分叉进程中执行 execl 以运行另一个程序。分叉的程序在 while(1) 中,所以它一直在运行。 2)- 我已经在我的父进程中完成了 waitpid,这样如果孩子死了,我就会知道。 3)- 现在如果父母自己死了怎么办?我仍然想监视那个分叉的进程。但是,当我的父母去世时,该分叉进程归 init 所有。因此 init (process id =1) 成为子进程的新父进程。但由于某些原因,我仍然想通过其他程序重新附加我的子进程。可以吗?

【问题讨论】:

  • 在这种情况下实际上是一个孤儿进程。但问题仍然是一样的。我可以再次附加该孤儿进程吗?
  • 如果在新进程中再次waitpid()会发生什么?您必须将子 pid 保存到原始父进程中的文件中。
  • 我也做过同样的事情。存储该进程的 pid 并再次对其进行 waitpid。但不幸的是它也不起作用,因为 waitpid 只能由它的父进程调用子进程,并且在孤立进程 init (pid =1) 已成为父进程的情况下,所以现在 waitpid 抛出一个错误,说 - 没有子进程pid

标签: c linux


【解决方案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.)

【讨论】:

  • @utkarshkatiyar 如果有帮助,请标记为答案。
  • 对不起。你的回答对我没有帮助。而是它帮助我了解了另一个方面,因此我将其标记为答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-09
  • 1970-01-01
  • 1970-01-01
  • 2012-04-24
  • 1970-01-01
  • 2011-04-25
  • 2016-09-20
相关资源
最近更新 更多