日期:2014-05-16  浏览次数:20656 次

消失的父进程
代码如下
-------------------------------------------------------------------

  1 #include "common.h"
  2 int main()
  3 {  
  4 pid_t pid1,pid2;
  5  
  6 if((pid1=fork()) == 0)
  7 {  
  8 printf("pid1=zi %d\n",getpid());
  9 sleep(3);
 10 printf("info1 from child process_1 == %d\n",getpid());
 11 exit(0);
 12 printf("info2 from child process_1\n");
 13 }
 14 else
 15 {  
 16 printf("pid1=fu %d\n",getpid());
 17 if((pid2=fork()) == 0)
 18 {
 19 printf("pid2=zi %d\n",getpid());
 20 sleep(1);
 21 printf("info1 from child process_2 -- %d\n",getpid());
 22 exit(0);
 23 }
 24 // else if(pid2 > 0)
 25 // { printf("pid2=i=====fu %d\n",getpid());}
 26 else
 27 {
 28  
 29 printf("pid2=fu %d\n",getpid());
 30 wait(NULL);
 31 wait(NULL);
 32 printf("info1 from parent process ++ %d\n",getpid());
 33 printf("info2 from parent process");
 34 _exit(0);
 35 }  
 36 }
 37  
 38 }
-------------------------------------------------------------------
输出结果为:
pid1=zi 6842
pid1=fu 6841
pid2=zi 6843
pid2=fu 6841
info1 from child process_2 -- 6843
info1 from child process_1 == 6842
info1 from parent process ++ 6841
------------------------
弄不懂PID2的父进程去哪里了 2个WAIT(NULL)起到了什么作用呢

------解决方案--------------------
一个父进程创建了2个子进程,然后wait了2个子进程退出, 没有问题啊?