进程学习回顾---两次fork避免僵尸进程

来源:互联网 发布:怎么评价张学良 知乎 编辑:程序博客网 时间:2024/06/05 20:52
本文档的Copyleft归rosetta所有,使用GPL发布,可以自由拷贝、转载,转载时请保持文档的完整性。
参考资料:APUE2e
来源:http://blog.csdn.net/rosetta
        一个进程如果终止后未被其父进程回收,它将仍然占用资源,这种进程称为僵尸进程(zombie),它将对系统产生危害。
        可以通过ps -aux查看系统中是否存在僵尸进程(其状态显示为Z)。
        为避免产生僵尸进程,可以通过两次fork()操作,第一次fork产生第一个子进程,第二次fork由第一个子进程产生第二个子进程,目前的状态变成:爷爷->父亲->孙子。爷爷回收父亲后退出,此时孙子的父进程自动托管给init进程。而init进程无论如何会回收子进程的,所以这样就不会产生僵尸进程了。
一、两个系统调用
       #include <sys/types.h>
       #include <unistd.h>
       pid_t fork(void);
        创建一个新进程
                父进程返回值是子进程的pid
                子进程返回值为0
                 出错返回-1

       #include <sys/types.h>
       #include <sys/wait.h>

       pid_t wait(int *status);
       pid_t waitpid(pid_t pid, int *status, int options);
        回收子进程
        如果pid==-1,与wait等效。
        如果pid>0,等待回收指定pid的进程。
        status指针指向的内存单元存放进程返回的状态,如果不关心其状态可以至NULL,返回状态可以由对应宏判断(APUE2eP180的宏)。
        options选项如果是WNOHANG,将不阻塞。      

二、源码实例

        下面代码片段通过两次fork()避免僵尸进程的产生,来到APUE2e

        #include "apue.h"
        #include <sys/wait.h>
        
        int
        main(void)
        {
            pid_t   pid;
        
            if ((pid = fork()) < 0) {
                err_sys("fork error");
            } else if (pid == 0) {      /* first child */ //产生的第一个子进程
                if ((pid = fork()) < 0)
                    err_sys("fork error");
                else if (pid > 0)//如果进程id为第一个子进程则退出
                    exit(0);    /* parent from second fork == first child */
                  //否则进入孙子进程代码片段执行
                /*
                 * We're the second child; our parent becomes init as soon
                 * as our real parent calls exit() in the statement above.
                 * Here's where we'd continue executing, knowing that when
                 * we're done, init will reap our status.
                 */
                sleep(2);//孙子进程睡眠2秒以便其父进程被爷爷进程回收。
                printf("second child, parent pid = %d\n", getppid());
                exit(0);
            }
        
            if (waitpid(pid, NULL, 0) != pid)   /* wait for first child *///实参pid是指定的第一个子进程的pid,如果执行成功其返回值也为其回收的子进程pid.
                err_sys("waitpid error");
        
            /*
             * We're the parent (the original process); we continue executing,
             * knowing that we're not the parent of the second child.
             */
            exit(0);
        }