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

来源:互联网 发布:net域名名站 编辑:程序博客网 时间:2024/06/08 14:41

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

#include<stdio.h>#include<sys/types.h>#include<unistd.h>#include<stdlib.h>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.

1 0
原创粉丝点击