When a process exits, a lot of stuff happens. One thing is that the kernel scans the process table to see if the exiting process has any children. If so, those children get a new parent. The new parent is always init which has a pid of 1.
The ppid is the current parent, not the, um, biological parent.
If you want reliably get the pid of the process that actually issued the fork, record it before the fork. But it isn't useful for anything, except maybe logging.
The problem in the previous code is that the parent does not wait for the termination of the child process....Hence sometimes the parent process dies before the child ..Hence the init process(pid=1) becomes the parent of the child process...
By adding wait() or waitpid() we ensure that the parent waits for the termination of the child...