| 1 |
.\" Copyright, the authors of the Linux man-pages project |
| 2 |
.\" |
| 3 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 4 |
.\" |
| 5 |
.TH clock_nanosleep 2 2025-10-29 "Linux man-pages 6.18" |
| 6 |
.SH NAME |
| 7 |
clock_nanosleep \- high-resolution sleep with specifiable clock |
| 8 |
.SH LIBRARY |
| 9 |
Standard C library |
| 10 |
.RI ( libc ,\~ \-lc ), |
| 11 |
since glibc 2.17 |
| 12 |
.P |
| 13 |
Before glibc 2.17, |
| 14 |
Real-time library |
| 15 |
.RI ( librt ,\~ \-lrt ) |
| 16 |
.SH SYNOPSIS |
| 17 |
.B #include <time.h> |
| 18 |
.nf |
| 19 |
.P |
| 20 |
.BI "int clock_nanosleep(clockid_t " clockid ", int " flags , |
| 21 |
.BI " const struct timespec *" t , |
| 22 |
.BI " struct timespec *_Nullable " remain ); |
| 23 |
.fi |
| 24 |
.P |
| 25 |
.RS -4 |
| 26 |
Feature Test Macro Requirements for glibc (see |
| 27 |
.BR feature_test_macros (7)): |
| 28 |
.RE |
| 29 |
.P |
| 30 |
.BR clock_nanosleep (): |
| 31 |
.nf |
| 32 |
_POSIX_C_SOURCE >= 200112L |
| 33 |
.fi |
| 34 |
.SH DESCRIPTION |
| 35 |
Like |
| 36 |
.BR nanosleep (2), |
| 37 |
.BR clock_nanosleep () |
| 38 |
allows the calling thread to sleep for an interval specified |
| 39 |
with nanosecond precision. |
| 40 |
It differs in allowing the caller to select the clock against |
| 41 |
which the sleep interval is to be measured, |
| 42 |
and in allowing the sleep interval to be specified as |
| 43 |
either an absolute or a relative value. |
| 44 |
.P |
| 45 |
The time values passed to and returned by this call are specified using |
| 46 |
.BR timespec (3) |
| 47 |
structures. |
| 48 |
.P |
| 49 |
The |
| 50 |
.I clockid |
| 51 |
argument specifies the clock against which the sleep interval |
| 52 |
is to be measured. |
| 53 |
This argument can have one of the following values: |
| 54 |
.\" Look in time/posix-timers.c (kernel 5.6 sources) for the |
| 55 |
.\" 'struct k_clock' structures that have an 'nsleep' method |
| 56 |
.TP |
| 57 |
.B CLOCK_REALTIME |
| 58 |
A settable system-wide real-time clock. |
| 59 |
.TP |
| 60 |
.BR CLOCK_TAI " (since Linux 3.10)" |
| 61 |
A system-wide clock derived from wall-clock time but counting leap seconds. |
| 62 |
.TP |
| 63 |
.B CLOCK_MONOTONIC |
| 64 |
A nonsettable, monotonically increasing clock that measures time |
| 65 |
since some unspecified point in the past that does not change after |
| 66 |
system startup. |
| 67 |
.\" On Linux this clock measures time since boot. |
| 68 |
.TP |
| 69 |
.BR CLOCK_BOOTTIME " (since Linux 2.6.39)" |
| 70 |
Identical to |
| 71 |
.BR CLOCK_MONOTONIC , |
| 72 |
except that it also includes any time that the system is suspended. |
| 73 |
.TP |
| 74 |
.B CLOCK_PROCESS_CPUTIME_ID |
| 75 |
A settable per-process clock that measures CPU time consumed |
| 76 |
by all threads in the process. |
| 77 |
.\" There is some trickery between glibc and the kernel |
| 78 |
.\" to deal with the CLOCK_PROCESS_CPUTIME_ID case. |
| 79 |
.P |
| 80 |
See |
| 81 |
.BR clock_getres (2) |
| 82 |
for further details on these clocks. |
| 83 |
In addition, the CPU clock IDs returned by |
| 84 |
.BR clock_getcpuclockid (3) |
| 85 |
and |
| 86 |
.BR pthread_getcpuclockid (3) |
| 87 |
can also be passed in |
| 88 |
.IR clockid . |
| 89 |
.\" Sleeping against CLOCK_REALTIME_ALARM and CLOCK_BOOTTIME_ALARM |
| 90 |
.\" is also possible (tested), with CAP_WAKE_ALARM, but I'm not |
| 91 |
.\" sure if this is useful or needs to be documented. |
| 92 |
.P |
| 93 |
If |
| 94 |
.I flags |
| 95 |
is 0, then the value specified in |
| 96 |
.I t |
| 97 |
is interpreted as an interval relative to the current |
| 98 |
value of the clock specified by |
| 99 |
.IR clockid . |
| 100 |
.P |
| 101 |
If |
| 102 |
.I flags |
| 103 |
is |
| 104 |
.BR TIMER_ABSTIME , |
| 105 |
then |
| 106 |
.I t |
| 107 |
is interpreted as an absolute time as measured by the clock, |
| 108 |
.IR clockid . |
| 109 |
If |
| 110 |
.I t |
| 111 |
is less than or equal to the current value of the clock, |
| 112 |
then |
| 113 |
.BR clock_nanosleep () |
| 114 |
returns immediately without suspending the calling thread. |
| 115 |
.P |
| 116 |
.BR clock_nanosleep () |
| 117 |
suspends the execution of the calling thread |
| 118 |
until either at least the time specified by |
| 119 |
.I t |
| 120 |
has elapsed, |
| 121 |
or a signal is delivered that causes a signal handler to be called or |
| 122 |
that terminates the process. |
| 123 |
.P |
| 124 |
If the call is interrupted by a signal handler, |
| 125 |
.BR clock_nanosleep () |
| 126 |
fails with the error |
| 127 |
.BR EINTR . |
| 128 |
In addition, if |
| 129 |
.I remain |
| 130 |
is not NULL, and |
| 131 |
.I flags |
| 132 |
was not |
| 133 |
.BR TIMER_ABSTIME , |
| 134 |
it returns the remaining unslept time in |
| 135 |
.IR remain . |
| 136 |
This value can then be used to call |
| 137 |
.BR clock_nanosleep () |
| 138 |
again and complete a (relative) sleep. |
| 139 |
.SH RETURN VALUE |
| 140 |
On successfully sleeping for the requested interval, |
| 141 |
.BR clock_nanosleep () |
| 142 |
returns 0. |
| 143 |
If the call is interrupted by a signal handler or encounters an error, |
| 144 |
then it returns one of the positive error number listed in ERRORS. |
| 145 |
.SH ERRORS |
| 146 |
.TP |
| 147 |
.B EFAULT |
| 148 |
.I t |
| 149 |
or |
| 150 |
.I remain |
| 151 |
specified an invalid address. |
| 152 |
.TP |
| 153 |
.B EINTR |
| 154 |
The sleep was interrupted by a signal handler; |
| 155 |
see |
| 156 |
.BR signal (7). |
| 157 |
.TP |
| 158 |
.B EINVAL |
| 159 |
The value in the |
| 160 |
.I tv_nsec |
| 161 |
field was not in the range [0, 999999999] or |
| 162 |
.I tv_sec |
| 163 |
was negative. |
| 164 |
.TP |
| 165 |
.B EINVAL |
| 166 |
.I clockid |
| 167 |
was invalid. |
| 168 |
.RB ( CLOCK_THREAD_CPUTIME_ID |
| 169 |
is not a permitted value for |
| 170 |
.IR clockid .) |
| 171 |
.TP |
| 172 |
.B ENOTSUP |
| 173 |
The kernel does not support sleeping against this |
| 174 |
.IR clockid . |
| 175 |
.SH STANDARDS |
| 176 |
POSIX.1-2024. |
| 177 |
.SH HISTORY |
| 178 |
POSIX.1-2001. |
| 179 |
Linux 2.6, |
| 180 |
glibc 2.1. |
| 181 |
.SH NOTES |
| 182 |
If the interval specified in |
| 183 |
.I t |
| 184 |
is not an exact multiple of the granularity underlying clock (see |
| 185 |
.BR time (7)), |
| 186 |
then the interval will be rounded up to the next multiple. |
| 187 |
Furthermore, after the sleep completes, there may still be a delay before |
| 188 |
the CPU becomes free to once again execute the calling thread. |
| 189 |
.P |
| 190 |
Using an absolute timer is useful for preventing |
| 191 |
timer drift problems of the type described in |
| 192 |
.BR nanosleep (2). |
| 193 |
(Such problems are exacerbated in programs that try to restart |
| 194 |
a relative sleep that is repeatedly interrupted by signals.) |
| 195 |
To perform a relative sleep that avoids these problems, call |
| 196 |
.BR clock_gettime (2) |
| 197 |
for the desired clock, |
| 198 |
add the desired interval to the returned time value, |
| 199 |
and then call |
| 200 |
.BR clock_nanosleep () |
| 201 |
with the |
| 202 |
.B TIMER_ABSTIME |
| 203 |
flag. |
| 204 |
.P |
| 205 |
.BR clock_nanosleep () |
| 206 |
is never restarted after being interrupted by a signal handler, |
| 207 |
regardless of the use of the |
| 208 |
.BR sigaction (2) |
| 209 |
.B SA_RESTART |
| 210 |
flag. |
| 211 |
.P |
| 212 |
The |
| 213 |
.I remain |
| 214 |
argument is unused, and unnecessary, when |
| 215 |
.I flags |
| 216 |
is |
| 217 |
.BR TIMER_ABSTIME . |
| 218 |
(An absolute sleep can be restarted using the same |
| 219 |
.I t |
| 220 |
argument.) |
| 221 |
.P |
| 222 |
POSIX.1 specifies that |
| 223 |
.BR clock_nanosleep () |
| 224 |
has no effect on signals dispositions or the signal mask. |
| 225 |
.P |
| 226 |
POSIX.1 specifies that after changing the value of the |
| 227 |
.B CLOCK_REALTIME |
| 228 |
clock via |
| 229 |
.BR clock_settime (2), |
| 230 |
the new clock value shall be used to determine the time |
| 231 |
at which a thread blocked on an absolute |
| 232 |
.BR clock_nanosleep () |
| 233 |
will wake up; |
| 234 |
if the new clock value falls past the end of the sleep interval, then the |
| 235 |
.BR clock_nanosleep () |
| 236 |
call will return immediately. |
| 237 |
.P |
| 238 |
POSIX.1 specifies that |
| 239 |
changing the value of the |
| 240 |
.B CLOCK_REALTIME |
| 241 |
clock via |
| 242 |
.BR clock_settime (2) |
| 243 |
shall have no effect on a thread that is blocked on a relative |
| 244 |
.BR clock_nanosleep (). |
| 245 |
.SH SEE ALSO |
| 246 |
.BR clock_getres (2), |
| 247 |
.BR nanosleep (2), |
| 248 |
.BR restart_syscall (2), |
| 249 |
.BR timer_create (2), |
| 250 |
.BR sleep (3), |
| 251 |
.BR timespec (3), |
| 252 |
.BR usleep (3), |
| 253 |
.BR time (7) |