前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >守护进程与僵尸进程

守护进程与僵尸进程

作者头像
bear_fish
发布2018-09-20 16:19:15
1.6K0
发布2018-09-20 16:19:15
举报
文章被收录于专栏:用户2442861的专栏

http://blog.csdn.net/russell_tao/article/details/7090033

04年时维护的第一个商业服务就用了两次fork产生守护进程的做法,前两天在网上看到许多帖子以及一些unix书籍,认为一次fork后产生守护进程足够了,各有道理吧,不过多了一次fork到底是出于什么目的呢?

进程也就是task,看看内核里维护进程的数据结构task_struct,这里有两个成员:

[cpp] view plaincopy

  1. struct task_struct {  
  2. volatile long state;  
  3. int exit_state;  
  4.     ...  
  5. }  

看看include/linux/sched.h里的value取值:

[cpp] view plaincopy

  1. #define TASK_RUNNING        0
  2. #define TASK_INTERRUPTIBLE  1
  3. #define TASK_UNINTERRUPTIBLE    2
  4. #define __TASK_STOPPED      4
  5. #define __TASK_TRACED       8
  6. /* in tsk->exit_state */
  7. #define EXIT_ZOMBIE     16
  8. #define EXIT_DEAD       32
  9. /* in tsk->state again */
  10. #define TASK_DEAD       64
  11. #define TASK_WAKEKILL       128
  12. #define TASK_WAKING     256
  13. #define TASK_STATE_MAX      512

可以看到,进程状态里除了大家都理解的running/interuptible/uninterruptible/stop等状态外,还有一个ZOMBIE状态,这个状态是怎么回事呢?

这是因为linux里的进程都属于一颗树,树的根结点是linux系统初始化结束阶段时启动的init进程,这个进程的pid是1,所有的其他进程都是它的子孙。除了init,任何进程一定有他的父进程,而父进程会负责分配(fork)、回收(wait4)它申请的进程资源。这个树状关系也比较健壮,当某个进程还在运行时,它的父进程却退出了,这个进程却没有成为孤儿进程,因为linux有一个机制,init进程会接管它,成为它的父进程。这也是守护进程的由来了,因为守护进程的其中一个要求就是希望init成为守护进程的父进程。

如果某个进程自身终止了,在调用exit清理完相关的内容文件等资源后,它就会进入ZOMBIE状态,它的父进程会调用wait4来回收这个task_struct,但是,如果父进程一直没有调用wait4去释放子进程的task_struct,问题就来了,这个task_struct谁来回收呢?永远没有人,除非父进程终止后,被init进程接管这个ZOMBIE进程,然后调用wait4来回收进程描述符。如果父进程一直在运行着,这个ZOMBIE会永远的占用系统资源,用KILL发任何信号量也不能释放它。这是很可怕的,因为服务器上可能会出现无数ZOMBIE进程导致机器挂掉。

来看看内核代码吧。进程在退出时执行sys_exit(C程序里在main函数返回会执行到),而它会调用do_exit,do_exit首先清理进程使用的资源,然后调用exit_notify方法,将进程置为僵尸ZOMBIE状态,决定是否要以init进程做为当前进程的父进程,最后通知当前进程的父进程:

kernel/exit.c

[cpp] view plaincopy

  1. static void exit_notify(struct task_struct *tsk)  
  2. {  
  3. int state;  
  4. struct task_struct *t;  
  5. struct list_head ptrace_dead, *_p, *_n;  
  6. if (signal_pending(tsk) && !tsk->signal->group_exit  
  7.         && !thread_group_empty(tsk)) {  
  8. /*
  9.          * This occurs when there was a race between our exit
  10.          * syscall and a group signal choosing us as the one to
  11.          * wake up.  It could be that we are the only thread
  12.          * alerted to check for pending signals, but another thread
  13.          * should be woken now to take the signal since we will not.
  14.          * Now we'll wake all the threads in the group just to make
  15.          * sure someone gets all the pending signals.
  16.          */
  17.         read_lock(&tasklist_lock);  
  18.         spin_lock_irq(&tsk->sighand->siglock);  
  19. for (t = next_thread(tsk); t != tsk; t = next_thread(t))  
  20. if (!signal_pending(t) && !(t->flags & PF_EXITING)) {  
  21.                 recalc_sigpending_tsk(t);  
  22. if (signal_pending(t))  
  23.                     signal_wake_up(t, 0);  
  24.             }  
  25.         spin_unlock_irq(&tsk->sighand->siglock);  
  26.         read_unlock(&tasklist_lock);  
  27.     }  
  28.     write_lock_irq(&tasklist_lock);  
  29. /*
  30.      * This does two things:
  31.      *
  32.      * A.  Make init inherit all the child processes
  33.      * B.  Check to see if any process groups have become orphaned
  34.      *  as a result of our exiting, and if they have any stopped
  35.      *  jobs, send them a SIGHUP and then a SIGCONT.  (POSIX 3.2.2.2)
  36.      */
  37.     INIT_LIST_HEAD(&ptrace_dead);  
  38.     <strong><span style="color:#ff0000;">forget_original_parent(tsk, &ptrace_dead);</span></strong>  
  39.     BUG_ON(!list_empty(&tsk->children));  
  40.     BUG_ON(!list_empty(&tsk->ptrace_children));  
  41. /*
  42.      * Check to see if any process groups have become orphaned
  43.      * as a result of our exiting, and if they have any stopped
  44.      * jobs, send them a SIGHUP and then a SIGCONT.  (POSIX 3.2.2.2)
  45.      *
  46.      * Case i: Our father is in a different pgrp than we are
  47.      * and we were the only connection outside, so our pgrp
  48.      * is about to become orphaned.
  49.      */
  50.     t = tsk->real_parent;  
  51. if ((process_group(t) != process_group(tsk)) &&  
  52.         (t->signal->session == tsk->signal->session) &&  
  53.         will_become_orphaned_pgrp(process_group(tsk), tsk) &&  
  54.         has_stopped_jobs(process_group(tsk))) {  
  55.         __kill_pg_info(SIGHUP, (void *)1, process_group(tsk));  
  56.         __kill_pg_info(SIGCONT, (void *)1, process_group(tsk));  
  57.     }  
  58. /* Let father know we died 
  59.      *
  60.      * Thread signals are configurable, but you aren't going to use
  61.      * that to send signals to arbitary processes. 
  62.      * That stops right now.
  63.      *
  64.      * If the parent exec id doesn't match the exec id we saved
  65.      * when we started then we know the parent has changed security
  66.      * domain.
  67.      *
  68.      * If our self_exec id doesn't match our parent_exec_id then
  69.      * we have changed execution domain as these two values started
  70.      * the same after a fork.
  71.      *  
  72.      */
  73. if (tsk->exit_signal != SIGCHLD && tsk->exit_signal != -1 &&  
  74.         ( tsk->parent_exec_id != t->self_exec_id  ||  
  75.           tsk->self_exec_id != tsk->parent_exec_id)  
  76.         && !capable(CAP_KILL))  
  77.         tsk->exit_signal = SIGCHLD;  
  78. /* If something other than our normal parent is ptracing us, then
  79.      * send it a SIGCHLD instead of honoring exit_signal.  exit_signal
  80.      * only has special meaning to our real parent.
  81.      */
  82. if (tsk->exit_signal != -1 && thread_group_empty(tsk)) {  
  83. int signal = tsk->parent == tsk->real_parent ? tsk->exit_signal : SIGCHLD;  
  84.         <span style="color:#ff0000;">do_notify_parent(tsk, signal);</span>  
  85.     } else if (tsk->ptrace) {  
  86.         do_notify_parent(tsk, SIGCHLD);  
  87.     }  
  88.     <span style="color:#ff0000;"><strong>state = EXIT_ZOMBIE;</strong></span>  
  89. if (tsk->exit_signal == -1 && tsk->ptrace == 0)  
  90.         state = EXIT_DEAD;  
  91.     tsk->exit_state = state;  
  92. /*
  93.      * Clear these here so that update_process_times() won't try to deliver
  94.      * itimer, profile or rlimit signals to this task while it is in late exit.
  95.      */
  96.     tsk->it_virt_value = 0;  
  97.     tsk->it_prof_value = 0;  
  98.     write_unlock_irq(&tasklist_lock);  
  99.     list_for_each_safe(_p, _n, &ptrace_dead) {  
  100.         list_del_init(_p);  
  101.         t = list_entry(_p,struct task_struct,ptrace_list);  
  102.         release_task(t);  
  103.     }  
  104. /* If the process is dead, release it - nobody will wait for it */
  105. if (state == EXIT_DEAD)  
  106.         release_task(tsk);  
  107. /* PF_DEAD causes final put_task_struct after we schedule. */
  108.     preempt_disable();  
  109.     tsk->flags |= PF_DEAD;  
  110. }  

大家可以看到这段内核代码的注释非常全。forget_original_parent这个函数还会把该进程的所有子孙进程重设父进程,交给init进程接管。

回过头来,看看为什么守护进程要fork两次。这里有一个假定,父进程生成守护进程后,还有自己的事要做,它的人生意义并不只是为了生成守护进程。这样,如果父进程fork一次创建了一个守护进程,然后继续做其它事时阻塞了,这时守护进程一直在运行,父进程却没有正常退出。如果守护进程因为正常或非正常原因退出了,就会变成ZOMBIE进程。

如果fork两次呢?父进程先fork出一个儿子进程,儿子进程再fork出孙子进程做为守护进程,然后儿子进程立刻退出,守护进程被init进程接管,这样无论父进程做什么事,无论怎么被阻塞,都与守护进程无关了。所以,fork两次的守护进程很安全,避免了僵尸进程出现的可能性。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015年01月02日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档