为什么 child 处理 运行 用于 parent 的代码

why is child process running code meant for parent

下面的代码片段也被 child 进程 运行,但我不知道为什么,因为根据我的理解,child 的 Pid 应该始终为 0,所以除了打印“我是 child”;

,它没有理由在下面做任何事情
 pid_t child_Pid1 = fork();

 if((int)getpid() == 0) {
 printf("I am child\n");
 } else {
    printf("I am parent\n");
    }

getpid() 始终 returns 当前进程的 pid 永远不会为零,因此在您当前的代码中,这两个进程都没有 execlp.

您想查看 child_Pid1 而不是 getpid()。在 child 它 returns 0 而不是 child 的 pid.