那么sigaction()和signal()有什么区别呢?

发布于 2024-11-09 06:57:21 字数 255 浏览 0 评论 0原文

可能的重复:
sigaction 和 signal 有什么区别?

似乎我认为它们都可以用来注册特定信号的回调。

您如何选择使用哪一个?

Possible Duplicate:
What is the difference between sigaction and signal?

It seems to me that both of them can be used to register a callback for a specific signal.

How do you choose which one to use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清晰传感 2024-11-16 06:57:21

TLPI

UNIX系统提供了两种方式
改变信号的处理:
signal()sigaction()。这
sigaction() 系统调用是
替代 signal() 进行设置
信号的处置。虽然
sigaction() 稍微复杂一些
使用比 signal() 来返回它
提供更大的灵活性。

sigaction 也比 signal 更易于移植。此外,使用sigaction,您可以指定接收附加参数的信号处理程序(sa_sigactionsa_handler)。

/* can be installed by signal / sigaction */
void(*) (int);

/* can be installed by sigaction only */
void(*) (int, siginfo_t *, void *);

TLPI

UNIX systems provide two ways of
changing the disposition of a signal:
signal() and sigaction(). The
sigaction() system call is an
alternative to signal() for setting
the disposition of a signal. Although
sigaction() is somewhat more complex
to use than signal(), in return it
provides greater flexibility.

sigaction is also more portable than signal. Also, with sigaction you can specify signal handlers that receive additional arguments (sa_sigaction versus sa_handler).

/* can be installed by signal / sigaction */
void(*) (int);

/* can be installed by sigaction only */
void(*) (int, siginfo_t *, void *);
虐人心 2024-11-16 06:57:21

从我的角度来看,区别(除了接口:)和可移植性)在于捕获信号后的行为:

如果将处置设置为函数,则首先将处置重置为 SIG_DFL,或者阻止信号(请参阅下面的可移植性),然后使用参数符号调用处理程序。如果处理程序的调用导致信号被阻止,则信号在从处理程序返回时被解除阻止。

而且,我还想重复一下手册页中所说的内容:

signal() 的行为因 Unix 版本而异,并且在不同版本的 Linux 中历史上也有所不同。避免使用它:使用 sigaction(2) 代替。

From my point of view, the difference (except interface :) and portability) is in behavior after signal is caught:

If the disposition is set to a function, then first either the disposition is reset to SIG_DFL, or the signal is blocked (see Portability below), and then handler is called with argument signum. If invocation of the handler caused the signal to be blocked, then the signal is unblocked upon return from the handler.

And, also I'd like to repeat the same as the man page says:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文