home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!elroy.jpl.nasa.gov!usc!rpi!uwm.edu!linac!pacific.mps.ohio-state.edu!cis.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
- From: torek@horse.ee.lbl.gov (Chris Torek)
- Newsgroups: comp.lang.c
- Subject: Re: Signal() function & CTRL-C checking...
- Date: 16 Dec 1992 19:01:08 GMT
- Organization: Lawrence Berkeley Laboratory, Berkeley CA
- Lines: 59
- Message-ID: <28013@dog.ee.lbl.gov>
- References: <1992Dec16.053939.1014@monu6.cc.monash.edu.au>
- NNTP-Posting-Host: 128.3.112.15
-
- In article <1992Dec16.053939.1014@monu6.cc.monash.edu.au>
- alien@yoyo.cc.monash.edu.au (Diego Barros) writes:
- >Could someone please explain what exactly the signal prototype in the
- >signal.h file is declaring (in english?!?) :)
-
- ><signal.h> extern void (*signal(int __sig, (*__func)(int)))(int);
-
- You left out a `void' (either that, or your <signal.h> is broken):
-
- void (*signal(int, void (*)(int)))(int);
-
- (note, I have broken up long lines below)
- % cdecl
- cdecl> explain void (*signal(int, void (*)(int)))(int)
- declare signal as function
- (int, pointer to function (int) returning void)
- returning pointer to function (int) returning void
-
- If your tabstops are set properly :-) (or even if not) a certain
- symmetry here should jump out at you: signal's second argument has
- the exact same type as signal's return value. If you take the
- `pointer to's off that type, you get something quite simple:
-
- function (int) returning void
-
- or, in C:
-
- void myfunc(int some_parameter) { ... }
-
- These are valid `signal functions'. When a signal occurs, if the
- signal has been attached to `myfunc', `myfunc' will get called with
- some_parameter set to the signal number. Hence:
-
- (void)signal(SIGINT, myfunc);
- raise(SIGINT);
-
- will call myfunc() with SIGINT, almost as if you replaced the raise()
- with
-
- myfunc(SIGINT);
-
- The return value from signal() is simply a pointer to the previous
- function, if any, or one of the special values (SIG_IGN, SIG_DFL,
- SIG_ERR).
-
- If you call a signal handler yourself, through raise(), it is fairly
- safe to do just about anything in that handler. If you trap `system'
- signals (SIGINT, SIGALRM, etc) and receive one of those, very little
- is safe. Portable, reliable programs generally just set a flag (of
- type volatile sig_atomic_t) and return; the remainder of such programs
- tends to be peppered with checks of the form:
-
- if (we_got_an_interrupt) take_care_of_it();
-
- (Most interactive programs have some sort of central loop, so that
- one does not need very many `if (we_got_an_interrupt)'s.)
- --
- In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
- Berkeley, CA Domain: torek@ee.lbl.gov
-