home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / sources / code.fra < prev    next >
Internet Message Format  |  1990-08-03  |  6KB

  1. From: jack@cscdec.UUCP (Jack Hudler)
  2. Subject: And alarm(1) for OS/2 1.2 (sources).
  3. Date: 18 Jul 90 05:51:54 GMT
  4. Organization: Computer Support Corporation. Dallas,Texas
  5.  
  6. Here is something I threw together one night while I was porting some code
  7. from unix to OS/2.
  8.  
  9. There is no equivalent function in the C libraries for ALARM(1) so I wrote
  10. this to simulate it. It does a very good job of it, infact if your porting code
  11. you don't have to make any changes, just include alarm.h,compile and link it in.
  12.  
  13. If you compile with 'cl -DTESTING alarm.c' it will compile the small test code
  14. in at the bottom, this allows you to test the code out.
  15.  
  16. Enjoy.
  17.  
  18. #] /bin/sh
  19. # This is a shell archive.  Remove anything before this line, then unpack
  20. # it by saving it into a file and typing "sh file".  To overwrite existing
  21. # files, type "sh file -c".  You can also feed this as standard input via
  22. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  23. # will see the following message at the end:
  24. #               "End of shell archive."
  25. # Contents:  alarm.c alarm.h
  26. # Wrapped by jack@cscdec on Wed Jul 18 00:41:16 1990
  27. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  28. if test -f alarm.c -a "${1}" ]= "-c" ; then
  29.   echo shar: Will not over-write existing file \"alarm.c\"
  30. else
  31. echo shar: Extracting \"alarm.c\" \(3865 characters\)
  32. sed "s/^X//" >alarm.c <<'END_OF_alarm.c'
  33. X/*
  34. X * This software is Copyright 1989 by Jack Hudler.
  35. X *
  36. X * Permission is hereby granted to copy, reproduce, redistribute or otherwise
  37. X * use this software as long as: there is no monetary profit gained
  38. X * specifically from the use or reproduction or this software, it is not
  39. X * sold, rented, traded or otherwise marketed, and this copyright notice is
  40. X * included prominently in any copy made.
  41. X *
  42. X * The author make no claims as to the fitness or correctness of this software
  43. X * for any use whatsoever, and it is provided as is. Any use of this software
  44. X * is at the user's own risk.
  45. X *
  46. X */
  47. X/****************************** Module Header ******************************\
  48. X* Module Name: alarm.c
  49. X* Created    : 11-08-89
  50. X* Author     : Jack Hudler  ╒jack@csccat.lonestar.org■
  51. X* Copyright  : 1988 Jack Hudler.
  52. X* Function   : Unix like alarm signal simulator.
  53. X\***************************************************************************/
  54. X/* Tested using OS2 1.2 with Microsoft C 5.1 and 6.0. */
  55. X#define INCL_DOSPROCESS
  56. X#define INCL_DOSSIGNALS
  57. X#define INCL_DOS
  58. X#include <os2.h>
  59. X#include <stdio.h>
  60. X#include <signal.h>
  61. X
  62. X#include "alarm.h"
  63. X
  64. X#define ALARM_STACK 4096    /* This maybe over kill, but the page size is 4K */
  65. Xstatic  PBYTE     pbAlarmStack;
  66. Xstatic  SEL       selAlarmStack;
  67. Xstatic  TID       tidAlarm;
  68. Xstatic  PID       pidMain;
  69. Xstatic  BOOL      bAlarmInit=FALSE;
  70. Xstatic  BOOL      bAlarmRunning=FALSE;
  71. Xstatic  ULONG     ulTime;
  72. XBOOL  x;
  73. X
  74. XVOID FAR alarm_thread ( VOID )
  75. X{
  76. X    while(1)
  77. X    {
  78. X      bAlarmInit = TRUE;
  79. X      if (bAlarmRunning)
  80. X      {
  81. X        DosSleep(1000L);
  82. X        ulTime--;
  83. X        if (ulTime==0L)
  84. X        {
  85. X          // send signal to the main process.. I could have put raise() here
  86. X          // however that would require the use of the multithreaded library,
  87. X          // and it does not contain raise()]
  88. X          // I tried it with the standard library, this signaled ok, but a
  89. X          // test printf in the signal would not work and even caused SEGV.
  90. X          // So I signal the process through OS/2 and then the process
  91. X          // signals itself.
  92. X          if (bAlarmRunning)
  93. X            DosFlagProcess(pidMain,FLGP_PID, PFLG_A,1);
  94. X          bAlarmRunning=FALSE;
  95. X        }
  96. X      }
  97. X      else
  98. X        DosSleep(100L);
  99. X    }
  100. X}
  101. X
  102. XVOID PASCAL FAR AlarmSignal(USHORT usSigArg,USHORT usSigNum)
  103. X{
  104. X    /*
  105. X     * this is not executed from the thread. The thread triggers Process
  106. X     * flag A which is in the main processes scope, this inturn triggers
  107. X     * (via the raise) SIGUSR1 which is defined to SIGALRM.
  108. X     */
  109. X    raise(SIGUSR1);
  110. X}
  111. X
  112. Xvoid alarm_init()
  113. X{
  114. X    PFNSIGHANDLER pfnPrev;
  115. X    USHORT       pfAction;
  116. X    PIDINFO      pid;
  117. X    if (]DosAllocSeg( ALARM_STACK, (PSEL) &selAlarmStack, SEG_NONSHARED ))
  118. X    {
  119. X      OFFSETOF(pbAlarmStack) = ALARM_STACK - 2;
  120. X      SELECTOROF(pbAlarmStack) = selAlarmStack;
  121. X      /* Create the thread */
  122. X      if (DosCreateThread( alarm_thread, &tidAlarm, pbAlarmStack ))
  123. X      {
  124. X        fprintf(stderr,"Alarm thread failed to start.\n");
  125. X        exit(1);
  126. X      }
  127. X      /* Setup the signal handler for Process Flag A */
  128. X      if (DosSetSigHandler(AlarmSignal,&pfnPrev,&pfAction,SIGA_ACCEPT,SIG_PFLG_
  129. X      {
  130. X        fprintf(stderr,"SigHandler Failed to install.\n");
  131. X        exit(1);
  132. X      }
  133. X      /* Save main process ID, we'll need it for triggering the signal */
  134. X      DosGetPID(&pid);
  135. X      pidMain = pid.pid;
  136. X    }
  137. X    else
  138. X      exit(1);
  139. X}
  140. X
  141. X
  142. X
  143. XULONG alarm(ULONG sec)
  144. X{
  145. X    if (]bAlarmInit) alarm_init();
  146. X    if (sec)
  147. X    {
  148. X      ulTime = sec;
  149. X      bAlarmRunning = TRUE;
  150. X    }
  151. X    else
  152. X      bAlarmRunning = FALSE;
  153. X
  154. X}
  155. X
  156. X#ifdef TESTING
  157. X/* A simple test to see if it works */
  158. X`
  159. Xtimeout()
  160. X{
  161. X    fprintf(stderr,"ALARM TRIGGERED]]\n");
  162. X    DosBeep(1000,500);
  163. X    x++;
  164. X}
  165. X
  166. Xmain()
  167. X{
  168. X    (void) signal(SIGALRM, timeout);
  169. X    (void) alarm(1L);
  170. X    printf("ALARM RUNNING]]\n");
  171. X    while(]x);
  172. X}
  173. X#endif
  174. END_OF_alarm.c
  175. if test 3865 -ne `wc -c <alarm.c`; then
  176.     echo shar: \"alarm.c\" unpacked with wrong size]
  177. fi
  178. # end of overwriting check
  179. fi
  180. if test -f alarm.h -a "${1}" ]= "-c" ; then
  181.   echo shar: Will not over-write existing file \"alarm.h\"
  182. else
  183. echo shar: Extracting \"alarm.h\" \(44 characters\)
  184. sed "s/^X//" >alarm.h <<'END_OF_alarm.h'
  185. X#define SIGALRM SIGUSR1
  186. XULONG alarm(ULONG);
  187. END_OF_alarm.h
  188. if test 44 -ne `wc -c <alarm.h`; then
  189.     echo shar: \"alarm.h\" unpacked with wrong size]
  190. fi
  191. # end of overwriting check
  192. fi
  193. echo shar: End of shell archive.
  194. exit 0
  195. --
  196. Jack           Computer Support Corporation             Dallas,Texas
  197. Hudler         Internet: jack@cscdec.lonestar.org
  198.