Linux系統編程——孤兒進程類比及分析__html5

來源:互聯網
上載者:User
孤兒進程介紹

父進程先於子進程結束,則子進程成為孤兒進程,子進程的父進程成為init進程,稱為init進程領養孤兒進程。孤兒進程就是父進程先死亡,子進程成為孤兒進了孤兒院了。 孤兒進程類比 代碼展示

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <fcntl.h>#include <error.h>#include <sys/errno.h>void perr(const char* str){    perror(str);    exit(1);}//驗證孤兒進程int main(int argc,char* argv[]){    pid_t pid;    if( (pid = fork()) ==  0 )  //子進程    {        printf("i am child process,pid = %d,ppid = %d,i sleep 5s.\n",getpid(),getppid());        sleep(5);        printf("now,i am orphan process,pid %d,ppid = %d.\n",getpid(),getppid());        exit(0);    }else if( pid > 0 )         //父進程    {        printf("i am parent process,pid = %d,ppid = %d.\n",getpid(),getppid());        sleep(1);        printf("parent process exit\n");    }else if( pid < 0 )    {        perr("fork error");    }    return 0;}
運行效果

孤兒進程分析

從代碼運行效果可以看出父進程先死亡,子進程成為孤兒進程後,父進程變為pid = 1的init進程由於父進程的父進程是bash進程,父進程執行完畢,回到bash調用回到控制台終端,此時孤兒進程還活著,5秒後要進行列印操作所以將輸出列印到螢幕所以bash的游標沒有在$的後面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.