home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!spool.mu.edu!yale.edu!ira.uka.de!math.fu-berlin.de!news.th-darmstadt.de!rbg.informatik.th-darmstadt.de!misar
- From: misar@rbg.informatik.th-darmstadt.de (walter misar)
- Subject: Re: Signal() function & CTRL-C checking...
- Sender: news@news.th-darmstadt.de (The News System)
- Message-ID: <1992Dec17.094135@rbg.informatik.th-darmstadt.de>
- Date: Thu, 17 Dec 1992 08:41:35 GMT
- References: <1992Dec16.053939.1014@monu6.cc.monash.edu.au>
- Nntp-Posting-Host: rbhp60.rbg.informatik.th-darmstadt.de
- Organization: TH Darmstadt
- Lines: 51
-
- In article <1992Dec16.053939.1014@monu6.cc.monash.edu.au>, alien@yoyo.cc.monash.edu.au (Diego Barros) writes:
- > I have a quesiton concerning the signal function and how it is used. I'm
- > trying to set up a routine which is called when anyone of 4 interrupts
- > occur. When one of them does occur then a single function is called
- > which will clean up and exit the program. The main reason is to detect
- > when the user presses CTRL-C to break program execution.
- >
- > Could someone please explain what exactly the signal prototype in the
- > signal.h file is declaring (in english?!?) :) As knowing what the
- > function is declared as would of course help my understanding as to how
- > to use the function call.
- >
- > <signal.h> extern void (*signal(int __sig, (*__func)(int)))(int);
- >
- > And how should I declare the function which is called when one of the 4
- > interrupts occur, what's its prototype? And how is signal called
- > exactly?
-
- Let's see: void (*signal(....))(int)
- => signal is a function returning a pointer to a function which takes an int
- as argument and doesn't return any value (void).
- Now for the parameters: int __sig nameof the signal (SIGINT for ^C)
- (*__func)(int) or void (*__func)(int) on other machines means, that the
- second argument is a pointer to the function that should be called upon
- occurrence of the signal. SIG_IGN (ignore) and SIG_DFL (default) can be
- specified also.
- Here's an small example programm:
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <signal.h>
-
- void ctrlc(int arg)
- { static int i=0;
-
- i++;
- fprintf(stderr,"CTRL-C hit !\n");
- signal(SIGINT,ctrlc);
- if (i==3) exit(0); /* exit on 3rd ctrl-c */
- }
-
- int main(int argc, char *argv[])
- { signal(SIGINT,ctrlc);
- for (;;); /* loop for a while */
- return 0; /* return, if you can */
- }
-
- Hope this helps.
- --
- Walter Misar It is impossible to enjoy idling thoroughly
- misar@rbg.informatik.th-darmstadt.de unless one has plenty of work to do.
-