免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1583 | 回复: 3
打印 上一主题 下一主题

fork()函数问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-30 16:00 |只看该作者 |倒序浏览
关于fork()函数创建子进程  ,是复制fork()语句后面的全部父进程吗?

书上有段fork()代码,执行起来有些不明白的地方!求高手讲解下,代码如下:

#include <stdio.h>
  2 #include <stdlib.h>
  3 #include <sys/types.h>
  4 #include <unistd.h>
  5 #include <sys/wait.h>
  6
  7 int main (void )
  8 {
  9     pid_t child1,child2,child;
10
11     child1 = fork();
12     child2 = fork();
13
14     if(child1 == -1)
15     {
16         printf("child1 fork error\n");
17         exit(1);
18     }
19     else if(child1 == 0)
20     {
21         printf("in child1 : execute 'ls -l'\n");
22         if(execlp("ls","ls","-l",NULL)<0)
23         {
24             printf("child1 execlp error\n");
25         }
26     }
27
28     if(child2 == -1)
29     {
30         printf("child2 fork error\n");
31         exit(1);
32     }
33     else if(child2 == 0)
34     {
35         printf("in child2 : sleep for 5 seconds and then exit\n");
36         sleep(5);
37         exit(0);
38     }
39      else
40     {
41         printf("in father process\n");
42         child=waitpid(child1,NULL,0);
43         if(child == child1)
44         {
45             printf("get child1 exit code\n");
46         }
47         else
48         {
49             printf("error \n");
50         }
51         do
52         {
53             child = waitpid(child2,NULL,WNOHANG);
54             if(child == 0)
55             {
56                 printf("the child2 process has not exited\n");
57                 sleep(1);
58             }
59         }
60         while(child == 0);
61
62         if(child == child2)
63         {
64             printf("get child2 exit code\n");
65         }
66         else
67         {
68             printf("error \n");
69         }
70     }
71     exit(0);
72 }

想问一下,为什么child1 和 child1再创建的子进程  不去执行对child2的判断语句!child1 也对child2进行过创建 也有child2的值阿 ,却在执行完child1 的判断直接结束掉了?是fork()函数的确定的代码段范围直到child1判断结束的地方?

我预想的输出结果应该是 2次 ls ,两次 in father ,两次 sleep  
但结果是  两次 ls, 一次 in father 和 一次 sleep !

论坛徽章:
30
黑曼巴
日期:2019-04-11 12:11:0215-16赛季CBA联赛之八一
日期:2019-06-11 14:31:1915-16赛季CBA联赛之八一
日期:2019-08-06 09:37:182016科比退役纪念章
日期:2020-04-26 09:09:56黑曼巴
日期:2020-12-23 10:47:1015-16赛季CBA联赛之吉林
日期:2021-04-02 13:50:272016科比退役纪念章
日期:2022-08-01 15:03:15黑曼巴
日期:2023-10-07 16:19:58黑曼巴
日期:2018-09-12 13:34:41CU十四周年纪念徽章
日期:2018-06-19 21:17:58洛杉矶湖人
日期:2015-03-02 16:07:282022北京冬奥会纪念版徽章
日期:2015-08-10 16:30:32
2 [报告]
发表于 2011-03-30 18:28 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
3 [报告]
发表于 2011-03-30 18:39 |只看该作者
好复杂的fork,研究了一下,我的理解。
需要注意的是exec之后就进入到ls了,不会再继续走了。
   
                    father--->father
                     /     \
                    /       \
    ls  <----child 1   child2--->sleep
                  /
                 /
    ls <--- child1-2

论坛徽章:
0
4 [报告]
发表于 2011-03-31 10:05 |只看该作者
谢谢  明白了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP