日期:2014-05-16  浏览次数:20740 次

一个signal的问题,gdb调试都无反应了
C/C++ code
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
void sig_handler(int signo)
{
   char data[100];
   int len ;
   len = read(STDIN_FILENO,&data,100);
   data[len]=0;
   printf("This signal is : %d\n",signo);
   printf("%s\n",data);
   exit(0);
}
int main()
{  
   int oflags;
   signal(SIGINT,sig_handler);
   signal(SIGTERM,sig_handler);
   signal(SIGIO,sig_handler);
   fcntl(STDIN_FILENO,F_SETOWN,getpid());
   printf("ID:%d\n",getpid());
   oflags = fcntl(STDIN_FILENO,F_GETFL);
   fcntl(STDIN_FILENO,F_SETFL,oflags|FASYNC);
   while(1);
}


Breakpoint 1 at 0x40125e: file sig.c, line 24.
(gdb) r
Starting program: /cygdrive/c/a/sig
[New Thread 9760.0x17cc]
[New Thread 9760.0x24a8]
ID:9760

Breakpoint 1, main () at sig.c:24
24 fcntl(STDIN_FILENO,F_SETFL,oflags|FASYNC);
(gdb) n
25 while(1);
(gdb) n
n
adflasjdflasfk
adflj
asdfkl
gggggh

无法退出来了。。。

------解决方案--------------------
没问题啊.

[liangdong@jx-csblog01.jx.baidu.com src]$ ./main
ID:27784
123
This signal is : 29
, len is : 4 
123


你看到len是4了么, read是底层I/O, 只认识字节, 回车也是1字节, 所以len是4.