Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!uakari.primate.wisc.edu!uflorida!mephisto!udel!princeton!phoenix!subbarao From: subbarao@phoenix.Princeton.EDU (Kartik Subbarao) Newsgroups: comp.lang.c Subject: Re: Catching Signals in 'C' Message-ID: <2905@idunno.Princeton.EDU> Date: 28 Sep 90 15:16:23 GMT References: <1990Sep28.120043.17628@NCoast.ORG> <2901@idunno.Princeton.EDU> Sender: news@idunno.Princeton.EDU Lines: 60 In article <2901@idunno.Princeton.EDU> pfalstad@bow.Princeton.EDU (Paul John Falstad) writes: >In article <1990Sep28.120043.17628@NCoast.ORG>, ramsey@NCoast.ORG (Cedric Ramsey) writes: >|> Hello peoplekind. I have a question about the behavior of the >|> signal function. Firstly, I want to trap the control-c, break, >|> and other interrupt keys the user may use to stop a program. >|> I did this by ; >|> >|> main() >|> { >|> signal (SIGINT, (*handler1) ()); >|> signal (SIGQUIT, (*handler2) ()); >|> ... >|> } >|> >|> >|> But, when I type a control-c the program handles the signal as >|> expected; however, when I type the control-c a second time the program >|> doesn't catch it and simply exits. > >I quote from K&R, p. 255: > >"When a signal 'sig' subsequently occurs, the signal is __restored to its >default behavior__; then the signal-handler is called, as if by (*handler)(sig)." Ahh... But Paul, don't we want to give him the answer to his question? Too many times K & R has been quoted...and yet no real solutions are given. > >The signal handler should reinstall itself before returning. signal() >apparently does not act this way in my implementation (SunOS)... Fortunately. But programming should be PORTABLE, should it not :-) A solution is suggested in "UNIX programming", by some author whom I've forgotten (a Howard SAMS publication). What you should put in your signal handlers (*handler1), (*handler2), is a simple signal(SIGINT, SIG_DFL); /* replace SIGINT by whatever signal you wish to trap */ This will restore the signal to previous status. >-- >Here is the address to complain to: >pfalstad@phoenix.princeton.edu PLink:HYPNOS GEnie:P.FALSTAD CIS: 70016,1355 >That address again, >sync@thumper.princeton.edu PLink:OHS738 GEnie:OHS738 CIS: 4128 143 1234 937 Or a simple "F" to alt.dev.null :-) -Kartik (I need a new .signature -- any suggestions?) subbarao@{phoenix or gauguin}.Princeton.EDU -|Internet kartik@silvertone.Princeton.EDU (NeXT mail) -| SUBBARAO@PUCC.BITNET - Bitnet