More precise variant of the UN*X system call
nanosleep() with a pointer to a
struct timespec containing time to sleep (allowing other processes to run), and another pointer (possibly NULL) into which will be written the time remaining to sleep.
struct timespec lets you specify the time with nanosecond precision, hence the system call's name.
Recall the difference between accuracy and precision. No current architecture or operating system is capable of giving you nanosecond accuracy (even if doing nothing else), especially not in the presence of interrupts.
nanosleep() will sleep at least for as long as specified (wallclock time), but possibly longer. If your application requires sleeps anywhere near this accurate, you'll have to use at least nonstandard APIs to the operating system, and most likely something other than stock UN*X.
If a signal is delivered to the process,
nanosleep() will return -1, with
errno. In that case, the time remaining to sleep will be returned. Note that this causes much precision to be lost: the process has no way of determining how much of the time left was used before it continued to run.
nanosleep() is probably to be preferred to
usleep(): it offers a higher precision, and may be in POSIX.1b (glibc2 claims it is, but I have found nothing to back this claim on Compaq UNIX or Solaris).