From: kodama@etl.go.jp (Yuetsu Kodama)
Subject: Re: BSD sleep function (In Japanese/Kanji)
Organization: Electrotechnical Lab.
Distribution: fj
Date: Wed, 16 Dec 1992 13:51:26 GMT

In article <224@sayuri.nssys.co.jp> shiba@sayuri.nssys.co.jp (Hiroshi  Shibayama) writes:
>
>  $B<F;3!wF|K\Am9g%7%9%F%`$G$9!#(B
>
>  $B#H#P!]#7#2#0$G#s#l#e#e#p4X?t$r;H$&$HICC10L$N%?%$%^!<$K$J$C$F(B
>$B$7$^$$$^$9$,!"#B#S#D$NMM$J%^%$%/%mICC10L(B($B$@$C$?$+$J!)(B)$B$N4X?t$r(B
>$BC5$7$F$$$^$9(B($B$G$-$l$P$=$N%=!<%9$b(B)$B!#(B

$B$$$o$f$k(B usleep $B$G$9$M!#(Bcomp.sys.hp $B$KDj4|E*$K(B BSD $B$+$i(B HP-UX $B$X$N%]!<(B
$B%F%#%s%0$N%F%/%K%C%/$,N.$l$F$$$^$9$,!"$=$NCf$K$"$j$^$9!#C;$$$G$9$N$G8e(B
$B$m$KE:IU$7$F$*$-$^$9!#B>$K$b$$$m$$$mM-MQ$J$3$H$,=q$+$l$F$$$^$9$N$G0lEY(B
$B855-;v(B Message-ID: <1992Aug10.190711.19417@alchemy.chem.utoronto.ca>
$B$b0lFI$5$l$k$3$H$r$*4+$aCW$7$^$9!#(B

		$B;y6L!wEEAm8&(B

usleep:
-------

/*
 *  NAME:
 *      usleep     -- This is the precision timer for Test Set
 *                    Automation. It uses the select(2) system
 *                    call to delay for the desired number of
 *                    micro-seconds. This call returns ZERO
 *                    (which is usually ignored) on successful
 *                    completion, -1 otherwise. 
 *
 *  ALGORITHM:
 *      1) We range check the passed in microseconds and log a
 *         warning message if appropriate. We then return without
 *         delay, flagging an error. 
 *      2) Load the Seconds and micro-seconds portion of the
 *         interval timer structure.
 *      3) Call select(2) with no file descriptors set, just the
 *         timer, this results in either delaying the proper
 *         ammount of time or being interupted early by a signal.
 *
 *  HISTORY:
 *      Added when the need for a subsecond timer was evident.
 *
 *  AUTHOR:
 *      Michael J. Dyer                   Telephone:   AT&T 414.647.4044
 *      General Electric Medical Systems        GE DialComm  8 *767.4044
 *      P.O. Box 414  Mail Stop 12-27         Sect'y   AT&T 414.647.4584
 *      Milwaukee, Wisconsin  USA 53201                      8 *767.4584
 *      internet:  mike@sherlock.med.ge.com     GEMS WIZARD e-mail: DYER
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <sys/param.h>
#include <sys/types.h>

int     usleep( unsigned long int microSeconds )
{
        unsigned int            Seconds, uSec;
        int                     nfds, readfds, writefds, exceptfds;
        struct  timeval         Timer;

        nfds = readfds = writefds = exceptfds = 0;

        if( (microSeconds == (unsigned long) 0) 
                || microSeconds > (unsigned long) 4000000 )
        {
                errno = ERANGE;         /* value out of range */
                perror( "usleep time out of range ( 0 -> 4000000 ) " );
                return -1;
        }

        Seconds = microSeconds / (unsigned long) 1000000;
        uSec    = microSeconds % (unsigned long) 1000000;

        Timer.tv_sec            = Seconds;
        Timer.tv_usec           = uSec;

        if( select( nfds, &readfds, &writefds, &exceptfds, &Timer ) < 0 )
        {
                perror( "usleep (select) failed" );
                return -1;
        }

        return 0;
}



Xref: sparky fj.sys.hp:288 fj.sources.d:495