Need help on finding sources for ualarm

Adrian J Ho adrianho at barkley.berkeley.edu
Thu Mar 28 12:27:24 AEST 1991


In article <1806 at umvlsi.ecs.umass.edu> srivasta at nazgul.ecs.umass.edu (Manoj Srivastava) writes:

>  I was   compiling a "third party software"  when I came across a
> reference to ualarm (int  arg),  and apparently,  from the context, it
> generates  a   signal (unix   SIGALARM)    after   arg   microseconds.
> unfortunately, my machine  has no such  beast, the closest it comes to
> is  alarm (unsigned arg),  which  takes an  arg    in seconds.  Is  it
> possible to get the sources of  any  such implementation, or  would it
> be too hardware dependent?  I am running ULTRIX v.4.1  on a DECstation
> 5000/200  (MIPS  Architecture).  Any     advice/ pointers   shall   be
> appreciated.

Well, I'm running Ultrix 3.1 on a 3100, but that shouldn't make a
difference in what I'm about to say.

The SunOS 4.1 man page for ualarm(3) sez:

----------
UALARM(3)              C LIBRARY FUNCTIONS              UALARM(3)

NAME
     ualarm - schedule signal after interval in microseconds

SYNOPSIS
     unsigned ualarm(value, interval)
     unsigned value;
     unsigned interval;

DESCRIPTION
     This is a simplified interface to setitimer()  (see  getiti-
     mer(2)).

     ualarm() sends signal SIGALRM, see signal(3V), to the invok-
     ing  process  in a number of microseconds given by the value
     argument.  Unless caught or ignored, the  signal  terminates
     the process.

     <etc.>
---------

As you can see, ualarm() is just a wrapper for setitimer(2).  After
reading the latter man page, I came up with this (off the top of my
head):

==========
#include <sys/time.h>

unsigned ualarm(value, interval)
unsigend value;
unsigned interval;
{
    struct itimerval itv, oitv;

    /* Set values in itv */
    itv.it_interval.tv_sec = interval / 1000000;
    itv.it_interval.tv_usec = interval % 1000000;
    itv.it_value.tv_sec = value / 1000000;
    itv.it_value.tv_usec = value % 1000000;

    /* Call setitimer() */
    setitimer(ITIMER_REAL,&itv,&oitv)

    /* and return old value */
    return (oitv.it_value.tv_sec * 1000000 + oitv.it_value.tv_usec);
}
==========

Two things to note:

1) This code is untested.  I can't think of a reason why it wouldn't
work, but.....

2) You won't get microsecond resolution -- don't be fooled by the
tv_usec field.  From the Ultrix 3.1 getitimer(3) man page:

----------
     Time values smaller than the resolution of the system clock
     are rounded up to this resolution (on MIPS, 3.906 mil-
     liseconds; on VAX, 10 milliseconds).
----------

Other than that, good luck!

-----------------------------------------------------------------------------
Adrian Ho, EECS (pronounced "eeks!") Dept.		Phone: (415) 642-5563
UC Berkeley					adrianho at barkley.berkeley.edu
Domain: sesame-street (telly,bigbird,snuffy,oscar,kermit,bert,grover,barkley)



More information about the Comp.unix.questions mailing list