I have a program (mixed C and Fortran, although that doesn't seem to be relevant) that uses nanosleep
. However, if my timespec
has a tv_sec
value of 0, it simply doesn't sleep. The tv_nsec
value can be microseconds shy of a full second, but it does not sleep. (If tv_sec
is 1
, it has no problem sleeping for a second.) Why would this be?
To make things more confusing, usleep
with an appropriate value (i.e. 995000
usec) sleeps for just about a second as expected.
I'm seeing this problem with a RHEL 5.8 and a RHEL 6.4 box. Both are using gcc
.
Here's the function that calls nanosleep:
void msleep(int *milliseconds)
{
long usec;
struct timespec sleep;
usec = (*milliseconds) % 1000;
sleep.tv_sec = (*milliseconds) / 1000;
sleep.tv_nsec = 1000*usec;
nanosleep(&sleep, NULL);
}
Obviously, I don't actually need nanosecond precision!
I've also tested a version in which I did check the return value; it was always 0
(success), and thus the rem
output parameter (remaining time if interrupted) never got set.