加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 09:47:17 所属栏目:百科 来源:网络整理
导读:在我的下面的代码中,如果我将old_act声明为全局变量,那么程序运行正常.如果在main中声明: 如果使用SA_RESTART,它可以正常工作 如果未使用SA_RESTART,则会导致分段错误. 有人可以帮我理解发生的事情. void sighandler(int signum){ printf("Caught signal:%d
在我的下面的代码中,如果我将old_act声明为全局变量,那么程序运行正常.如果在main中声明:

>如果使用SA_RESTART,它可以正常工作
>如果未使用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 loopn");
        int remain=sleep(10);
        printf("remaining time in sec : %dn",remain);
        printf("Before second sleepn");
        sleep(10);
        printf("This is an infinite loopn");
        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,sizeof(act_h));
        act_h.sa_handler = sighandler;
        .... //continue your code;

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读