Chinaunix

标题: 求助关于getpid(),getppid()的问题 [打印本页]

作者: mustconfident    时间: 2013-11-20 20:07
标题: 求助关于getpid(),getppid()的问题
  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,按道理应该相等的,为什么不相等呢?

作者: wenhq    时间: 2013-11-21 15:15
是不是你的父进程推出而子进程被init进程接管了,所谓的orphan进程?

作者: noah_wang    时间: 2013-11-28 00:23
父进程wait()就得到你想要的了




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2