在WSL中创建僵尸进程失败

前言:最近想在WSL中试一下僵尸进程的创建,但发现并不能成功,最后在原生Ubuntu上成功。
编写C语言文件a.c用于创建僵尸进程,内容如下:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
    pid_t pid = fork();
    if(pid<0)
    {
        printf("ERROR
");
    }
    else if(pid==0)
    {
        exit(0);
    }
    else
    {
        while(1){}
    }
    return 0;
}

编译并在后台运行:

gcc a.c -o test
./test &

并通过ps -l查看,S一栏的内容为Z的即为僵尸进程
但在WSL中经过多次实验均不能成功创建僵尸进程,最后用ubuntu一次成功,效果如下:

可见产生了僵尸进程,其pid为8906

Kill掉父进程后僵尸进程消失

后记:猜想WSL中具体的实现与真正的linux仍存在区别,因此上谷歌查询,发现也有人提过类似的问题:https://unix.stackexchange.com/questions/519463/wsl-different-behavior-for-zombie-processes
贴一段下面回答者的总结吧:
Since there is actually very little you can do with a zombie process, and the zombies are not cluttering up the PID number space, it might be a valid choice for WSL to not attempt to fully reproduce the concept of zombie processes at all for commands like ps.
简单来讲就是不要尝试在WSL上去学习僵尸进程的概念

原文地址:https://www.cnblogs.com/drperry/p/12688870.html