【问题标题】:is psutil.STATUS_DEAD status same as psutil.STATUS_ZOMBIE?psutil.STATUS_DEAD 状态是否与 psutil.STATUS_ZOMBIE 相同?
【发布时间】:2021-07-24 15:20:23
【问题描述】:

在 python psutil 模块中,我看到一个进程的两个状态,psutil.STATUS_DEAD psutil.STATUS_ZOMBIE。需要了解两者的区别。我可以使用 'kill -1' 和 'kill -3' 命令模拟 Zombie 进程,但无法模拟 Dead 进程。

这里有什么想法吗?

【问题讨论】:

    标签: python-3.8 psutil


    【解决方案1】:

    它们与 'psutil/_commom.py' 中定义的不同:

    STATUS_ZOMBIE = "zombie"
    STATUS_DEAD = "dead"
    

    但如果你在 5.9.0 版本中搜索 STATUS_DEAD,你可能会发现它们在某种意义上可能是相同的。

    在 'psutil/_psbsd.py' 中显示 STATUS_DEAD 未用于任何 BSD 平台

    # OPENBSD
            # According to /usr/include/sys/proc.h SZOMB is unused.
            # test_zombie_process() shows that SDEAD is the right
            # equivalent. Also it appears there's no equivalent of
            # psutil.STATUS_DEAD. SDEAD really means STATUS_ZOMBIE.
            # cext.SZOMB: _common.STATUS_ZOMBIE,
            cext.SDEAD: _common.STATUS_ZOMBIE,
            cext.SZOMB: _common.STATUS_ZOMBIE,
    

    在关于 Linux 的 'psutil/_pslinux.py' 中显示:

    # See:
    # https://github.com/torvalds/linux/blame/master/fs/proc/array.c
    # ...and (TASK_* constants):
    # https://github.com/torvalds/linux/blob/master/include/linux/sched.h
    PROC_STATUSES = {
        "R": _common.STATUS_RUNNING,
        "S": _common.STATUS_SLEEPING,
        "D": _common.STATUS_DISK_SLEEP,
        "T": _common.STATUS_STOPPED,
        "t": _common.STATUS_TRACING_STOP,
        "Z": _common.STATUS_ZOMBIE,
        "X": _common.STATUS_DEAD,
        "x": _common.STATUS_DEAD,
        "K": _common.STATUS_WAKE_KILL,
        "W": _common.STATUS_WAKING,
        "I": _common.STATUS_IDLE,
        "P": _common.STATUS_PARKED,
    }
    

    状态对应linux的任务状态:

    // https://github.com/torvalds/linux/blame/master/fs/proc/array.c
    static const char * const task_state_array[] = {
    
        /* states in TASK_REPORT: */
        "R (running)",      /* 0x00 */
        "S (sleeping)",     /* 0x01 */
        "D (disk sleep)",   /* 0x02 */
        "T (stopped)",      /* 0x04 */
        "t (tracing stop)", /* 0x08 */
        "X (dead)",     /* 0x10 */
        "Z (zombie)",       /* 0x20 */
        "P (parked)",       /* 0x40 */
    
        /* states beyond TASK_REPORT: */
        "I (idle)",     /* 0x80 */
    };
    

    因此,它们在 linux 中的区别是显而易见的。 What Is a “Zombie Process” on Linux?

    在 psutil 中,如果您使用 psutil.Popen() 创建了一个进程 p,则将其杀死。 只要父进程没有终止或者你不调用p.wait(),该进程将始终保持“僵尸”状态。

    【讨论】:

      猜你喜欢
      • 2016-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-13
      • 2011-01-28
      相关资源
      最近更新 更多