我正在生成信号并面对奇怪的行为
I am generating signal and facing the strange behaviour
我已经开始在 Linux 中获取信号,但是我的代码中发生了一些奇怪的行为。我刚开始,我也搜索过,但我没有找到任何东西,如果问题太蹩脚,抱歉,这是代码-
void Handler(int sig ){
printf("Inside Handler\n");
}
int main(int argc, char * argv[], char * envp[]){
if( signal(SIGINT, Handler) ==SIG_ERR )
exit(EXIT_FAILURE);
for(size_t i = 0; ; i++){
printf("%d\n", i);
sleep(2);
}
}
我知道printf
,signal
调用不是个好主意,但我到现在还没有研究sigaction
。现在根据我的书和其他教程,如果我按 ctrl+c
那么它每次都必须调用 Handler
但奇怪的是:当我按一次 ctrl+c
时,它调用 Handler
但是下一次它终止程序。为什么会发生这种奇怪的事情?
signal
在传递 SIG_DFL
或 SIG_IGN
以外的处理程序时的行为未定义。为此目的使用 sigaction
。
正如 docs 所说:
The only portable use of signal() is to set a signal's disposition to SIG_DFL
or SIG_IGN
. The semantics when using signal() to establish a signal handler vary across systems (and POSIX.1 explicitly permits this variation); do not use it for this purpose.
signal
系统调用的手册页说:
If the signal signum is delivered to the process, then one of the following happens:
If the disposition is set to SIG_IGN, then the signal is ignored.
If the disposition is set to SIG_DFL, then the default action associated with the signal (see signal(7)) occurs.
If the disposition is set to a function, then first either the disposition is reset to SIG_DFL, or the signal is blocked (see Portability below), and then handler is called with argument signum. If invocation of the handler caused the signal to be blocked, then the signal is unblocked upon return from the handler.
由于 SIGINT 的默认行为是在信号重置为默认行为后终止,因此后续行为不同。
这是我在您的帮助下编写的代码,它工作得非常好,我相信它在每个平台上都能很好地工作
#define _GNU_SOURCE
void Handler(int sig ){
printf("Inside Handler\n");
}
int main(int argc, char *argv[], char *envp[] ){
sighandler_t Action =Handler;
if( Action == SIG_DFL ){
Action = Handler;
}
if( signal(SIGINT, Action ) == SIG_ERR )
exit(EXIT_FAILURE );
for(size_t k = 0 ; ; k++ ){
printf("%d\n", k );
sleep(2);
}
}
我已经开始在 Linux 中获取信号,但是我的代码中发生了一些奇怪的行为。我刚开始,我也搜索过,但我没有找到任何东西,如果问题太蹩脚,抱歉,这是代码-
void Handler(int sig ){
printf("Inside Handler\n");
}
int main(int argc, char * argv[], char * envp[]){
if( signal(SIGINT, Handler) ==SIG_ERR )
exit(EXIT_FAILURE);
for(size_t i = 0; ; i++){
printf("%d\n", i);
sleep(2);
}
}
我知道printf
,signal
调用不是个好主意,但我到现在还没有研究sigaction
。现在根据我的书和其他教程,如果我按 ctrl+c
那么它每次都必须调用 Handler
但奇怪的是:当我按一次 ctrl+c
时,它调用 Handler
但是下一次它终止程序。为什么会发生这种奇怪的事情?
signal
在传递 SIG_DFL
或 SIG_IGN
以外的处理程序时的行为未定义。为此目的使用 sigaction
。
正如 docs 所说:
The only portable use of signal() is to set a signal's disposition to
SIG_DFL
orSIG_IGN
. The semantics when using signal() to establish a signal handler vary across systems (and POSIX.1 explicitly permits this variation); do not use it for this purpose.
signal
系统调用的手册页说:
If the signal signum is delivered to the process, then one of the following happens:
If the disposition is set to SIG_IGN, then the signal is ignored.
If the disposition is set to SIG_DFL, then the default action associated with the signal (see signal(7)) occurs.
If the disposition is set to a function, then first either the disposition is reset to SIG_DFL, or the signal is blocked (see Portability below), and then handler is called with argument signum. If invocation of the handler caused the signal to be blocked, then the signal is unblocked upon return from the handler.
由于 SIGINT 的默认行为是在信号重置为默认行为后终止,因此后续行为不同。
这是我在您的帮助下编写的代码,它工作得非常好,我相信它在每个平台上都能很好地工作
#define _GNU_SOURCE
void Handler(int sig ){
printf("Inside Handler\n");
}
int main(int argc, char *argv[], char *envp[] ){
sighandler_t Action =Handler;
if( Action == SIG_DFL ){
Action = Handler;
}
if( signal(SIGINT, Action ) == SIG_ERR )
exit(EXIT_FAILURE );
for(size_t k = 0 ; ; k++ ){
printf("%d\n", k );
sleep(2);
}
}