当前位置: 技术问答>linux和unix
s信号处理时,为什么信号动作要复位。
来源: 互联网 发布时间:2017-05-28
本文导语: APUE:在旧版本中进程每次接到信号对其进行处理时,随即该信号动作复位为默认值。 不过我写了几行代码: #include #include #include void sig_usr(int signo){ printf("get a signaln"); } int main(){ if(signal(SIGUSR1,sig_usr)==SIG_ERR){ p...
APUE:在旧版本中进程每次接到信号对其进行处理时,随即该信号动作复位为默认值。
不过我写了几行代码:
结果不会出现这种情况,不再次安装处理函数,可以一直接受到sigusr1信号。
话说,旧版系统为什么会有那么奇怪的处理方式呢?处理函数一直保留不是挺好的吗。
不过我写了几行代码:
#include
#include
#include
void sig_usr(int signo){
printf("get a signaln");
}
int main(){
if(signal(SIGUSR1,sig_usr)==SIG_ERR){
printf("cath sig usr failedn");
}
for(;;)
pause();
}
结果不会出现这种情况,不再次安装处理函数,可以一直接受到sigusr1信号。
话说,旧版系统为什么会有那么奇怪的处理方式呢?处理函数一直保留不是挺好的吗。
|
多线程处理的碰到过一次,然后我改成sigaction了,不过是aix~~
|
可能就是实现者的意思吧
|
gcc -ansi 编译就可以啦
也可以用哦
gcc -D-D_XOPEN_SOURCE 编译
Portability
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 varia-
tion); do not use it for this purpose.
POSIX.1 solved the portability mess by specifying sigaction(2), which provides explicit control of the seman-
tics when a signal handler is invoked; use that interface instead of signal().
In the original Unix systems, when a handler that was established using signal() was invoked by the delivery of
a signal, the disposition of the signal would be reset to SIG_DFL, and the system did not block delivery of
further instances of the signal. System V also provides these semantics for signal(). This was bad because
the signal might be delivered again before the handler had a chance to reestablish itself. Furthermore, rapid
deliveries of the same signal could result in recursive invocations of the handler.
BSD improved on this situation by changing the semantics of signal handling (but, unfortunately, silently
changed the semantics when establishing a handler with signal()). On BSD, when a signal handler is invoked,
the signal disposition is not reset, and further instances of the signal are blocked from being delivered while
the handler is executing.
The situation on Linux is as follows:
* The kernel’s signal() system call provides System V semantics.
* By default, in glibc 2 and later, the signal() wrapper function does not invoke the kernel system call.
Instead, it calls sigaction(2) using flags that supply BSD semantics. This default behavior is provided as
long as the _BSD_SOURCE feature test macro is defined. By default, _BSD_SOURCE is defined; it is also
implicitly defined if one defines _GNU_SOURCE, and can of course be explicitly defined.
On glibc 2 and later, if the _BSD_SOURCE feature test macro is not defined, then signal() provides System V
semantics. (The default implicit definition of _BSD_SOURCE is not provided if one invokes gcc(1) in one of
its standard modes (-std=xxx or -ansi) or defines various other feature test macros such as _POSIX_SOURCE,
_XOPEN_SOURCE, or _SVID_SOURCE; see feature_test_macros(7).)
* The signal() function in Linux libc4 and libc5 provide System V semantics. If one on a libc5 system includes
instead of , then signal() provides BSD semantics.
也可以用哦
gcc -D-D_XOPEN_SOURCE 编译
Portability
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 varia-
tion); do not use it for this purpose.
POSIX.1 solved the portability mess by specifying sigaction(2), which provides explicit control of the seman-
tics when a signal handler is invoked; use that interface instead of signal().
In the original Unix systems, when a handler that was established using signal() was invoked by the delivery of
a signal, the disposition of the signal would be reset to SIG_DFL, and the system did not block delivery of
further instances of the signal. System V also provides these semantics for signal(). This was bad because
the signal might be delivered again before the handler had a chance to reestablish itself. Furthermore, rapid
deliveries of the same signal could result in recursive invocations of the handler.
BSD improved on this situation by changing the semantics of signal handling (but, unfortunately, silently
changed the semantics when establishing a handler with signal()). On BSD, when a signal handler is invoked,
the signal disposition is not reset, and further instances of the signal are blocked from being delivered while
the handler is executing.
The situation on Linux is as follows:
* The kernel’s signal() system call provides System V semantics.
* By default, in glibc 2 and later, the signal() wrapper function does not invoke the kernel system call.
Instead, it calls sigaction(2) using flags that supply BSD semantics. This default behavior is provided as
long as the _BSD_SOURCE feature test macro is defined. By default, _BSD_SOURCE is defined; it is also
implicitly defined if one defines _GNU_SOURCE, and can of course be explicitly defined.
On glibc 2 and later, if the _BSD_SOURCE feature test macro is not defined, then signal() provides System V
semantics. (The default implicit definition of _BSD_SOURCE is not provided if one invokes gcc(1) in one of
its standard modes (-std=xxx or -ansi) or defines various other feature test macros such as _POSIX_SOURCE,
_XOPEN_SOURCE, or _SVID_SOURCE; see feature_test_macros(7).)
* The signal() function in Linux libc4 and libc5 provide System V semantics. If one on a libc5 system includes
instead of , then signal() provides BSD semantics.