如何在Linux中判断哪个进程向我的进程发送了信号 [英] How can I tell in Linux which process sent my process a signal
问题描述
我有一个SIG TERM
的Java应用程序.我想知道发送此信号的进程的pid.
有可能吗?
I have a java application that got SIG TERM
. I want to know the pid of the process that sent this signal.
Is that possible?
推荐答案
两种特定于Linux的方法是 signalfd()
,它允许程序接收有关已发送信号的很多详细信息,包括发送方的PID.
Two Linux-specific methods are SA_SIGINFO
and signalfd()
, which allows programs to receive very detailed information about signals sent, including the sender's PID.
-
致电
sigaction()
并将其传递给struct sigaction
,该struct sigaction
在sa_sigaction
中具有所需的信号处理程序,在sa_flags
中具有SA_SIGINFO
标志.使用此标志,您的信号处理程序将接收三个参数,其中一个是包含发送者的PID和UID的siginfo_t
结构.
Call
sigaction()
and pass to it astruct sigaction
which has the desired signal handler insa_sigaction
and theSA_SIGINFO
flag insa_flags
set. With this flag, your signal handler will receive three arguments, one of which is asiginfo_t
structure containing the sender's PID and UID.
致电 signalfd()
并从中读取signalfd_siginfo
结构(通常以某种选择/轮询循环的形式).内容将类似于siginfo_t
.
Call signalfd()
and read signalfd_siginfo
structures from it (usually in some kind of a select/poll loop). The contents will be similar to siginfo_t
.
使用哪种取决于您的应用程序的编写方式;它们可能无法在普通C之外正常工作,而且我也没有希望在Java中使用它们.它们在Linux之外也无法移植.它们也可能是您要实现的目标的非常错误的方式.
Which one to use depends on how your application is written; they probably won't work well outside plain C, and I wouldn't have any hope of getting them work in Java. They are also unportable outside Linux. They also likely are the Very Wrong Way of doing what you are trying to achieve.
这篇关于如何在Linux中判断哪个进程向我的进程发送了信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!