nanosleep() replacement Take 2

Darren Tucker dtucker at
Tue Mar 18 10:45:21 EST 2003

Gert Doering wrote:
> Tested on SCO OSR 3.0.  openssh-current (as of right now) with your
> appended patch.  Configures, compiles, and "scp -l <number>" seems to
> do things that correlate with the specified number (I assume it's
> kbit/second and the progress bar is Kbyte/second).

Yes that's the case.

> -l is fairly unprecise, but this is likely due to a fairly slow machine,
> select()/scheduler granularity, and other things eating CPU.

I saw that too with Tim's function but I was able to get pretty good
results (5% variance or less from specified bandwidth) on a somewhat
loaded 170 MHz SPARC.

I suspect there's some cases it doesn't handle correctly (I'm trying to
find an example).

Would you mind trying the following function for comparison?


int nanosleep(const struct timespec *req, struct timespec *rem)
       int rc;
       extern int errno;
       int saverrno;
       struct timeval tstart, tstop, tremain, time2wait;

       TIMESPEC_TO_TIMEVAL(&time2wait, req)

       gettimeofday(&tstart, NULL);
       rc = select(0, NULL, NULL, NULL, &time2wait);
       saverrno = errno;
       gettimeofday (&tstop, NULL);

       tremain.tv_sec = tstop.tv_sec - tstart.tv_sec - time2wait.tv_sec;
       tremain.tv_usec = tstop.tv_usec - tstart.tv_usec -
       tremain.tv_sec += tremain.tv_usec / 1000000L;
       tremain.tv_usec %= 1000000L;
       TIMEVAL_TO_TIMESPEC(&tremain, rem)

       if (rc < 0) {
               rc = -1;
               errno = saverrno;

Darren Tucker (dtucker at
GPG Fingerprint D9A3 86E9 7EEE AF4B B2D4  37C9 C982 80C7 8FF4 FA69
    Good judgement comes with experience. Unfortunately, the experience
usually comes from bad judgement.

More information about the openssh-unix-dev mailing list