Chinaunix首页 | 论坛 | 博客
  • 博客访问: 37748
  • 博文数量: 41
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 357
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-20 16:26
文章分类

全部博文(41)

文章存档

2014年(41)

我的朋友

分类: C/C++

2014-04-30 13:45:44

今天看了一本书看到有一个结构类似的下面程序,你说这程序总共建立多少个进程??
书上写的竟然带原来的父进程一起总共只有三个进程???
自己写个程序测试一下。。。。

  8 #include
  9 #include
 10 #include
 11 #include
 12 #include
 13
 14 int main()
 15 {
 16     int s=0;
 17     pid_t child1;
 18     pid_t child2;
 19
 20     child1 = fork();
 21     child2 = fork();
 22     printf("how many pid: %d \n", getpid());
 23     printf("**************child2 pid:%d\n", child2);
 24     if(child1 < 0)
 25     {
 26         perror("fork");
 27         exit(1);
 28     }
 29     else if(child1 == 0)
 30     {
 31         printf("child1 pid: %d\n", getpid());
 32         if(child2 == 0)
 33         {
 34             printf("in child1's child2 pid:%d\n", getpid());
 35             exit(1);
 36         }
 37         exit(0);
 38     }
 39     else if(child2 < 0)
 40     {
 41         perror("fork");
 42         exit(1);
 43     }
 44     else if(child2 == 0)
 45     {
 46         printf("child2 pid:%d\n", getpid());
 47         exit(1);
 48     }
 49     sleep(3);
 50     wait(NULL);
 51     wait(NULL);
52     wait(NULL);
 53     printf("parent pid: %d\n", getpid());
 54
 55     return 0;
 56 }
 57
阅读(195) | 评论(0) | 转发(0) |
0

上一篇:fork进程创建。。。

下一篇:feof函数

给主人留下些什么吧!~~