• 【APUE】孤儿进程与僵死进程


    基本概念:

    在unix/linux中,正常情况下,子进程是通过父进程创建的,子进程在创建新的进程。子进程的结束和父进程的运行是一个异步过程,即父进程永远无法预测子进程 到底什么时候结束。 当一个 进程完成它的工作终止之后,它的父进程需要调用wait()或者waitpid()系统调用取得子进程的终止状态。

    孤儿进程:一个父进程退出,而它的一个或多个子进程还在运行,那么那些子进程将成为孤儿进程。孤儿进程将被init进程(进程号为1)所收养,并由init进程对它们完成状态收集工作。

    僵尸进程:一个进程使用fork创建子进程,如果子进程退出,而父进程并没有调用wait或waitpid获取子进程的状态信息,那么子进程的进程描述符仍然保存在系统中。这种进程称之为僵死进程。

    问题与危害

    unix提供了一种机制可以保证只要父进程想知道子进程结束时的状态信息, 就可以得到。这种机制就是: 在每个进程退出的时候,内核释放该进程所有的资源,包括打开的文件,占用的内存等。 但是仍然为其保留一定的信息(包括进程号the process ID,退出状态the termination status of the process,运行时间the amount of CPU time taken by the process等)。直到父进程通过wait / waitpid来取时才释放。 但这样就导致了问题,如果进程不调用wait / waitpid的话, 那么保留的那段信息就不会释放,其进程号就会一直被占用,但是系统所能使用的进程号是有限的,如果大量的产生僵死进程,将因为没有可用的进程号而导致系统不能产生新的进程. 此即为僵尸进程的危害,应当避免。孤儿进程是没有父进程的进程,孤儿进程这个重任就落到了init进程身上

    每当出现一个孤儿进程的时候,内核就把孤 儿进程的父进程设置为init,而init进程会循环地wait()它的已经退出的子进程。因此孤儿进程并不会有什么危害。

    孤儿进程与僵死进程示例

    孤儿进程:

    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <unistd.h>
    
    int main()
    {
        pid_t pid;
        //创建一个进程
        pid = fork();
        //创建失败
        if (pid < 0)
        {
            perror("fork error:");
            exit(1);
        }
        //子进程
        if (pid == 0)
        {
            printf("I am the child process.
    ");
            //输出进程ID和父进程ID
            printf("pid: %d	ppid:%d
    ",getpid(),getppid());
            printf("I will sleep five seconds.
    ");
            //睡眠5s,保证父进程先退出
            sleep(5);
            printf("pid: %d	ppid:%d
    ",getpid(),getppid());
            printf("child process is exited.
    ");
        }
        //父进程
        else
        {
            printf("I am father process.
    ");
            //父进程睡眠1s,保证子进程输出进程id
            sleep(1);
            printf("father process is  exited.
    ");
        }
        return 0;
    }

    僵死进程:

    #include <stdio.h>
    #include <unistd.h>
    #include <errno.h>
    #include <stdlib.h>
    
    int main()
    {
        pid_t pid;
        pid = fork();
        if (pid < 0)
        {
            perror("fork error:");
            exit(1);
        }
        else if (pid == 0)
        {
            printf("I am child process.I am exiting.
    ");
            exit(0);
        }
        printf("I am father process.I will sleep two seconds
    ");
        //等待子进程先退出
        sleep(2);
        //输出进程信息
        system("ps -o pid,ppid,state,tty,command");
        printf("father process is exiting.
    ");
        return 0;
    }

    僵死进程解决办法:

    1)通过信号机制

    子进程退出时向父进程发送SIGCHLD信号,父进程处理SIGCHLD信号。在信号处理函数中调用wait进行处理僵尸进程

    2)fork两次

    第一个child process创建子进程后自己退出,这样它的子进程变成孤儿进程。将子进程成为孤儿进程,从而其的父进程变为init进程,通过init进程可以处理僵尸进程

    参考:

    http://www.cnblogs.com/Anker/p/3271773.html

  • 相关阅读:
    第二个月课堂009python之总结(002)
    python的接口自动化测试+ddt数据驱动(*****)
    python题(002)
    selenium问题归纳一:多个句柄索引会乱的问题
    多测师讲解python讲解__xlwt__模块(拓展莫模块002)
    多测师python面试题__整理和答案(001)高级讲师肖sir
    第二个月课堂012讲解 _ Unittest框架(上)_高级讲师肖sir
    多测师讲解课堂11课__selenium__错归纳_高级讲师肖sir
    ActionChains定位元素
    css使用方法
  • 原文地址:https://www.cnblogs.com/ljygoodgoodstudydaydayup/p/3873323.html
Copyright © 2020-2023  润新知