Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!know!zaphod.mps.ohio-state.edu!usc!elroy.jpl.nasa.gov!lll-winken!sun-barr!newstop!texsun!csccat!cscdec!jack From: jack@cscdec.lonestar.org (Jack Hudler) Newsgroups: comp.os.os2.programmer Subject: Unix like alarm(2) simulator. Message-ID: <56@cscdec.lonestar.org> Date: 16 Aug 90 22:08:30 GMT Reply-To: jack@cscdec.lonestar.org (Jack Hudler) Organization: Computer Support Corporation. Dallas,Texas Lines: 175 I threw the together one day while porting some software from Unix to OS/2. Enjoy! Jack #! /bin/sh # This is a shell archive. Remove anything before this line, then unpack # it by saving it into a file and typing "sh file". To overwrite existing # files, type "sh file -c". You can also feed this as standard input via # unshar, or by typing "sh alarm.c <<'END_OF_alarm.c' X/* X * Alarm(2) simualtor for OS/2. X * X * This software is Copyright 1990 by Jack Hudler X * X * Permission is hereby granted to copy, reproduce, redistribute or X * otherwise use this software as long as: there is no monetary X * profit gained specifically from the use or reproduction or this X * software, it is not sold, rented, traded or otherwise marketed, and X * this copyright notice is included prominently in any copy X * made. X * X * The author make no claims as to the fitness or correctness of X * this software for any use whatsoever, and it is provided as is. X * Any use of this software is at the user's own risk. X * X */ X#define INCL_DOSPROCESS X#define INCL_DOSSIGNALS X#define INCL_NOPM X#include X X#define ALARM_STACK 4096 /* One page */ Xstatic PBYTE pbAlarmStack; Xstatic SEL selAlarmStack; Xstatic TID tidAlarm; Xstatic PID pidMain; Xstatic BOOL fAlarmInit=FALSE; Xstatic BOOL fAlarmRunning=FALSE; Xstatic ULONG ulTime; X#ifdef TESTING Xstatic BOOL fTriggered=FALSE; X#endif XVOID FAR alarm_thread ( VOID ) X{ X for(;;) X { X fAlarmInit = TRUE; X if (fAlarmRunning) X { X DosSleep(1000L); X ulTime--; X if (ulTime==0L) X { X // send signal to the main process.. I could have put raise() here X // however that would require the use of the multithreaded library, X // and it does not support raise()! X // I tried it with the standard library, this signaled ok, but a X // test printf in the signal would not work and even caused SEGV. X // So I signal the process through OS/2 and then the process X // signals itself. X if (fAlarmRunning) X DosFlagProcess(pidMain,FLGP_PID, PFLG_A,1); X fAlarmRunning=FALSE; X } X } X else X DosSleep(100L); /* sleep for only a 10th if doing nothing */ X } X} X XVOID PASCAL FAR AlarmSignal(USHORT usSigArg,USHORT usSigNum) X{ X raise(SIGUSR1); X} X Xvoid alarm_init() X{ X PFNSIGHANDLER pfnPrev; X USHORT pfAction; X PIDINFO pid; X X if (!DosAllocSeg( ALARM_STACK, (PSEL) &selAlarmStack, SEG_NONSHARED )) X { X OFFSETOF(pbAlarmStack) = ALARM_STACK - 2; X SELECTOROF(pbAlarmStack) = selAlarmStack; X if (DosCreateThread( alarm_thread, &tidAlarm, pbAlarmStack )) X { X fprintf(stderr,"Alarm thread failed to start.\n"); X exit(1); X } X if (DosSetSigHandler(AlarmSignal,&pfnPrev,&pfAction, X SIGA_ACCEPT,SIG_PFLG_A)) X { X fprintf(stderr,"SigHandler Failed to install.\n"); X exit(1); X } X DosGetPID(&pid); X pidMain = pid.pid; X fAlarmInit = TRUE; X } X else X exit(1); X} X X X XULONG alarm(ULONG sec) X{ X if (!fAlarmInit) alarm_init(); X if (sec) X { X ulTime = sec; X fAlarmRunning = TRUE; X } X else X fAlarmRunning = FALSE; X X} X X#ifdef TESTING Xtimeout() X{ X fprintf(stderr,"ALARM TRIGGERED!!\n"); X DosBeep(1000,500); X fTriggered++; X} X Xmain() X{ X (void) signal(SIGALRM, timeout); X (void) alarm(1L); X printf("ALARM RUNNING!!\n"); X while(!fTriggered); X} X#endif END_OF_alarm.c if test 3127 -ne `wc -c alarm.h <<'END_OF_alarm.h' X#ifndef SIGUSR1 X#include X#endif X X#define SIGALRM SIGUSR1 X XULONG alarm(ULONG); END_OF_alarm.h if test 89 -ne `wc -c