What 's the meaning of the number 1 in SIG_IGN macro definition?

前端 未结 2 1492
清歌不尽
清歌不尽 2021-01-24 17:00
#define SIG_IGN     (void (*)(int))1
#define SIG_HOLD    (void (*)(int))5
#define SIG_ERR     ((void (*)(int))-1)

I know what (void (*)(int))

2条回答
  •  野性不改
    2021-01-24 17:15

    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): .

    Further reading:

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

提交回复
热议问题