免费注册 查看新帖 |

Chinaunix

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

[学习分享] 求助关于getpid(),getppid()的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-11-20 20:07 |只看该作者 |倒序浏览
  1 #include<stdio.h>
  2 #include<unistd.h>
  3 #include<stdlib.h>
  4
  5 int main()
  6 {
  7     pid_t id;
  8     id=fork();
  9      if(id == -1)
10 {
11     perror("fujinchengdiaoyongshibai");
12 }
13      if(id == 0)
14     {
15         printf("child process id is %d,the father id is %d \n",getpid(),getppid());
16     }
17
18     if(id > 0)
19     {
20         printf("father process id is %d,the father father id is%d\n",getpid(),getppid());
21     }
22     //  printf("PID = %d\n",getpid());
23     //  printf("PPID = %d\n",getppid());


为什么我得到的结果是呢?

father process id is 2531,the father father id is2335
child process id is 2532,the father id is 1
父进程是2531,但是在子进程中得到的父进程却是1,按道理应该相等的,为什么不相等呢?

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每月发帖之星
日期:2015-09-11 19:30:52IT运维版块每周发帖之星
日期:2015-09-11 19:20:31IT运维版块每日发帖之星
日期:2015-08-26 06:20:00每日论坛发贴之星
日期:2015-08-20 06:20:00IT运维版块每日发帖之星
日期:2015-08-20 06:20:002015年辞旧岁徽章
日期:2015-03-03 16:54:15金牛座
日期:2014-05-04 16:58:09双子座
日期:2013-12-17 16:44:37辰龙
日期:2013-11-22 15:20:59狮子座
日期:2013-11-18 22:55:08射手座
日期:2013-11-12 10:54:26
2 [报告]
发表于 2013-11-21 15:15 |只看该作者
是不是你的父进程推出而子进程被init进程接管了,所谓的orphan进程?

论坛徽章:
0
3 [报告]
发表于 2013-11-28 00:23 来自手机 |只看该作者
父进程wait()就得到你想要的了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP