如何获取进程状态( 运行 , killed )事件?
how to get process status ( running , killed ) event?
如何获取另一个进程的状态?
我想知道另一个进程的执行状态。
我想接收事件并将其作为 inotify 处理。
不按句点搜索 /proc。
如何处理另一个进程状态(运行 , killed )事件?
系统:linux、solaris、aix
Linux
在 Linux 下(可能还有很多 Unixes 系统)你可以通过调用 ptrace
来实现,然后使用 waitpid 等待状态:
联机帮助页:
- ptrace 调用:http://man7.org/linux/man-pages/man2/ptrace.2.html
- waitpid 调用:https://linux.die.net/man/2/waitpid
来自联机帮助页:
Death under ptrace
When a (possibly multithreaded) process receives a killing signal
(one whose disposition is set to SIG_DFL and whose default action is
to kill the process), all threads exit. Tracees report their death
to their tracer(s). Notification of this event is delivered via
waitpid(2).
请注意,在某些情况下您需要获得特殊授权。看看/proc/sys/kernel/yama/ptrace_scope
。 (如果你可以修改目标程序,你也可以通过调用ptrace(PTRACE_TRACEME, 0, nullptr, nullptr);
来改变ptrace的行为
要使用 ptrace,首先你必须得到你的进程 PID,然后调用 PTRACE_ATTACH
:
// error checking removed for the sake of clarity
#include <sys/ptrace.h>
pid_t child_pid;
// ... Get your child_pid somehow ...
// 1. attach to your process:
long err;
err = ptrace(PTRACE_ATTACH, child_pid, nullptr, nullptr);
// 2. wait for your process to stop:
int process_status;
err = waitpid(child_pid, &process_status, 0);
// 3. restart the process (continue)
ptrace(PTRACE_CONT, child_pid, nullptr, nullptr);
// 4. wait for any change in status:
err = waitpid(child_pid, &process_status, 0);
// while waiting, the process is running...
// by default waitpid will wait for process to terminate, but you can
// change this with WNOHANG in the options.
if (WIFEXITED(status)) {
// exitted
}
if (WIFSIGNALED(status)) {
// process got a signal
// WTERMSIG(status) will get you the signal that was sent.
}
AIX:
该解决方案需要进行一些调整才能与 AIX 一起使用,请查看那里的文档:
- ptrace 文档:https://www.ibm.com/support/knowledgecenter/en/ssw_aix_72/com.ibm.aix.basetrf1/ptrace.htm
- waitpid 文档:https://www.ibm.com/support/knowledgecenter/en/ssw_aix_72/com.ibm.aix.basetrf1/ptrace.htm
Solaris
如前所述here ptrace 在您的 Solaris 版本上可能不可用,您可能不得不求助于 procfs。
如何获取另一个进程的状态?
我想知道另一个进程的执行状态。
我想接收事件并将其作为 inotify 处理。
不按句点搜索 /proc。
如何处理另一个进程状态(运行 , killed )事件?
系统:linux、solaris、aix
Linux
在 Linux 下(可能还有很多 Unixes 系统)你可以通过调用 ptrace
来实现,然后使用 waitpid 等待状态:
联机帮助页:
- ptrace 调用:http://man7.org/linux/man-pages/man2/ptrace.2.html
- waitpid 调用:https://linux.die.net/man/2/waitpid
来自联机帮助页:
Death under ptrace When a (possibly multithreaded) process receives a killing signal (one whose disposition is set to SIG_DFL and whose default action is to kill the process), all threads exit. Tracees report their death to their tracer(s). Notification of this event is delivered via waitpid(2).
请注意,在某些情况下您需要获得特殊授权。看看/proc/sys/kernel/yama/ptrace_scope
。 (如果你可以修改目标程序,你也可以通过调用ptrace(PTRACE_TRACEME, 0, nullptr, nullptr);
要使用 ptrace,首先你必须得到你的进程 PID,然后调用 PTRACE_ATTACH
:
// error checking removed for the sake of clarity
#include <sys/ptrace.h>
pid_t child_pid;
// ... Get your child_pid somehow ...
// 1. attach to your process:
long err;
err = ptrace(PTRACE_ATTACH, child_pid, nullptr, nullptr);
// 2. wait for your process to stop:
int process_status;
err = waitpid(child_pid, &process_status, 0);
// 3. restart the process (continue)
ptrace(PTRACE_CONT, child_pid, nullptr, nullptr);
// 4. wait for any change in status:
err = waitpid(child_pid, &process_status, 0);
// while waiting, the process is running...
// by default waitpid will wait for process to terminate, but you can
// change this with WNOHANG in the options.
if (WIFEXITED(status)) {
// exitted
}
if (WIFSIGNALED(status)) {
// process got a signal
// WTERMSIG(status) will get you the signal that was sent.
}
AIX:
该解决方案需要进行一些调整才能与 AIX 一起使用,请查看那里的文档:
- ptrace 文档:https://www.ibm.com/support/knowledgecenter/en/ssw_aix_72/com.ibm.aix.basetrf1/ptrace.htm
- waitpid 文档:https://www.ibm.com/support/knowledgecenter/en/ssw_aix_72/com.ibm.aix.basetrf1/ptrace.htm
Solaris
如前所述here ptrace 在您的 Solaris 版本上可能不可用,您可能不得不求助于 procfs。