SIG_IGN宏定义中的数字1是什么意思?
What 's the meaning of the number 1 in SIG_IGN macro definition?
#define SIG_IGN (void (*)(int))1
#define SIG_HOLD (void (*)(int))5
#define SIG_ERR ((void (*)(int))-1)
我知道 (void (*)(int))
是什么意思:将 unknown_name 转换为指向返回 void 的函数 (int) 的指针。
但是下面的1
是什么意思?
使用常量是为了将其与有效的函数指针区分开来。它本身没有任何意义(除了不同之外)。
例如:
#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 是您可以调用的有效函数地址。所以它们作为特殊值很有用,可以用来说明如何处理信号。
POSIX顺便不提这些常量-1
、0
或1
,宁愿只说符号常量(无论如何在预期的地方):<signal.h>
.
进一步阅读:
- executing default signal handler
- 24.3.1 Basic Signal Handling(GNU C 库)
添加关于已接受答案的有用参考 material。
来自 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.
是的,它确保你在尝试做愚蠢的事情时会出错(也许是其他 (useful/stupid) 的事情,我不知道):
#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);
}
这里调用f1(3)
等于调用SIG_DFL(3)
,每个函数都有一个地址但是SIG_DFL
(0) 不是有效地址,所以我得到SIGSEGV
错误。
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).
#define SIG_IGN (void (*)(int))1
#define SIG_HOLD (void (*)(int))5
#define SIG_ERR ((void (*)(int))-1)
我知道 (void (*)(int))
是什么意思:将 unknown_name 转换为指向返回 void 的函数 (int) 的指针。
但是下面的1
是什么意思?
使用常量是为了将其与有效的函数指针区分开来。它本身没有任何意义(除了不同之外)。
例如:
#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 是您可以调用的有效函数地址。所以它们作为特殊值很有用,可以用来说明如何处理信号。
POSIX顺便不提这些常量-1
、0
或1
,宁愿只说符号常量(无论如何在预期的地方):<signal.h>
.
进一步阅读:
- executing default signal handler
- 24.3.1 Basic Signal Handling(GNU C 库)
添加关于已接受答案的有用参考 material。
来自 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 fromsignal
. 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.
是的,它确保你在尝试做愚蠢的事情时会出错
#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);
}
这里调用f1(3)
等于调用SIG_DFL(3)
,每个函数都有一个地址但是SIG_DFL
(0) 不是有效地址,所以我得到SIGSEGV
错误。
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).