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 _XOPEN_SOURCE >= 600 || _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.
52
53 If flags is 0, then the value specified in request is interpreted as an
54 interval relative to the current value of the clock specified by
55 clock_id.
56
57 If flags is TIMER_ABSTIME, then request is interpreted as an absolute
58 time as measured by the clock, clock_id. If request is less than or
59 equal to the current value of the clock, then clock_nanosleep() returns
60 immediately without suspending the calling thread.
61
62 clock_nanosleep() suspends the execution of the calling thread until
63 either at least the time specified by request has elapsed, or a signal
64 is delivered that causes a signal handler to be called or that termi‐
65 nates the process.
66
67 If the call is interrupted by a signal handler, clock_nanosleep() fails
68 with the error EINTR. In addition, if remain is not NULL, and flags
69 was not TIMER_ABSTIME, it returns the remaining unslept time in remain.
70 This value can then be used to call clock_nanosleep() again and com‐
71 plete a (relative) sleep.
72
74 On successfully sleeping for the requested interval, clock_nanosleep()
75 returns 0. If the call is interrupted by a signal handler or encoun‐
76 ters an error, then it returns one of the positive error number listed
77 in ERRORS.
78
80 EFAULT request or remain specified an invalid address.
81
82 EINTR The sleep was interrupted by a signal handler.
83
84 EINVAL The value in the tv_nsec field was not in the range 0 to
85 999999999 or tv_sec was negative.
86
87 EINVAL clock_id was invalid. (CLOCK_THREAD_CPUTIME_ID is not a permit‐
88 ted value for clock_id.)
89
91 The clock_nanosleep() system call first appeared in Linux 2.6. Support
92 is available in glibc since version 2.1.
93
95 POSIX.1-2001.
96
98 If the interval specified in request is not an exact multiple of the
99 granularity underlying clock (see time(7)), then the interval will be
100 rounded up to the next multiple. Furthermore, after the sleep com‐
101 pletes, there may still be a delay before the CPU becomes free to once
102 again execute the calling thread.
103
104 Using an absolute timer is useful for preventing timer drift problems
105 of the type described in nanosleep(2). (Such problems are exacerbated
106 in programs that try to restart a relative sleep that is repeatedly
107 interrupted by signals.) To perform a relative sleep that avoids these
108 problems, call clock_gettime(2) for the desired clock, add the desired
109 interval to the returned time value, and then call clock_nanosleep()
110 with the TIMER_ABSTIME flag.
111
112 clock_nanosleep() is never restarted after being interrupted by a sig‐
113 nal handler, regardless of the use of the sigaction(2) SA_RESTART flag.
114
115 The remain argument is unused, and unnecessary, when flags is
116 TIMER_ABSTIME. (An absolute sleep can be restarted using the same
117 request argument.)
118
119 POSIX.1 specifies that clock_nanosleep() has no effect on signals dis‐
120 positions or the signal mask.
121
122 POSIX.1 specifies that after changing the value of the CLOCK_REALTIME
123 clock via clock_settime(2), the new clock value shall be used to deter‐
124 mine the time at which a thread blocked on an absolute
125 clock_nanosleep() will wake up; if the new clock value falls past the
126 end of the sleep interval, then the clock_nanosleep() call will return
127 immediately.
128
129 POSIX.1 specifies that changing the value of the CLOCK_REALTIME clock
130 via clock_settime(2) shall have no effect on a thread that is blocked
131 on a relative clock_nanosleep().
132
134 clock_getres(2), nanosleep(2), restart_syscall(2), timer_create(2),
135 sleep(3), usleep(3), time(7)
136
138 This page is part of release 3.53 of the Linux man-pages project. A
139 description of the project, and information about reporting bugs, can
140 be found at http://www.kernel.org/doc/man-pages/.
141
142
143
144Linux 2013-07-30 CLOCK_NANOSLEEP(2)