fork() child and parent processes

后端 未结 5 506
遥遥无期
遥遥无期 2021-02-01 10:37

I am trying to create a program that uses fork() to create a new process. The sample output should look like so:

This is the child process. My pid is 733 and my parent\'

5条回答
  •  遇见更好的自我
    2021-02-01 11:06

    It is printing the statement twice because it is printing it for both the parent and the child. The parent has a parent id of 0

    Try something like this:

     pid_t  pid;
     pid = fork();
     if (pid == 0) 
        printf("This is the child process. My pid is %d and my parent's id is %d.\n", getpid(),getppid());
     else 
        printf("This is the parent process. My pid is %d and my parent's id is %d.\n", getpid(), getppid() );
    

提交回复
热议问题