6

I'd like to get a list of all the immediate children of a given PID. I'm OK with using /proc but /proc/<PID>/task/<PID>/children is NOT precise and may return inaccurate results (see section 3.7 here). I'd like a more reliable method of doing this.

I'd prefer not using a wrapper around a shell command.

Coder
  • 597
  • 7
  • 22

1 Answers1

1

Why not use psutils?

Here is an example where I kill all the children.

def infanticide(pid):
    try:
      parent = psutil.Process(pid)
    except psutil.NoSuchProcess:
      return
    children = parent.children(recursive=True)
    for p in children:
        os.kill(p.pid, signal.SIGKILL)
Ian A. Mason
  • 207
  • 3
  • 8