How can I continue the process and print out their process ids?

So far my program is working, but I need my program to continue with 9 more processes indicating process 2 is the parent of process 3 and process 3 is the parent of process 4, and so on. So far I've been able to output the parent with process id, along with the child process id. The issue is, it only outputs the parent and child once. I require that my program create a chain of 10 processes and prints out their parent and child process id's. Currently I've been able to output parent id and its child id only once. What would be the best method to accomplish this?
Here is my c++ code:
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
using namespace std;

int main ()
{
int pid;
printf ("\nI'm the original process with PID %d.\n", getpid ());
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.\n", getpid());
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.\n", getpid ());
pid = fork();
printf ("I'm the child's child with PID %d.\n", getpid ());
}
printf ("PID %d terminates.\n", getpid());
}
Topic archived. No new replies allowed.