What 's the meaning of the number 1 in SIG_IGN

2019-03-03 07:59发布

问题:

#define SIG_IGN     (void (*)(int))1
#define SIG_HOLD    (void (*)(int))5
#define SIG_ERR     ((void (*)(int))-1)

I know what (void (*)(int)) means: cast unknown_name into pointer to function (int) returning void.

But what's the meaning of the following 1?

回答1:

The constant is used so that it can be distinguished from a valid function pointer. It has no meaning in itself (other than being distinct).

For example:

#define SIG_DFL ((__sighandler_t)0)     /* default signal handling */
#define SIG_IGN ((__sighandler_t)1)     /* ignore signal */
#define SIG_ERR ((__sighandler_t)-1)    /* error return from signal */

None of those constant values is something that you could call as a valid function address. So they are useful as special values that can be used to say how to handle signals.

POSIX by the way does not mention these constants -1, 0 or 1, preferring to say only symbolic constants (in the expected place, anyway): <signal.h>.

Further reading:

  • executing default signal handler
  • 24.3.1 Basic Signal Handling (The GNU C library)


标签: c linux sigpipe