首页 > 代码库 > 关于进程fork()

关于进程fork()

#include<stdio.h>

main()
{
  int fork_3;
  printf("Before:my pid is %d\n",getpid());

  fork_3 = fork();

  if(fork_3 ==-1)
  perror("fork");
  else if(fork_3 ==0)
  printf("I am the child,my pid =%d\n,fork is %d\n",getpid(),fork_3);  //此时getpid()获取的当前进程是子进程的pid,fork()返回的值是0
  else
  printf("I am the parent,my child is %d,crrent ID is %d\n",fork_3,getpid());  //此时getpid()获取的当前进程是父进程的pid,fork()返回的值是子进程的pid
}

编译运行如下: