#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