c - sigaction 信号处理程序中的段错误

标签 c linux gdb segmentation-fault signals

在我下面的代码中,如果我将 old_act 声明为全局变量,那么程序可以正常工作。如果在 main 中声明:

  1. 如果使用 SA_RESTART,它工作正常
  2. 如果不使用 SA_RESTART,则会导致段错误。

谁能帮我理解发生了什么。

void sighandler(int signum)
{
        printf("Caught signal:%d pressed ctrl+c!!\n",signum);
}

int main()
{
        struct sigaction act_h;
        struct sigaction old_act;
        act_h.sa_handler = sighandler;
//      act_h.sa_flags = SA_RESTART;

       sigaction(SIGINT,&act_h,&old_act);

        printf("This is an infinite loop\n");
        int remain=sleep(10);
        printf("remaining time in sec : %d\n",remain);
        printf("Before second sleep\n");
        sleep(10);
        printf("This is an infinite loop\n");
        return 0;
}

从 gdb 看来,某些函数调用发生在非法位置,但不确定:

This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/diwakar/Documents/my_C_codes/L2IT/SigHandling/a.out...done.
[New LWP 5661]

warning: Can't read pathname for load map: Input/output error.
Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
#0  0xb77c1938 in ?? ()
(gdb) 


(gdb) bt
#0  0xb77c1938 in ?? ()
Cannot access memory at address 0xe


(gdb) run
Starting program: /home/diwakar/Documents/my_C_codes/L2IT/SigHandling/a.out 
This is an infinite loop
^C
Program received signal SIGINT, Interrupt.
0xb7fdd424 in __kernel_vsyscall ()
(gdb) bt
#0  0xb7fdd424 in __kernel_vsyscall ()
#1  0xb7ed2f00 in nanosleep () from /lib/i386-linux-gnu/libc.so.6
#2  0xb7ed2d1f in sleep () from /lib/i386-linux-gnu/libc.so.6
#3  0x08048502 in main () at signal.c:33
(gdb) disassemble
Dump of assembler code for function __kernel_vsyscall:
   0xb7fdd414 <+0>: push   %ecx
   0xb7fdd415 <+1>: push   %edx
   0xb7fdd416 <+2>: push   %ebp
   0xb7fdd417 <+3>: mov    %esp,%ebp
   0xb7fdd419 <+5>: sysenter 
   0xb7fdd41b <+7>: nop
   0xb7fdd41c <+8>: nop
   0xb7fdd41d <+9>: nop
   0xb7fdd41e <+10>:    nop
   0xb7fdd41f <+11>:    nop
   0xb7fdd420 <+12>:    nop
   0xb7fdd421 <+13>:    nop
   0xb7fdd422 <+14>:    int    $0x80
=> 0xb7fdd424 <+16>:    pop    %ebp
   0xb7fdd425 <+17>:    pop    %edx
   0xb7fdd426 <+18>:    pop    %ecx
   0xb7fdd427 <+19>:    ret    
End of assembler dump.
(gdb) 

最佳答案

尝试在分配之前将 act_h 的所有成员重置为零。 sa_flags 很可能有一些随机值,这使得信号操作的行为有所不同。

int main()
{
        struct sigaction act_h;
        struct sigaction old_act;

        //reset all members
        memset(&act_h, 0, sizeof(act_h));
        act_h.sa_handler = sighandler;
        .... //continue your code;

}

关于c - sigaction 信号处理程序中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18606468/

相关文章:

c 使用 lseek 以相反的顺序复制文件

c - XV6-循环调度程序

c++ - 如何生成自由函数

ruby - unicorn 服务器崩溃,回溯最少

c++ - 有没有办法完全禁用 gdb 输出?

c++ - 有没有可靠的工具可以去除 ASM/C/C++ 代码中的注释?

python - 使用 linux 命令从宽格式转换为长格式

c - pthread_self() 和 gettid() 返回值之间的差异

linux - "Acknowledge interrupt on exit"VT-x 中的控制导致 CPU 锁定

c - 静态链接不在库 api strlen() 和 puts() 的 gdb 中提供代码