关于进程fork()

#include

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
}

编译运行如下:

关于进程fork()_第1张图片

转载于:https://www.cnblogs.com/liuhp/p/3705804.html

你可能感兴趣的:(关于进程fork())