usleep by poll()
Andrew H. Marrinson
andy at xwkg.Icom.Com
Wed Apr 3 07:15:18 AEST 1991
libes at cme.nist.gov (Don Libes) writes:
I recommend one slight fix to this usleep routine. Some versions of
5.3 have a bug where the first argument to poll is checked for a valid
memory address even if the second argument is zero. Thus, the version
you posted will fail with an EFAULT on systems with the bug. Changing
it as follows will improve this:
>/*
>subseconds sleeps for System V - or anything that has poll()
>Don Libes, 4/1/1991
>[...]
>Returns 0 if successful timeout, -1 if unsuccessful.
>*/
>#include <poll.h>
>int
>usleep(usec)
>unsigned int usec; /* microseconds */
>{
> static subtotal = 0; /* microseconds */
> int msec; /* milliseconds */
struct pollfd foo; /* ADD!!! */
> subtotal += usec;
> /* if less then 1 msec request, do nothing but remember it */
> if (subtotal < 1000) return(0);
> msec = subtotal/1000;
> subtotal = subtotal%1000;
> return poll(&foo,(unsigned long)0,msec); /* CHANGE!!! */
>}
Hope this helps those who couldn't get the above to work on System
V/386 and perhaps others...
--
Andrew H. Marrinson
Icom Systems, Inc.
Wheeling, IL, USA
(andy at icom.icom.com)
More information about the Comp.unix.wizards
mailing list