天天看點

Linux:僵死程序

僵死程序的産生:

     1.當父程序未結束,子程序結束,并且父程序未擷取子程序的退出狀态。子程序被稱為僵死程序。

     2.程序執行結束,程序主體(執行代碼、資料、資源)都釋放,而其PCB 并未釋放。

     3.孤兒程序:父程序死後僵死程序成為“孤兒程序”,由init接管

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

void fun(int sign)
{
    wait(NULL);
}

int main()
{
    pid_t n=fork();
    assert(n!=NULL);
    
    if(n==0)
    {
        printf("child start\n");
        sleep(5);
        printf("child end\n");
    }
    else()
    {
        printf("father start\n");
        sleep(50);
        printf("father end\n");
    }
}
           

僵死程序的處理:

父程序直接進行wait(NULL),若程序為阻塞運作即程序狀态變為阻塞,若非阻塞運作,即條件未完成,立即傳回,就會出錯。

繼續閱讀