用 sigaction(2) 的原因
Linux man page中提到:
The behavior of signal() varies across Unix versions, and has also varied
historically across different versions of Linux. Avoid its use: use sigaction(2) instead.
…
The only portable use of signal() is to set a signal’s disposition to SIG_DFL or SIG_IGN. The semantics when using signal() to establish a signal handler vary across systems (and POSIX.1 explicitly permits this variation); do not use it for this purpose.
所以signal(2)的可移植性首先就差了一截子。还有就是,signal(2)也不能像sigaction(2)那样去屏蔽信号,POSIX中提到:
When a signal is caught by a signal-catching function installed by
sigaction(), a new signal mask is calculated and installed for the
duration of the signal-catching function (or until a call to either
sigprocmask() or sigsuspend() is made). This mask is formed by taking
the union of the current signal mask and the value of the sa_mask for
the signal being delivered unless SA_NODEFER or
SA_RESETHAND is set, And then including the signal being
delivered. If and when the user’s signal handler returns normally, the
original signal mask is restored.