Operating System - HP-UX
1833768 Members
2255 Online
110063 Solutions
New Discussion

Catching signals sent by OS to the process

 
Shashibhushan Gokhale
Occasional Contributor

Catching signals sent by OS to the process

Hello,

I want to execute some code when a SIGSEGV/SIGBUS is sent by OS to my process.

I have written the attached code(gencatchsignal.C) to do this:

I compile it using aCC on HP-UX 11i as:
aCC -g gencatchsignal.C -lpthread

The line 33: "*str_tok_end = '\0';" generates a SIGBUS error.
But the application immediately dumps the core.

I had expected that the function: Signalhandler_SIGBUS which is configured to handle SIGBUS should get invoked.

Can anyone please suggest a way for me to handle this SIGBUS signal.

Regards,
Shashibhushan Gokhale
***********************************************
PS:
Actually I want to implement this for a SIGSEGV, but am facing the following problem. I understand that if I generate SIGSEGV using the following code snippet(which I got from Elmar in response to my earlier question):

int main(int argc, char** argv)
{ int i;
for (i =0; ; i++)
argv[1] = argv[i];
}
The system stack gets exhausted and hence SignalHandler cannot be loaded.

Hence have taken this example of SIGBUS.

It would be fantastic if someone can generate and then catch a SIGSEGV(The function SignalHandler_SIGSEGV should get called) in my program.
5 REPLIES 5
Graham Cameron_1
Honored Contributor

Re: Catching signals sent by OS to the process

I can't test this as I don't have aCC, but isn't is simply that you have not yet called your signalhandlers at the point you generate the violation ?

ie if you copy the 2 calls to

signal(SIGBUS,SignalHandler_SIGBUS) ;
signal(SIGSEGV,SignalHandler_SIGSEGV) ;

to the first executable lines of main(), then you will arm the signal handlers in time and the signal will be trapped.

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Elmar P. Kolkman
Honored Contributor

Re: Catching signals sent by OS to the process

Strange... I have it compiled using gcc, after some simple altering (cout construct you use doesn't compile in gcc, iostream.h is not available, interupthandler needs a parameter name) and it does exactly what you want: end in the SEGV routine.

At first I tried setting the signal function calls before creating the pthread, but that is not necessary. But perhaps it is in your aCC to make sure that this is set before you generate the signal. I am not sure if the interupthandler is called before the thread is started, you see.
Every problem has at least one solution. Only some solutions are harder to find.
Umapathy S
Honored Contributor

Re: Catching signals sent by OS to the process

Shashi,
Let me compensate for my previous response.

Attached is the file. This is for SIGSEGV. Uncomment the other half for SIGBUS.

HTH,
Umapathy
Arise Awake and Stop NOT till the goal is Reached!
Zeev Schultz
Honored Contributor

Re: Catching signals sent by OS to the process

ok, compiled it on aCC 03.33 (11.00)
with warnings (ignorable)

Warning 829: "sign.c", line 29 # Implicit conversion of string literal to
'char *' is deprecated.
txt_ErrMsg = "TEST DATA";
^^^^^^^^^^^
Warning 829: "sign.c", line 15 # Implicit conversion of string literal to
'char *' is deprecated.
char *name="SHASHI";
^^^^^^^^
Warning 361: "sign.c", line 37 # Value-returning function might end without
executing a return statement.
void* interruptHandler(void*)

executable fails with pthread_create failed with error code:251Exiting...

....didn't check libc,pthread,aCC patches.
So computers don't think yet. At least not chess computers. - Seymour Cray
yogiraj
Advisor

Re: Catching signals sent by OS to the process

Shahi,
I've not used pthread see if its ok with you,
==============================
#include
#include
#include
#include

void hndl_sgnl(int);
int main(int argc,char **argv)
{
signal(SIGSEGV,hndl_sgnl);

for (int i =0;i < 40 ; i++)
{
argv[1] = *(argv + i);
printf("\n %s\n",argv[1]);
}

printf("\n Oops ! Didn't do it \n");

return 0;
}

void hndl_sgnl(int sgnl)
{
printf("\n Recvd <%d> signal\n",sgnl);
printf("\n Carry out cleaning activity and then exit\n");
exit(-1);
}
===================================
patilyogi