0

我必须在 linux C 中杀死/清理一个僵尸进程。我所知道的只是僵尸的 PID。

我在一个循环中创建了一些僵尸进程:

int i = 0;
for (i; i<5; i++)
{
  system("(: & exec sleep 30) &"); // create zombie for 30 sec
}

我可以使用以下方法获取他们的 PID 号:

system("ps aux | awk '{ print $8 " " $2 }' | grep -w Z");

但是如何仅使用 PID 杀死/清理它们?我将 PID 保存在变量和标准信号中:

kill(PID,9) 

甚至不起作用,因为该过程已经死了。还有其他方法可以实现吗?请不要问我为什么要制造僵尸来杀死他们。事实并非如此。

4

5 回答 5

3

“僵尸”是一个已经死亡的进程。你不能再杀了它。

“僵尸”实际上只是进程表中的一个条目,其中包含进程的退出状态。为了摆脱它,父进程必须调用wait()以检索该信息。或者,父进程可以自行退出;然后孩子成为所谓的“孤儿”并被initID 为 1 的进程收养。init自动收割所有死去的孩子。

于 2012-12-02T13:18:13.980 回答
3

这是我创建的用于杀死所有僵尸进程的脚本。它使用 GDB 调试器附加到父进程并发送一个 waitpid 来杀死僵尸进程。这将使父母活着,只会杀死僵尸。

需要安装 GDB 调试器,并且您需要以附加到进程的权限登录。这已经在 Centos 6.3 上测试过了

#!/bin/bash
##################################################################
# Script: Zombie Slayer
# Author: Mitch Milner
# Date:   03/13/2013 ---> A good day to slay zombies
#
# Requirements: yum install gdb
#               permissions to attach to the parent process
#
# This script works by using a debugger to
# attach to the parent process and then issuing
# a waitpid to the dead zombie. This will not kill
# the living parent process.
##################################################################

clear
# Wait for user input to proceed, give user a chance to cancel script
echo "***********************************************************"
echo -e "This script will terminate all zombie process."
echo -e "Press [ENTER] to continue or [CTRL] + C to cancel:"
echo "***********************************************************"
read cmd_string
echo -e "\n"

# initialize variables
intcount=0
lastparentid=0

# remove old gdb command file
rm -f /tmp/zombie_slayer.txt

# create the gdb command file
echo "***********************************************************"
echo "Creating command file..."
echo "***********************************************************"
ps -e -o ppid,pid,stat,command | grep Z | sort | while read LINE; do
  intcount=$((intcount+1))
  parentid=`echo $LINE | awk '{print $1}'`
  zombieid=`echo $LINE | awk '{print $2}'`
  verifyzombie=`echo $LINE | awk '{print $3}'`

  # make sure this is a zombie file and we are not getting a Z from
  # the command field of the ps -e -o ppid,pid,stat,command
  if [ "$verifyzombie" == "Z" ]
  then
    if [ "$parentid" != "$lastparentid" ]
    then
      if [ "$lastparentid" != "0" ]
      then
        echo "detach" >> /tmp/zombie_slayer.txt
      fi
    echo "attach $parentid" >> /tmp/zombie_slayer.txt
    fi
    echo "call waitpid ($zombieid,0,0)" >> /tmp/zombie_slayer.txt
    echo "Logging: Parent: $parentid  Zombie: $zombieid"
    lastparentid=$parentid
  fi
done
if [ "$lastparentid" != "0" ]
then
  echo "detach" >> /tmp/zombie_slayer.txt
fi

# Slay the zombies with gdb and the created command file
echo -e "\n\n"
echo "***********************************************************"
echo "Slaying zombie processes..."
echo "***********************************************************"
gdb -batch -x /tmp/zombie_slayer.txt
echo -e "\n\n"
echo "***********************************************************"
echo "Script complete."
echo "***********************************************************"

享受。

于 2013-03-14T21:01:07.907 回答
2

你是对的,你不能杀死僵尸,因为它们已经死了。

他们是僵尸的原因是因为进程已经完成,但父级没有收获退出代码。

最终,init当僵尸变成孤儿时,应该为你做这件事。一个真正的僵尸,一个仍然有父进程但该父进程尚未获取退出代码的进程,将持续到以下任一情况:

  • 父母确实收获了退出代码;或者
  • 当父母退出时,它成为孤儿。
于 2012-12-02T13:19:10.997 回答
1

你在做什么似乎很扭曲。我将假设您已尝试创建一个最小的测试用例来显示更广泛的问题并无论如何都要回答 - 当羽化进程未清理(调用 waitpid 和朋友)其子进程资源时会创建僵尸。

因此,要么正确地编写父进程代码以响应 SIGCHLD 并调用 waitpid,或者,如果您无法控制父进程,正如我猜的那样,杀死父进程(向父进程发送 SIGTERM)。

后者起作用的原因是所有没有父亲的孩子都被重新设置为 init (PID 1),它将执行所需的清理。

于 2012-12-02T13:19:32.783 回答
0

您可以尝试使用 SIGCHLD 信号。例如在 C 中:

signal(SIGCHLD, handleSIGCHLD);


  void handleSIGCHLD(int signum) {
  int stat;

 /*Kills all the zombie processes*/
  while(waitpid(-1, &stat, WNOHANG) > 0);
}
于 2012-12-02T13:23:21.833 回答