1

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?

1

2 Answers 2

4

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

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.