如何杀死僵尸进程?

时间:2013-07-14 22:38:06

标签: c macos terminal zombie-process

我有两个问题:如果任何一个子问题得到解决,我制作的程序就会出现故障,并且会产生几乎无法解决的问题。我相信这两个问题都很容易解决。我在OSX 10.6.8上运行了2008年初的Macbook。

/ 问题#1,编码: /

我一直在使用termios.h I / O来玩iRobot Create。我编译没有警告或错误,可以毫不费力地运行程序。我通过usb连接到机器人,它解释了输入“/dev/tty.usbserial”。

 gcc simple.c -o simple
 ./simple /dev/tty.usbserial

程序首先检查给定的参数,然后尝试使用biscConnect()函数连接给定的参数(char * device是/dev/tty.usbserial)。它在我的Mac上失败了。

//in file simple.c:
int main(int argc, char *argv[]){
if(argc!=2){
    printf("Put port... like /dev/tty.usbserial or something\n");
    exit(EXIT_FAILURE);
}
printf("Starting...\n");
biscConnect(argv[1]);
}
void biscConnect(char *device){
struct termios tty;

// Try to open the device from the input
    if((fd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK))==-1){
        fprintf(stderr, "Serial port at %s could not be opened: %s\n", device, strerror(errno));
        exit(EXIT_FAILURE);
    }
     tcflush (fd, TCIOFLUSH);
     tcgetattr(fd, &tty);
     tty.c_iflag     = IGNBRK | IGNPAR;
     tty.c_lflag     = 0;
     tty.c_oflag     = 0;
     tty.c_cflag     = CREAD | CS8 | CLOCAL;
     cfsetispeed(&tty, B57600);
     cfsetospeed(&tty, B57600);
     tcsetattr(fd, TCSANOW, &tty);

     //The code fails prior to this point
}

然后,我会向机器人发送字节,以便在此之前没有卡住的情况下移动它。

/ 问题#2,无法终止的流程: / 当我运行该文件时,终端进入一个奇怪的模式,提示消失了,我可以输入任何我想要的东西(通常表示进程正在运行)。我无法使用control-c退出。我似乎退出的唯一方法就是关闭终端窗口。这无法终止正在运行的进程。

我可以轻松查找pid,但是活动监视器但强制退出无法终止进程,kill -9 [pid]失败,killall [程序名称]失败等,尽管确认程序存在。强制终止进程的唯一方法似乎是从物理上关闭计算机的电源并重新启动它(即,由于它尝试并且失败,终止进程,因此无法正常工作)。如果要调试程序,我每次运行都需要重新启动笔记本电脑,这会浪费很多时间!我可以不断创建更多进程,但无法删除它们。

我想如果我知道父进程,我可能会杀死这些“僵尸”进程,但我不知道父进程是什么。

关于如何在没有动力循环的情况下摆脱这些过程的任何想法都将是巨大的帮助,谢谢!

2 个答案:

答案 0 :(得分:1)

在Mac OS X 10.8.4上,我从zombie创建了一个程序zombie.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    pid_t pid;

    if ((pid = fork()) >= 0)
    {
        if (pid == 0)
        {
            printf("%d: committing suicide (parent %d)\n", (int)getpid(), (int)getppid());
            exit(0);
        }
        else
        {
            printf("%d: going to sleep for a while - child %d might die while I snooze\n",
                (int)getpid(), (int)pid);
            sleep(30);
            printf("%d: awake\n", (int)getpid());
        }
    }
    return 0;
}

当我在一个终端窗口中运行它时,输出为:

$ ./zombie
2443: going to sleep for a while - child 2444 might die while I snooze
2444: committing suicide (parent 2443)
2443: awake
$

在另一个终端窗口中,运行ps -f生成:

  UID   PID  PPID   C STIME   TTY           TIME CMD
  503   260   249   0 12:42PM ttys004    0:00.08 -bash
  503  2443   260   0  5:11PM ttys004    0:00.00 ./zombie
  503  2444  2443   0  5:11PM ttys004    0:00.00 (zombie)

括号(zombie)是已失效的进程,括号中的名称是进程的原始名称。当我将程序复制到living-dead时,相应的输出是:

  UID   PID  PPID   C STIME   TTY           TIME CMD
  503   260   249   0 12:42PM ttys004    0:00.09 -bash
  503  2454   260   0  5:13PM ttys004    0:00.00 ./living-dead
  503  2455  2454   0  5:13PM ttys004    0:00.00 (living-dead)

(在大多数系统中,已失效的流程标记为<defunct>或类似的东西。)

显然,PPID列中的值标识了僵尸的父进程,并且各种进程ID与程序本身的输出相匹配。

答案 1 :(得分:0)

您可以通过运行ps ef或使用htop找出是谁制造了这种不死生物进程。

PID   TTY      STAT   TIME COMMAND
21138 tty1     T      0:00 sudo /usr/bin/aura -Akax open_watcom openwatcom-extras-hg HOME=/home/hav3lock USER=hav3lock SHELL=/bin/zsh TERM=linux PATH=/usr/local/sbin:/u
21139 tty1     T      0:00  \_ /usr/bin/aura -Akax open_watcom openwatcom-extras-hg TERM=linux PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/bin/site_perl:/usr/bin/
22111 tty1     Z      0:00      \_ [su] <defunct>`

在上面我有一个僵尸进程谁是妈妈 / usr / bin / aura ,这是一个由 sudo / usr / bin / aura 产生的过程。

运行sudo kill -9 21138会杀死父级,从而杀死它的僵尸产卵。

PID   TTY      STAT   TIME COMMAND
23858 pts/9    S+     0:00 sudo sudo ps ef LANG=en_US.utf8 DISPLAY=:0 SHLVL=3 LOGNAME=hav3lock XDG_VTNR=1 PWD=/home/hav3lock/sy.l/repos/pub_rel/slen/linux/src HG=/usr/b
23860 pts/9    S+     0:00  \_ sudo ps ef LANG=en_US.utf8 DISPLAY=:0 LS_COLORS=no=0:fi=0:di=34:ln=00;96:or=91;4:mi=00;31;9:mh=01;37:pi=33;7:so=01;35:do=35:bd=01;33:cd=9
23861 pts/9    R+     0:00      \_ ps ef LANG=en_US.utf8 DISPLAY=:0 LS_COLORS=no=0:fi=0:di=34:ln=00;96:or=91;4:mi=00;31;9:mh=01;37:pi=33;7:so=01;35:do=35:bd=01;33:cd=93
13405 tty2     S<sl+   3:49 X :0 HOME=/home/hav3lock USER=hav3lock SHELL=/bin/zsh TERM=linux PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/bin/site_perl:/usr/bin/ve`
相关问题