LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need help in catching and handling SIGSEGV signal (https://www.linuxquestions.org/questions/programming-9/need-help-in-catching-and-handling-sigsegv-signal-624107/)

janardhan@eilabs.com 02-26-2008 11:06 PM

Need help in catching and handling SIGSEGV signal
 
Hi,

Need assistance regarding SIGSEGV signal handling.
I want the signal handler to catch the signal do some coding there, and return to main.
I succeeded in catching the signal SIGSEGV.
But the control does not return to main from signal handler function.
Please guide me to solve this problem.

I have written a sample code to catch the SIGSEGV signal and handle it.

My Sample Code:
_______________

#include <unistd.h>
#include <sys/types.h>
#include <signal.h>

void seg()
{
printf("SEGV caught\n");
}

int main ( )
{
int* ptr = (int*)0;

signal( SIGSEGV, seg);

*ptr = 1;

printf("End of main\n");

return 0;
}

OUTPUT:
---------------
SEGV caught
SEGV caught
....
....
Goes on like this infinitely and control does not return to main.

Thanks,
Regards,
Janardhan

PatrickNew 02-26-2008 11:15 PM

I think the problem is that control *is* returning to main. Trying to dereference the null pointer generates the signal, but it seems that control is resuming right before that statement every time, so that you dereference your pointer, handle that SEGV, then return only to dereference the pointer again.

I'm not sure if this is the expected behavior, but I think it's good design that your signal handler should in some way handle the signal, ie. prevent it from happening again.

reddazz 02-27-2008 05:08 AM

Moved: This thread is more suitable in the Programming forum and has been moved accordingly to help your thread/question get the exposure it deserves.


All times are GMT -5. The time now is 03:55 AM.