Parent-> fork
------------1st Child(A)-> fork again
-------------------->1st Child's Child(Aa)
If I am the parent, how to get the child's child(Aa) pid in C program? Or how to get all the pid within this group?
There's not a direct way to get the child pid as for the parent pid (getppid()
), but...
fork()
returns one of three values: child pid, 0, and -1.
fork()
had error(s).... so you already have the child(rens) pid(s) before and after they fork()
. If you want the child pid after execl
, for example, the easiest way is to just hang on to it when it is returned by fork()
, i.e.; store it.
If that's not efficient enough for you there's the possibility, as a commenter commented, you could set up a pipe()
or two for communicating it back to the parent.
int io[2];
pipe(io);
io[0]
is in; io[1]
is out. You would use write()
and read()
.