国产激情久久久久影院小草_国产91高跟丝袜_99精品视频99_三级真人片在线观看

Linux下子進(jìn)程與父進(jìn)程的關(guān)系

時(shí)間:2024-10-10 01:33:15 Linux認(rèn)證 我要投稿
  • 相關(guān)推薦

Linux下子進(jìn)程與父進(jìn)程的關(guān)系

  我們知道,Linux下父進(jìn)程可以使用fork 函數(shù)創(chuàng)建子進(jìn)程,但是當(dāng)父進(jìn)程先退出后,子進(jìn)程會(huì)不會(huì)也退出呢?到底Linux下父進(jìn)程和子進(jìn)程的關(guān)系如何呢?下文為大家分享最新代碼如下:

  通過(guò)下面這個(gè)小實(shí)驗(yàn),我們能夠很好的看出來(lái):

  復(fù)制代碼

  /******** basic.c ********/

  1 #include "basic.h"

  2

  3 pid_t Fork(void)

  4 {

  5 pid_t pid = fork();

  6 if (pid < 0) {

  7 fprintf(stderr, "Fork error: %s\n", strerror(errno));

  8 exit(0);

  9 }

  10

  11 return pid;

  12 }

  復(fù)制代碼

  1 ********** basic.h ***********

  2

  3 #ifndef __CSAPP_BASIC_H

  4 #define __CSAPP_BASIC_H

  5

  6 #include

  7 #include

  8 #include

  9 #include

  10 #include

  11 #include

  12 /* function definition concerned with basic.c */

  13 pid_t Fork();

  14

  15 #endif

  復(fù)制代碼

  1 ******* fork.c *********

  2

  3 #include "basic.h"

  4

  5 int main()

  6 {

  7 int pid = Fork();

  8 int x = 2;

  9

  10 if (pid == 0) {

  11 printf("child: pid = %d, ppid = %d, x = %d\n", getpid(), getppid(), ++x);

  12 sleep(3);

  13

  14 printf("child: pid = %d, ppid = %d, x = %d\n", getpid(), getppid(), ++x);

  15 exit(0);

  16 }

  17

  18 printf("parent: pid = %d, ppid = %d, x = %d\n", getpid(), getppid(), --x);

  19

  20 }

  通過(guò) gcc fork.c basic.c -o fork 編譯即可的 fork 程序。 運(yùn)行 ./fork

  可以看出父進(jìn)程首先退出,退出前child的PPID為12256, 退出后子進(jìn)程的PPID變?yōu)榱?1.說(shuō)明父進(jìn)程退出后的子進(jìn)程由 init 超級(jí)進(jìn)程1領(lǐng)養(yǎng)。而該進(jìn)程是不絕不會(huì)退出的。

【Linux下子進(jìn)程與父進(jìn)程的關(guān)系】相關(guān)文章:

Linux進(jìn)程關(guān)系最新解讀201609-09

Linux系統(tǒng)守護(hù)進(jìn)程的啟動(dòng)方法09-11

關(guān)于linux查看進(jìn)程ps top區(qū)別06-11

多進(jìn)程O(píng)SPF及進(jìn)程號(hào)的意義05-18

Linux讓進(jìn)程在后臺(tái)可靠運(yùn)行的幾種方法09-08

圍棋棋局的進(jìn)程劃分10-02

常見(jiàn)系統(tǒng)進(jìn)程大全07-26

如何避免出現(xiàn)僵尸進(jìn)程09-27

php多進(jìn)程編程詳解201706-04

DOS中判斷進(jìn)程是否存在的方法07-05