1. 程式人生 > >Linux Guard Service - 殺死守護進程

Linux Guard Service - 殺死守護進程

div hsi death onf confd sig host roo 執行

殺死某個子進程

殺死守護進程的子進程後,改進程會變為僵屍進程

 14087 ?        Ss     0:00 ./test4-1
 14088 ?        S      0:00  \_ ./test4-1
 14089 ?        S      0:00  \_ ./test4-1
 14090 ?        S      0:00  \_ ./test4-1
 14091 ?        S      0:00  \_ ./test4-1
 14092 ?        S      0:00  \_ ./test4-1
 14093 ?        S      0:00  \_ ./test4-1
 14094 ?        S      0:00  \_ ./test4-1
 14095 ?        S      0:00  \_ ./test4-1
 14096 ?        S      0:00  \_ ./test4-1
 14097 ?        S      0:00  \_ ./test4-1
[root@localhost 04]# kill -9 14090
[root@localhost 04]# ps -xf

執行後:

 14087 ?        Ss     0:00 ./test4-1
 14088 ?        S      0:00  \_ ./test4-1
 14089 ?        S      0:00  \_ ./test4-1
 14090 ?        Z      0:00  \_ [test4-1] <defunct>
 14091 ?        S      0:00  \_ ./test4-1
 14092 ?        S      0:00  \_ ./test4-1
 14093 ?        S      0:00  \_ ./test4-1
 14094 ?        S      0:00  \_ ./test4-1
 14095 ?        S      0:00  \_ ./test4-1
 14096 ?        S      0:00  \_ ./test4-1
 14097 ?        S      0:00  \_ ./test4-1
[root@localhost 04]#

殺死父進程

守護進程的父進程後,僵屍進程釋放,正常子進程變為正常進程

[root@localhost 04]# kill -9 14087
[root@localhost 04]# ps -xf

僵屍進程消失了

 14088 ?        S      0:00 ./test4-1
 14089 ?        S      0:00 ./test4-1
 14091 ?        S      0:00 ./test4-1
 14092 ?        S      0:00 ./test4-1
 14093 ?        S      0:00 ./test4-1
 14094 ?        S      0:00 ./test4-1
 14095 ?        S      0:00 ./test4-1
 14096 ?        S      0:00 ./test4-1
 14097 ?        S      0:00 ./test4-1

殺死所有進程

直接使用進程名稱

pkill 進程名

讓子進程隨父進程一同結束

在創建進程後使用prctl,監聽父進程的DEATHSIG

for (i = 0; i < 10; i++) {
    sleep(3);
    printf("new fork() process pid = %d \n", pid);
    pid = fork();
    if (pid == 0) {
            prctl(PR_SET_PDEATHSIG,SIGKILL);
            break;

    }
}

當父進程死亡後,子進程會自動收到信號並結束,不會產生僵屍進程和孤兒進程

 14508 ?        Ss     0:00 ./test4-2
 14509 ?        S      0:00  \_ ./test4-2
 14510 ?        S      0:00  \_ ./test4-2
 14511 ?        S      0:00  \_ ./test4-2
 14512 ?        S      0:00  \_ ./test4-2
 14513 ?        S      0:00  \_ ./test4-2
 14514 ?        S      0:00  \_ ./test4-2
 14515 ?        S      0:00  \_ ./test4-2
 14516 ?        S      0:00  \_ ./test4-2
 14517 ?        S      0:00  \_ ./test4-2
 14518 ?        S      0:00  \_ ./test4-2
[root@localhost 04]# kill -9 14508
[root@localhost 04]# ps -xf

  6786 ?        S      0:00 /usr/libexec/gconfd-2
[root@localhost 04]# 
[root@localhost 04]# 
[root@localhost 04]#

Linux Guard Service - 殺死守護進程