ubuntu中孤兒進程的父進程pid並不是1??

    剛剛寫了一個孤兒進程,順手打印了他的ppid,居然發現不是1,什麼鬼??!!!
因爲在發現這個結果之前這個進程已經跑了很多遍了,新fork的進程都沒有退出,以爲是因爲這個原因所以結果跟我想的一樣。
     然後sudo reboot,接着運行發現運行結果孤兒進程的ppid並不是1,ps aux發現這個代替了init進程來接管孤兒進程的進程名字叫做init–user,重啓了幾回都是這樣的,到現在問題還沒解決,寫博文備存。貼上代碼。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


int creat_orphan_proc()
{
        pid_t pid;
        int i;

        pid = fork();
        for(i - 0;i < 3;i++)
        {

        if(pid == -1)
        {
                printf("creat child error ...\n");
                return -1;
        }
        else if(pid == 0)
        {
                printf("I`m child proc my pid is %d,my parent pid is %d\n",getpid(),getppid());
                sleep(3);
        }

        else
        {
                printf("I`m parent proc my pid is %d,I will exit\n",getpid());
                exit(0);
        }
        }
}

int main()
{
        creat_orphan_proc();
         return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章