Ubuntu可以自动回收僵尸进程了?

题目描述

最近在恶补Linux C编程,然后按照教程编写多进程程序,结果尴尬了,下面这段代码没有使用wait回收子进程,但是在ps aux中却找不到已经子进程了!

百度找了很久也没找到相关资料,我记得我在CentOS上跑的时候应该没有问题。

相关代码

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main()
{
    pid_t pid = fork();
    if (pid == 0) {
        printf("I am child, pid = %d, ppid = %d\n", getpid(), getppid());
        sleep(2);
        printf("I am child, I will die! \n");
    } else if (pid > 0) {
        while(1) {
            printf("I am father, very happy, pid = %d\n", getpid());
            sleep(1);
        }
    }
    return 0;
}

开发环境

GCC:

gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Linux:

Windows10子系统,在应用商店下载的Ubuntu。

阅读 2.3k
1 个回答
新手上路,请多包涵

在进程运行时候另起一个界面去ps查看

父进程退出,系统自动回收子进程

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题