POSIX 故障产生的信号是同步产生的还是异步产生的?

Are POSIX signals resulting from a fault generated synchronously or asynchronously?

根据POSIX definitions,

3.28 Asynchronously-Generated Signal

A signal that is not attributable to a specific thread. Examples are signals sent via kill(), signals sent from the keyboard, and signals delivered to process groups. Being asynchronous is a property of how the signal was generated and not a property of the signal number. All signals may be generated asynchronously.

然后,

3.379 Synchronously-Generated Signal

A signal that is attributable to a specific thread.

For example, a thread executing an illegal instruction or touching invalid memory causes a synchronously-generated signal. Being synchronous is a property of how the signal was generated and not a property of the signal number.

如果非法指令导致同步生成信号,如何可以异步生成它?

例如,假设我有一个运行两个线程 A 和 B 的程序。现在,假设在 A 中发生了一条非法指令并导致发出信号 SIGILL。是否需要符合 POSIX 的系统来调用线程 A 中为 SIGILL 定义的信号处理程序?还是允许打断 线程 B 并在 B 中调用该信号处理程序?

相关:

List of Synchronous and Asynchronous Linux/Posix Signals

是的,您可以通过 kill(2) 将 SIGILL、SIGSEGV、SIGBUS、SIGFPE 等发送到进程,或通过 pthread_kill(3) 将特定线程发送到进程。屏蔽仅适用于终止生成的信号,因此如果您为 SIGILL 安装处理程序并对其进行屏蔽,则只会为 真实 非法指令陷阱调用您的处理程序。

两种场景不同,'synchronous' vs. 'asynchronous'在生成的时候就确定了

稍微改一下你的第二个问题,你问,“可以将属于特定线程的 synchronously-generated SIGILL 传递给同一进程中的 不同 线程吗?

没有。 synchronously-generated 信号只能传递给引起它的线程。 (2.4.1 Signal Generation and Delivery)

现在作为警告,如 , normal signal masking semantics do not apply to some synchronously-generated signals. In particular, as the specs pthread_sigmasksigprocmask 中所述,[i]f 任何 SIGFPE、SIGILL、SIGSEGV,或 SIGBUS 信号在它们被阻塞时生成,结果是 未定义,除非信号是[异步] 生成的。 (强调)