Hi,
I have a problem with signal handler algorithm in linux. My code is hanging ( It is continuously looping inside the signal handler) . I am pasting my code here...
any help is appreciated
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
struct sigaction oldHandler;
void myHandler(int sig, siginfo_t *siginfo, void *context) {
// if i have not written any code inside this function, the program will give a feel of hang( this functions is getting continuously called
if(siginfo->si_code == SEGV_MAPERR)
{
write(1,"address not mapped to object",strlen("address not mapped to object"));
}
else if (siginfo->si_code == SEGV_ACCERR)
{
write(1,"invalid permissions for mapped object",strlen("invalid permissions for mapped object"));
}
write(1,"\n",1);
// exit(0); if this exit(0) is not present, program will get continuous calls to this signal handler
return;
}
int main(int argc, char *argv[]) {
/* Install mySignalHandler for SIGSEGV */
struct sigaction sigAct;
int status = 0;
char *addr = NULL;
sigAct.sa_handler = 0;
sigAct.sa_sigaction = myHandler;
sigfillset(&sigAct.sa_mask);
sigAct.sa_flags = SA_SIGINFO;
status = sigaction(SIGSEGV, &sigAct, &oldHandler);
if (status != 0) {
perror("Failed to install handler for signal SIGSEGV");
exit(1);
}
#if 1
/* This will invoke the signal handler */
// addr = malloc(strlen("Hello"));
strcpy(addr,"Hello");
printf("%s\n",addr);
#endif
printf("Returning from main\n");
return 0;
}