fork() child and parent processes

后端 未结 5 500
遥遥无期
遥遥无期 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:18

    This is the correct way for getting the correct output.... However, childs parent id maybe sometimes printed as 1 because parent process gets terminated and the root process with pid = 1 controls this orphan process.

     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(), pid);
    

提交回复
热议问题