linux fork兩次避免殭屍進程的程序(簡單)

#include<stdio.h>
#include<unistd.h>

int main()
{
    pid_t pid,pid1;
    int status;

    if( (pid = fork())<0)
    {
        printf("fork error\n");
        return -1;
    }
    else if( pid == 0)
    {
        printf("%d,%d\n",getpid(),getppid());
        if( (pid1 = fork())<0)
        {
            printf("fork error\n");
            return -1;
        }
        else if(pid1 == 0)
        {
            printf("%d,%d\n",getpid(),getppid());
            system("ps -ef|grep dlsc30");
            return 0;
        }
        exit(0);
    }
    wait(&status);
    printf("%d,%d\n",getpid(),getppid());
    while(1)
    {
        printf("i will not break\n");
        sleep(5);
    }
    exit(0);
}
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息