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

旧街凉风 提交于 2019-12-02 08:46:05

问题


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



回答2:


Add a useful reference material as to the accepted answer.

From APUE:

If we examine the system’s header , we will probably find declarations of the form

#define SIG_ERR (void (*)()) -1
#define SIG_DFL (void (*)())  0
#define SIG_IGN (void (*)())  1

These constants can be used in place of the ‘‘pointer to a function that takes an integer argument and returns nothing,’’ the second argument to signal, and the return value from signal. The three values used for these constants need not be −1, 0, and 1. They must be three values that can never be the address of any declarable function. Most UNIX systems use the values shown.

Yes, it ensures you will get a error when you try to do stupid things like me (maybe other (useful/stupid) things, I don't know):

#include <signal.h>
#include <stdio.h>

void signal_handler(int signal)
{
    printf("hahahah\n");
}

int main(void)
{
    void (*f1)(int);
    f1 = signal(SIGINT, signal_handler);
    f1(3);  //Get signal SIGSEGV and failed
            //Here I am calling SIG_DFL(3).
    raise(SIGINT);
}

Here calling f1(3) equals calling SIG_DFL(3), every function has an address but SIG_DFL (0) is not a valid one, so I get SIGSEGV error.

SIGSEGV This signal indicates that the process has made an invalid memory reference (which is usually a sign that the program has a bug, such as dereferencing an uninitialized pointer).



来源:https://stackoverflow.com/questions/35105452/what-s-the-meaning-of-the-number-1-in-sig-ign-macro-definition

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!