#include<stdio.h>
main()
{
int fork_3;
printf("Before:my pid is %d
",getpid());
fork_3 = fork();
if(fork_3 ==-1)
perror("fork");
else if(fork_3 ==0)
printf("I am the child,my pid =%d
,fork is %d
",getpid(),fork_3); //此时getpid()获取的当前进程是子进程的pid,fork()返回的值是0
else
printf("I am the parent,my child is %d,crrent ID is %d
",fork_3,getpid()); //此时getpid()获取的当前进程是父进程的pid,fork()返回的值是子进程的pid
}
编译运行如下: