Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!uakari.primate.wisc.edu!aplcen!haven!decuac!shlump.nac.dec.com!michaud From: michaud@decvax.dec.com (Jeff Michaud) Newsgroups: comp.unix.ultrix Subject: Re: looking for usleep() implementation for ULTRIX Message-ID: <5508@shlump.nac.dec.com> Date: 18 Oct 89 19:33:16 GMT References: <5499@shlump.nac.dec.com> Sender: news@shlump.nac.dec.com Organization: DEC Lines: 47 > Here is a macro for usleep() which appeared in comp.sources.x in the > program xchomp. > > #ifdef ULTRIX > #include > 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@decwrl.dec.com michaud@decvax.dec.com| |DECnet-ULTRIX #include | \--------------------------------------------------------------/