The child process which is made orphan by killing its parent is not getting adopted by init. How do I fix this?
<p>The program is to avoid zombie by forking twice. I am getting the value for parent pid of the orphan process everything but 1, which is not right.</p>

<p>Here is the code that I tried. If there is error in the logic please help me out.</p>

<pre><code>#include&lt;stdio.h&gt;
#include&lt;unistd.h&gt;
#include&lt;stdlib.h&gt;

int main()
{
    pid_t processid;

    if((processid=fork())&lt;0)
        printf("Fork error\n");
    else if(processid==0)
    {
        if((processid=fork())&lt;0)
            printf("Fork error\n");

        if(processid&gt;0)
        {exit(0);}

        sleep(2);
        printf("second child, parent pid =%d\n",getppid());
        exit(0);
    }
    exit(0);
}
</code></pre>

<p>This is the output I am getting:</p>

<pre><code>root@kali:~# gedit avoidzombies.c
root@kali:~# gcc avoidzombies.c
root@kali:~# ./a.out
root@kali:~# second child, parent pid =1069
</code></pre>