码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2016-02-01 09:50:23      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

 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]

标签:

原文地址:http://www.cnblogs.com/wireless-dragon/p/5174434.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!