当另一个进程收到信号时,有没有办法得到通知? [英] Is there any way to be notified when another process receives a signal?

查看:29
本文介绍了当另一个进程收到信号时,有没有办法得到通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道进程如何响应专门发送给它的信号(例如 SIGINTSIGTERMSIGUSR2 等).但是,是否可以将发送到不同进程的信号通知进程?

I know how a process can respond to signals that were sent to it specifically (e.g. SIGINT, SIGTERM, SIGUSR2, etc.). But can a process be notified of signals that were sent to a different process?

推荐答案

不在标准 Unix 或 POSIX 中,您无法收到发送到另一个进程的信号的通知.参见 signal(7)信号安全(7).

Not in standard Unix or POSIX, you cannot be notified for signals sent to another process. See signal(7) and signal-safety(7).

不过,waitpid(2) 和朋友们可以告诉您 子进程是否因信号终止.并且 killpg(2)进程组(和 kill(2) 也使用负目标 pid 执行此操作).并且 getrusage(2) 可以计算信号(由某些其他过程).你也可以使用 proc(5) 来查询关于其他过程.你可以使用 signalfd(2)ptrace(2) 等等....

However, waitpid(2) and friends can tell you if a child process has terminated with a signal. And killpg(2) sends a signal to a process group (and kill(2) does also that with a negative target pid). And getrusage(2) can count signals (recieved by some other process). You could also use proc(5) to query information about other processes. And you might use signalfd(2) or ptrace(2) etc....

信号是一种非常有限且糟糕的进程间通信.有更好的方法.

Signals are a very limited and poor form of inter-process communication. There are better ways.

顺便说一句,sigaction(2) 可以使用使用 SA_SIGINFO 然后你的处理程序得到一个指向 siginfo_t 的指针和另一个指向 ucontext_t 的指针,这样你就可以获得很多信息.

BTW, sigaction(2) can be used with SA_SIGINFO and then your handler gets a pointer to siginfo_t and another to ucontext_t so you get a lot of information.

注意进程组和会话是相关的.另请参阅 setpgid(2)setsid(2), credentials(7) 并且还与终端和伪 ttys 有关(阅读 tty 揭秘 和关于 作业控制).

Notice that process groups and sessions are related. See also setpgid(2), setsid(2), credentials(7) and also related to terminals and pseudo-ttys (read the tty demystified and about job control).

我猜您的其他问题与这些有关.但是你没有在那里提到他们中的任何一个.

I guess that your other question is about these. But you don't mention any of them there.

这篇关于当另一个进程收到信号时,有没有办法得到通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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