进程创建fork函数的调试[1]

 1 #include<stdio.h>
 2 #include<sys/types.h>
 3 #include<unistd.h>
 4 #include<stdlib.h>
 5 int main()
 6 {
 7     int count=0;
 8     pid_t pid;//此时只有一个进程
 9     pid=fork();//此时创建了两个进程
10     if(pid<0)
11     {
12         printf("error in fork!");
13         exit(1);/*fork出错退出*/
14     }
15     else if(pid==0)
16     printf("I am the children process,the count is %d,my process ID is %d\n",count,getpid());//得到子进程ID
17 else 18 printf("I am the parent process,the count is %d,my process ID is %d\n",++count,getpid());//得到父进程ID 19 return 0; 20 }
//程序运行结果如下:
1
I am the parent process,the count is 1,my process ID is 3176 2 I am the children process,the count is 0,my process ID is 3177

为什么运行结果如此呢?
因为父子进程的运行次序是不确定的!

 

你可能感兴趣的:(进程创建fork函数的调试[1])