ubuntu下孤儿进程的ppid为何不是1?

345 Views

linux下用fork()可以创建一个新进程,如果该进程的父进程先于该进程死亡,该子进程就会变成孤儿进程,被init进程收养,其ppid本应为1,即init进程. 代码如下:

#include

#include

#include

#include

int main(void)

{

pid_t pid;

printf("Process Creation Study\n");

pid = fork();

switch(pid) {

case 0:

while(1) {

printf("I'm a child process,my pid is %d,my parent pid id %d\n",getpid(),getppid());

sleep(2);

}

case -1:

perror("Process creation failed\n");

break;

default:

printf("I'm a Parent process,my pid id %d\n",getpid());

exit(0);

}

return 0;

}

但我在ubuntu下执行时却发现不是init进程,而是/sbin/upstart –user

4 1000 1496 1479 20 0 48360 4944 poll_s Ss ? 0:00 /sbin/upstart --user

其父进程是:lightdm

root 1479 0.0 0.0 230300 6388 ? Sl 08:08 0:00 lightdm --session-child 12 19

百度一下发现这是个桌面软件,那么不在图形界面是不是就被init进程收养了呢? 然后ctrl+alt+F1进入字符界面,再次运行刚才程序,出现了结果,ppid=1.

TCL家用电器(合肥)有限公司怎么样?
怎么修一字眉(新手如何修好一字眉)