1CLOCK_NANOSLEEP(2) Linux Programmer's Manual CLOCK_NANOSLEEP(2)
2
3
4
6 clock_nanosleep - high-resolution sleep with specifiable clock
7
9 #include <time.h>
10
11 int clock_nanosleep(clockid_t clock_id, int flags,
12 const struct timespec *request,
13 struct timespec *remain);
14
15 Link with -lrt (only for glibc versions before 2.17).
16
17 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19 clock_nanosleep():
20 _POSIX_C_SOURCE >= 200112L
21
23 Like nanosleep(2), clock_nanosleep() allows the calling thread to sleep
24 for an interval specified with nanosecond precision. It differs in
25 allowing the caller to select the clock against which the sleep inter‐
26 val is to be measured, and in allowing the sleep interval to be speci‐
27 fied as either an absolute or a relative value.
28
29 The time values passed to and returned by this call are specified using
30 timespec structures, defined as follows:
31
32 struct timespec {
33 time_t tv_sec; /* seconds */
34 long tv_nsec; /* nanoseconds [0 .. 999999999] */
35 };
36
37 The clock_id argument specifies the clock against which the sleep
38 interval is to be measured. This argument can have one of the follow‐
39 ing values:
40
41 CLOCK_REALTIME A settable system-wide real-time clock.
42
43 CLOCK_MONOTONIC A nonsettable, monotonically increasing clock that
44 measures time since some unspecified point in the past
45 that does not change after system startup.
46
47 CLOCK_PROCESS_CPUTIME_ID
48 A settable per-process clock that measures CPU time
49 consumed by all threads in the process.
50
51 See clock_getres(2) for further details on these clocks. In addition,
52 the CPU clock IDs returned by clock_getcpuclockid(3) and
53 pthread_getcpuclockid(3) can also be passed in clock_id.
54
55 If flags is 0, then the value specified in request is interpreted as an
56 interval relative to the current value of the clock specified by
57 clock_id.
58
59 If flags is TIMER_ABSTIME, then request is interpreted as an absolute
60 time as measured by the clock, clock_id. If request is less than or
61 equal to the current value of the clock, then clock_nanosleep() returns
62 immediately without suspending the calling thread.
63
64 clock_nanosleep() suspends the execution of the calling thread until
65 either at least the time specified by request has elapsed, or a signal
66 is delivered that causes a signal handler to be called or that termi‐
67 nates the process.
68
69 If the call is interrupted by a signal handler, clock_nanosleep() fails
70 with the error EINTR. In addition, if remain is not NULL, and flags
71 was not TIMER_ABSTIME, it returns the remaining unslept time in remain.
72 This value can then be used to call clock_nanosleep() again and com‐
73 plete a (relative) sleep.
74
76 On successfully sleeping for the requested interval, clock_nanosleep()
77 returns 0. If the call is interrupted by a signal handler or encoun‐
78 ters an error, then it returns one of the positive error number listed
79 in ERRORS.
80
82 EFAULT request or remain specified an invalid address.
83
84 EINTR The sleep was interrupted by a signal handler; see signal(7).
85
86 EINVAL The value in the tv_nsec field was not in the range 0 to
87 999999999 or tv_sec was negative.
88
89 EINVAL clock_id was invalid. (CLOCK_THREAD_CPUTIME_ID is not a permit‐
90 ted value for clock_id.)
91
93 The clock_nanosleep() system call first appeared in Linux 2.6. Support
94 is available in glibc since version 2.1.
95
97 POSIX.1-2001, POSIX.1-2008.
98
100 If the interval specified in request is not an exact multiple of the
101 granularity underlying clock (see time(7)), then the interval will be
102 rounded up to the next multiple. Furthermore, after the sleep com‐
103 pletes, there may still be a delay before the CPU becomes free to once
104 again execute the calling thread.
105
106 Using an absolute timer is useful for preventing timer drift problems
107 of the type described in nanosleep(2). (Such problems are exacerbated
108 in programs that try to restart a relative sleep that is repeatedly
109 interrupted by signals.) To perform a relative sleep that avoids these
110 problems, call clock_gettime(2) for the desired clock, add the desired
111 interval to the returned time value, and then call clock_nanosleep()
112 with the TIMER_ABSTIME flag.
113
114 clock_nanosleep() is never restarted after being interrupted by a sig‐
115 nal handler, regardless of the use of the sigaction(2) SA_RESTART flag.
116
117 The remain argument is unused, and unnecessary, when flags is
118 TIMER_ABSTIME. (An absolute sleep can be restarted using the same
119 request argument.)
120
121 POSIX.1 specifies that clock_nanosleep() has no effect on signals dis‐
122 positions or the signal mask.
123
124 POSIX.1 specifies that after changing the value of the CLOCK_REALTIME
125 clock via clock_settime(2), the new clock value shall be used to deter‐
126 mine the time at which a thread blocked on an absolute
127 clock_nanosleep() will wake up; if the new clock value falls past the
128 end of the sleep interval, then the clock_nanosleep() call will return
129 immediately.
130
131 POSIX.1 specifies that changing the value of the CLOCK_REALTIME clock
132 via clock_settime(2) shall have no effect on a thread that is blocked
133 on a relative clock_nanosleep().
134
136 clock_getres(2), nanosleep(2), restart_syscall(2), timer_create(2),
137 sleep(3), usleep(3), time(7)
138
140 This page is part of release 4.15 of the Linux man-pages project. A
141 description of the project, information about reporting bugs, and the
142 latest version of this page, can be found at
143 https://www.kernel.org/doc/man-pages/.
144
145
146
147Linux 2017-09-15 CLOCK_NANOSLEEP(2)