signal问题

camperbird 2008-10-18 06:38:44
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了,

测试代码
#include <signal.h>
#include <stdio.h>
#include <bits/signum.h>
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 signal\n");
}


[ob@localhost mycode]$ ./main &
[1] 3974
[ob@localhost mycode]$ kill -USR1 3974
received user signal

是不是这样的?signal仅仅是设置处理方法,对于该nu信号究竟怎么处理,还是要看最后的signal(nu, func)?
...全文
148 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
cceczjxy 2008-10-18
  • 打赏
  • 举报
回复
是不是这样的?signal仅仅是设置处理方法,对于该nu信号究竟怎么处理,还是要看最后的signal(nu, func)?

正确,signal仅仅是注册了一个信号的处理函数,即当信号到达是要调用的函数.

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧