孤儿进程
父进程运行结束,但子进程还在运行(未运行结束)的子进程就称为孤儿进程(Orphan Process)。
孤儿进程最终会被 init 进程(进程号为 1 )所收养,并由 init 进程对它们完成状态收集工作。
在32位系统中为进程号1的进程收养,在64位系统中则不同
孤儿进程是没有父进程的进程,为避免孤儿进程退出时无法释放所占用的资源而变为僵尸进程(什么是僵尸进程),进程号为 1 的 init 进程将会接受这些孤儿进程,这一过程也被称为“收养”。
init 进程就好像是一个孤儿院,专门负责处理孤儿进程的善后工作。每当出现一个孤儿进程的时候,内核就把孤 儿进程的父进程设置为 init ,而 init 进程会循环地 wait() 它的已经退出的子进程。
这样,当一个孤儿进程凄凉地结束了其生命周期的时候,init 进程就会出面处理它的一切善后工作。因此孤儿进程并不会有什么危害。
注意:如果是64位系统,孤儿进程的父进程号并不是 1 号。
孤儿进程的测试例子:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main()
{
pid_t pid;
//创建进程
pid = fork();
if (pid < 0)
{ // 出错
perror("fork error:");
exit(1);
}
else if (pid == 0)
{//子进程
sleep(2); // 延迟一段时间 保证父进程先结束
printf("son proess: [son id] = %d, [son's father id] = %d\n", getpid(), getppid());
exit(0);
}
else if(pid > 0)
{ // 父进程
printf("father process, i am exited\n");
exit(0);
}
return 0;
}
父进程先退出,子进程后退出,子进程的资源释放会交给1号进程
运行结果如下:
僵尸进程(Zombie Process)
进程已运行结束,但进程的占用的资源未被回收,这样的进程称为僵尸进程。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
// 父进程没有退出,子进程先退出,然后父进程没有回收资源
int main()
{
//fork a child process
pid_t pid = fork();
if (pid > 0) //parent process
{
printf("in parent process, sleep for one miniute...zZ...\n");
for(int i = 0; i < 30; i++){
sleep(1);
printf("sleeping:%d,pid:%d, ppid:%d\n",i,getpid(),getppid());
}
printf("after sleeping, and exit!\n");
}
else if (pid == 0)
{
//child process exit, and to be a zombie process
printf("im child process, and exit! pid:%d, ppid:%d",getpid(),getppid());
exit(0);
}
return 0;
}
运行结果:
在终端敲:ps -ef | grep defunct,后面尖括号里是 defunct 的都是僵尸进程。我们另启一个终端,查看进程的状态,有哪些是僵尸进程:
如何避免僵尸进程?
1)最简单的方法,父进程通过 wait() 和 waitpid() 等函数等待子进程结束,但是,这会导致父进程挂起。
2)如果父进程要处理的事情很多,不能够挂起,通过 signal() 函数人为处理信号 SIGCHLD , 只要有子进程退出自动调用指定好的回调函数,因为子进程结束后, 父进程会收到该信号 SIGCHLD ,可以在其回调函数里调用 wait() 或 waitpid() 回收
测试代码如下:
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <signal.h>
int main()
{
pid_t pid;
// 忽略子进程退出信号的信号
// 那么子进程结束后,内核会回收, 并不再给父进程发送信号
signal(SIGCHLD, SIG_IGN);
pid = fork(); // 创建进程
if (pid < 0){ // 出错
perror("fork error:");
exit(1);
}else if(pid == 0){ // 子进程
printf("I am child process,pid id %d.I am exiting.\n",getpid());
exit(0);
}else if(pid > 0){ // 父进程
sleep(2); // 保证子进程先运行
printf("I am father, i am exited\n\n");
system("ps -ef | grep defunct"); // 查看有没有僵尸进程
#if 0
/***************************************************************
// 父进程处理第一个子进程退出,回收其资源
if (waitpid(pid, NULL, 0) != pid){
perror("waitepid error:");
exit(1);
}
********************************************************************/
#endif
}
return 0;
}
可以通过信号捕获并忽略交给系统去回收,也可以自己主动回收,但是会导致主进程挂起,一直等待子进程结束回收资源