What 's the meaning of the number 1 in SIG_IGN

2019-03-03 08:07发布

#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?

标签: c linux sigpipe
1条回答
混吃等死
2楼-- · 2019-03-03 08:48

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:

查看更多
登录 后发表回答