[comp.os.os2.programmer] Unix like alarm

jack@cscdec.lonestar.org (Jack Hudler) (08/17/90)

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 <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  alarm.c alarm.h
# Wrapped by jack@cscdec on Thu Aug 16 17:02:26 1990
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f alarm.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"alarm.c\"
else
echo shar: Extracting \"alarm.c\" \(3127 characters\)
sed "s/^X//" >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 <os2.h>
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.c`; then
    echo shar: \"alarm.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f alarm.h -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"alarm.h\"
else
echo shar: Extracting \"alarm.h\" \(89 characters\)
sed "s/^X//" >alarm.h <<'END_OF_alarm.h'
X#ifndef SIGUSR1
X#include <signal.h>
X#endif
X
X#define SIGALRM	SIGUSR1
X
XULONG alarm(ULONG);
END_OF_alarm.h
if test 89 -ne `wc -c <alarm.h`; then
    echo shar: \"alarm.h\" unpacked with wrong size!
fi
# end of overwriting check
fi
echo shar: End of shell archive.
exit 0
-- 
Jack           Computer Support Corporation             Dallas,Texas
Hudler         Internet: jack@cscdec.lonestar.org