qq_45592634 2020-06-05 16:22 采纳率: 0%
浏览 242

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

  • 写回答

2条回答 默认 最新

  • threenewbee 2020-06-05 16:24
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗