fork函数在程序中是如何运行的,下面代码是如何运行的?

#include<stdlib.h>
#include<unistd.h>
#include <stdio.h>
main()
{ int p,count;
   count=0;
   p=fork();
   printf("In parent or child,p returned by calling fork is: %d\n",p);
   if(p==0)
{ 
  count++;
     printf("In the child process, count=%d\n",count);
}
  else if(p>0)
{   
      printf("this is the parent process ,the child has the ID: %d\n",p);
      printf("In the parent process, count=%d\n",count);
     }  
else
  {printf("fork failed\n");
   }
   }


```运行结果如下:

In parent or child,p returned by calling fork is: 68872
In parent or child,p returned by calling fork is: 0
In the child process, count=1
this is the parent process ,the child has the ID: 68872
In the parent process, count=0

重新启动一个进程,复制所有的变量过去,包括局部变量,然后从fork的位置,执行新的程序。

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^