環境:Vmware Workstation;CentOS-6.4-x86_64
說明:
1、僵死進程:子進程死亡,父進程存在。
2、子進程不會被回收,回收的時機是父進程退出;也就是說,僵死進程的子進程,只有父進程退出的時候纔會被回收。
下面的程序是實現僵死進程的一個例子:
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *args[])
{
// 執行fork並獲取返回值
pid_t id = fork();
// 判斷fork是否成功
// 當返回值爲-1時,說明fork失敗
if (id == -1)
{
printf("fork failed : %s", strerror(errno));
}
if (id > 0)
{
// 父進程休眠20秒
sleep(20);
}
else
{
// 子進程立即退出
exit(0);
}
return 0;
}
編譯並執行程序:
[negivup@negivup mycode]$ gcc -o main main.c
[negivup@negivup mycode]$ main
程序還在執行,沒有退出的時候,打開一個新的終端,查看僵死進程的狀態
另一個終端: [negivup@negivup ~]$ ps -aux
negivup 15025 0.0 0.0 3916 340 pts/1 S+ 18:16 0:00 main 這是僵死進程
negivup 15026 0.0 0.0 0 0 pts/1 Z+ 18:16 0:00 [main] <defunct> 這是等待回收的子進程,defunct意思“死的”
這個死的進程,只有在父進程消亡的時候纔會被回收。
PS:根據傳智播客視頻學習整理得出。