looking for usleep() implementation for ULTRIX

Jeff Michaud michaud at decvax.dec.com
Thu Oct 19 05:33:16 AEST 1989


>     Here is a macro for usleep() which appeared in comp.sources.x in the
>     program xchomp.
> 
>     #ifdef ULTRIX
>     #include <sys/time.h>
>     EXTERN struct timeval   st_delay;
>     #define usleep(x)       { st_delay.tv_usec = (x); st_delay.tv_sec = 0; \
>                               select(32, NULL, NULL, NULL, &st_delay); }
>     #endif

	Note however that select will return a invalid argument error if the of
	u-seconds you give is equiv to a second or more.  So something along

		#ifdef ultrix
		#define usleep(x)	\
			{ struct timeval delay;				\
				delay.tv_sec  = (int)(x) / 1000000;	\
				delay.tv_usec = (int)(x) % 1000000;	\
				select(0, (int *)0, (int *)0, (int *)0, &delay); }
		#endif

	Note also that I use 0 as the first argument to select().  Also note that
	I used a lowercase "ultrix" for the #ifdef as "ultrix" is predefined by
	ULTRIX's cpp but "ULTRIX" isn't.

	To make this really generic it should be defined as a function.  As a
	macro things like:

		if( ... )
			usleep(...);
		else
			....

	because the "usleep(...);" is two statements, ie. a compound statement and
	the null statement.  This can be worked around if you use an explicit
	compound statement around the "usleep(...);" while keeping your code
	portable.

	The question I have is, is the real usleep() function defined to be
	interruptable?  select() is (but that too can be fixed by explicitly
	blocking signals or by restarting the select() (taking into account
	time already slept).

/--------------------------------------------------------------\
|Jeff Michaud    michaud at decwrl.dec.com  michaud at decvax.dec.com|
|DECnet-ULTRIX   #include <standard/disclaimer.h>              |
\--------------------------------------------------------------/



More information about the Comp.unix.ultrix mailing list