Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!julius.cs.uiuc.edu!psuvax1!rutgers!njin!princeton!dae!pfalstad From: pfalstad@dae.Princeton.EDU (Paul John Falstad) Newsgroups: comp.lang.c Subject: Re: Catching Signals in 'C' Summary: Stop and post before you think. Message-ID: <2910@idunno.Princeton.EDU> Date: 28 Sep 90 16:59:08 GMT References: <1990Sep28.120043.17628@NCoast.ORG> <2901@idunno.Princeton.EDU> <2905@idunno.Princeton.EDU> Sender: news@idunno.Princeton.EDU Organization: Princeton University, Princeton, New Jersey Lines: 69 In article <2905@idunno.Princeton.EDU> subbarao@phoenix.Princeton.EDU (Kartik Subbarao) writes: >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: >>|> 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. I did answer his question. Oh, sorry, you wanted a code fragment I suppose. Ok: void handler1(whatever) { printf("You're not getting out so easily!\n"); signal(SIGINT,handler1); /* (*handler1) () not necessary */ } This code implements the following sentence. >>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 :-) True. Why are you telling me this? My above answer is portable. I was just commenting on the fact that SunOS does not implement signal this way. >What you should put in your signal handlers (*handler1), (*handler2), >is a simple >signal(SIGINT, SIG_DFL); >This will restore the signal to previous status. Thank you! You have just found a portable way to implement the bug that the original poster wanted to fix. Now it doesn't work on nearly ALL implementations of C instead of merely ANSI compliant ones. Next time, read the article before following up to it. >>Here is the address to complain to: >Or a simple "F" to alt.dev.null :-) Why didn't you then? >(I need a new .signature -- any suggestions?) Disclaimer: I have for a long time been suffering from a species of brain injury which I incurred during the rigors of childbirth; and I'd like to conclude by putting my finger up my nose. How's that? ;-) -- 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