c - Segmentation fault in sigaction signal handler -


in below code, if i'm declaring old_act global variable program works fine. if declared inside main:

  1. if sa_restart used, works fine
  2. if sa_restart not used, causes segmentation fault.

can please me comprehend happening.

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 infinite loop\n");         int remain=sleep(10);         printf("remaining time in sec : %d\n",remain);         printf("before second sleep\n");         sleep(10);         printf("this infinite loop\n");         return 0; } 

from gdb looks function call happening @ illegal location ,but not sure:

this gdb configured "i686-linux-gnu". bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... reading symbols /home/diwakar/documents/my_c_codes/l2it/sighandling/a.out...done. [new lwp 5661]  warning: can't read pathname load map: input/output error. core generated `./a.out'. program terminated signal 11, segmentation fault. #0  0xb77c1938 in ?? () (gdb)    (gdb) bt #0  0xb77c1938 in ?? () cannot access memory @ address 0xe   (gdb) run starting program: /home/diwakar/documents/my_c_codes/l2it/sighandling/a.out  infinite loop ^c program received signal sigint, interrupt. 0xb7fdd424 in __kernel_vsyscall () (gdb) bt #0  0xb7fdd424 in __kernel_vsyscall () #1  0xb7ed2f00 in nanosleep () /lib/i386-linux-gnu/libc.so.6 #2  0xb7ed2d1f in sleep () /lib/i386-linux-gnu/libc.so.6 #3  0x08048502 in main () @ signal.c:33 (gdb) disassemble dump of assembler code 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)  

try resetting members of act_h 0 before assigning it. sa_flags has random value making signal action behave differently.

int main() {         struct sigaction act_h;         struct sigaction old_act;          //reset members         memset(&act_h, 0, sizeof(act_h));         act_h.sa_handler = sighandler;         .... //continue code;  } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -