zoukankan      html  css  js  c++  java
  • Linux _孤儿进程和僵尸进程 浅见

    孤儿进程与僵尸进程

    1. 什么是孤儿进程、僵尸进程
      1) 孤儿进程
      父进程先结束,则其子进程变成“孤儿进程”。
      变成孤儿进程之后,该进程由init接管并清理(认init作为父进程)

      实例:main1.c
      2) 僵尸进程
      子进程结束后的状态,取决于该子进程结束时,其父进程的状态:
      如果子进程结束时,其父进程还没有结束,而其父进程没有在执行waitpid或者wait(parm1), 则该子进程变成僵尸进程。
      注意:事实上,即使子进程结束时,父进程正在执行wait(),也只能达到同步效果(父进程等待子进程结束后才不再阻塞)
      该子进程仍将处于僵尸状态。直到该僵尸进程的父进程结束,从而该僵尸进程变成孤儿进程,再由init接管并回收。

      子进程变成僵尸进程后,直到父进程调用waitpid回收该子进程。
      子进程变成僵尸进程后,如果直到父进程结束,该父进程都没有调用waitpit,
      则当该父进程结束后,该僵尸进程变成孤儿进程,再由init接管并回收。

      处于僵尸状态的进程,不能用kill -9杀死!
      
      实例: main2_1.c  
              main2_2.c (直接使用wait(), 不能回收僵尸进程)
      

      3) 孤儿进程与僵尸进程的影响
      僵尸进程会占用系统资源(在进程表占用一个进程表项),如果很多,则会严重影响服务器的性能
      孤儿进程不会占用系统资源,对系统无影响(它将被init进程接管并清理)

      4) 孤儿进程的避免
      方法1: 父进程使用waitpid
      当父进程还没有执行waitpid或者wait(param1)时,
      子进程持续维持僵尸状态。
      直到父进程执行waitpid或者wait(param1)
      main5.c

              缺点:使用wait和waitpid时将阻塞父进程
      

      方法2: 在父进程中捕捉SIGCHLD信号(子进程结束时,产生该信号)
      然后在该信号的处理函数中(自定义)使用wait或waitpid
      main3.c

      方法3:调用两次fork, 用孙子进程执行原子进程的任务,远来的子进程马上结束。
      使得,孙子进程成为孤儿进程。
      注意,需要在主进程中回收子进程,否则子进程又变成僵尸进程。
      main4.c

      方法4:信号处理

    main1.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <signal.h>
    
    
    int main(void)
    {
        pid_t pd;
    
        pd = fork();
        if (pd == -1) {
            printf("fork error!
    ");
            exit(1);
        } else if (pd  == 0) {
            printf("child process's parent porcess is %d
    ", getppid());
            sleep(5);
            printf("child process's parent porcess is %d
    ", getppid());
            exit(0);
        } else {
              sleep(1);
              printf("parent process end!
    ");
        }
    }
    

    main2_1.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <signal.h>
    
    
    int main(void)
    {
        pid_t pd;
    
        pd = fork();
        if (pd == -1) {
            printf("fork error!
    ");
            exit(1);
        } else if (pd  == 0) {
            printf("child process end!
    ");
            exit(0);
        } else {
            while(1);
        }
    }

    main2_2.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <signal.h>
    
    
    int main(void)
    {
        pid_t pd;
    
        pd = fork();
        if (pd == -1) {
            printf("fork error!
    ");
            exit(1);
        } else if (pd  == 0) {
            //sleep(3);
            printf("child process end!
    ");
            exit(0);
        } else {
            printf("wait begin...
    ");
    
            sleep(10);
    
            system("ps");
                wait(pd, NULL, 0);
               system("ps");
                //wait();
            printf("wait end!
    ");
    
            while(1);
        }
    }
    

    main3.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <signal.h>
    
    
    static void sig_child_handler(int sig)
    {
        sleep(10);
        wait();
        printf("SIGCHLD had catched!
    ");           
    }
    
    int main(void)
    {
        pid_t pd;
    
        signal(SIGCHLD, sig_child_handler);
        pd = fork();
        if (pd == -1) {
            printf("fork error!
    ");
            exit(1);
        } else if (pd  == 0) {
            sleep(1);
            printf("child process end!
    ");
            exit(0);
        } else {
            while(1);
        }
    }
    

    main4.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <signal.h>
    
    
    
    
    int main(void)
    {
        pid_t pd;
    
        pd = fork();
        if (pd == -1) {
            printf("fork error!
    ");
            exit(1);
        } else if (pd  == 0) {
            //◊”Ω¯≥Ã
            pd = fork();
            if (pd == -1) {
                printf("fork error!
    ");
                exit(1);
            } else if (pd == 0) {
                //ÀÔ◊”Ω¯≥Ã
                printf("parent process is %d
    ", getppid());
                sleep(1);
                printf("parent process is %d
    ", getppid());
                exit(0);
            } else {
                //◊”Ω¯≥Ã
                exit(0);
            }
    
        } else {
               waitpid(pd, 0, 0);     
            while(1);
        }
    }
    
  • 相关阅读:
    常用的 JavaScript 简写方法
    user-select详解
    惊人的CSS和JavaScript动画logos例子
    debounce与throttle区别
    Vue.js中data,props和computed数据
    字体图标出现乱码的兼容方案
    ios调用Html内JS alert 不能点击关闭为甚?
    统一诊断服务 (Unified diagnostic services , UDS) (二)
    统一诊断服务 (Unified diagnostic services , UDS) (一)
    CAN总线同步
  • 原文地址:https://www.cnblogs.com/Sico2Sico/p/5384204.html
Copyright © 2011-2022 走看看