signal()函数无法正常工作

系统安装、升级讨论
版面规则
我们都知道新人的确很菜,也喜欢抱怨,并且带有浓厚的Windows习惯,但既然在这里询问,我们就应该有责任帮助他们解决问题,而不是直接泼冷水、简单的否定或发表对解决问题没有任何帮助的帖子。乐于分享,以人为本,这正是Ubuntu的精神所在。
回复
urey
帖子: 1
注册时间: 2008-05-09 21:00

signal()函数无法正常工作

#1

帖子 urey » 2009-06-04 14:50

简单的有关进程信号的程序
/*signaltest.c*/

#include <signal.h>
#include <stdio.h>
#include <unistd.h>

void ouch(int sig)
{

printf("OUCH! - I got signal %d\n", sig);

}


//self defined signal deal function

int main()
{
(void) signal(SIGINT, ouch);

while(1){
printf("Hello World! \n");
sleep(1);
}

}
/////////////////////////////////////
//gdb debug run
Starting program: /home/urey/signtest
Hello World!
Hello World!
Hello World!
(press ctrl-c when running here)
Program received signal SIGINT, Interrupt.
0xb7fb7410 in __kernel_vsyscall ()
(gdb)

signal()函数无法正常工作,没有将SIGINT信号传递给ouch()函数处理,而是依照内核默认中断处理. 程序在别人的电脑上可以正常运行。程序应当输出"OUCH! - I got signal 2" 在按下ctrl-c之后. 你可以自己尝试一下.非常感谢。
头像
xhy
帖子: 3916
注册时间: 2005-12-28 1:16
系统: Ubuntu 12.10 X64
来自: 火星

Re: signal()函数无法正常工作

#2

帖子 xhy » 2009-06-04 18:29

代码: 全选

(gdb) handle   SIGINT   nostop   print   pass     
SIGINT is used by the debugger.
Are you sure you want to change it? (y or n) y
Signal        Stop      Print   Pass to program Description
SIGINT        No        Yes     Yes             Interrupt
(gdb) run
Starting program: /home/sagaxu/a.out 
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
Hello World! 

Program received signal SIGINT, Interrupt.
OUCH! - I got signal 2
Hello World! 
Hello World! 

Program received signal SIGINT, Interrupt.
OUCH! - I got signal 2
Hello World! 
Hello World! 
Hello World! 
Hello World! 

Program received signal SIGINT, Interrupt.
OUCH! - I got signal 2
Hello World! 
目前负债150多万
回复