SIGSEGV Handling

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ilikesuresh
    New Member
    • Aug 2007
    • 47

    SIGSEGV Handling

    Hi,
    please any one explain how to handle sigsegv signal with example.....

    Thanks
  • ruskalym
    New Member
    • Sep 2007
    • 65

    #2
    Hi!

    How would you like to handle it?

    Do you want to catch it inside a program you wrote or to catch another program's signal?

    Comment

    • ilikesuresh
      New Member
      • Aug 2007
      • 47

      #3
      In the same program i need to catch the signal

      Comment

      • ruskalym
        New Member
        • Sep 2007
        • 65

        #4
        Here is a tiny code listing that catches the SIGSEGV signal :

        Code:
        void my_action(int sig)
        {
           printf("Caught signal\n");
        }
        
        int main()
        {
           struct sigaction sa;
        
           memset(&sa, 0, sizeof(struct sigaction));
           sa.sa_handler = my_action;
           sigemptyset(&sa.sa_mask);
           sigaction(SIGSEGV, &sa, NULL);
        
           while (1);
        
           return 0;
        }
        We first create a sigaction structure and set its handler to my_action.

        If a SIGSEGV is caught after the sigaction() call, my_action() will be called.

        However, the program will crash or call my_action() in an infinite loop if the SIGSEGV originates from program's memory violation.

        Comment

        • janardhan reddy
          New Member
          • Feb 2008
          • 1

          #5
          Hi,

          Need assistance regarding SIGSEGV handling.

          I tried the above example and suprisingly program enters into infinite loop in signal handler function.

          I want the control to come back to main from my_action() api without killing the program. Is it possible ? If possible please send me the answer.

          Thanks,
          Regards,
          Janardhan.

          Comment

          • GangadharM

            #6
            SIGSEGV is a synchronous signal. That mean it happens as part of current executing instruction. In general we do not handle SIGSEGV. If you provide your handler for these, handler will be executed and then your faulted instruction is tried again. This is where asynchronous signals differ. This is expected behavior.

            Comment

            Working...