2 * This software is Copyright 1989 by Jack Hudler.
4 * Permission is hereby granted to copy, reproduce, redistribute or otherwise
5 * use this software as long as: there is no monetary profit gained
6 * specifically from the use or reproduction or this software, it is not
7 * sold, rented, traded or otherwise marketed, and this copyright notice is
8 * included prominently in any copy made.
10 * The author make no claims as to the fitness or correctness of this software
11 * for any use whatsoever, and it is provided as is. Any use of this software
12 * is at the user's own risk.
16 /****************************** Module Header ******************************\
17 * Module Name: alarm.c
19 * Author : Jack Hudler [jack@csccat.lonestar.org]
20 * Copyright : 1988 Jack Hudler.
21 * Function : Unix like alarm signal simulator.
22 \***************************************************************************/
24 /* Tested using OS2 1.2 with Microsoft C 5.1 and 6.0. */
26 #define INCL_DOSPROCESS
27 #define INCL_DOSSIGNALS
37 #define ALARM_STACK 4096 /* This maybe over kill, but the page size is 4K */
39 static PBYTE pbAlarmStack;
40 static SEL selAlarmStack;
43 static BOOL bAlarmInit=FALSE;
44 static BOOL bAlarmRunning=FALSE;
47 static VOID FAR alarm_thread ( VOID )
57 // send signal to the main process.. I could have put raise() here
58 // however that would require the use of the multithreaded library,
59 // and it does not contain raise()!
60 // I tried it with the standard library, this signaled ok, but a
61 // test printf in the signal would not work and even caused SEGV.
62 // So I signal the process through OS/2 and then the process
65 DosFlagProcess(pidMain,FLGP_PID, PFLG_A,1);
74 static VOID PASCAL FAR AlarmSignal(USHORT usSigArg,USHORT usSigNum)
77 * this is not executed from the thread. The thread triggers Process
78 * flag A which is in the main processes scope, this inturn triggers
79 * (via the raise) SIGUSR1 which is defined to SIGALRM.
84 static void alarm_init(void)
86 PFNSIGHANDLER pfnPrev;
92 if (!DosAllocSeg( ALARM_STACK, (PSEL) &selAlarmStack, SEG_NONSHARED ))
94 OFFSETOF(pbAlarmStack) = ALARM_STACK - 2;
95 SELECTOROF(pbAlarmStack) = selAlarmStack;
96 /* Create the thread */
97 if (DosCreateThread( alarm_thread, &tidAlarm, pbAlarmStack ))
99 fprintf(stderr,"Alarm thread failed to start.\n");
102 /* Setup the signal handler for Process Flag A */
103 if (DosSetSigHandler(AlarmSignal,&pfnPrev,&pfAction,SIGA_ACCEPT,SIG_PFLG_A))
105 fprintf(stderr,"SigHandler Failed to install.\n");
108 /* Save main process ID, we'll need it for triggering the signal */
116 unsigned alarm(unsigned sec)
118 if (!bAlarmInit) alarm_init();
123 bAlarmRunning = TRUE;
126 bAlarmRunning = FALSE;
132 /* A simple test to see if it works */
137 fprintf(stderr,"ALARM TRIGGERED!!\n");
144 (void) signal(SIGALRM, timeout);
146 printf("ALARM RUNNING!!\n");