如何杀死c ++中的进程,知道它的名称的一部分 [英] How to kill process in c++, knowing only part of its name

查看:295
本文介绍了如何杀死c ++中的进程,知道它的名称的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前段时间我需要编写c ++代码来杀死一些进程。在我的主程序中,我运行大型CAE系统包与系统(...)与输入不同的文件名字符串。 CAE软件创建许多进程,其中包含进程名称字符串 filename )。一些CAE进程 worktime > * max_time *,我需要关闭它们:

Some time ago I needed to write c++ code to kill some process. In my main program I run large CAE-system package with system("...") with different filename strings on input. CAE-software creates many processes, that contain in process name string filename). Some of the CAE-processes worktime > *max_time*, than I need to shut them down:

//filename contains part of CAE-process name
    string s="/bin/kill -9 `ps aux | grep "+filename+" | awk {'print $2'}`";
    system(s.c_str());

输出为:

Usage:
  kill pid ...              Send SIGTERM to every process listed.
  kill signal pid ...       Send a signal to every process listed.
  kill -s signal pid ...    Send a signal to every process listed.
  kill -l                   List all signal names.
  kill -L                   List all signal names in a nice table.
  kill -l signal            Convert between signal numbers and names.

我尝试使用execvp运行,尝试不同的方式运行kill或pkill over bash脚本,调用系统(name_of_script.sh),其中脚本包含 kill -9 * filename * ,但结果是一样的。

使用 kill / bin / kill 给出相同的输出, bash -c kill ...

使用 kill 从我的系统(Ubuntu Natty) gnome-terminal

I tried to run with execvp, tried different ways running kill or pkill over bash script, calling system("name_of_script.sh"), where script contained kill -9 *filename* but the result was the same.
Using kill and /bin/kill gave the same output, bash -c kill... too.
Using kill from my system (Ubuntu Natty) gnome-terminal:

kill -9 `ps aux | grep filename | awk {'print $2'}`

关闭所有必要的进程!

shutdown all necessary processes! It works.

我还尝试使用pthreads将计算过程包装到子线程中,并使用pthread_cancel停止它,但它不工作,因为CAE系统进程没有接收信号(我认为,陷阱他们),唯一的办法是SIGTERM。

使用pthread_kill杀死子线程 - wrap也杀死父(我的主程序)。

I also tried to wrap computational process into a child thread using pthreads and stop it with pthread_cancel, but it doesn't work because of CAE-system process doesn't receive signals (I think, trapping them), the only way is SIGTERM.
Killing child-thread-"wrap" with pthread_kill also kills the parent (my main program).

我不知道CAE进程pids调用 kill from signals.h

关闭主程序不停止CAE进程(并且没有-Z标志,所以它们不是我的程序进程childs)

I don't know CAE-process pids to call kill from signals.h
Closing main program do not stop CAE-processes (and the do not have -Z flag, so are they aren't my program process childs??)

如何关闭CAE进程,从我的主程序运行> MAXTIME?

How can I close CAE-processes, that run > MAXTIME from my main program?

问题是我通过调试器(gdb)在QtCreator中运行主程序。没有QtCreator shell脚本以正确的方式运行参数,但参数以正确的方式传递。

The problem was that I was running main program via debugger (gdb) in QtCreator. Without QtCreator shell-script runs with arguments the right way, though arguments are passed correctly both ways.

此外,我必须清除一些CAE进程,在cmdline中没有文件名,但是这个进程的父级或子级。
在shell脚本中,您可以使用:

Also I have to clear some CAE processes, that don't have filename in cmdline, but that are parents or children of this process. In shell-script you can use:

cat /proc/"$P"/status | grep PPid | grep -o "[0-9]*"

其中 $ P 是一个带有杀死进程pid的变量。
这里有几种方法 a>杀死所有子进程。

where $P is a variable with pid of killed process. Here are several methods to kill all child processes.

我会写smth。类似于C ++中将扫描 / proc / xxxx / status 直到 PPid = ppid_of_my main_program并剪切该分支。 p>

I'll write smth. similar in C++ that will scan /proc/xxxx/status till PPid= ppid_of_my main_program and cut that branch.

推荐答案

您不必打开一个shell来杀死进程。只需使用kill函数:

You don't have to open a shell to kill a process. Just use the "kill" function:

#include <sys/types.h>
#include <signal.h>

int kill(pid_t pid, int sig);

http://linux.die.net/man/2/kill

要找到要停止的进程,请读取以下目录:

To find a process to kill read the following directory:

/ procline / #### / cmdline

/proc/####/cmdline

其中####是任何正在运行的进程标识的编号。因此,代码大致是读取/ proc目录并列出所有数值目录,这些是目前正在运行的进程,并且在该目录的cmdline文件中找到生成该进程的命令的名称。然后,您可以使用正则表达式或字符串比较来标识要杀死的进程。

Where #### is the number of any running process id. So the code roughly would be to read the /proc directory and list out all the numerical directories, these are the current running processes, and you find the name of the command that spawned that process in the "cmdline" file in that directory. You can then use a regular expression, or a string comparison to identify processes to kill.

这篇关于如何杀死c ++中的进程,知道它的名称的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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