home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / mssup201.arc / SIGNAL.MAN < prev    next >
Text File  |  1987-08-09  |  3KB  |  93 lines

  1. --------------------------------------------------------------------
  2. signal
  3. --------------------------------------------------------------------
  4.  
  5. NAME
  6.  
  7.      signal -- implements trapping of user interrupts
  8.  
  9. USAGE
  10.  
  11.      int (*signal (int sig, int (*action))) ()
  12.  
  13. PROTOTYPE IN
  14.  
  15.      signal.h /* but you must add it yourself */
  16.  
  17. DESCRIPTION
  18.  
  19. The function `signal' is used to establish an action routine for servicing a
  20. user interrupt.  Under MS-DOS, typing Ctrl-C causes a user interrupt.  (On
  21. IBM-compatible systems the Ctrl-Break key will cause the same user
  22. interrupt, and may work when a Ctrl-C fails.)
  23.  
  24. The first argument to `signal', sig, is a number identifying the signal for
  25. which an action is established.  The value for sig must be SIGINT.
  26.  
  27. The second parameter, `action', specifies the action to be taken when SIGINT
  28. occurs.  It is either the name of a user-defined function or one of the
  29. constants SIG_DFL (default, which aborts the process) or SIG_IGN (ignore,
  30. which causes SIGINT to be ignored;  however, in the case of MS-DOS, a '^C'
  31. is always echoed to the controlling device).
  32.  
  33. There are no special restrictions on what the action function may do,
  34. including continuing execution, returning normally or with a longjmp
  35. statement, or terminating executing with exit().  If the function returns
  36. normally, execution continues where it was interrupted by the occurrence of
  37. the SIGINT signal, except that any MS-DOS system call that was in progress
  38. may need to be restarted.
  39.  
  40. Before the user-defined function is called, the action on the event is
  41. restored to be SIG_DFL.  Thus a second SIGINT event will cause termination
  42. of the process unless the signal handler takes care to restore its action
  43. with a call to `signal'.  Even if it does so, however, a race condition
  44. still exists and two SIGINTs occurring rapidly will terminate the process.
  45. (But nobody types that fast.)
  46.  
  47. RETURN VALUE
  48.  
  49. If the specified signal is not SIGINT, the return value is SIG_ERR, which is
  50. defined in `signal.h', and errno is set to EINVAL.  Otherwise the return
  51. value is the value of `action' that was supplied to `signal' in the most
  52. recent legal call, or SIG_DFL if `signal' has never been called.
  53.  
  54. NOTE
  55.  
  56. A header file `signal.h' is already provided with Turbo C.  Revise it by
  57. adding the following line at the end:
  58.  
  59.      int (*_Cdecl signal (int sig, int (*action)())) ();  /* R.D */
  60.  
  61. AUTHOR
  62.  
  63.      Rahul Dhesi
  64.  
  65. EXAMPLE
  66.  
  67. #include <stdio.h>
  68. #include <signal.h>
  69.  
  70. int my_handler()
  71. {
  72.    signal (SIGINT, SIG_IGN);      /* ignore signals for now */
  73.    fflush (stdout);
  74.    printf ("\n<Break>\n");
  75.    fflush (stdout);
  76.    signal (SIGINT, my_handler);   /* reinstall signal handler */
  77. }
  78.  
  79. unsigned _stklen = 4000;          /* reserve stack */
  80.  
  81. main()
  82. {
  83.    int c;
  84.    signal (SIGINT, my_handler);
  85.    printf ("Control C is being trapped\n");
  86.    printf ("Type Q then <return> to exit\n");
  87.    for (;;) {
  88.       c = getchar();
  89.       if (c == 'q' || c == 'Q' || c == EOF)
  90.          break;
  91.    }
  92. }
  93.