NAME
clocknanosleep - high-resolution sleep with specifiable clock SYNOPSIS
#include
int clocknanosleep(clockidt clockid, int flags, const struct timespec *request, struct timespec *remain); Link with -lrt (only for glibc versions before 2.17). Feature Test Macro Requirements for glibc (see featuretestmacros(7)): clocknanosleep(): XOPENSOURCE >= 600 || POSIXCSOURCE >= 200112L DESCRIPTION Like nanosleep(2), clocknanosleep() allows the calling thread to sleep for an interval specified with nanosecond precision. It differs in allowing the caller to select the clock against which the sleep inter‐ val is to be measured, and in allowing the sleep interval to be speci‐ fied as either an absolute or a relative value. The time values passed to and returned by this call are specified using timespec structures, defined as follows: struct timespec { timet tvsec; /* seconds */ long tvnsec; /* nanoseconds [0 .. 999999999] */ }; The clockid argument specifies the clock against which the sleep interval is to be measured. This argument can have one of the follow‐ ing values:
CLOCKREALTIME A settable system-wide real-time clock. CLOCKMONOTONIC A nonsettable, monotonically increasing clock that measures time since some unspecified point in the past that does not change after system startup. CLOCKPROCESSCPUTIMEID
A settable per-process clock that measures CPU time consumed by all threads in the process. See clockgetres(2) for further details on these clocks. If flags is 0, then the value specified in request is interpreted as an interval relative to the current value of the clock specified by clockid. If flags is TIMERABSTIME, then request is interpreted as an absolute time as measured by the clock, clockid. If request is less than or equal to the current value of the clock, then clocknanosleep() returns immediately without suspending the calling thread. clocknanosleep() suspends the execution of the calling thread until either at least the time specified by request has elapsed, or a signal is delivered that causes a signal handler to be called or that termi‐ nates the process. If the call is interrupted by a signal handler, clocknanosleep() fails with the error EINTR. In addition, if remain is not NULL, and flags was not TIMERABSTIME, it returns the remaining unslept time in remain. This value can then be used to call clocknanosleep() again and com‐ plete a (relative) sleep. RETURN VALUE On successfully sleeping for the requested interval, clocknanosleep() returns 0. If the call is interrupted by a signal handler or encoun‐ ters an error, then it returns one of the positive error number listed in ERRORS. ERRORS EFAULT request or remain specified an invalid address. EINTR The sleep was interrupted by a signal handler. EINVAL The value in the tvnsec field was not in the range 0 to 999999999 or tvsec was negative. EINVAL clockid was invalid. (CLOCKTHREADCPUTIMEID is not a permit‐ ted value for clockid.) VERSIONS The clocknanosleep() system call first appeared in Linux 2.6. Support is available in glibc since version 2.1. CONFORMING TO
POSIX.1-2001. NOTES If the interval specified in request is not an exact multiple of the granularity underlying clock (see time(7)), then the interval will be rounded up to the next multiple. Furthermore, after the sleep com‐ pletes, there may still be a delay before the CPU becomes free to once again execute the calling thread. Using an absolute timer is useful for preventing timer drift problems of the type described in nanosleep(2). (Such problems are exacerbated in programs that try to restart a relative sleep that is repeatedly interrupted by signals.) To perform a relative sleep that avoids these problems, call clockgettime(2) for the desired clock, add the desired interval to the returned time value, and then call clocknanosleep() with the TIMERABSTIME flag. clocknanosleep() is never restarted after being interrupted by a sig‐ nal handler, regardless of the use of the sigaction(2) SARESTART flag. The remain argument is unused, and unnecessary, when flags is TIMERABSTIME. (An absolute sleep can be restarted using the same request argument.) POSIX.1 specifies that clocknanosleep() has no effect on signals dis‐ positions or the signal mask. POSIX.1 specifies that after changing the value of the CLOCKREALTIME clock via clocksettime(2), the new clock value shall be used to deter‐ mine the time at which a thread blocked on an absolute clocknanosleep() will wake up; if the new clock value falls past the end of the sleep interval, then the clocknanosleep() call will return immediately. POSIX.1 specifies that changing the value of the CLOCKREALTIME clock via clocksettime(2) shall have no effect on a thread that is blocked on a relative clocknanosleep(). SEE ALSO clockgetres(2), nanosleep(2), restartsyscall(2), timercreate(2), sleep(3), usleep(3), time(7) COLOPHON
This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2013-07-30 CLOCKNANOSLEEP(2)