开发者

So what's the difference between sigaction() and signal()? [duplicate]

开发者 https://www.devze.com 2023-03-08 03:59 出处:网络
This question already has answers here: 开发者_如何学Python Closed 11 years ago. Possible Duplicate:
This question already has answers here: 开发者_如何学Python Closed 11 years ago.

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?


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 *);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消