-1

好的,所以我想编写一个可以结束当前正在运行的特定进程的 c++ 程序。我已经搜索了互联网,我遇到的所有解决方案对我来说都没有意义。有没有一种简单的方法来结束一个进程?

4

2 回答 2

5

在 POSIX 上,您调用kill(3)发送SIGTERM到进程。在 Windows 上,您调用TerminateProcess().

于 2011-06-19T20:30:33.317 回答
0

Assuming that you're on a *nix platform and that you have the process ID (i.e. you spawned the process yourself used some other method to infer its pid), using kill(2) should work:

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

void main() {
    /* ... */
    pid_t pid = ???;
    kill(pid, SIGTERM);
}

It will only work under certain conditions, though:

For a process to have permission to send a signal it must either be privileged (under Linux: have the CAP_KILL capability), or the real or effective user ID of the sending process must equal the real or saved set-user-ID of the target process. In the case of SIGCONT it suffices when the sending and receiving processes belong to the same session.

于 2011-06-19T20:42:40.680 回答