Here is a tiny code listing that catches the SIGSEGV signal :
- 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.