nanosleep() replacement Take 2

Darren Tucker dtucker at zip.com.au
Tue Mar 18 12:36:42 EST 2003


Darren Tucker wrote:
> Difference in select() behaviour between patforms?
[snip]

Nothing so exotic, just some bugs (sigh).

The version I tested had "if (rc < 0) errno=EINTR" and the arithmetic
for the remainder was just plain wrong.  This returned a negative
remainder which caused select to fail next time round with invalid
paramters, which the function converted into errno=EINTR.  Amazingly,
this worked!

The following works better for me.

		-Daz.

int nanosleep(const struct timespec *req, struct timespec *rem)
{
       int rc, saverrno;
       extern int errno;
       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);

       if (rc == -1) {
               tremain.tv_sec = time2wait.tv_sec - (tstop.tv_sec -
tstart.tv_sec);
               tremain.tv_usec = time2wait.tv_usec - (tstop.tv_usec -
tstart.tv_usec);
               tremain.tv_sec += tremain.tv_usec / 1000000L;
               tremain.tv_usec %= 1000000L;
       } else {
                tremain.tv_sec = 0;
                tremain.tv_usec = 0;
       }
       TIMEVAL_TO_TIMESPEC(&tremain, rem)

      if (rc == -1)
              errno = saverrno;
      return(rc);
}

-- 
Darren Tucker (dtucker at zip.com.au)
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