I write a program in C. I do fork() in the main process in order to do execve() in the forked child process to execute an unknown app (given by a user in the command line). I know a PID of the process of the executed app - it is returned by fork(), but this unknown app can possibly fork() many times and I do not know PIDs of all its children (they are grandchildren of the main parent process). How can I check in the main parent process WHEN its child process (it is the unknown app) and ALL children of the unknown app exit? (I do not know even how many children it can have and I do not know PIDs of these children).
-
1Possible duplicate of [How to get child process from parent process](http://stackoverflow.com/questions/17743879/how-to-get-child-process-from-parent-process) – klutt Mar 17 '17 at 21:34
-
I'm pretty sure you can't do that by normal means. Maybe something involving `ptrace`? – melpomene Mar 17 '17 at 21:45
-
The app should take care of its children. You shouldn't have to worry about them. – Petr Skocik Mar 17 '17 at 21:47
-
@klutt This is not a duplicate, since my answer does not work by looking up process IDs of processes that are not direct children. – jilles Mar 18 '17 at 13:03
-
@PSkocik I do not worry. I just have to know when all of them exits. – dluki Mar 18 '17 at 20:15
2 Answers
This can be done by making your parent process a subreaper. A subreaper gets all children orphaned by its descendants, which would traditionally always go to init (process ID 1). The subreaper status needs to be enabled before forking the interesting child process. Once this is done, a waitpid()
or similar call for any process will return the child process and all orphaned descendants until it returns error [ECHILD]
when the entire tree is gone.
On Linux, this is enabled using prctl()
's PR_SET_CHILD_SUBREAPER
option and on FreeBSD this is enabled using procctl()
PROC_REAP_ACQUIRE
command (see man pages for details).
On Linux you will be able to monitor only one child process individually this way, since the orphans do not remember from which original fork call they came. On FreeBSD, PROC_REAP_GETPIDS
allows distinguishing individual subtrees, although this is less efficient if the tree contains many processes.

- 10,509
- 2
- 26
- 39
-
Thanks @jilles ! It seems it is what I was looking for. I will try it. BTW: this explanation is also useful: http://unix.stackexchange.com/questions/250153/what-is-a-subreaper-process – dluki Mar 18 '17 at 20:35
You can use waitpid(-1,NULL, WNOHANG) to tell if one child has exited. If you receive a positive number (a pid) then one child has exited. In your parent process you have a line that checks if the amount of child processes you have, here called x, is more than 0. if it is use this command to see if any child process has ended. If you have x items then when you add an item increment x and when one exits decrement x. When x, the amount of children you have, is zero all you children have been killed.

- 1
-
The question is about grandchildren, not children of the main process. – melpomene Mar 17 '17 at 21:46
-
-
@Charles I want to know when all children of the unknown app which I executed using execve() (so my grandchildren) exit. I even do not know how many children this unknown app can have and I do not know PIDs of these children. – dluki Mar 18 '17 at 20:22