如何杀死一个进程,其PID不断变化? [英] How to kill a process whose pid keeps changing?

查看:5774
本文介绍了如何杀死一个进程,其PID不断变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用的伎俩如果(叉())​​出口(0); 来改变当前进程的PID。所以,下面的程序将有一个PID变化非常快。如何杀死这样一个过程?有一些不是执行了很多 killall PROCNAME ,直到一赠能跑杀()更好的方法之前它叉?我知道这是不是一个'过程',但是,对于一个几微秒运行多个进程的每个

I know I can use the trick if (fork()) exit(0); to change the pid of the current process. So, the following program would have a pid changing very quickly. How to kill a process like this? Is there some better method than executing a lot of killall procname until one get able to run kill() before it forks? I know it is not a 'process', but many processes that run for a few microseconds each.

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

int main()
{
    pid_t self = getpid();
    while (1)
    {
        if (fork()) exit(0);
        if (self + 10000 < getpid()) break; // Just to kill it after some time
        usleep(1000);
    }
    return 0;
}

此外,我发现列出进程执行的唯一途径 PS -A | grep的PROCNAME 几次,直到有表现出一定的输出。为什么没有这个过程始终上市?

Also the only way I found to list the process was executing ps -A | grep procname a few times until one showed some output. Why isn't the process always listed?

推荐答案

这样的方法是由系统管理员称为彗星

Such a process is called a "comet" by systems administrators.

进程组ID(PGID)不会改变叉子,这样你就可以通过发送信号到进程组杀它(或 SIGSTOP 吧)(您传递否定PGID,而不是一个PID为)。

The process group ID (PGID) doesn't change on fork, so you can kill it (or SIGSTOP it) by sending a signal to the process group (you pass a negated PGID instead of a PID to kill).

这篇关于如何杀死一个进程,其PID不断变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆