Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4732306
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: LINUX

2010-06-17 15:44:24

1、何谓僵尸进程?
         在linux系统中,一个已经终止但父进程尚未对其进行善后处理(释放子进程相关信息占用的资源)的子进程叫做僵尸进程 。子进程结束时,父进程调用pid_t wait(int *statloc)或者pid_t waitpid(pid_t pid,int *statloc,int options)获取内核中为子进程保存的信息(进程id,终止状态)。

2、僵尸进程避免分析
        创建进程时,子进程的终止状态要返回给父进程,但调用fork函数时候,若父进程比子进程提前结束,则由init进程领养:在一个进程终止时候,内核逐个检查所有活动进程,判断它是否是被终止进程的子进程,如果是,则将该进程父进程的ID更改为1,这样,每个进程都有父进程,init 进程只要检测有子进程终止就会调用wait或waitpid释放资源,防止僵尸进程。

3、解决办法:调用两次fork避免僵尸进程

#include "apue.h"
#include
int main(void)
{
    pid_t    pid;
    if ((pid = fork()) < 0) {
        err_sys("fork error");
    } else if (pid == 0) {                                               // 第一个子进程
        if ((pid = fork()) < 0)
            err_sys("fork error");
        else if (pid > 0)
            exit(0);                                                              //第二个子进程的父进程终止(第一个子进程)
       /*
         * 这里是第二个子进程(孤儿进程),其父进程已经变成了init进程,当亲生父进程在上面调用exit()终止后,在此处继续执         *行,当第二个子进程终止的时候,父进程init会获取其状态,从而避免僵尸进程嗯。
         */
        sleep(2);                                                               //保证父进程优先运行(第一个子进程)
        printf("second child, parent pid = %d\n", getppid());  //获取父进程ID(即init ID,为1)
        exit(0);                                                                  //第二个子进程终止
    }

    if (waitpid(pid, NULL, 0) != pid)                        //等待第一个子进程
        err_sys("waitpid error");

      printf("i am the parents of your parents\n");
    /*
     * 这里是继续执行原始父进程(第一个父进程),且知道不是第二个子进程的父进程
     */
    exit(0);                                                                     //第一个父进程终止
}


 

阅读(3066) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-06-20 10:13:01

你好!博客狠棒cheapMBT Shoes on sale! MBT Shoes are the combination of quality and fashion. MBT Shoes are perfect for lovers of the outdoors, hikers, mountain climbers, campers, and nature enthusiasts. MBT Lami Shoe are perfect for the rugged and the casual. In fact, they are perfect for just about anyone an