淘先锋技术网

首页 1 2 3 4 5 6 7
int main(void)
{
    pid_t  pid;

    printf("init pid: %d\n", pid);
    if(pipe(fd) < 0)
    {
    printf("pipe err\n");
    }
    if((pid = fork()) < 0)
    {
        printf("fork err\n");
    }
    else if(pid > 0) // parent
    {
      printf("parent's parent pid: %d\n", getppid());
    printf("parent pid: %lu\n", getpid());
    printf("parent fun's var-pid: %lu\n", pid);
    }
    else if(pid == 0) // child
    {
        printf("child's parent pid: %lu\n", getppid());
    printf("child pid: %lu\n", getpid());
    printf("child fun's var-pid: %lu\n", pid);

    }

    exit(0);
}

输出为:

init pid: 0
parent’s parent pid: 9999
parent pid: 3240
parent fun’s var-pid: 3268
child’s parent pid: 3240
child pid: 3268
child fun’s var-pid: 0