home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / advos2 / ch13 / signal.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-12  |  1.7 KB  |  55 lines

  1. /*
  2.     SIGNAL.C  Example signal handler in Microsoft C.
  3.               Registers a signal handler then goes to
  4.               sleep for 10 seconds.  During the sleep
  5.               you can enter ^C and the signal handler
  6.               will display a message.
  7.  
  8.     Copyright (C) 1988 Ray Duncan
  9.  
  10.     Compile:    CL SIGNAL.C
  11.  
  12.     Usage:      SIGNAL
  13. */
  14.  
  15. #include <stdio.h>
  16.  
  17. #define SIGINTR 1                       /* Ctrl-C signal number */
  18.  
  19. void far pascal handler(unsigned, int); /* function prototypes */       
  20.  
  21. #define API unsigned extern far pascal
  22.  
  23. API DosSetSigHandler(void (pascal far *)(unsigned, int), 
  24.                      unsigned long far *, unsigned far *, int, int);
  25. API DosSleep(unsigned long);
  26.  
  27. main()
  28. {
  29.     unsigned long prevhandler;          /* previous handler address */
  30.     unsigned prevaction;                /* previous handler action */
  31.     int i;                              /* scratch variable */
  32.  
  33.                                         /* register Ctrl-C handler */
  34.     DosSetSigHandler(handler, &prevhandler, &prevaction, 2, SIGINTR);
  35.  
  36.     fprintf(stderr,"\nSignal handler registered.");
  37.     fprintf(stderr,"\nEnter Ctrl-C to demonstrate handler.\n");
  38.  
  39.     for(i=0; i<10; i++)                 /* sleep for 10 seconds */
  40.         DosSleep(1000L);    
  41. }
  42.  
  43. void far pascal handler(unsigned sigarg, int signum)
  44. {
  45.     unsigned long dummy1;               /* scratch variables */
  46.     unsigned dummy2;
  47.  
  48.                                         /* reset signal */
  49.     DosSetSigHandler(handler, &dummy1, &dummy2, 4, SIGINTR);
  50.  
  51.                                         /* show something happened */
  52.     fprintf(stderr,"\nSignal Received!\n");
  53. }
  54.  
  55.