只能由我的应用程序使用的信号

Signal that can be used just by my application

我正在编写 Linux 中的进程树,我想知道是否有任何信号可以用于从 A 进程发送到 B 进程 而不影响 B进程.

例如,假设B_pid是进程B的ID,如果进程A调用kill(B_pid, SIGSTOP);那么A会暂停B。我要找的是一个信号,比方说SIGNOTHING,当 A 调用 kill(B_pid, SIGNOTHING) 时,它只是简单地向 B 发送一条消息,而不是对 B 和系统都做一些事情。

SIGUSR1SIGUSR2 就是为此目的而设计的。

基本上 Linux 中的每个信号都有与之关联的操作。

信号手册页:

   Signal dispositions
       Each signal has a current disposition, which determines how the
       process behaves when it is delivered the signal.

       The entries in the "Action" column of the table below specify the
       default disposition for each signal, as follows:

       Term   Default action is to terminate the process.

       Ign    Default action is to ignore the signal.

       Core   Default action is to terminate the process and dump core (see
              core(5)).

       Stop   Default action is to stop the process.

       Cont   Default action is to continue the process if it is currently
              stopped.

SIGSTOP

SIGSTOP      P1990      Stop    Stop process
A process can change the disposition of a signal using sigaction(2)
       or signal(2).  (The latter is less portable when establishing a
       signal handler; see signal(2) for details.)  Using these system
       calls, a process can elect one of the following behaviors to occur on
       delivery of the signal: perform the default action; ignore the
       signal; or catch the signal with a signal handler, a programmer-
       defined function that is automatically invoked when the signal is
       delivered.

您可以定义自己的信号处理程序并定义进程的行为。

注意:SIGKILLSIGSTOP无法被捕获

如果您在 shell 上调用以下命令:

kill -l

您将获得适用于您的系统的完整信号列表。

大多数信号都可以用于在目标端简单地"receive"它们。但是:大多数信号也被系统本身用来告诉应用程序发生了一些特殊的事情,比如 SIGSEGV。所以使用信号是没有意义的,因为它们用于从 kernel/OS 到应用程序进行通信,因此具有固定的含义。

对于用户信号,您保留了两个信号,可用于任何您喜欢的信号:SIGUSR1SIGUSR2

并不是所有的 Unix 系统都有这些信号!所以先看看你现在的系统可以使用哪些信号吧!

附加提示:

检查您的信号处理程序及其所在的上下文 运行。在某些实现中,不允许从处理程序的上下文中调用不可重入函数。因此,通过管道或任何其他 IPC 方法进行通信可能更有用。

有些信号是供用户程序使用的。来自 man signal:

The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.

SIGSTOP 将始终停止程序,SIGKILL 将始终终止程序。

进程间信号通信常用的用户自定义信号有两种:

SIGUSR1 ... User-defined signal 1
SIGUSR2 ... User-defined signal 2

并且在 SIGRTMINSIGRTMAX 之间还有一个完整的 范围 实时信号用作用户定义的信号,必须至少有 8 个信号(即 SIGRTMAX - SIGRTMIN >= 8),linux 支持 33 个信号。这些都是为了让用户应用程序做任何它想做的事情。