i have created another fork() inside the else tag, and think that I am right in saying that the code now creates a child process that in itself then creates its own child process.
This is the code I have come up with:
Code:
#include <stdio.h>
main ()
{
int pid, pid2;
printf ("\nI'm the original process with PID %d and PPID %d.\n\n", getpid (), getppid ());
pid = fork (); /* Duplicate process. Child and parent continue from here */
if (pid != 0) /* pid is non-zero, so I must be the parent */
{
printf ("I'm the parent process with PID %d and PPID %d.\n",
getpid (), getppid ());
printf ("My child's PID is %d\n", pid);
}
else /* pid is zero, so I must be the child */
{
printf ("I'm the child process with PID %d and PPID %d.\n",
getpid (), getppid ());
pid2= fork();
printf ("I'm the child's child with PID %d and PPID %d.\n",
getpid (), getppid ());
}
printf ("PID %d terminates.\n", getpid () );
}
The output of this is:
Code:
I'm the original process with PID 9213 and PPID 7921.
I'm the child process with PID 9214 and PPID 9213.
I'm the child's child with PID 9215 and PPID 9214.
PID 9215 terminates.
I'm the parent process with PID 9213 and PPID 7921.
My child's PID is 9214
PID 9213 terminates.
I'm the child's child with PID 9214 and PPID 1. /*THE PROBLEM IS HERE!!*/
PID 9214 terminates.
Why is the i'm the child's child' printf statement being printed out TWICE? i don't want it to do this - also why is the PPID 1? which i realize is the PID of init.