python-3.8psutil

is psutil.STATUS_DEAD status same as psutil.STATUS_ZOMBIE?


In python psutil module, I see two status for a process, psutil.STATUS_DEAD and psutil.STATUS_ZOMBIE. I need to understand the difference between both. I am able to simulate Zombie process using kill -1 and kill -3 command, but not able to simulate Dead process.

Any thought here?


Solution

  • They are different as defined in the 'psutil/_commom.py':

    STATUS_ZOMBIE = "zombie"
    STATUS_DEAD = "dead"
    

    But if you search STATUS_DEAD in version 5.9.0, you may find they may be the same in some sense.

    In 'psutil/_psbsd.py' shows that STATUS_DEAD is not used in any BSD platforms

    # 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,
    

    In 'psutil/_pslinux.py' about Linux shows:

    # 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,
    }
    

    The statuses corresponds to the linux's task states:

    // 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 */
    };
    

    Therefore, the difference between them in linux is clear. What Is a “Zombie Process” on Linux?

    In psutil, if you create one process p using psutil.Popen(), then kill it. As long as the parent process isn't terminated or you don't call p.wait(), the process will always keep 'Zombie' status.