I hope the following may help you:
If I haven't misundertood you, you are in the following case:
ProgramA
if (fork()==0)
execv("blablabla", "bliblibli");
else
...
That is to say program A doesn't wait for the completion of his son (no waitpid). Let us suppose programA doesn't stop just after the fork, his son will indeed keep PPID=father's PID.
But if ProgramA could ever stop just after the fork, his son will then be attach to PPID=1.Ok, so, here is what I can purpose you:
ProgramA
if ((pid=fork())==0)
if (fork()==0)
execv("blablabla", "bliblibli");
else
exit(1);
else
waitpid(pid);
...
ProgramA will wait until completion of his son, which is quasi-immediate, but not for the completion of his "grand-son", which will loose his father (the son of programA), and then will attach to PPID=1. Is it all clear?
Here's an example:
#include <stdio.h>
#include <unistd.h>
main ()
{
int pid, i;
if ((pid=fork ()) == 0)
{ /* First, create on son */
printf ("\t> SON 1, pid:%d, ppid:%d\n", getpid(), getppid());
if ((pid=fork ()) == 0)
{ /* SON 1 create SON 2*/
while (1)
{
printf ("\t\t> SON 2, pid:%d, ppid:%d\n", getpid(), getppid());
sleep(1);
}
printf("\t\t> SON 2 ends up.\n");
exit(1);
}
for (i=0;i<10;i++)
{
for (i=0;i<10;i++)
{
printf ("\t> SON 1, pid:%d, ppid:%d\n", getpid(), getppid());
sleep(1);
}
printf("\t> SON 1 ends up\n");
exit(1);
}
else if (pid>0)
{
printf ("FATHER, pid:%d, fils:%d\n", getpid(), pid);
waitpid (pid);
}
else printf ("FORK ERROR\n");
printf("> Father ends up.\n");
I tried it on HPUX and LINUX (RHEL3), it's OK, SON2 is stille alive even after his father (SON1) dies. And by the way, its nex "father" his PID=1, even if his "grand-father" is ProgramA.
Regards
Xavier