7

If I fork and the child process then exec and creates more child processes (which themselves can create more processes) , how do I get a list of pids of all the descendent process from the first process?

Is there a better way then looping though /proc/ and checking the PPid (the parent of process's id) of each of process?

Bilal Syed Hussain
  • 8,664
  • 11
  • 38
  • 44
  • the answer from that question using pgrep -P only gives the child process pids of a pid, NOT all processes that the children of that child might have created which themselves may have created more processes. – Bilal Syed Hussain Dec 12 '13 at 02:53
  • Sure, but at that point you just apply recursion. – Oliver Charlesworth Dec 12 '13 at 02:55
  • Fair enough I recuse using pgrep -P – Bilal Syed Hussain Dec 12 '13 at 03:07
  • 1
    @OliCharlesworth Not sure about it being a duplicate, as the question you refer to is specific to shell scripting, which has different solutions than this question, which is C-related, and may have, for example(?), a kernel call as a solution? – mydoghasworms Dec 12 '13 at 05:03
  • 1
    C does not provide a standard way to achieve this through system calls. – alk Dec 12 '13 at 09:34
  • 1
    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:20

2 Answers2

3

Iterating through /proc is the "standard" way to do this. That's how ps and friends (pstree, etc.) are implemented.

oakad
  • 6,945
  • 1
  • 22
  • 31
2

If you are looking for commands to see the process tree, check below commands.

ps -ejH
ps axjf
pstree $PID

Check the man pages of ps and pstree commands.

vareda
  • 125
  • 2
  • 10