Here is a code that creates two childern using fork. My question is how can we create mutiple grandchildern and great grandchildern at the same time . And the line 21 of code gives an unexpected output i.e instead on one parent id it gives three id's.

Here is the output :-
This is a child1 process and pid is 3271
This is child2 process and pid is 3272
The parent process has pid 2206
The parent process has pid 3270
The parent process has pid 1.

#include<stdio.h>
main()
{
  int child1,child2;
  int i;
  child1 = fork();
  if(child1==0)
  {
    printf("This is a child1 process and pid is %d\n", getpid());
  }
   else
    {
          child2=fork();     
      if(child2==0)
        {
              printf("This is child2  process and pid is %d\n",getpid());
        }
      }
    sleep(1);
       printf("The parent process has pid %d\n",getppid());


}

Dani AI

Generated

As discovered, every process created by fork() continues executing the code that follows it. That is why you see multiple "The parent process has pid ..." lines: the original parent and the child processes all reach that printf. The reason one line shows pid 1 is reparenting — if the original parent exits before a child calls getppid(), the child is reparented to init (pid 1). Two practical fixes: have the parent wait for its children (use wait()/waitpid()) so it stays alive until they finish, or have each child capture the parent's pid immediately after fork (pid_t orig_ppid = getppid()) and print that saved value later.

For creating multiple generations (grandchildren, great‑grandchildren) in a controlled way, make each node fork a fixed number of children and wait for them before returning. This produces a k‑ary process tree and prevents reparenting because parents stay alive while their descendants run:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

void make_tree(int depth, int branches) {
    if (depth <= 0) return;
    for (int i = 0; i < branches; ++i) {
        pid_t pid = fork();
        if (pid < 0) { perror("fork"); exit(1); }
        if (pid == 0) {               /* child */
            printf("pid %d parent %d depth %d\n", getpid(), getppid(), depth);
            make_tree(depth - 1, branches);
            exit(0);
        }
    }
    for (int i = 0; i < branches; ++i) wait(NULL);
}

int main(void) { make_tree(3, 2); return 0; }

Cautions: small depths/branches only — process count grows exponentially; check process limits (ulimit -u); always handle fork errors; and in multithreaded programs prefer posix_spawn or fork+exec to avoid subtle issues. As implied, clear scoping plus explicit waiting is the key to predictable output and to preventing children from becoming orphans.

Look at the scoping of your statements. Each process will output info about the parent process, including the root process, hence the 3 messages about parent process pids. This is where properly indenting your blocks of code can be helpful. IE:

#include <stdio.h>
int main(void)
{
    int child1,child2;
    int i;
    child1 = fork();
    if(child1==0)
    {
        printf("This is a child1 process and pid is %d\n", getpid());
    }
    else
    {
        child2=fork();     
        if(child2==0)
        {
            printf("This is child2  process and pid is %d\n",getpid());
        }
    }
    sleep(1);
    printf("The parent process has pid %d\n",getppid());
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.