简体   繁体   中英

How can I create three child processes by a child process?

I want to create three child processes from a child process of the main process (P0). So something like -->

P0 -> P1 ->P2  
             ->P3
             ->P4

However, whenever I run it I get (for the processes P2,P3,P4) the ppid of the main process (ppid = 1).

I am using fork() system call in order to create the children and the implementation of the program is in C.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
    int i, pid1, pid2;
    pid1 = fork();
    printf("Pid1 pid -> %d ppid -> %d\n",getpid(),getppid());
    if(pid1 != 0)
    {
        pid2 = fork();
        printf("Pid2 pid -> %d ppid -> %d\n",getpid(),getppid());
        if(pid2 == 0)
        {
            for(i=2; i<5; i++)
            {
                //if(fork()==0)
                //{
                    printf("Child %d pid -> %d Parent -> %d\n",i+1,getpid(),getppid());
                //}
                exit(0);
            }
        }
        else 
        {
            printf("Pid2 father process \n");
        }  
    }
    else 
    {
        printf("Pid1 child process\n");

    }
}

Warning, in

pid1 = fork();
printf("Pid1 pid -> %d ppid -> %d\n",getpid(),getppid());

the printf is done both in P0 and P1, this is unclear at all, you have first to check pid1 before to print where you are.

Warning, in

pid1 = fork(); ... if(pid1;= 0) { pid2 = fork();

you supposed to be in P1 when you fork again, but you are still in P0 because pid1 is not 0

It is also recommended to check fork success (return value is not -1), and wait for the child(ren) termination rather than to exit the parent process before

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

int main()
{
    int pid1 = fork();

    if (pid1 == -1)
      perror("cannot fork p1");
    else if(pid1 != 0)
    {
      int pid2;

      printf("In P1, Pid1 pid -> %d ppid -> %d\n", getpid(), getppid());

      pid2 = fork();

      if (pid2 == -1)
        perror("cannot fork p2");
      else if (pid2 == 0) {
        int pid3[2];
        int i;

        printf("in P2, Pid2 pid -> %d ppid -> %d\n", getpid(), getppid());

        for (i = 0; i != 2; ++i) {
          pid3[i] = fork();
          if (pid3[i] == -1)
            perror("cannot fork P2 child");
          else if (pid3[i] == 0) {
            printf("in Child %d pid -> %d Parent -> %d\n",i+1,getpid(),getppid());
            return 0;
          }
          else
            puts("in p2");
        }
        waitpid(pid3[0], 0, 0); /* erroned if fork failed, whatever */
        waitpid(pid3[1], 0, 0); /* erroned if fork failed, whatever */
      }
      else {
        puts("still in p1");
        waitpid(pid2, 0, 0);
      }
    }
    else 
    {
      puts("in P0");
      waitpid(pid1, 0, 0);
    }

    return 0;
}

Compilation and execution:

/tmp % ./a.out
In P1, Pid1 pid -> 68995 ppid -> 54669
in P0
still in p1
in P2, Pid2 pid -> 68997 ppid -> 68995
in p2
Child 1 pid -> 68998 Parent -> 68997
in p2
Child 2 pid -> 68999 Parent -> 68997
/tmp % 

Note the written pid are wrong, from the manual of getppid :

then a call to getpid() in the child will return the wrong value (to be precise: it will return the PID of the parent process)

You want something like the following:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
    int i, pid1, pid2;

    pid1 = fork();
    printf("Pid1 pid -> %d ppid -> %d\n",getpid(),getppid());

    // parent
    if(pid1 != 0)
    {
        pid2 = fork();
        printf("Pid2 pid -> %d ppid -> %d\n",getpid(),getppid());

        if(pid2 == 0)
        {
            for(i=2; i<5; i++)
            {
                if(fork()==0)
                {
                    printf("Child %d pid -> %d Parent -> %d\n",i+1,getpid(),getppid());
                    // some functionality here
                    exit(0);
                }
            }
            exit(0);
        }
        else 
        {
            printf("Pid2 father process \n");
        }  
    }
    else 
    {
        printf("Pid1 child process\n");

    }
}

Giving the following output in my machine:

Pid1 pid -> 17764 ppid -> 32242
Pid1 pid -> 17765 ppid -> 17764
Pid1 child process
Pid2 pid -> 17764 ppid -> 32242
Pid2 father process
Pid2 pid -> 17766 ppid -> 17764
Child 3 pid -> 17767 Parent -> 17766
Child 4 pid -> 17768 Parent -> 17766
Child 5 pid -> 17769 Parent -> 17766

Thus, with the following hierarchy:

Parent (pid: 17764) -> C1 (17765) -> C2 -> 17666
                                          C3-> 17767
                                          C4-> 17768
                                          C5-> 17769

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM