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

2

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

Absorbance answered 30/1, 2016 at 18:31 Comment(1)
Where do the "5" and "-1" in your title come from?Mitchell
C
3

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:

Consumer answered 30/1, 2016 at 18:33 Comment(0)
C
1

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

Chroma answered 28/3, 2019 at 6:25 Comment(2)
Haha, nice example!Drue
@AnttiHaapala haha, I am trying to complete this and the other similar question, based on my silly experience. :DChroma

© 2022 - 2024 — McMap. All rights reserved.