2

I am currently working on a program that tracks the memory usage of the hierarchy of a certain process in linux. While it is possibe to find the parent ID quite easily, it is difficult to find the childs of that particular process. Is there any way I can find all the childs of a process and their childs and so on without resorting to shell scripts?

  • I know no *C* way to do it. You will have to process the output of the `ps` command, or browse the `/proc` folder to get a list of all active processes with their id and parent id and rebuild the tree from there. And beware of race conditions like process starting or ending during that operation... – Serge Ballesta Nov 29 '16 at 13:11
  • I've answered this question on https://stackoverflow.com/questions/17743879/how-to-get-child-process-from-parent-process/63425440#63425440 – y_159 Aug 15 '20 at 11:38

1 Answers1

1

The process hierarchy is the other way around - children track their parent. You'd have to go through the whole of /proc to check the parent of each process. This is what all these tools implement:

Alternatively, if you can monitor the process from its start, e.g. like strace does, then you could track all the fork/clone system calls and record the returned child pid.

Community
  • 1
  • 1
OrangeDog
  • 36,653
  • 12
  • 122
  • 207