如何在Linux中判断哪个进程向我的进程发送了信号 [英] How can I tell in Linux which process sent my process a signal

查看:480
本文介绍了如何在Linux中判断哪个进程向我的进程发送了信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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的方法是

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 sigactionsa_sigaction中具有所需的信号处理程序,在sa_flags中具有SA_SIGINFO标志.使用此标志,您的信号处理程序将接收三个参数,其中一个是包含发送者的PID和UID的siginfo_t结构.

  • Call sigaction() and pass to it a struct sigaction which has the desired signal handler in sa_sigaction and the SA_SIGINFO flag in sa_flags set. With this flag, your signal handler will receive three arguments, one of which is a siginfo_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屋!

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