home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / signals.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  2.7 KB  |  91 lines

  1. ;/* signals.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 signals.c
  3. Blink FROM LIB:c.o,signals.o TO signals LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5. */
  6.  
  7. #include <exec/types.h>
  8. #include <exec/memory.h>
  9. #include <dos/dos.h>
  10.  
  11. #include <clib/exec_protos.h>
  12. #include <clib/dos_protos.h>
  13. #include <clib/alib_protos.h>
  14. #include "stdio.h"
  15.  
  16. #ifdef LATTICE
  17. int CXBRK(void) { return(0); }     /* Disable Lattice CTRL/C handling */
  18. int chkabort(void) { return(0); }  /* really */
  19. #endif
  20.  
  21. static UBYTE *VersTag = "$VER: signals 37.1 (28.3.91)";
  22.  
  23. void subtaskcode(void);    /* prototype for our subtask routine */
  24.  
  25. LONG  mainsignum = -1;
  26. ULONG mainsig, wakeupsigs;
  27. struct Task *maintask = NULL, *subtask = NULL;
  28. UBYTE subtaskname[] = "RKM_signal_subtask";
  29.  
  30. void main(int argc, char **argv)
  31. {
  32. BOOL Done = FALSE, WaitingForSubtask = TRUE;
  33.  
  34. /* We must allocate any special signals we want to receive. */
  35. mainsignum = AllocSignal(-1);
  36. if(mainsignum == -1)
  37.     printf("No signals available\n");
  38. else
  39.     {
  40.     mainsig = 1L << mainsignum;    /* subtask can access this global */
  41.     maintask = FindTask(NULL);     /* subtask can access this global */
  42.  
  43.     printf("We alloc a signal, create a task, wait for signals\n");
  44.     subtask = CreateTask(subtaskname, 0L, subtaskcode, 2000);
  45.     if(!subtask)
  46.         printf("Can't create subtask\n");
  47.     else
  48.         {
  49.         printf("After subtask signals, press CTRL-C or CTRL-D to exit\n");
  50.  
  51.         while((!Done)||(WaitingForSubtask))
  52.             {
  53.             /* Wait on the combined mask for all of the signals we are
  54.              * interested in.  All processes have the CTRL_C thru CTRL_F
  55.              * signals.  We're also Waiting on the mainsig we allocated
  56.              * for our subtask to signal us with.  We could also Wait on
  57.              * the signals of any ports/windows our main task created ... */
  58.  
  59.             wakeupsigs = Wait(mainsig | SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D);
  60.  
  61.             /* Deal with all signals that woke us up - may be more than one */
  62.             if(wakeupsigs & mainsig)
  63.                 {
  64.                 printf("Signalled by subtask\n");
  65.                 WaitingForSubtask = FALSE;   /* OK to kill subtask now */
  66.                 }
  67.             if(wakeupsigs & SIGBREAKF_CTRL_C)
  68.                 {
  69.                 printf("Got CTRL-C signal\n");
  70.                 Done = TRUE;
  71.                 }
  72.             if(wakeupsigs & SIGBREAKF_CTRL_D)
  73.                 {
  74.                 printf("Got CTRL-D signal\n");
  75.                 Done = TRUE;
  76.                 }
  77.             }
  78.         Forbid();
  79.         DeleteTask(subtask);
  80.         Permit();
  81.         }
  82.     FreeSignal(mainsignum);
  83.     }
  84. }
  85.  
  86. void subtaskcode(void)
  87.     {
  88.     Signal(maintask,mainsig);
  89.     Wait(0L);    /* safe state in which this subtask can be deleted */
  90.     }
  91.