题目描述
最近在恶补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。
在进程运行时候另起一个界面去ps查看
父进程退出,系统自动回收子进程