home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18522 < prev    next >
Encoding:
Text File  |  1992-12-17  |  2.5 KB  |  64 lines

  1. Newsgroups: comp.lang.c
  2. 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
  3. From: misar@rbg.informatik.th-darmstadt.de (walter misar)
  4. Subject: Re: Signal() function & CTRL-C checking...
  5. Sender: news@news.th-darmstadt.de (The News System)
  6. Message-ID: <1992Dec17.094135@rbg.informatik.th-darmstadt.de>
  7. Date: Thu, 17 Dec 1992 08:41:35 GMT
  8. References:  <1992Dec16.053939.1014@monu6.cc.monash.edu.au>
  9. Nntp-Posting-Host: rbhp60.rbg.informatik.th-darmstadt.de
  10. Organization: TH Darmstadt
  11. Lines: 51
  12.  
  13. In article <1992Dec16.053939.1014@monu6.cc.monash.edu.au>, alien@yoyo.cc.monash.edu.au (Diego Barros) writes:
  14. > I have a quesiton concerning the signal function and how it is used. I'm
  15. > trying to set up a routine which is called when anyone of 4 interrupts
  16. > occur. When one of them does occur then a single function is called
  17. > which will clean up and exit the program. The main reason is to detect
  18. > when the user presses CTRL-C to break program execution.
  19. > Could someone please explain what exactly the signal prototype in the
  20. > signal.h file is declaring (in english?!?) :) As knowing what the
  21. > function is declared as would of course help my understanding as to how
  22. > to use the function call.  
  23. > <signal.h>    extern void (*signal(int __sig, (*__func)(int)))(int);
  24. > And how should I declare the function which is called when one of the 4
  25. > interrupts occur, what's its prototype? And how is signal called
  26. > exactly?
  27.  
  28. Let's see: void (*signal(....))(int)
  29. => signal is a function returning a pointer to a function which takes an int
  30. as argument and doesn't return any value (void).
  31. Now for the parameters: int __sig nameof the signal (SIGINT for ^C)
  32. (*__func)(int) or void (*__func)(int) on other machines means, that the
  33. second argument is a pointer to the function that should be called upon
  34. occurrence of the signal. SIG_IGN (ignore) and SIG_DFL (default) can be
  35. specified also.
  36. Here's an small example programm:
  37.  
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <signal.h>
  41.  
  42. void ctrlc(int arg)
  43. { static int i=0;
  44.  
  45.   i++;
  46.   fprintf(stderr,"CTRL-C hit !\n");
  47.   signal(SIGINT,ctrlc);
  48.   if (i==3) exit(0);    /* exit on 3rd ctrl-c    */
  49. }
  50.  
  51. int main(int argc, char *argv[])
  52. { signal(SIGINT,ctrlc);
  53.   for (;;);    /* loop for a while    */
  54.   return 0;    /* return, if you can    */
  55. }
  56.  
  57. Hope this helps.
  58. -- 
  59. Walter Misar                        It is impossible to enjoy idling thoroughly
  60. misar@rbg.informatik.th-darmstadt.de       unless one has plenty of work to do.
  61.