home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!fuug!demos!kiae!glas!demos!mdd.comm.mot.com!mitchell
- From: mitchell@mdd.comm.mot.com
- Newsgroups: comp.lang.c
- Date: 17 Jul 92 18:12 MDT
- Subject: Re: trapping "control C"
- Sender: Notesfile to Usenet Gateway <notes@glas.apc.org>
- Message-ID: <1992Jul17.141248.4837@mdd.comm.m>
- References: <13t9osINNqam@darkstar.ucsc.edu>
- Nf-ID: #R:13t9osINNqam@darkstar.ucsc.edu:-1109617974:1992Jul17.141248.4837@mdd.comm.m:-1220607600:001:2554
- Nf-From: mdd.comm.mot.com!mitchell Jul 17 18:12:00 1992
- Lines: 74
-
-
- in comp.lang.c, dylan@ibmpcug.co.uk (Matthew Farwell) said:
-
- >In article <13t9osINNqam@darkstar.UCSC.EDU> gil@cse.ucsc.edu (Elmer Fudd) writes:
- >>can anyone suggest where i can get info/examples on how to trap a
- >>control C in a program?
- >>[...]
- >
- >In most cases, using signal() to trap a SIGINT will do what you
- >want it to do. But again, this is machine dependent - under Unix you
- >can set the interupt key to be whatever you want (mine is currently set
- >to DEL, not CTRL C).
- >
- >However, here is some code which will probably do what you want it to
- >do...
- >
- >#include <stdio.h>
- >#include <stdlib.h>
- >#include <signal.h>
- >
- >void handler()
- >{
- > fprintf(stderr, "got a SIGINT. Exiting.\n");
- >
- > /* do some cleanup */
- >
- > exit(1);
- >}
- >
- >int main()
- >{
- > if (signal(SIGINT, SIG_IGN) != SIG_IGN)
- > signal(SIGINT, handler);
- >
- > while (1) printf("Output\n"); /* loop endlessly */
- >
- > /* NOTREACHED */
- >
- > return 0; /* for gcc -Wall */
- >}
- >
-
- Having been recently bitten by a signal handler, I thought I'd comment here.
-
- Looking in PJP's "The Standard C Library", chapter 9, I see that he goes on
- at some length about syncronization and other problems which can occur in
- handling signals. In part, he says:
-
- The occurence of a signal introduces a second thread of control within
- a program. That raises all sorts of issues about syncronization and
- reliable operation. The C Standard promises little in either regard.
- C programs have been handling signals since the earliest days of the
- language. Nevertheless, a portable program can safely take very few
- actions within a signal handler.
-
- The case above is pretty vanilla, except for the as-yet undefined cleanup,
- since the program terminates from within the handler. If the handler
- didn't terminate, it would have to be very careful about what actions
- it took. The safest approach I know is to do _NOTHING_ inside the handler
- except to set a global variable to indicate that the signal occurred, and
- place code to test that variable and react to the signal in the mainline
- control thread. PJP points out that even this is not necessarily 100%
- bulletproof. It's generally a good idea, however, to do as little as
- possible in a signal handler.
-
- I think it's also a good idea to make that a stylistic habit, and extend the
- maxim of doing as little as possible with a signal handler even to handlers
- which could terminate the program from within the handler control thread.
- Set a flag and return. React to the flag in the mainline control thread.
-
- --
- mitchell@mdd.comm.mot.com (Bill Mitchell)
-
-
-