当前位置: 技术问答>linux和unix
signal问题
来源: 互联网 发布时间:2016-04-19
本文导语: One specific example is how an interactive shell treats the interrupt and quit signals for a background process. With a shell that doesn't support job control, when we execute a process in the background, as in cc main.c & the shell au...
One specific example is how an interactive shell treats the interrupt and quit signals for a background process. With a shell that doesn't support job control, when we execute a process in the background, as in
cc main.c &
the shell automatically sets the disposition of the interrupt and quit signals in the background process to be ignored. This is so that if we type the interrupt character, it doesn't affect the background process. If this weren't done and we typed the interrupt character, it would terminate not only the foreground process, but also all the background processes.
Many interactive programs that catch these two signals have code that looks like
void sig_int(int), sig_quit(int);
if (signal(SIGINT, SIG_IGN) != SIG_IGN)
signal(SIGINT, sig_int);
if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
signal(SIGQUIT, sig_quit);
Doing this, the process catches the signal only if the signal is not currently being ignored.
These two calls to signal also show a limitation of the signal function: we are not able to determine the current disposition of a signal without changing the disposition. We'll see later in this chapter how the sigaction function allows us to determine a signal's disposition without changing it.
这段程序问题是,对于shell设置了后台程序忽略中断,退出信号,那么处理方法没问题
要是shell没有设置设置忽略中断退出信号,if(signal(SIGINT,SIG_INT) != SIG_IGN)会忽略SIGINT,后面的signal(SIGINT, sig_int)又捕捉SIGINT信号,这会发生什么事呢?相当于对SIGINT处理了两次阿,而第一次就已经ignore了,
测试代码
[ob@localhost mycode]$ ./main &
[1] 3974
[ob@localhost mycode]$ kill -USR1 3974
received user signal
是不是这样的?signal仅仅是设置处理方法,对于该nu信号究竟怎么处理,还是要看最后的signal(nu, func)?
cc main.c &
the shell automatically sets the disposition of the interrupt and quit signals in the background process to be ignored. This is so that if we type the interrupt character, it doesn't affect the background process. If this weren't done and we typed the interrupt character, it would terminate not only the foreground process, but also all the background processes.
Many interactive programs that catch these two signals have code that looks like
void sig_int(int), sig_quit(int);
if (signal(SIGINT, SIG_IGN) != SIG_IGN)
signal(SIGINT, sig_int);
if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
signal(SIGQUIT, sig_quit);
Doing this, the process catches the signal only if the signal is not currently being ignored.
These two calls to signal also show a limitation of the signal function: we are not able to determine the current disposition of a signal without changing the disposition. We'll see later in this chapter how the sigaction function allows us to determine a signal's disposition without changing it.
这段程序问题是,对于shell设置了后台程序忽略中断,退出信号,那么处理方法没问题
要是shell没有设置设置忽略中断退出信号,if(signal(SIGINT,SIG_INT) != SIG_IGN)会忽略SIGINT,后面的signal(SIGINT, sig_int)又捕捉SIGINT信号,这会发生什么事呢?相当于对SIGINT处理了两次阿,而第一次就已经ignore了,
测试代码
#include
#include
#include
static void deposition(int );
int main()
{
if(signal(SIGUSR1, SIG_IGN) != SIG_IGN)//默认为终止,if肯定成立
{
signal(SIGUSR1, deposition);
}
for(;;)
pause();
}
static void deposition(int stat)
{
printf("received user signaln");
}
[ob@localhost mycode]$ ./main &
[1] 3974
[ob@localhost mycode]$ kill -USR1 3974
received user signal
是不是这样的?signal仅仅是设置处理方法,对于该nu信号究竟怎么处理,还是要看最后的signal(nu, func)?
|
是不是这样的?signal仅仅是设置处理方法,对于该nu信号究竟怎么处理,还是要看最后的signal(nu, func)?
正确,signal仅仅是注册了一个信号的处理函数,即当信号到达是要调用的函数.
正确,signal仅仅是注册了一个信号的处理函数,即当信号到达是要调用的函数.