Thursday 15 July 2010

c - executing default signal handler -



c - executing default signal handler -

i have written application have registered number of signal handler different signals in linux . after process receives signal command transferred signal handler had registered. in signal handler work need do, , phone call default signal hander i.e sif_dfl or sig_ign . however, sig_dfl , sig_ing both macros expand numeric values 0 , 1 respectively, invalid function addresses.

is there way can phone call default actions i.e sig_dfl or sig_ign ?

in order accomplish effect of sig_dfl or sig_ing phone call exit(1) , nil , respectively . signals sigsegv have core dump . in general want default behavior same sig_dfl , ignore behavior same sig_ign , way operating scheme .

the gnu c library reference manual has whole chapter explaining signal handling.

you set signal handler (a function pointer) when install own handler (see manpages signal() or sigaction()).

previous_handler = signal(sigint, myhandler);

the general rule is, can reset previous handler , raise() signal again.

void myhandler(int sig) { /* own stuff .. */ signal(sig, previous_handler); raise(sig); /* when returns here .. set our signal handler 1 time again */ signal(sig, myhandler); }

there 1 disadvantage of general rule: hardware exceptions mapped signals assigned instruction caused exception. so, when raise signal again, associated instruction not same originally. can should not harm other signal handlers.

another disadvantage is, each raised signal causes lot of processing time. prevent excessive utilize of raise() can utilize next alternatives:

in case of sig_dfl function pointer points address 0 (which no valid address). thus, have to reset handler , raise() signal again.

if (previous_handler == sig_dfl) { signal(sig, sig_dfl); raise(sig); signal(sig, myhandler); }

sig_ign has value 1 (also invalid address). here can homecoming (do nothing).

else if (previous_handler == sig_ign) { return; }

otherwise (neither sig_ign nor sig_dfl) have received valid function pointer , can phone call handler directly,

else { previous_handler(sig); }

of course, have consider different apis (see manpages signal() , sigaction()).

c linux signals handlers

No comments:

Post a Comment